Sobes.tech
Junior

What means are used to establish relationships between tables in a relational database?

sobes.tech AI

Answer from AI

Relationships between tables in a relational database are established using keys:

  • Primary Key — a unique identifier for a record in a table.
  • Foreign Key — a field in one table that references the primary key of another table.

This allows establishing relationships between tables, such as one-to-one, one-to-many, or many-to-many.

Example:

CREATE TABLE authors (
    author_id INT PRIMARY KEY,
    name VARCHAR(100)
);

CREATE TABLE books (
    book_id INT PRIMARY KEY,
    title VARCHAR(100),
    author_id INT,
    FOREIGN KEY (author_id) REFERENCES authors(author_id)
);

Here, author_id in the books table links a book to its author from the authors table.