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?
- ALTER TABLE Student ADD COLUMN city VARCHAR(30);
- UPDATE Student ADD COLUMN city VARCHAR(30);
- INSERT INTO Student (city) VALUES (NULL);
- 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
- 3
- 0
- 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?
- The department and its three employees are deleted
- The department is deleted, and dept_id becomes NULL for the three employees
- The department is deleted, and the three employees keep dept_id = 5
- 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
- A table Student(roll, name) already holds data. Which statement adds a new…
- What happens when the last statement runs?
- What does the final SELECT return?
- What happens when the INSERT runs?
- 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?
- In PostgreSQL, MySQL and SQLite, which follow the SQL standard on this point…
- 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…
- 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…
- The four INSERT statements run one after another. Which of them is the first…
- In standard SQL, which column(s) of the parent table may a FOREIGN KEY…
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
- SELECT, WHERE & ORDER BY: Filtering, DISTINCT, IN, BETWEEN, LIKE, sorting and LIMIT.
- SQL Joins: Inner, left, right, full, cross and self joins, and row counts.
- Aggregates & GROUP BY: COUNT, SUM, AVG, grouping and HAVING versus WHERE.
- Subqueries & CTEs: Scalar and correlated subqueries, IN, EXISTS and WITH.
- Window Functions: ROW_NUMBER, RANK, DENSE_RANK, PARTITION BY, LAG and running totals.
- NULL Handling: Three-valued logic, IS NULL, COALESCE and NULL in aggregates.
- SQL Functions & CASE: CASE WHEN, conditional aggregation, string and numeric functions.
- UNION, INTERSECT & EXCEPT: Combining results, duplicates and column rules.
- DML & Transactions: INSERT, UPDATE, DELETE, COMMIT, ROLLBACK and ACID.
Other subjects
- Quantitative Aptitude: Arithmetic, percentages, time and work, speed, probability and data interpretation: the numerical section of every fresher test.
- Logical Reasoning: Series, coding-decoding, arrangements, syllogisms and puzzles: the section that separates prepared candidates from the rest.
- Verbal Ability: Vocabulary, grammar, reading comprehension and sentence order: tested in nearly every IT services hiring exam.
- Technical & Programming: CS fundamentals, output prediction and pseudo-code: the technical MCQ round for IT roles.
- Python: Python quizzes by topic, from types and strings to classes and generators: output prediction and interview concepts.
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