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

# Creating Relationships with Foreign Keys

> Learn how to create database relationships in ChartDB using foreign keys.

<Frame>
  <img src="https://mintcdn.com/buckledev/Nk8pE0k5UNFAdU1u/docs/images/relationships.png?fit=max&auto=format&n=Nk8pE0k5UNFAdU1u&q=85&s=746c022f734de7aaff70154a90c5d380" width="2874" height="1530" data-path="docs/images/relationships.png" />
</Frame>

In ChartDB, relationships between tables are established through **foreign keys**.
This means a column in one table (the **primary table**) points to a column in another table (the **referencing table**), linking records that belong together.

<Note>
  For a relationship to be valid, the [field
  types](/diagrams/tables-fields#data-type) of the columns you choose in both the
  primary and referencing tables **must match**.
</Note>

## Create Relationships

ChartDB offers three user-friendly ways to create relationships between your database tables:

<Steps>
  <Step icon="arrow-pointer" title="Drag & Drop Table Dots">
    This is the most straightforward and visually intuitive method for creating relationships.

    To create a relationship, simply click and drag from a connection dot on your primary table to a column in your target table.
    ChartDB will highlight compatible columns as you drag.

    <Frame>
      <img src="https://mintcdn.com/buckledev/Nk8pE0k5UNFAdU1u/docs/images/drag-and-drop-relationships.png?fit=max&auto=format&n=Nk8pE0k5UNFAdU1u&q=85&s=0f3e4cbafa8269c2a6c7f0e3b923dedf" width="2144" height="1698" data-path="docs/images/drag-and-drop-relationships.png" />
    </Frame>
  </Step>

  <Step icon="computer-mouse" title="Right-Click Context Menu">
    This method provides an alternative, especially useful when tables are positioned far apart on the canvas, making drag and drop less convenient.

    Right-click on any table and select "Create Relationship" from the menu.
    In the sidebar that appears, select your primary and referencing tables, choose the columns to connect, and click Save to create the relationship.
  </Step>

  <Step icon="list" title="Use the 'Relationship' Menu">
    ChartDB also provides a dedicated "Relationship" menu within its interface, offering a structured approach to relationship management.

    Navigate to the "Relationship" menu and click "Add Relationship".
    In the configuration sidebar, select your primary and referencing tables, choose the columns to connect, and click Save to create the relationship.

    <Frame>
      <img src="https://mintcdn.com/buckledev/Nk8pE0k5UNFAdU1u/docs/images/create-relationship-modal.png?fit=max&auto=format&n=Nk8pE0k5UNFAdU1u&q=85&s=b9b9f61b9531466bc85f83077def2b0a" width="2880" height="1800" data-path="docs/images/create-relationship-modal.png" />
    </Frame>
  </Step>
</Steps>

## View Relationship Cardinality

By default, cardinality indicators are not visible on the diagram. To display the cardinality of relationships:

* In the top menu bar of ChartDB, click on the **View** dropdown menu and click on the **Show Cardinality** option from the dropdown.

Once enabled, ChartDB will display cardinality indicators (e.g., 1, N) on the relationship lines in your diagram, visually representing the type of relationship (one-to-many, many-to-many, etc.).

<Frame>
  <img src="https://mintcdn.com/buckledev/Nk8pE0k5UNFAdU1u/docs/images/view-menu.png?fit=max&auto=format&n=Nk8pE0k5UNFAdU1u&q=85&s=c8831158614f098f36ce3d5509233acf" width="828" height="558" data-path="docs/images/view-menu.png" />
</Frame>

## Modify Relationship Cardinality

ChartDB supports 4 relationship cardinalities, including **One-to-One**, **One-to-Many**, **Many-to-One**, and **Many-to-Many**.
To adjust the **cardinality** of a relationship:

1. **Select the Relationship:** Click on the relationship line connecting the two tables in your diagram. This will typically open the relationship details in the sidebar.
2. **Modify Cardinality:** In the sidebar, you will find a dropdown menu labeled "Cardinality" or similar. Use this menu to select the new cardinality for the relationship.

<Frame>
  <img src="https://mintcdn.com/buckledev/Nk8pE0k5UNFAdU1u/docs/images/cardinality-selector.png?fit=max&auto=format&n=Nk8pE0k5UNFAdU1u&q=85&s=b0055f1e09b583c567e202ddf2d7b7f6" width="1478" height="382" data-path="docs/images/cardinality-selector.png" />
</Frame>

Once you've created a relationship, ChartDB immediately visualizes it on your diagram. You'll see a line connecting the related tables, clearly indicating the established link.
