What are database relationships
· Category: SQL & Databases
Short answer
Relational databases model associations between tables as one-to-one, one-to-many, and many-to-many relationships.
How it works
- One-to-one: A row in table A relates to exactly one row in table B. Often used for splitting optional or sensitive attributes.
- One-to-many: A row in table A relates to many rows in table B. The most common pattern, implemented with a foreign key on the many side.
- Many-to-many: Rows in table A relate to many rows in table B and vice versa. Implemented through a junction table.
Example
-- One-to-many: a customer has many orders
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT REFERENCES customers(id));
Why it matters
Choosing the correct relationship ensures data integrity, reduces redundancy, and makes queries intuitive. Misidentifying relationships leads to brittle schemas and complex workarounds.r