// tables reference

ALTER TABLE DROP PRIMARY KEY

Removes the table's key. MySQL has a nameless clause, PostgreSQL and Oracle offer CASCADE, and SQL Server wants the constraint name plus an empty dependency list.

카테고리
테이블
In this category
8 of 16

구문

ALTER TABLE t DROP CONSTRAINT pk_name  -- SQL Server, PostgreSQL; SQL Server takes IF EXISTS from 2016
ALTER TABLE t DROP PRIMARY KEY  -- MySQL: nameless, the key is literally called PRIMARY
ALTER TABLE t DROP PRIMARY KEY CASCADE  -- Oracle: add KEEP INDEX to spare the backing index
-- SQLite: no DROP CONSTRAINT; rebuild the table without the key

Each block is a separate form. The support table and the examples show which engine takes which.

엔진 지원

엔진Support메모
SQL Server 예 drop by constraint name; if the key was clustered the table reverts to a heap
PostgreSQL 예 RESTRICT is the default: the drop refuses while foreign keys reference the key, CASCADE removes them
MySQL 예 DROP PRIMARY KEY; an AUTO_INCREMENT column with no other index must lose that attribute in the same statement
SQLite No equivalent no DROP CONSTRAINT; rebuild the table
Oracle 예 DROP CONSTRAINT name or DROP PRIMARY KEY; the backing index goes too unless you add KEEP INDEX

예시

SQL Server

-- find the name, then drop it
SELECT kc.name
FROM sys.key_constraints AS kc
WHERE kc.parent_object_id = OBJECT_ID(N'dbo.orders') AND kc.type = 'PK';
ALTER TABLE dbo.orders DROP CONSTRAINT pk_orders;

PostgreSQL

ALTER TABLE orders DROP CONSTRAINT pk_orders;
-- or take the referencing foreign keys down with it:
ALTER TABLE orders DROP CONSTRAINT pk_orders CASCADE;

MySQL

-- AUTO_INCREMENT blocks the drop; remove it in the same ALTER
ALTER TABLE orders MODIFY id BIGINT NOT NULL, DROP PRIMARY KEY;

Oracle

ALTER TABLE orders DROP PRIMARY KEY;
-- keep the index for a quick re-add later:
ALTER TABLE orders DROP PRIMARY KEY KEEP INDEX;

SQLite

-- rebuild without the key; PRAGMA foreign_keys is ignored inside
-- a transaction, so turn it off before BEGIN
PRAGMA foreign_keys = OFF;
BEGIN;
CREATE TABLE orders_new (id INTEGER NOT NULL, total NUMERIC);
INSERT INTO orders_new (id, total) SELECT id, total FROM orders;
DROP TABLE orders;
ALTER TABLE orders_new RENAME TO orders;
COMMIT;
PRAGMA foreign_keys = ON;

알아 두면 좋은 정보

  • MySQL refuses to drop the primary key when it is the only index on the AUTO_INCREMENT column, because every auto-increment column must sit in a key (error 1075). Remove the attribute and the key in one statement: ALTER TABLE t MODIFY id BIGINT NOT NULL, DROP PRIMARY KEY.
  • PostgreSQL's CASCADE drops every foreign key that references the key, here and on other tables, and reports them in a single NOTICE (drop cascades to N other objects) with one DETAIL line per constraint. Without it, the default RESTRICT behaviour fails the statement while references exist.
  • SQL Server checks references too: every foreign key pointing at the key must be dropped first, and disabling it with NOCHECK does not help (error 3725 either way). And when the key was the clustered index, dropping it turns the table back into a heap, which changes storage and can hurt range scans.
  • Oracle's KEEP INDEX keeps the backing index and its statistics while dropping the constraint, which is the cheap path when you plan to re-add a key over the same columns.

Questions about DROP PRIMARY KEY

How do I find my primary key's constraint name?

SQL Server: sys.key_constraints WHERE type = 'PK' and parent_object_id = OBJECT_ID(N'dbo.orders'). PostgreSQL: psql's \d orders, or pg_constraint WHERE contype = 'p'. Oracle: user_constraints WHERE constraint_type = 'P'. MySQL: no name to find, the clause is DROP PRIMARY KEY.

Why can I not drop the primary key on my MySQL auto-increment column?

MySQL allows one AUTO_INCREMENT column per table and it must be part of a key. Dropping the key would leave the column unkeyed, so the server refuses. Strip the attribute with MODIFY in the same ALTER TABLE and the drop goes through.

What happens to foreign keys that point at the key?

PostgreSQL and Oracle drop them with CASCADE, or refuse without it. SQL Server and MySQL refuse until you drop the referencing foreign keys yourself. The child rows are untouched either way; only the checks disappear.

Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.

// dialect-aware editing

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.