Sobes.tech
Middle

How to physically implement a one-to-one relationship between two tables?

sobes.tech AI

Answer from AI

The one-to-one relationship between two tables at the physical level can be implemented in several ways:

  1. Shared primary key — both tables have the same primary key value, with this key in one table also serving as a foreign key to the other table.

  2. Unique foreign key — a foreign key is created in one table referencing the other table with a UNIQUE constraint, ensuring that each record corresponds to no more than one record in the related table.

Example with a shared primary key:

CREATE TABLE Person (
    PersonID INT PRIMARY KEY,
    Name VARCHAR(100)
);

CREATE TABLE Passport (
    PersonID INT PRIMARY KEY,
    PassportNumber VARCHAR(50),
    FOREIGN KEY (PersonID) REFERENCES Person(PersonID)
);

Here, each record in Person can have no more than one record in Passport, and vice versa.

How to physically implement a one-to-one relationship… - sobes.tech