Introduction
A one-to-one relationship links two pieces of information so each item in one table matches exactly one item in another table. This keeps data organized and avoids repeating details.
Jump into concepts and practice - no test required
CREATE TABLE TableA ( id INT PRIMARY KEY, columnA datatype ); CREATE TABLE TableB ( id INT PRIMARY KEY, columnB datatype, FOREIGN KEY (id) REFERENCES TableA(id) );
CREATE TABLE Person ( person_id INT PRIMARY KEY, name VARCHAR(100) ); CREATE TABLE Passport ( person_id INT PRIMARY KEY, passport_number VARCHAR(20), FOREIGN KEY (person_id) REFERENCES Person(person_id) );
CREATE TABLE Employee ( employee_id INT PRIMARY KEY, name VARCHAR(100) ); CREATE TABLE EmployeeDetails ( employee_id INT PRIMARY KEY, address VARCHAR(200), phone VARCHAR(15), FOREIGN KEY (employee_id) REFERENCES Employee(employee_id) );
CREATE TABLE User ( user_id INT PRIMARY KEY, username VARCHAR(50) ); CREATE TABLE UserProfile ( user_id INT PRIMARY KEY, bio TEXT, FOREIGN KEY (user_id) REFERENCES User(user_id) ); INSERT INTO User (user_id, username) VALUES (1, 'alice'); INSERT INTO UserProfile (user_id, bio) VALUES (1, 'Loves hiking and reading.'); SELECT u.user_id, u.username, p.bio FROM User u JOIN UserProfile p ON u.user_id = p.user_id;
one-to-one relationship in database design?CREATE TABLE Person ( PersonID INT PRIMARY KEY, Name VARCHAR(50) ); CREATE TABLE Passport ( PassportID INT PRIMARY KEY, PersonID INT UNIQUE, Number VARCHAR(20), FOREIGN KEY (PersonID) REFERENCES Person(PersonID) );
UNIQUE constraint on PersonID in Passport ensure?CREATE TABLE Employee ( EmployeeID INT PRIMARY KEY, Name VARCHAR(50) ); CREATE TABLE EmployeeDetails ( DetailID INT PRIMARY KEY, EmployeeID INT, Address VARCHAR(100), FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID) );
Employee and EmployeeDetails?User and UserProfile. Each user has exactly one profile. Which design best enforces this one-to-one relationship?