PostgreSQL Enterprise (50 Qs)•Part 1: Basic & Foundational Concepts (Junior / Entry-Level)
What are Sequence objects and the `SERIAL` vs `GENERATED ALWAYS AS IDENTITY` types?
junior LevelCode ExamplepostgresqlBasic
In Simple Words:
SERIAL(Legacy): Creates an implicit standalone sequence and sets the column default tonextval('seq_name'). Problem: Dropping the table or copying schemas can leave dangling sequences or permissions issues.GENERATED ALWAYS AS IDENTITY(SQL Standard - Best Practice): Integrates the sequence directly into the column definition. Prevents accidental manual insertion of duplicate IDs and complies with ANSI SQL standards:
sql
CREATE TABLE accounts (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name TEXT NOT NULL
);