Tables, Keys & Constraints · Question 8 · Medium
In PostgreSQL, MySQL and SQLite, which follow the SQL standard on this point, what does the final SELECT return?
CREATE TABLE Users (
id INT PRIMARY KEY,
email VARCHAR(40) UNIQUE
);
INSERT INTO Users VALUES (1, NULL);
INSERT INTO Users VALUES (2, NULL);
INSERT INTO Users VALUES (3, '[email protected]');
SELECT COUNT(*) FROM Users;
- 2
- 1
- 3
- Nothing: the second INSERT fails with a UNIQUE violation
Answer and explanation
Answer: C. 3
Explanation: UNIQUE forbids two equal non-NULL values. NULL is not considered equal to another NULL, so these engines accept any number of NULLs in a UNIQUE column, and all three rows are stored. (SQL Server is the well-known exception: it allows only one NULL.)
Previous: What happens when the INSERT with a price of -50 runs? · Next: Table Member: id | name 1 | Asha 2 | Bala The table Member below already has…
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
- How many PRIMARY KEY constraints can one table have?
- Which statement can remove only those rows of a table that satisfy a…
- What happens when the INSERT with a price of -50 runs?
- Table Member: id | name 1 | Asha 2 | Bala The table Member below already has…
- Table Dept has rows (1, IT) and (2, HR). Table Emp has rows (10, Kiran, dept…
- Table Team has rows (1, Mumbai) and (2, Chennai). Table Player has rows…
- Table Emp: id | name | salary 1 | Asha | 42000 2 | Bala | 65000 3 | Chirag |…
- Table Emp: id | name | salary 1 | Asha | 42000 2 | Bala | 65000 3 | Chirag |…
- The primary key below is made of two columns. What does the final SELECT return?
- A table Orders with 50 lakh rows is searched very often by customer_id, so an…
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