Junior — Middle
Can you explain what a primary key in a database is and how it is used?
sobes.tech AI
Answer from AI
A Primary Key in a database is a unique identifier for each record in a table. It guarantees that each row can be uniquely identified.
Main properties of a primary key:
- Uniqueness: primary key values do not repeat.
- Cannot be NULL: each record must have a primary key value.
The primary key is used for:
- Fast data search and access.
- Table relationships through foreign keys.
Example in SQL:
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100)
);
Here, id is the primary key, uniquely identifying each user.