In relational databases, what is the difference between a primary key and a foreign key?

Prepare for the 241 Computer Science Certification Exam with comprehensive flashcards and multiple choice questions. Enhance knowledge with explanations and hints to excel in your test journey!

Multiple Choice

In relational databases, what is the difference between a primary key and a foreign key?

Explanation:
The main idea is that a primary key uniquely identifies each row in its own table, while a foreign key creates a link to a row in another table by referencing that table’s primary key. This pairing is what establishes and enforces the relationships between tables. Think of a Customers table with customer_id as its primary key. Each customer has a unique id. Now, an Orders table can include a column called customer_id that serves as a foreign key referencing Customers(customer_id). This means every order points to a valid customer, preserving referential integrity. The primary key guarantees each row in its table is unique, and the foreign key ensures relationships across tables are consistent. Why the other ideas don’t fit: a primary key is not allowed to be NULL and its job isn’t just indexing; it uniquely identifies rows. A foreign key does reference a row in another table to maintain the relationship. And the primary key isn’t what enforces referential integrity—that role belongs to the foreign key, while the primary key ensures uniqueness within its own table.

The main idea is that a primary key uniquely identifies each row in its own table, while a foreign key creates a link to a row in another table by referencing that table’s primary key. This pairing is what establishes and enforces the relationships between tables.

Think of a Customers table with customer_id as its primary key. Each customer has a unique id. Now, an Orders table can include a column called customer_id that serves as a foreign key referencing Customers(customer_id). This means every order points to a valid customer, preserving referential integrity. The primary key guarantees each row in its table is unique, and the foreign key ensures relationships across tables are consistent.

Why the other ideas don’t fit: a primary key is not allowed to be NULL and its job isn’t just indexing; it uniquely identifies rows. A foreign key does reference a row in another table to maintain the relationship. And the primary key isn’t what enforces referential integrity—that role belongs to the foreign key, while the primary key ensures uniqueness within its own table.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy