ALTER TABLE ADD PRIMARY KEY
Turns an existing column or column list into the table's key. Four engines share one clause; the differences are the prerequisites, the lock you take and where the index lands.
- 類別
- 資料表
- In this category
- 7 of 16
語法
ALTER TABLE t ADD CONSTRAINT pk_name PRIMARY KEY (col [, ...]) -- SQL Server, PostgreSQL, MySQL, Oracle
ALTER TABLE t ADD CONSTRAINT pk_name PRIMARY KEY USING INDEX idx_name -- PostgreSQL: promote an existing unique index
-- SQLite: no ALTER form; the key belongs in CREATE TABLE
Each block is a separate form. The support table and the examples show which engine takes which.
引擎支援
| 引擎 | Support | 備註 |
|---|---|---|
| SQL Server | 是 | the column must already be NOT NULL; the key becomes the clustered index unless you write NONCLUSTERED or the table already has a clustered index |
| PostgreSQL | 是 | also accepts ADD PRIMARY KEY without a name; USING INDEX promotes a unique index you built CONCURRENTLY |
| MySQL | 是 | InnoDB stores rows in primary key order, so ADD PRIMARY KEY rebuilds the table; a CONSTRAINT name is accepted but the index is always named PRIMARY |
| SQLite | No equivalent | no ADD CONSTRAINT: declare the key in CREATE TABLE or rebuild the table |
| Oracle | 是 | creates the backing unique index unless you hand it one with USING INDEX |
範例
PostgreSQL
ALTER TABLE orders ADD CONSTRAINT pk_orders PRIMARY KEY (id);
PostgreSQL
-- build the index without blocking writes, then promote it CREATE UNIQUE INDEX CONCURRENTLY idx_orders_id ON orders (id); ALTER TABLE orders ADD CONSTRAINT pk_orders PRIMARY KEY USING INDEX idx_orders_id;
SQL Server
-- nullable columns are rejected: tighten the column first ALTER TABLE dbo.orders ALTER COLUMN id BIGINT NOT NULL; ALTER TABLE dbo.orders ADD CONSTRAINT pk_orders PRIMARY KEY (id);
MySQL
ALTER TABLE orders ADD PRIMARY KEY (id);
Oracle
-- reuse an index you already built instead of growing a new one CREATE UNIQUE INDEX idx_orders_id ON orders (id); ALTER TABLE orders ADD CONSTRAINT pk_orders PRIMARY KEY (id) USING INDEX idx_orders_id;
SQLite
-- the key must be in the table definition: rebuild CREATE TABLE orders_new ( id INTEGER PRIMARY KEY, customer_id INTEGER NOT NULL, status TEXT NOT NULL ); INSERT INTO orders_new SELECT id, customer_id, status FROM orders; DROP TABLE orders; ALTER TABLE orders_new RENAME TO orders;
小知識
- The ALTER fails on data before it fails on syntax: existing NULLs or duplicates stop the constraint in every engine. SQL Server adds a harder rule in front, refusing a nullable column until you ALTER COLUMN it to NOT NULL.
- On MySQL, and on SQL Server by default, the primary key is the clustered index, so adding one to a large table rewrites the whole table. SQL Server builds it offline, blocking the table, unless you add WITH (ONLINE = ON) (Enterprise and Developer editions); MySQL rebuilds in place and allows concurrent DML, but still copies every row. PostgreSQL takes ACCESS EXCLUSIVE and scans the table to build the index. Schedule all three.
- PostgreSQL's promotion path avoids the long scan under lock: CREATE UNIQUE INDEX CONCURRENTLY first (it cannot run inside a transaction and must not be partial or expression-based), then ADD CONSTRAINT ... PRIMARY KEY USING INDEX. The final ALTER is brief only when the key columns are already NOT NULL; otherwise it scans the whole table to set NOT NULL while holding ACCESS EXCLUSIVE. A DESC index is rejected too.
- Oracle drops the unique index it created with the constraint when you drop the constraint later, but an index you supplied with USING INDEX survives. MySQL's PRIMARY index and SQL Server's key index go with the drop too, and so does a PostgreSQL index, even one you promoted with USING INDEX.
Questions about ADD PRIMARY KEY
Can I add a primary key without a long lock?
On PostgreSQL, yes: build a unique index with CONCURRENTLY, then promote it with PRIMARY KEY USING INDEX; the last ALTER still takes an exclusive lock, which stays brief only if the key columns are already NOT NULL. MySQL and SQL Server have online DDL options that depend on version and edition, so check your release's docs before the maintenance window. SQLite always copies the table.
Why does SQL Server reject my ADD PRIMARY KEY?
The column is still nullable, and SQL Server demands NOT NULL before it will build the key. If no NULLs are present, ALTER COLUMN ... NOT NULL goes through; otherwise backfill them first, often with a DEFAULT or an UPDATE.
What is the difference between a primary key and a unique constraint?
A table gets at most one primary key and it allows no NULLs. A table can carry several unique constraints, and they accept NULLs: PostgreSQL, MySQL, SQLite and Oracle allow any number of NULL rows, while SQL Server and PostgreSQL 15+ with NULLS NOT DISTINCT allow just one. On MySQL, and on SQL Server by default, the primary key is also where the rows physically cluster.
Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.
DDL that matches the engine you're connected to
SQLly checks your statements against the dialect of the open connection, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle, so an ALTER TABLE that works on one engine gets flagged before it runs on another.