Junior — Middle
What is the role of a primary key in a database table and why is it necessary?
sobes.tech AI
Answer from AI
A Primary Key is a unique identifier for each record in a database table. Its roles and necessity:
- Uniqueness: ensures that each row in the table is different from others.
- Identification: allows unambiguous reference to a specific record.
- Query optimization: primary keys are usually indexed, which speeds up search and table joins.
- Relationships between tables: used to create foreign keys, ensuring data integrity.
For example, in a users table, the id field can be a primary key to uniquely identify each user.