Junior — Middle
Can you list the main types of relationships between tables in database management systems?
sobes.tech AI
Answer from AI
Main types of relationships between tables in databases:
-
One-to-One: Each record in the first table is related to exactly one record in the second table and vice versa. Used when data is logically separated but directly linked.
-
One-to-Many: One record in the first table can be related to multiple records in the second table, but each record in the second table is related to only one record in the first. For example, one author and multiple books.
-
Many-to-Many: Records in the first table can be related to multiple records in the second table and vice versa. Implemented through an intermediate (linking) table.
Example of a "one-to-many" relationship:
-- Authors table
CREATE TABLE authors (
id INT PRIMARY KEY,
name VARCHAR(100)
);
-- Books table
CREATE TABLE books (
id INT PRIMARY KEY,
title VARCHAR(100),
author_id INT,
FOREIGN KEY (author_id) REFERENCES authors(id)
);