Junior — Middle
Can you list the main types of relationships used in SQL relational databases?
sobes.tech AI
Answer from AI
In relational databases, the main types of relationships between tables are:
- One to one (1:1) — each record in the first table corresponds to exactly one record in the second table and vice versa.
- One to many (1:N) — one record in the first table corresponds to multiple records in the second.
- Many to many (M:N) — records in one table can correspond to many records in another and vice versa. Usually, an intermediate linking table is used to implement this.
Example of a "one to many" relationship:
-- Users table
CREATE TABLE Users (
UserID INT PRIMARY KEY,
UserName VARCHAR(100)
);
-- Orders table
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
UserID INT,
OrderDate DATE,
FOREIGN KEY (UserID) REFERENCES Users(UserID)
);
Here, one user can have many orders.