ALTER TABLE
Changes an existing table: adds, changes or drops a column, adds or drops a constraint, or renames the table, a column or an index.
Access SQL, with LibRed extensions. ACE accepts the ADD, ALTER COLUMN and DROP forms. The three
RENAME forms, and a calculated column in ADD COLUMN, are LibRed extensions, marked where they are described.
Syntax
Section titled “Syntax”ALTER TABLE table_name{ ADD [ COLUMN ] column_definition | ADD table_constraint | ALTER [ COLUMN ] column_name data_type [ column_constraint … ] | ALTER [ COLUMN ] column_name SET DEFAULT [ = ] expression | ALTER [ COLUMN ] column_name DROP DEFAULT | DROP COLUMN column_name | DROP CONSTRAINT constraint_name | RENAME TO new_table_name | RENAME COLUMN column_name TO new_column_name | RENAME INDEX index_name TO new_index_name}column_definition, column_constraint, table_constraint and data_type are as in
CREATE TABLE.
Arguments
Section titled “Arguments”table_name
Section titled “table_name”The table to change.
ADD [ COLUMN ] column_definition
Section titled “ADD [ COLUMN ] column_definition”Adds a column. COLUMN is optional. Rows already in the table read Null in the new column; a DEFAULT applies
to rows inserted afterwards.
The column’s own constraints apply to it as they do in CREATE TABLE: a PRIMARY KEY, a UNIQUE index and a
REFERENCES foreign key. A primary key fails the statement if the table already has one, or if the table has
rows, since they would all be Null. An AutoNumber is the exception: COUNTER numbers the existing rows 1, 2, …
so it can be made the primary key of a table that has rows.
A calculated column (AS (expression)) can be added too. This is a LibRed extension; see
CREATE TABLE.
The statement fails if the table already has a column of that name.
ADD table_constraint
Section titled “ADD table_constraint”Adds a PRIMARY KEY, UNIQUE, FOREIGN KEY or CHECK constraint. A foreign key checks the rows already in
the table, and the statement fails if one has no matching row in the referenced table. A primary key fails if the
table already has one, or if a row has a Null in a key column. An unnamed primary key is named PrimaryKey.
ALTER [ COLUMN ] column_name data_type
Section titled “ALTER [ COLUMN ] column_name data_type”Changes a column’s type or size. Every row is rewritten and its value converted to the new type. The statement
fails, and changes nothing, if a value cannot be converted: a Text value 'abc' cannot become a LONG.
After the type, the column may take NULL or NOT NULL, a DEFAULT and an IDENTITY, which set the column’s
nullability, default and AutoNumber as they would in CREATE TABLE. It may also take PRIMARY KEY, UNIQUE and
REFERENCES, which create the key, index or relationship as ADD COLUMN does. Each checks the rows already in the
table: a primary key fails if the table already has one or a row has a Null or a repeated value in the column, and a
relationship fails if a row has no matching row in the referenced table.
The statement fails for a column that is part of a relationship, on either side, or that a CHECK constraint
reads. Drop the relationship or constraint first.
On an AutoNumber column, COUNTER(seed, increment) changes where the counter goes next and nothing else: after
ALTER COLUMN Id COUNTER(1000, 1) the next row gets 1000. LONG turns an AutoNumber back into a plain Long
column that keeps its values.
ALTER [ COLUMN ] column_name SET DEFAULT expression
Section titled “ALTER [ COLUMN ] column_name SET DEFAULT expression”Sets or replaces the column’s default, without changing its type. The rules for the expression are those of
DEFAULT in CREATE TABLE.
ALTER [ COLUMN ] column_name DROP DEFAULT
Section titled “ALTER [ COLUMN ] column_name DROP DEFAULT”Removes the column’s default. A NOT NULL column stays NOT NULL, so an INSERT must then name a value for it.
DROP COLUMN column_name
Section titled “DROP COLUMN column_name”Drops a column and its values. The statement fails if the column is part of a relationship, is in an index or key, or is read by a calculated column. Drop those first.
DROP CONSTRAINT constraint_name
Section titled “DROP CONSTRAINT constraint_name”Drops a foreign key, primary key, unique constraint or CHECK constraint by name. A primary key or unique
constraint is an index, so this also drops any index by its name, as DROP INDEX does. An index
that a relationship uses cannot be dropped while the relationship exists; drop the foreign key first.
RENAME TO new_table_name
Section titled “RENAME TO new_table_name”LibRed extension. Renames the table. ACE has no SQL for any rename; Access renames through DAO. Relationships that name the table are updated, and keep their names and enforcement. A table in a relationship can be renamed.
RENAME COLUMN column_name TO new_column_name
Section titled “RENAME COLUMN column_name TO new_column_name”LibRed extension. Renames a column. Relationships that name the column are updated, and the column keeps its default and other properties.
RENAME INDEX index_name TO new_index_name
Section titled “RENAME INDEX index_name TO new_index_name”LibRed extension. Renames an index, a primary key or unique constraint included.
Remarks
Section titled “Remarks”Each statement makes one change. ALTER TABLE T ADD COLUMN A LONG, ADD COLUMN B LONG is a syntax error.
A rename does not change stored queries. A view or procedure that names the old table or column stops working until you recreate it. ACE behaves the same way: Access’s Name AutoCorrect, which fixes such queries, is a feature of the Access application, not of the engine.
ADD COLUMN and ALTER COLUMN raise the file’s format version when the new type needs it (BIGINT,
DATETIME2, a calculated column), as CREATE TABLE does. See
File format upgrade.
There is no column-level CHECK in ADD COLUMN. Add the column, then add the check with
ADD CONSTRAINT … CHECK (…).
Differences from ACE
Section titled “Differences from ACE”- The
RENAMEforms are LibRed extensions. ACE has no rename syntax. - A calculated column in
ADD COLUMNis a LibRed extension. ALTER COLUMNcan make a plain Long column an AutoNumber (ALTER COLUMN N COUNTER). ACE refuses this.DROP COLUMNrefuses a column a calculated column reads. ACE drops it and leaves the calculated column unreadable, with no way to repair it short of recreating the column.
Examples
Section titled “Examples”These run against the Northwind sample database, as in the playground. They follow on from each other, starting with a table of their own:
CREATE TABLE Contacts (ContactID COUNTER CONSTRAINT pkContacts PRIMARY KEY, CustomerID TEXT(5), Name TEXT(50))Add columns:
ALTER TABLE Contacts ADD COLUMN Email TEXT(100)ALTER TABLE Contacts ADD COLUMN Priority BYTE NOT NULL DEFAULT 1Add a foreign key to Customers and a CHECK:
ALTER TABLE Contacts ADD CONSTRAINT fkContactsCustomer FOREIGN KEY (CustomerID) REFERENCES Customers (CustomerID)ALTER TABLE Contacts ADD CONSTRAINT ckPriority CHECK (Priority BETWEEN 1 AND 5)Widen a column and make it required:
ALTER TABLE Contacts ALTER COLUMN Name TEXT(100) NOT NULLChange and remove a default:
ALTER TABLE Contacts ALTER COLUMN Priority SET DEFAULT 3ALTER TABLE Contacts ALTER COLUMN Priority DROP DEFAULTMake the AutoNumber continue from 1000:
ALTER TABLE Contacts ALTER COLUMN ContactID COUNTER(1000, 1)Drop a constraint and a column:
ALTER TABLE Contacts DROP CONSTRAINT ckPriorityALTER TABLE Contacts DROP COLUMN EmailRename a column, the primary key and the table (LibRed extensions):
ALTER TABLE Contacts RENAME COLUMN Name TO FullNameALTER TABLE Contacts RENAME INDEX pkContacts TO PrimaryKeyALTER TABLE Contacts RENAME TO CustomerContactsThis fails, because Orders.CustomerID is part of the relationship between Orders and Customers:
ALTER TABLE Orders ALTER COLUMN CustomerID TEXT(10)