ALTER TABLE DROP DEFAULT
Stops new inserts from picking up the old fallback. PostgreSQL and MySQL share one clause; SQL Server drops a named constraint; Oracle has no DROP at all; SQLite rebuilds.
- Categoría
- Columnas
- In this category
- 5 of 5
Sintaxis
ALTER TABLE t ALTER COLUMN c DROP DEFAULT -- PostgreSQL, MySQL
ALTER TABLE t DROP CONSTRAINT df_name -- SQL Server: the default is a constraint
ALTER TABLE t MODIFY c DEFAULT NULL -- Oracle: the way to remove a default
-- SQLite: no clause; rebuild the table without the default
Each block is a separate form. The support table and the examples show which engine takes which.
Soporte del motor
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Different syntax | drop the constraint by name; unnamed defaults got machine-generated names you must look up in sys.default_constraints |
| PostgreSQL | Sí | one clause, metadata only; afterwards an omitted nullable column inserts NULL |
| MySQL | Sí | one clause, metadata only (ALGORITHM=INSTANT); afterwards the column has no default at all, so strict mode rejects inserts that omit it even when it is nullable (SET DEFAULT NULL keeps a NULL fallback) |
| SQLite | No equivalent | no clause; rebuild the table, or use the writable_schema edit SQLite documents for default changes, which corrupts the database if the new CREATE TABLE text is wrong |
| Oracle | No equivalent | MODIFY c DEFAULT NULL is how Oracle drops a default; there is no DROP DEFAULT clause |
Ejemplos
PostgreSQL
ALTER TABLE orders ALTER COLUMN status DROP DEFAULT;
SQL Server
-- find the generated name first SELECT dc.name FROM sys.default_constraints AS dc WHERE dc.parent_object_id = OBJECT_ID(N'dbo.orders'); ALTER TABLE dbo.orders DROP CONSTRAINT DF__orders__status__5AEE82B9;
MySQL
ALTER TABLE orders ALTER COLUMN status DROP DEFAULT;
SQLite
-- no DROP DEFAULT: rebuild without the clause (shortened; -- the full procedure also re-creates indexes and triggers) 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;
Oracle
-- DEFAULT NULL means no default ALTER TABLE orders MODIFY status DEFAULT NULL;
Conviene saberlo
- Removing a default changes no stored value: existing rows keep what they have, and inserts that spell the column out keep working. Only inserts that omit the column notice the difference.
- On SQL Server the generated name is the obstacle: query sys.default_constraints for parent_object_id = OBJECT_ID(N'dbo.orders') to get it. DROP CONSTRAINT takes IF EXISTS from SQL Server 2016 on, but you still need the name.
- After the drop, a nullable column falls back to NULL on SQL Server, PostgreSQL and Oracle, and a NOT NULL column fails any insert that omits it. MySQL differs: DROP DEFAULT leaves the column with no default at all, so strict mode rejects an insert that omits it even on a nullable column (error 1364), and non-strict mode stores NULL or the type's implicit default ('' or 0) with a warning. On MySQL, use SET DEFAULT NULL when you want the NULL fallback.
- PostgreSQL and MySQL do this as a catalog change, so it is instant at any table size. Oracle's DEFAULT NULL is also metadata-only. SQLite's rebuild copies the whole table, indexes and triggers included.
Questions about DROP DEFAULT
How do I find the default constraint's name on SQL Server?
Query sys.default_constraints: SELECT name FROM sys.default_constraints WHERE parent_object_id = OBJECT_ID(N'dbo.orders') AND parent_column_id = COLUMNPROPERTY(OBJECT_ID(N'dbo.orders'), 'status', 'ColumnId'). Generated names look like DF__orders__status__5AEE82B9, which is why naming defaults at creation pays off.
What do inserts do once the default is gone?
A nullable column gets NULL where the column is omitted, except on MySQL: there DROP DEFAULT leaves no default at all, and strict mode fails the insert with error 1364 (SET DEFAULT NULL keeps the NULL fallback). A NOT NULL column makes the insert fail since there is no value to use; only non-strict MySQL lets it through, storing the type's implicit default with a warning. Inserts that name the column explicitly never notice the drop.
Why does Oracle use DEFAULT NULL to drop a default?
In Oracle a default is the value after the DEFAULT keyword, nothing more. Setting it to NULL is identical to having no default, so the engine never needed a DROP DEFAULT clause; MODIFY c DEFAULT NULL is the idiom.
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.