ADD PRIMARY KEY lets you declare one or more columns as the table’s unique identifier, improving query planning and data modeling in Amazon Redshift.
Primary keys are metadata only—Redshift does not enforce uniqueness—yet they guide the query planner and SVV tables, enabling better join strategies and vacuum operations.
Use ALTER TABLE for existing tables or specify the constraint inside CREATE TABLE. Give the constraint a meaningful name for maintainability.
Embed the PRIMARY KEY clause directly after the column list.This approach keeps DDL concise and avoids later metadata changes.
Run ALTER TABLE … ADD CONSTRAINT. Redshift rewrites only system metadata, so the statement is fast even on large tables.
Yes—list multiple columns inside the parentheses.Composite keys are common in join tables like OrderItems.
Pick low-cardinality columns, avoid wide composite keys, and co-locate distribution style with primary key where possible to minimize data movement.
Declaring a primary key and assuming uniqueness enforcement is the biggest pitfall. Another mistake is forgetting to ANALYZE after bulk loads, which leaves the planner blind.
.
No. Redshift writes only catalog metadata and skips validation. Ensure uniqueness before declaring the constraint.
Yes. Use ALTER TABLE … DROP CONSTRAINT or ADD a new one. Remember to refresh dependent views.
It can. Redshift’s optimizer uses key information to eliminate duplicate scans and choose better join types, especially with DISTKEY aligned.