Senior
What happens when you declare a field as a primary key in PostgreSQL?
sobes.tech AI
Answer from AI
When you declare a field as a primary key in PostgreSQL, the following happens:
- A uniqueness constraint (UNIQUE) is imposed on this field, meaning the values in this column must be unique.
- The field automatically becomes NOT NULL — NULL values are not allowed in a primary key.
- To ensure fast search and uniqueness verification, a unique index is created on this field.
Thus, the primary key guarantees the unique identification of each row in the table and ensures data integrity.
Example:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name TEXT
);
Here, id is the primary key, unique, and cannot be NULL.