> ## Documentation Index
> Fetch the complete documentation index at: https://shivamgoyal.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Schema Design

> Design and optimize your database schema in CodeNull

# Schema Design

Creating an effective database schema is crucial for your application's performance and scalability. CodeNull's visual Schema Designer makes it easy to create, visualize, and optimize your database structure without writing complex code.

## Schema Designer Overview

The Schema Designer provides a visual interface for defining your data models and their relationships:

<Frame>
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/shivamgoyal/images/schema-designer-overview.png" alt="Schema Designer Interface" />
</Frame>

Key areas of the interface include:

* **Canvas**: Visual workspace for arranging and connecting models
* **Model Editor**: Define fields, types, and validation rules
* **Relationship Manager**: Create connections between models
* **Properties Panel**: Configure detailed settings for selected elements
* **Tool Palette**: Access common schema design tools

## Creating Data Models

<Steps>
  <Step title="Add a New Model">
    Click the "Add Model" button in the tool palette or right-click on the canvas and select "New Model".
  </Step>

  <Step title="Define Basic Information">
    Enter the model's name and description. For MongoDB, this will be a collection; for SQL databases, a table.

    <Frame>
      <img src="https://mintlify.s3.us-west-1.amazonaws.com/shivamgoyal/images/model-basic-info.png" alt="Model Basic Information" />
    </Frame>
  </Step>

  <Step title="Add Fields">
    Click "Add Field" to add properties to your model. For each field, specify:

    * Field name
    * Data type
    * Required status
    * Default value
    * Description
    * Validation rules
  </Step>

  <Step title="Configure Advanced Settings">
    Set additional model properties:

    * Indexes for query optimization
    * Unique constraints
    * Timestamps (created/updated)
    * Soft delete options
    * Access control rules
  </Step>
</Steps>

## Defining Relationships

CodeNull makes it easy to create relationships between your data models:

<Steps>
  <Step title="Start Relationship Creation">
    Click the "Create Relationship" tool in the palette, then click on the source model.
  </Step>

  <Step title="Select Relationship Type">
    Choose the type of relationship:

    * One-to-One
    * One-to-Many
    * Many-to-Many
  </Step>

  <Step title="Connect to Target Model">
    Click on the target model to complete the connection.
  </Step>

  <Step title="Configure Relationship Details">
    Set properties for the relationship:

    * Foreign key field names
    * Cascade behavior (on update/delete)
    * Relationship constraints
    * Indexing options

    <Frame>
      <img src="https://mintlify.s3.us-west-1.amazonaws.com/shivamgoyal/images/relationship-configuration.png" alt="Relationship Configuration" />
    </Frame>
  </Step>
</Steps>

## Relationship Types Explained

<AccordionGroup>
  <Accordion title="One-to-One Relationships">
    Each record in Model A relates to exactly one record in Model B.

    **Example**: A User has one Profile

    **Implementation options**:

    * Reference field in either model
    * Embedding in MongoDB

    **Best practices**:

    * Use when the related data is accessed together
    * Consider embedding in MongoDB for better read performance
  </Accordion>

  <Accordion title="One-to-Many Relationships">
    Each record in Model A relates to multiple records in Model B.

    **Example**: An Author has many Books

    **Implementation options**:

    * Reference field in the "many" side
    * Array of references in the "one" side (MongoDB)

    **Best practices**:

    * Use reference in the "many" side for most cases
    * Consider array of references only for small collections
  </Accordion>

  <Accordion title="Many-to-Many Relationships">
    Records in Model A can relate to multiple records in Model B, and vice versa.

    **Example**: Students and Courses

    **Implementation options**:

    * Junction/join table (SQL)
    * Array of references on both sides (MongoDB)
    * Separate collection for the relationship (MongoDB)

    **Best practices**:

    * Use junction table for SQL databases
    * For MongoDB, use separate collection for relationships with metadata
  </Accordion>
</AccordionGroup>

## Schema Optimization

CodeNull provides tools to optimize your database schema for performance:

### Indexing Strategy

<Frame>
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/shivamgoyal/images/index-configuration.png" alt="Index Configuration" />
</Frame>

Create and manage indexes to improve query performance:

* **Single Field Indexes**: Speed up queries on one field
* **Compound Indexes**: Optimize queries that filter or sort on multiple fields
* **Text Indexes**: Enable text search capabilities
* **Unique Indexes**: Enforce uniqueness constraints
* **TTL Indexes**: Automatically remove documents after a specified time

## Schema Validation

Ensure data integrity with comprehensive validation rules:

<Frame>
  <img src="https://mintlify.s3.us-west-1.amazonaws.com/shivamgoyal/images/validation-rules-editor.png" alt="Validation Rules Editor" />
</Frame>

Configure validation for:

* **Required Fields**: Ensure certain fields must have values
* **Data Type Validation**: Enforce correct data types
* **Value Ranges**: Set min/max values for numbers
* **String Patterns**: Apply regex patterns for strings
* **Custom Validation**: Create complex validation rules with JavaScript functions

## Best Practices

<Tip>
  When creating relationships, design your schema based on how the data will be accessed rather than just how it's logically structured.
</Tip>

* **Use descriptive names**: Choose clear, consistent names for models and fields
* **Document your schema**: Add descriptions to models and fields to maintain clarity
* **Plan for queries**: Design your schema with your most common queries in mind
* **Be cautious with array fields**: Limit array sizes in MongoDB to avoid performance issues
* **Use appropriate data types**: Choose the most specific type for each field
* **Consider read/write ratio**: Optimize for reads in read-heavy applications

## Next Steps

After designing your schema, you can:

* [Add data to your database](/database-dashboard/add-data)
* [Set up data migration processes](/database-dashboard/data-migration)
* [Connect your database to your API endpoints](/api-dashboard/endpoints)
