‹ All guides

Make sense of database relationships

One-to-one, one-to-many, and many-to-many: learn how to connect your data with purpose.

A table describes one kind of thing: a user, a product, or an order. Relationships describe how those things fit together. Modeling the connection explicitly helps your database keep records consistent as your application grows.

Start with the question your data answers

For an online store, ask: who placed this order? Which products are in it? A user may place many orders, and an order may contain many products. Those two observations lead to different relationship patterns.

One to many: the everyday connection

Put a foreign key on the “many” side. If a user can place several orders, each order has a user_id that references the user’s primary key. Several orders can use the same value.

CREATE TABLE users (
  id INTEGER PRIMARY KEY
);

CREATE TABLE orders (
  id INTEGER PRIMARY KEY,
  user_id INTEGER NOT NULL,
  FOREIGN KEY (user_id) REFERENCES users (id)
);

In SchemaCraft, drag from the right handle of users.id to the left handle of orders.user_id. The source must identify one row through a primary key or unique constraint. The two columns must have compatible data types.

One to one: add uniqueness

A separate user profile can have at most one record for each user. Make the profile’s user_id unique as well as a foreign key. This limits how many profiles can point to the same user. It does not require every user to have a profile; mandatory participation usually needs additional application logic.

Select “One to one” in the relationship inspector. SchemaCraft adds a unique constraint to the target column when it generates SQL. For nullable unique columns, review your database’s handling of NULL values; SQL Server differs from PostgreSQL, MySQL, and SQLite.

Many to many: give the connection a table

An order contains many products, and a product can appear in many orders. A junction table, such as order_items, holds a foreign key to each side. It is also the natural place for facts about the connection: quantity and the price paid at purchase.

For a simple association, the two foreign key columns can form a composite primary key. In the relationship dialog, choose “Many to many” to create a junction table automatically. If repeated associations are meaningful, adapt the key design to those requirements.

Self references are relationships too

An employee can reference another employee as their manager. Use two different columns in the same table, such as id and manager_id. A nullable manager field represents employees without a manager. A foreign key alone does not prevent longer cycles, so organizational rules may need application checks.

Choose deletion behavior deliberately

Crow’s Foot notation puts a branching endpoint on the “many” side and bars on the “one” side. SchemaCraft shows cardinality in the diagram; inspect nullable settings to understand whether a reference is optional.

For database-specific rules, see PostgreSQL’s constraint documentation and SQLite foreign key support.

Put it into practice.

Make a small schema and explore the SQL as you go.

Open the designer