Middle
Kuidas rakendada füüsilisel tasandil ühe-ühele suhet kahe tabeli vahel?
sobes.tech AI
Vastus AI-lt
At the physical level, a one-to-one relationship between two tables is implemented in several ways:
-
Shared primary key — both tables have the same primary key value, and in one of them, this key also serves as a foreign key to the other table.
-
Unique foreign key — in one table, a foreign key to another table is created with a UNIQUE constraint, ensuring that each record corresponds to at most one record in the related table.
Example with 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 at most one record in Passport, and vice versa.