Timedora

Relational Tables and Keys

How relational tables organize records and use keys to identify rows and represent relationships.

Tables, Keys, and Relationships

Tables give data a structure

A relational database stores data in tables, also called relations. Each table describes one kind of entity, such as a customer or an order. A row is one record; a column is an attribute, and its data type specifies what kind of value it holds. The table’s schema defines its columns, keys, and rules.

A primary key identifies a row uniquely. It can be a single column, such as customer_id, or a combination of columns. Keys let tables refer to related records without repeating all their information.

Relationship patterns
RelationshipMeaningRepresentation
One-to-oneEach row corresponds to at most one row in the other table.Keys connect the paired records.
One-to-manyOne record can relate to many records in another table.The many-side table stores a foreign key to the one-side table.
Many-to-manyRecords on either side can relate to many on the other.A junction table stores a row for each pairing.
Common relationship patterns and where their keys are stored.

Following the keys

In the source example, each customer has a customer_id, and each order has an order_id. The orders table also stores customer_id as a foreign key, representing the one-to-many pattern: one customer may have many orders. A non-null foreign-key reference must match a key in the referenced table, preserving referential integrity.

The source’s order_items table illustrates a many-to-many relationship between orders and products: an order can include many products, and a product can appear in many orders. It stores each order–product pairing as a row. The combination of order_id and product_id is its primary key, so together those columns identify a pairing. This design connects records through keys rather than copying all customer, order, and product details into each row.