eolas/Databases/Primary_key.md

19 lines
1,014 B
Markdown
Raw Normal View History

2022-04-23 13:26:53 +01:00
---
2022-08-16 11:58:34 +01:00
categories:
2022-04-23 13:26:53 +01:00
- Databases
2022-08-21 11:00:04 +01:00
tags: [relational-databases]
2022-04-23 13:26:53 +01:00
---
2022-08-05 09:00:04 +01:00
# Primary key
2022-04-23 13:26:53 +01:00
2022-08-16 11:58:34 +01:00
> Every table in a relational database should have a **primary key**. A primary key is one **field that uniquely identifies each record**.
2022-04-23 13:26:53 +01:00
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.
2022-08-05 09:00:04 +01:00
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. In both cases the following constraints **must be met:**
2022-04-23 13:26:53 +01:00
1. No two records can have the **same** primary key data
1. 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.
1. A primary key value **must not be modified** once it has been created
1. A primary key **must have a value**; it cannot be `null`