What are the main characteristics that distinguish a primary key in a database?
sobes.tech AI
Answer from AI
Main characteristics of a primary key in a database:
-
Uniqueness: The primary key values must be unique for each record in the table. This guarantees unambiguous identification of each row.
-
Cannot be NULL: The field(s) used in the primary key cannot contain NULL, as this would violate uniqueness and identification.
-
Stability: The primary key values should not change frequently, as this can lead to data integrity and relationship issues.
-
Single column or composite key: The primary key can consist of one column or multiple columns (composite key).
-
Indexing: Usually, the database automatically creates an index on the primary key to speed up searches.
Example of creating a table with a primary key in SQL:
CREATE TABLE Users (
id INT PRIMARY KEY,
username VARCHAR(50),
email VARCHAR(100)
);
Here, id is the primary key, unique and cannot be NULL.