Given two tables Users and Profiles with a one-to-one relationship on user_id, what is the output of the following query?
SELECT u.user_id, u.name, p.bio FROM Users u JOIN Profiles p ON u.user_id = p.user_id ORDER BY u.user_id;
Tables:
Users
user_id | name
1 | Alice
2 | Bob
3 | Carol
Profiles
user_id | bio
1 | Loves cats
3 | Enjoys hiking
SELECT u.user_id, u.name, p.bio FROM Users u JOIN Profiles p ON u.user_id = p.user_id ORDER BY u.user_id;
Remember that an INNER JOIN only returns rows with matching keys in both tables.
The JOIN returns only users who have matching profiles. Bob has no profile, so he is excluded.
Which of the following best describes a one-to-one relationship in database design?
Think about the meaning of 'one-to-one' literally.
A one-to-one relationship means each row in one table corresponds to exactly one row in the other table, and vice versa.
Which SQL statement correctly creates two tables with a one-to-one relationship using a foreign key?
One-to-one requires the foreign key to be unique in the child table.
Option B defines user_id in Profiles as UNIQUE and a foreign key referencing Users, enforcing one-to-one.
You have two tables Employees and EmployeeDetails in a one-to-one relationship. Which query is the most efficient to get all employees with their details?
Consider that every employee has exactly one detail record.
Since the relationship is one-to-one and every employee has details, an INNER JOIN (option A) is most efficient and clear.
Given these tables:
CREATE TABLE Customers (customer_id INT PRIMARY KEY, name VARCHAR(50)); CREATE TABLE CustomerProfiles (profile_id INT PRIMARY KEY, customer_id INT UNIQUE, details TEXT, FOREIGN KEY (customer_id) REFERENCES Customers(customer_id));
When inserting this data:
INSERT INTO Customers VALUES (1, 'John'); INSERT INTO CustomerProfiles VALUES (10, 1, 'Likes sports'); INSERT INTO CustomerProfiles VALUES (11, 1, 'Likes music');
What error will the last insert cause?
Think about the UNIQUE constraint on customer_id in CustomerProfiles.
The second insert tries to add a row with the same customer_id 1, violating the UNIQUE constraint that enforces one-to-one.