![]() |
|
|
|
Introduction
The SQL ALTER TABLE statement is used to restructure a table.
Syntax ALTER TABLE [IF EXISTS] table_reference | [REDEFINE [COLUMN] [IF EXISTS] | [DROP [COLUMN] [IF EXISTS] column_name]] [, ADD [COLUMN] column_name [, ADD [CONSTRAINT constraint_name] [, DROP [CONSTRAINT constraint_name] PRIMARY KEY] [TEXT INDEX (column_name, [column_name])] [DESCRIPTION table_description] [INDEX PAGE SIZE index_page_size] [LOCALE locale_name | LOCALE CODE locale_code] [ENCRYPTED WITH password] [USER MAJOR VERSION user-defined_major_version] [LAST AUTOINC last_autoinc_value] [NOBACKUP] Use the ALTER TABLE statement to alter the structure of an existing table. It is possible to delete one column and add another in the same ALTER TABLE statement as well as redefine an existing column without having to first drop the column and then re-add the same column name. This is what is sometimes required with other database engines and can result in loss of data. DBISAM's REDEFINE keyword removes this problem. In addition, the IF EXISTS and IF NOT EXISTS clauses can be used with the ADD, REDEFINE, and DROP keywords to allow for action on columns only if they do or do not exist.
The DROP keyword requires only the name of the column to be deleted. The ADD keyword requires the same combination of column name, data type and possibly dimensions, and extended column definition information as the CREATE TABLE statement when defining new columns.
The statement below deletes the column FullName and adds the column LastName, but only if the LastName column doesn't already exist: ALTER TABLE Names It is possible to delete and add a column of the same name in the same ALTER TABLE statement, however any data in the column is lost in the process. An easier way is to use the extended syntax provided by DBISAM's SQL with the REDEFINE keyword: ALTER TABLE Names Note In order to remove the full text index completely, you would specify no columns in the TEXT INDEX clause like this: ALTER TABLE Customer NOBACKUP Clause
The NOBACKUP clause specifies that no backup files should be created during the process of altering the table's structure.
Please see the CREATE TABLE statement for more information on all other clauses used in the ALTER TABLE statement. Their usage is the same as with the CREATE TABLE statement.
|