Bird
0
0

Given these tables:

medium📝 query result Q4 of 15
SQL - Table Constraints
Given these tables:
CREATE TABLE Departments (DeptID INT PRIMARY KEY, DeptName VARCHAR(50));
CREATE TABLE Employees (EmpID INT PRIMARY KEY, EmpName VARCHAR(50), DeptID INT, FOREIGN KEY (DeptID) REFERENCES Departments(DeptID));
What happens if you run:
INSERT INTO Employees (EmpID, EmpName, DeptID) VALUES (1, 'Alice', 10);
Assuming no DeptID = 10 exists in Departments?
AThe insert succeeds and adds the record.
BThe insert fails due to foreign key constraint violation.
CThe insert succeeds but DeptID is set to NULL.
DThe insert triggers creation of DeptID 10 in Departments.
Step-by-Step Solution
Solution:
  1. Step 1: Understand foreign key enforcement

    The Employees table's DeptID must exist in Departments table.
  2. Step 2: Check insert with non-existing DeptID

    Since DeptID 10 does not exist in Departments, the insert violates the foreign key constraint and fails.
  3. Final Answer:

    The insert fails due to foreign key constraint violation. -> Option B
  4. Quick Check:

    Foreign key violation = insert fails [OK]
Quick Trick: Foreign key requires existing parent key for insert [OK]
Common Mistakes:
MISTAKES
  • Assuming insert auto-creates parent record
  • Thinking foreign key allows NULL automatically
  • Believing insert succeeds despite missing parent key

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes