Middle+
What is a primary key and what are its constraints?
sobes.tech AI
Answer from AI
A Primary Key is a unique identifier for a record in a database table. It guarantees the uniqueness of each row and is used for quick data retrieval and establishing relationships between tables.
Main constraints of a primary key:
- Uniqueness: values in the primary key column(s) must be unique to unambiguously identify a record.
- Cannot be NULL: the primary key must have a value; NULL is not allowed.
- Immutability: although technically possible to change, in practice, primary key values are not changed to maintain data integrity.
Example of creating a table with a primary key in SQL:
CREATE TABLE Users (
UserID INT PRIMARY KEY,
UserName VARCHAR(100),
Email VARCHAR(100)
);