ALTER TABLE, DROP TABLE
Rename a table or a column, add a column to a reserved slot or drop one, or drop a table and reclaim its pages.
In this article
ALTER TABLE renames a table or a column, adds a column, or drops one. DROP TABLE removes a table. None of them rewrites a row: a rename changes the catalog, an added column takes a slot every row already has, a dropped column keeps its bytes, and a dropped table’s rows stay untouched until its pages are reclaimed after commit.
Syntax
ALTER TABLE <table> RENAME TO <new_name>
ALTER TABLE <table> RENAME COLUMN <column> TO <new_name>
ALTER TABLE <table> ADD [COLUMN] <column> <type> NULL
ALTER TABLE <table> DROP [COLUMN] <column>
DROP TABLE <table>
Examples
Rename a column
ALTER TABLE items RENAME COLUMN category TO section;
RENAMED COLUMN items.category TO section
Add and drop columns
A table can take new columns only if it reserved slots for them when it was created, with SPARE n:
kdbs> CREATE TABLE notes (id int64, title varchar(32), body varchar) SPARE 4
CREATED oid=4051
kdbs> INSERT INTO notes VALUES ('first', 'hello')
INSERTED oid=4051 id=1 page=159 slot=0
kdbs> ALTER TABLE notes ADD COLUMN pinned bool NULL
ADDED COLUMN notes.pinned
kdbs> ALTER TABLE notes ADD COLUMN tag varchar(16) NULL
ADDED COLUMN notes.tag
kdbs> SELECT * FROM notes
id title body pinned tag
-- ----- ----- ------ ----
1 first hello NULL NULL
kdbs> ALTER TABLE notes DROP COLUMN pinned
DROPPED COLUMN notes.pinned
A row written before the column was added reads NULL in it.
Drop a table
CREATE TABLE coupons (id int64, code char(8), percent_off int64);
DROP TABLE coupons;
DROPPED TABLE coupons oid=4047
RENAME
-
A table is identified internally by a number, not by its name, so after a rename every foreign key, index, Cabin and Transition keeps working without being declared again.
-
The primary-key column can be renamed like any other.
-
A new column name must differ, ignoring case, from the table’s other columns.
RENAME COLUMN owner TO OWNERis accepted: it changes only the spelling. -
A table that has an assertion cannot be renamed, and neither can its columns. Drop the assertion, rename, and declare it again.
kdbs> ALTER TABLE tickets RENAME TO tix ERR assertion 'seat_limit' stores its declaration against this relation's current names; DROP ASSERTION, rename, then re-declare it -
A rename is not transactional:
ROLLBACKdoes not undo it. -
Statistics the engine recorded for the old name stop matching. That can make queries slower until new traffic is recorded; it never changes a result.
ADD COLUMN
- The table must have a free slot, reserved by
SPARE natCREATE TABLE. A slot is 16 bytes, so the column may be any fixed-width type,char(N)with N up to 16, orvarchar(16). Avarchar(16)holds a value of any length; a value longer than 13 bytes is stored outside the row. - The column must be declared
NULL, because the rows that already exist have no value for it. There is noDEFAULT. REFERENCESand a Cabin policy are not accepted on an added column.- The name must differ, ignoring case, from every live column’s name. The name of a dropped column is free again.
- The new column goes after the existing ones, so
SELECT *lists it last.
DROP COLUMN
-
The column disappears from
SELECT *,DESCRIBEand name resolution at once. Its bytes stay in every row, and its slot is never reused: a laterADD COLUMNneeds another free slot. -
These columns cannot be dropped, and the error names what holds them:
- the primary key;
- a column an index keys or covers, that a foreign key names, that a Transition governs or that a Cabin is on;
- any column of a table that has an assertion.
kdbs> ALTER TABLE tickets DROP COLUMN passenger_id ERR column 'passenger_id' cannot be dropped (byte 32): index 'ix_tickets_passenger' keys or covers it; DROP INDEX first
After ADD or DROP
-
ADD COLUMNandDROP COLUMNare transactional: inside a transaction,ROLLBACKundoes them. They wait for open writes and running reads of the table, and a wait longer thanlock_wait_fault_net_ms(default 1 second) is refused with a retryableTXN_CONFLICT. -
Readers are not isolated from them. Until the change commits or rolls back, another session may see the new column set: a column missing from
SELECT *, or an added column readingNULL. No value it shows is wrong. -
A positional
INSERTmust name every column. Once a table’s columns have changed,INSERT ... VALUESwithout a column list must give one value for every live column, with the primary key writtenNULLto have it issued. The short form that leaves the key out is refused, because it cannot be told apart from a row written for the old columns:kdbs> INSERT INTO notes VALUES ('third', 'x', 'blue') ERR expected 4 value(s), one per column, with tuple key column 'id' NULL to have it issued; got 3 (byte 26). The relation's columns were changed by ALTER TABLE, so a row that leaves the key out is refused: it cannot be told from a row written for the old columnsAn
INSERTwith a column list, such asINSERT INTO notes (title, body, tag) VALUES ('fourth', 'y', 'green'), is never affected by a column change. See INSERT. -
A positional
INSERTwritten before a change can still be admitted with its values in the wrong columns: after aDROP, an old row that leaves its key out has the new full length. Use a column list in code that must survive column changes.
DROP TABLE
-
The name is free immediately. The table’s internal number is never reused.
-
The table’s indexes, Cabins, Transitions, columns and the foreign keys it declares as a child are dropped with it.
-
RESTRICT: a table that another table references with a foreign key cannot be dropped, even if no child row exists. A table with an assertion cannot be dropped either. Drop those first.
kdbs> DROP TABLE items ERR relation 'items' is referenced by a foreign key on 'purchases'; drop the referencing relation first -
DROP TABLEwaits for statements that are already reading the table and for open transactions that have written to it. If it waits longer thanlock_wait_fault_net_ms(default 1 second), it is refused with a retryableTXN_CONFLICT. -
Inside a transaction,
ROLLBACKrestores the table and its rows. A table with a Transition cannot be dropped inside a transaction; drop it in autocommit. -
The table’s pages return to the allocator after the drop commits and the log has moved past it, usually within a checkpoint or two. The data file never shrinks; later tables reuse the space.
Limitations
-
Type changes and widening a column are refused, and so are
MODIFYandSETforms. Every row has a fixed size, so changing a column’s width means rewriting the table:kdbs> ALTER TABLE accounts MODIFY owner varchar(80) ERR NOT_IMPLEMENTED retryable=0 ALTER TABLE MODIFY is not supported (byte 21); ALTER changes catalog rows and no tuple byte, and this form would rewrite every row of the relation (invariant 13) -
ADD COLUMNon a table with no free slot is refused withNOT_IMPLEMENTED. A table created withoutSPAREhas none. -
A dropped column’s bytes and slot are never reclaimed.
-
DROP TABLEis not isolated. Between theDROPand itsCOMMITorROLLBACK, other sessions already see the table as gone. If the transaction rolls back, it comes back. The rows are never at risk. -
A system table (
sys.*) cannot be renamed or dropped.