Tables, Keys & Constraints · Question 19 · Hard
The four INSERT statements run one after another. Which of them is the first to fail?
CREATE TABLE Berth (
pnr INT PRIMARY KEY,
coach VARCHAR(5),
seat_no INT,
UNIQUE (coach, seat_no)
);
INSERT INTO Berth VALUES (501, 'S1', 10);
INSERT INTO Berth VALUES (502, 'S2', 10);
INSERT INTO Berth VALUES (503, 'S1', 11);
INSERT INTO Berth VALUES (504, 'S1', 10);
- The 2nd INSERT
- The 4th INSERT
- The 3rd INSERT
- None of them fails
Answer and explanation
Answer: B. The 4th INSERT
Explanation: UNIQUE (coach, seat_no) makes the pair unique, not each column. (S1, 10), (S2, 10) and (S1, 11) are all different pairs. The 4th row repeats the pair (S1, 10), so it is rejected even though its pnr is different.
Previous: The status column has a DEFAULT, but this INSERT passes NULL for it… · Next: In standard SQL, which column(s) of the parent table may a FOREIGN KEY…
Practise tables, keys & constraints
Try a fresh set of tables, keys & constraints questions with an explanation after every answer, free and without signing up: Tables, Keys & Constraints practice. Or sit a full placement mock test.
More tables, keys & constraints questions
- The second INSERT passes NULL as the price. What does the final SELECT return?
- Emp.dept_id is declared as FOREIGN KEY REFERENCES Dept(id) with no ON DELETE…
- The status column has a DEFAULT, but this INSERT passes NULL for it…
- In standard SQL, which column(s) of the parent table may a FOREIGN KEY…
More sql practice
DML & Transactions · SELECT, WHERE & ORDER BY · SQL Joins · Aggregates & GROUP BY · Subqueries & CTEs · Window Functions · NULL Handling · SQL Functions & CASE
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge