Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a foreign key in a database?
A foreign key is a column or set of columns in one table that refers to the primary key in another table. It creates a link between the two tables.
Click to reveal answer
beginner
Why do we use foreign keys?
Foreign keys help keep data consistent by ensuring that the value in one table matches a value in another table. This prevents errors like referencing a non-existent record.
Click to reveal answer
intermediate
How does a foreign key create a mental model of linking tables?
Think of a foreign key as a bridge connecting two tables. It helps you understand how data in one table relates to data in another, like linking a customer to their orders.
Click to reveal answer
intermediate
What happens if you try to insert a foreign key value that doesn't exist in the referenced table?
The database will reject the insert because it violates the foreign key constraint. This keeps data accurate and linked properly.
Click to reveal answer
intermediate
Can a foreign key be null? What does that mean?
Yes, a foreign key can be null if the relationship is optional. It means the record doesn't currently link to any record in the other table.
Click to reveal answer
What does a foreign key in a table do?
AStores duplicate data
BCreates a new table
CDeletes data from another table
DLinks to a primary key in another table
✗ Incorrect
A foreign key links to a primary key in another table to create a relationship.
What happens if you insert a foreign key value that does not exist in the referenced table?
AThe database creates a new record automatically
BThe insert is rejected
CThe foreign key value is ignored
DThe database crashes
✗ Incorrect
The database rejects the insert to maintain data integrity.
Which of these best describes a foreign key?
AA unique identifier for a table
BA column that stores text data
CA link to another table's primary key
DA way to speed up queries
✗ Incorrect
A foreign key links to another table's primary key to connect data.
Can a foreign key column contain NULL values?
AYes, if the relationship is optional
BNo, it must always have a value
COnly if the primary key is NULL
DOnly in the primary table
✗ Incorrect
Foreign keys can be NULL to indicate no current link.
What is the main purpose of a foreign key constraint?
ATo enforce data consistency between tables
BTo speed up data entry
CTo allow duplicate records
DTo delete related records automatically
✗ Incorrect
Foreign key constraints ensure data stays consistent and linked.
Explain in your own words how a foreign key links two tables in a database.
Think about how one table points to another like a bridge.
You got /3 concepts.
Describe what happens if you try to add a record with a foreign key value that does not exist in the referenced table.
Consider the database's role in keeping data accurate.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a foreign key in a database?
easy
A. To link one table to another and ensure data consistency
B. To store large amounts of text data
C. To speed up database queries
D. To create a backup of the database
Solution
Step 1: Understand the role of foreign keys
A foreign key connects one table to another by referencing a primary key in the related table.
Step 2: Identify the purpose of this connection
This connection helps keep data consistent and organized by preventing invalid data entries.
Final Answer:
To link one table to another and ensure data consistency -> Option A
Quick Check:
Foreign key = link tables + data consistency [OK]
Hint: Foreign keys link tables to keep data correct [OK]
Common Mistakes:
Thinking foreign keys store data themselves
Confusing foreign keys with indexes
Believing foreign keys speed up queries directly
2. Which of the following is the correct syntax to declare a foreign key in SQL?
easy
A. FOREIGN KEY column_name REFERENCES other_table
B. PRIMARY KEY (column_name) REFERENCES other_table(other_column)
C. FOREIGN KEY (column_name) REFERENCES other_table(other_column)
D. KEY FOREIGN (column_name) REFERENCES other_table(other_column)
Solution
Step 1: Recall the standard foreign key syntax
The correct syntax includes the keywords FOREIGN KEY, the column in parentheses, then REFERENCES followed by the referenced table and column in parentheses.
Step 2: Compare options to syntax
FOREIGN KEY (column_name) REFERENCES other_table(other_column) matches the correct syntax exactly. Other options have wrong keyword order or missing parentheses.
Final Answer:
FOREIGN KEY (column_name) REFERENCES other_table(other_column) -> Option C
Quick Check:
FOREIGN KEY + REFERENCES + (table.column) = A [OK]
Hint: FOREIGN KEY (col) REFERENCES table(col) is correct syntax [OK]
Common Mistakes:
Omitting parentheses around column names
Swapping PRIMARY KEY with FOREIGN KEY
Incorrect keyword order
3. Given these tables: CREATE TABLE Authors (AuthorID INT PRIMARY KEY, Name VARCHAR(50)); CREATE TABLE Books (BookID INT PRIMARY KEY, Title VARCHAR(100), AuthorID INT, FOREIGN KEY (AuthorID) REFERENCES Authors(AuthorID)); What happens if you try to insert INSERT INTO Books (BookID, Title, AuthorID) VALUES (1, 'My Book', 99); when there is no author with AuthorID = 99 in Authors?
medium
A. The insert succeeds but AuthorID is set to NULL
B. The insert fails due to foreign key constraint violation
C. The database creates a new author with AuthorID 99 automatically
A foreign key requires that the referenced value exists in the parent table to maintain data integrity.
Step 2: Apply this to the insert statement
Since AuthorID 99 does not exist in Authors, the insert violates the foreign key constraint and fails.
Final Answer:
The insert fails due to foreign key constraint violation -> Option B
Quick Check:
Foreign key requires existing parent row = D [OK]
Hint: Foreign key insert fails if parent key missing [OK]
Common Mistakes:
Assuming automatic creation of missing parent rows
Thinking insert will succeed with NULL foreign key
Ignoring foreign key constraints
4. Consider this table creation: CREATE TABLE Orders (OrderID INT PRIMARY KEY, CustomerID INT, FOREIGN KEY CustomerID REFERENCES Customers(CustomerID)); What is wrong with this statement?
medium
A. Foreign key cannot reference Customers table
B. CustomerID should be declared as PRIMARY KEY
C. PRIMARY KEY must be declared after FOREIGN KEY
D. Missing parentheses around the foreign key column name
Solution
Step 1: Check foreign key syntax
The foreign key column name must be enclosed in parentheses after FOREIGN KEY.
Step 2: Identify the error in the statement
The statement uses FOREIGN KEY CustomerID without parentheses, which is invalid syntax.
Final Answer:
Missing parentheses around the foreign key column name -> Option D
Quick Check:
FOREIGN KEY (col) needs parentheses [OK]
Hint: Always use parentheses around foreign key columns [OK]
Common Mistakes:
Omitting parentheses in FOREIGN KEY declaration
Misordering PRIMARY and FOREIGN KEY declarations
Confusing foreign key with primary key requirements
5. You have two 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) ON DELETE SET NULL); If a department is deleted, what happens to employees linked to that department?
hard
A. Their DeptID is set to NULL automatically
B. The delete is blocked and fails
C. Employees linked to that department are deleted
D. Nothing happens; DeptID remains unchanged
Solution
Step 1: Understand ON DELETE SET NULL behavior
This option means when the referenced row is deleted, the foreign key column in dependent rows is set to NULL.
Step 2: Apply to Employees and Departments
Deleting a department sets DeptID to NULL in Employees who referenced it, keeping employees but removing the link.
Final Answer:
Their DeptID is set to NULL automatically -> Option A
Quick Check:
ON DELETE SET NULL means foreign keys become NULL [OK]
Hint: ON DELETE SET NULL clears foreign keys on delete [OK]