Junior — Middle
Could you explain the purpose of a primary foreign key in a database?
sobes.tech AI
Answer from AI
In a database, a foreign key is a field or a set of fields in one table that references the primary key of another table. This establishes a link between the tables and maintains data integrity.
Primary foreign key is a foreign key that references the primary key of another table. It ensures that the value in the foreign key field exists in the related table.
For example, there is a Users table with a primary key user_id, and an Orders table where user_id is a foreign key. This means each record in Orders is linked to an existing user from Users.
This mechanism prevents the appearance of "dangling" references and ensures data consistency between tables.