PRIMARY KEY sets the unique identifier, it allows two or more columns. A primary key columns implicitly have a NOT NULL and UNIQUE constraints.
TODO: int vs GUIDs (base on the course said, GUIDs is less efficient when matching foreign key)
It is common in Many-to-Many relationships.
When the primary key is set, Postgres automatically creates a unique B-tree index:
CREATE UNIQUE INDEX table_name_pkey ON table_name (col1, col2);
Learn More:Index
IDENTITYIs portable to other DBs and has two modes:
GENERATED ALWAYS (refuses manual INSERTs)GENERATED BY DEFAULT (allows manual INSERTs when needed)id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY
SERIALIs a legacy Postgres shortcut (~ PostgreSQL 9), not a SQL-standard type, not recommended. It can be manually inserted.
It is a macro that expands into: