Tables, Keys & Constraints · Question 4 · Easy

What happens when the INSERT runs?

CREATE TABLE Employee (
  id INT PRIMARY KEY,
  name VARCHAR(30) NOT NULL,
  city VARCHAR(30)
);
INSERT INTO Employee (id, city) VALUES (1, 'Kochi');
  1. It succeeds, and name is stored as NULL
  2. It succeeds, and name is stored as an empty string
  3. It fails, because an INSERT must always list every column of the table
  4. It fails, because name is NOT NULL and no value or default is supplied for it
Answer and explanation

Answer: D. It fails, because name is NOT NULL and no value or default is supplied for it

Explanation: name is left out of the INSERT and has no DEFAULT, so the database would have to store NULL in it. The NOT NULL constraint forbids that, so the statement is rejected. Leaving out a column is fine only when it allows NULL or has a default.

Previous: What does the final SELECT return? · Next: How many PRIMARY KEY constraints can one table have?

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

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