eolas/Databases/Primary_key.md
2022-04-23 18:30:04 +01:00

1 KiB

tags
Programming_Languages
Databases

Every table in a relational database should have a primary key. A primary key is one field that uniquely identifies each record.

This is essential for carrying out operations across database tables and for creating and deleting database entires. It is also a safeguard: it means you can always identify a record by itself and don't have to rely on generic queries to identify it.

Sometimes you will have a dedicated field such as UNIQUE_ID for the primary key. Other times you can use an existing field to fulfil that function (a bit like using the key attribute in React). In both cases the following constraints must be met:

  1. No two records can have the same primary key data
  2. The primary key value should never be reused. Thus, if a record is deleted from the table, it should not be re-allocated to a new record.
  3. A primary key value must not be modified once it has been created
  4. A primary key must have a value; it cannot be null