ALTER TABLE, DROP TABLE

v3.2.0

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 OWNER is 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: ROLLBACK does 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 n at CREATE TABLE. A slot is 16 bytes, so the column may be any fixed-width type, char(N) with N up to 16, or varchar(16). A varchar(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 no DEFAULT.
  • REFERENCES and 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 *, DESCRIBE and name resolution at once. Its bytes stay in every row, and its slot is never reused: a later ADD COLUMN needs 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 COLUMN and DROP COLUMN are transactional: inside a transaction, ROLLBACK undoes them. They wait for open writes and running reads of the table, and a wait longer than lock_wait_fault_net_ms (default 1 second) is refused with a retryable TXN_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 reading NULL. No value it shows is wrong.

  • A positional INSERT must name every column. Once a table’s columns have changed, INSERT ... VALUES without a column list must give one value for every live column, with the primary key written NULL to 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 columns
    

    An INSERT with a column list, such as INSERT INTO notes (title, body, tag) VALUES ('fourth', 'y', 'green'), is never affected by a column change. See INSERT.

  • A positional INSERT written before a change can still be admitted with its values in the wrong columns: after a DROP, 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 TABLE waits for statements that are already reading the table and for open transactions that have written to it. If it waits longer than lock_wait_fault_net_ms (default 1 second), it is refused with a retryable TXN_CONFLICT.

  • Inside a transaction, ROLLBACK restores 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 MODIFY and SET forms. 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 COLUMN on a table with no free slot is refused with NOT_IMPLEMENTED. A table created without SPARE has none.

  • A dropped column’s bytes and slot are never reclaimed.

  • DROP TABLE is not isolated. Between the DROP and its COMMIT or ROLLBACK, 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.

See also