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 constraint in a database?
A FOREIGN KEY constraint is a rule that links one table's column to another table's primary key column. It ensures that the value in the foreign key column matches a value in the referenced table, keeping data consistent.
Click to reveal answer
beginner
Why do we use FOREIGN KEY constraints?
We use FOREIGN KEY constraints to keep data accurate and connected. They prevent adding values in one table that don't exist in the related table, helping avoid mistakes and keeping relationships clear.
Click to reveal answer
intermediate
What happens if you try to insert a value in a FOREIGN KEY column that does not exist in the referenced table?
The database will reject the insert and show an error because the FOREIGN KEY constraint requires the value to exist in the referenced table to keep data consistent.
Click to reveal answer
intermediate
How do you define a FOREIGN KEY constraint when creating a table?
You define a FOREIGN KEY constraint by specifying the column and the referenced table and column using the syntax: FOREIGN KEY (column_name) REFERENCES referenced_table(referenced_column).
Click to reveal answer
intermediate
Can a FOREIGN KEY column contain NULL values?
Yes, a FOREIGN KEY column can contain NULL values unless you specify NOT NULL. NULL means no link to another table, which is allowed in many cases.
Click to reveal answer
What does a FOREIGN KEY constraint do?
ACreates a unique index on a column
BLinks a column in one table to a primary key in another table
CDeletes all rows in a table
DChanges the data type of a column
✗ Incorrect
A FOREIGN KEY constraint links a column in one table to a primary key in another table to maintain data consistency.
What happens if you insert a value in a FOREIGN KEY column that does not exist in the referenced table?
AThe database rejects the insert with an error
BThe value is converted to NULL
CThe referenced table is automatically updated
DThe insert succeeds without any error
✗ Incorrect
The database rejects the insert because the FOREIGN KEY constraint requires the value to exist in the referenced table.
Which SQL keyword is used to define a FOREIGN KEY constraint?
ACHECK
BPRIMARY KEY
CUNIQUE
DFOREIGN KEY
✗ Incorrect
FOREIGN KEY is the keyword used to define a foreign key constraint linking tables.
Can a FOREIGN KEY column have NULL values?
ANo, it must always have a value
BOnly if the referenced table allows NULL
CYes, unless NOT NULL is specified
DOnly if the column is a primary key
✗ Incorrect
A FOREIGN KEY column can have NULL values unless it is declared NOT NULL.
What is the main purpose of a FOREIGN KEY constraint?
ATo enforce referential integrity between tables
BTo store large text data
CTo speed up queries
DTo create backup copies of data
✗ Incorrect
The main purpose of a FOREIGN KEY constraint is to enforce referential integrity between tables.
Explain what a FOREIGN KEY constraint is and why it is important in databases.
Think about how tables stay connected and data stays accurate.
You got /4 concepts.
Describe how to create a FOREIGN KEY constraint when making a new table in SQL.
Remember the syntax FOREIGN KEY (column) REFERENCES table(column).
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a FOREIGN KEY constraint in a database?
easy
A. To store large amounts of text data efficiently
B. To speed up database queries by creating indexes
C. To link two tables by ensuring values in one table match values in another
D. To automatically backup the database
Solution
Step 1: Understand the role of FOREIGN KEY
A FOREIGN KEY connects columns in two tables to keep data related and consistent.
Step 2: Compare options with this role
Only To link two tables by ensuring values in one table match values in another describes linking tables by matching values, which is the purpose of FOREIGN KEY.
Final Answer:
To link two tables by ensuring values in one table match values in another -> Option C
Quick Check:
FOREIGN KEY links tables = A [OK]
Hint: FOREIGN KEY links tables by matching columns [OK]
Common Mistakes:
Confusing FOREIGN KEY with indexing
Thinking FOREIGN KEY stores data
Assuming FOREIGN KEY backs up data
2. Which of the following is the correct syntax to add a FOREIGN KEY constraint to an existing table Orders referencing Customers(CustomerID)?
easy
A. ALTER TABLE Orders ADD FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID);
B. ALTER TABLE Orders ADD PRIMARY KEY (CustomerID) REFERENCES Customers(CustomerID);
C. ALTER TABLE Orders ADD FOREIGN KEY CustomerID REFERENCES Customers(CustomerID);
D. ALTER TABLE Orders ADD FOREIGN KEY (CustomerID) TO Customers(CustomerID);
Solution
Step 1: Recall correct ALTER TABLE syntax for FOREIGN KEY
The correct syntax uses: ALTER TABLE table_name ADD FOREIGN KEY (column) REFERENCES other_table(column);
Step 2: Check each option
ALTER TABLE Orders ADD FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID); matches the correct syntax exactly. Options A, B, and C have syntax errors or wrong keywords.
Final Answer:
ALTER TABLE Orders ADD FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID); -> Option A
Quick Check:
Correct ALTER TABLE FOREIGN KEY syntax = D [OK]
Hint: Use ADD FOREIGN KEY (col) REFERENCES table(col) syntax [OK]
Common Mistakes:
Using PRIMARY KEY instead of FOREIGN KEY
Omitting parentheses around column name
Using TO instead of REFERENCES keyword
3. Given these tables: CREATE TABLE Customers (CustomerID INT PRIMARY KEY, Name VARCHAR(50)); CREATE TABLE Orders (OrderID INT PRIMARY KEY, CustomerID INT, FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)); What happens if you try to insert INSERT INTO Orders (OrderID, CustomerID) VALUES (1, 999); when there is no customer with CustomerID = 999?
medium
A. The insert fails due to FOREIGN KEY constraint violation
B. The insert succeeds but CustomerID is set to NULL
C. The insert succeeds and adds the order with CustomerID 999
D. The insert succeeds but triggers a warning
Solution
Step 1: Understand FOREIGN KEY enforcement
FOREIGN KEY requires the referenced value to exist in the parent table before inserting.
Step 2: Apply to the insert statement
Since CustomerID 999 does not exist in Customers, the insert violates the FOREIGN KEY rule and fails.
Final Answer:
The insert fails due to FOREIGN KEY constraint violation -> Option A
Quick Check:
Insert with missing parent key = fails [OK]
Hint: Insert fails if referenced key doesn't exist [OK]
Common Mistakes:
Assuming insert sets foreign key to NULL automatically
Thinking insert triggers only warnings, not errors
Believing insert succeeds without parent key
4. You have 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. PRIMARY KEY cannot be used with FOREIGN KEY in the same table
B. CustomerID must be declared as PRIMARY KEY
C. REFERENCES keyword is not allowed in FOREIGN KEY constraints
D. FOREIGN KEY must be declared with parentheses around the column name
Solution
Step 1: Check FOREIGN KEY syntax
FOREIGN KEY columns must be enclosed in parentheses, like FOREIGN KEY (CustomerID).
Step 2: Identify the error in the statement
The statement misses parentheses around CustomerID in FOREIGN KEY declaration, causing syntax error.
Final Answer:
FOREIGN KEY must be declared with parentheses around the column name -> Option D
Quick Check:
FOREIGN KEY columns need parentheses [OK]
Hint: Always use parentheses around FOREIGN KEY columns [OK]
Common Mistakes:
Omitting parentheses around foreign key columns
Thinking PRIMARY KEY conflicts with FOREIGN KEY
Misunderstanding REFERENCES usage
5. You want to delete a customer from Customers table who has orders in Orders table. The Orders table has a FOREIGN KEY on CustomerID referencing Customers(CustomerID) with ON DELETE CASCADE. What will happen when you delete that customer?
hard
A. The delete fails because orders exist for that customer
B. The customer is deleted and all their orders are automatically deleted
C. The customer is deleted but orders remain with invalid CustomerID
D. The delete succeeds but sets CustomerID in orders to NULL
Solution
Step 1: Understand ON DELETE CASCADE effect
ON DELETE CASCADE means deleting a parent row also deletes all related child rows automatically.
Step 2: Apply to deleting a customer with orders
Deleting the customer will also delete all orders linked by CustomerID in Orders table.
Final Answer:
The customer is deleted and all their orders are automatically deleted -> Option B
Quick Check:
ON DELETE CASCADE deletes related rows [OK]
Hint: ON DELETE CASCADE removes child rows with parent [OK]