Junior
What is a primary key?
sobes.tech AI
Answer from AI
A Primary Key is one or more fields (columns) in a database table that uniquely identify each record (row).
Main characteristics:
- Uniqueness: The value of the primary key must be unique for each row in the table.
- Not Null (NOT NULL): The primary key value cannot be NULL.
- Identification: Used to establish relationships between tables via foreign keys.
- Indexing: By default, the database automatically creates an index for the primary key to speed up search and join operations.
Example of creating a table with a primary key:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY, -- The 'id' column is the primary key
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(100)
);