Tables, Keys & Constraints questions and answers

CREATE, ALTER, primary and foreign keys, UNIQUE and CHECK. Practise tables, keys & constraints questions from the sql section of placement tests, with a worked explanation after every answer. Practice is free and needs no sign-up.

Sample tables, keys & constraints questions with worked answers

Question 1

A table Student(roll, name) already holds data. Which statement adds a new column city to it without losing the existing rows?

  1. ALTER TABLE Student ADD COLUMN city VARCHAR(30);
  2. UPDATE Student ADD COLUMN city VARCHAR(30);
  3. INSERT INTO Student (city) VALUES (NULL);
  4. CREATE TABLE Student (roll INT, name VARCHAR(30), city VARCHAR(30));

Answer: A. ALTER TABLE Student ADD COLUMN city VARCHAR(30);

Explanation: ALTER TABLE changes the structure of an existing table and keeps its rows. UPDATE and INSERT change data, not structure, and CREATE TABLE fails because the table already exists.

Question 2

Table Dept has rows (1, IT) and (2, HR). Table Emp has rows (10, Kiran, dept 1), (11, Leela, dept 1) and (12, Mohan, dept 2). Emp.dept_id is declared as FOREIGN KEY REFERENCES Dept(id) ON DELETE CASCADE. After DELETE FROM Dept WHERE id = 1 runs, how many rows are left in Emp?

  1. 1
  2. 3
  3. 0
  4. 2

Answer: A. 1

Explanation: ON DELETE CASCADE deletes the child rows that refer to a deleted parent row. Kiran and Leela belong to department 1 and are removed with it, so only Mohan's row remains.

Question 3

Emp.dept_id is declared as FOREIGN KEY REFERENCES Dept(id) with no ON DELETE clause, and foreign keys are being enforced. Three employees have dept_id = 5. What happens when DELETE FROM Dept WHERE id = 5 runs?

  1. The department and its three employees are deleted
  2. The department is deleted, and dept_id becomes NULL for the three employees
  3. The department is deleted, and the three employees keep dept_id = 5
  4. The DELETE is rejected with an error, and nothing changes

Answer: D. The DELETE is rejected with an error, and nothing changes

Explanation: Without an ON DELETE clause the default action is NO ACTION, which behaves like RESTRICT here: a parent row cannot be deleted while child rows still refer to it. Cascading or setting NULL happens only when it is asked for explicitly.

All 20 tables, keys & constraints questions with answers

Tables, Keys & Constraints: common questions

What are tables, keys & constraints questions?

CREATE, ALTER, primary and foreign keys, UNIQUE and CHECK. They appear in the sql section of many placement tests.

Where can I practise tables, keys & constraints questions with answers for free?

Right here on LogicWiz Exams. Every tables, keys & constraints question has a worked explanation after you answer. Practice is free and needs no sign-up.

How should I practise tables, keys & constraints?

Start with easy questions, then move to medium and hard. Read the explanation after each answer, and come back to the ones you missed.

Start practising

On this page you can start a fresh set of tables, keys & constraints questions, choose easy, medium or hard, and time yourself if you want to. Or practise the whole subject: SQL practice.

More sql topics

Other subjects

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge · Test from a job description · LogicWiz Open: free team quiz · Free interview prep