eolas/Databases/Primary_key.md

20 lines
1 KiB
Markdown
Raw Normal View History

2022-04-23 13:26:53 +01:00
---
tags:
- Programming_Languages
- Databases
2022-08-05 09:00:04 +01:00
- 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
> 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.
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`