0
0
SQLquery~5 mins

FOREIGN KEY constraint in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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
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
Which SQL keyword is used to define a FOREIGN KEY constraint?
ACHECK
BPRIMARY KEY
CUNIQUE
DFOREIGN KEY
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
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
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.