Sobes.tech
Junior — Middle

What is a primary key in a database and what properties does it have?

sobes.tech AI

Answer from AI

A Primary Key in a database is a field or a set of fields that uniquely identifies each record in a table.

Main properties of a primary key:

  • Uniqueness: the primary key values do not repeat in the table.
  • Cannot be NULL: each value must be specified.
  • Stability: primary key values should not change frequently.
  • Automatically indexed: to speed up search by key.

For example, in a users table, the user_id field can be the primary key to uniquely identify each user.