Difficulty: Easy
Correct Answer: A column or set of columns in one table that refers to the primary key of another table, enforcing referential integrity between the tables
Explanation:
Introduction / Context:
Relational databases organize data into related tables. These relationships are represented using keys. A foreign key is essential for linking rows in different tables and maintaining consistency across those relationships. Understanding foreign keys is fundamental for both database design and SQL querying.
Given Data / Assumptions:
Concept / Approach:
A foreign key is a column or set of columns in one table that references the primary key or a candidate key of another table. Its main purpose is to enforce referential integrity, which means that any value in the foreign key column must either match an existing primary key value in the referenced table or be NULL if allowed. This ensures that relationships such as customer to orders or department to employees are consistent.
Step-by-Step Solution:
Step 1: Recall that a primary key uniquely identifies rows in its own table.
Step 2: Understand that a foreign key lives in a different table and stores values that should correspond to the primary key in the parent table.
Step 3: Recognize that the database can enforce rules such as preventing deletion of a parent row when related child rows exist, unless cascading is configured.
Step 4: Compare this with the options and see that option a precisely describes a foreign key and mentions referential integrity.
Step 5: Confirm that other options either describe primary keys, null only columns, indexes, or data types, none of which match the role of a foreign key.
Verification / Alternative check:
Consider a simple example with a customer table and an orders table. The customer table has a primary key customer_id. The orders table has a column customer_id that references the customer table. This column is defined as a foreign key. If you try to insert an order with a customer_id that does not exist in the customer table, the foreign key constraint can block the insertion, preserving data consistency.
Why Other Options Are Wrong:
A unique identifier that serves as the primary key within its own table is the definition of a primary key, not a foreign key. A column that must always contain NULL values is unrelated to referential integrity. An index used only to speed up queries is a performance feature and does not automatically enforce relationships between tables. A data type used for storing text in multiple languages is not a key concept at all.
Common Pitfalls:
Students sometimes confuse foreign keys and primary keys or think that foreign keys must always be unique. In fact, foreign key columns commonly contain repeated values because many child rows can refer to the same parent row. It is also important to remember that foreign keys can be composite and can reference candidate keys, not just primary keys, depending on the design.
Final Answer:
A foreign key is a column or set of columns in one table that refers to the primary key of another table, enforcing referential integrity between the tables.
Discussion & Comments