Bird
0
0

Given these tables:

medium📝 query result Q4 of 15
SQL - Table Relationships
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 try to insert INSERT INTO Employees VALUES (1, 'Alice', 10); when there is no department with DeptID 10?
AThe insert fails due to foreign key constraint violation.
BThe insert succeeds and adds the row.
CThe insert succeeds but sets DeptID to NULL.
DThe insert deletes the Departments table.
Step-by-Step Solution
Solution:
  1. Step 1: Understand foreign key enforcement on insert

    The foreign key requires DeptID in Employees to exist in Departments.
  2. Step 2: Analyze the insert statement

    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 A
  4. Quick Check:

    Foreign key violation blocks insert = The insert fails due to foreign key constraint violation. [OK]
Quick Trick: Foreign key blocks inserts with missing referenced keys [OK]
Common Mistakes:
MISTAKES
  • Assuming insert succeeds without referenced key
  • Thinking foreign key sets missing values to NULL
  • Believing foreign key deletes other tables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes