4D v14

ALTER TABLE

Home

 
4D v14
ALTER TABLE

ALTER TABLE  


 

 

ALTER TABLE sql_name
{ADD column_definition [PRIMARY KEY] [TRAILING] |
DROP sql_name |
ADD
primary_key_definition |
DROP PRIMARY KEY
|
ADD
foreign_key_definition |
DROP CONSTRAINT
sql_name |
[{ENABLE | DISABLE} REPLICATE] |
[{ENABLE | DISABLE} LOG] |
[{ENABLE | DISABLE} AUTO_INCREMENT] |
[{ENABLE | DISABLE} AUTO_GENERATE] |
SET SCHEMA sql_name}

The ALTER TABLE command is used to modify an existing table (sql_name). You can carry out one of the following actions:
Passing ADD column_definition adds a column to the table. The PRIMARY KEY keyword is used to set the primary key when a column is added.
The TRAILING keyword forces column to be created after the last existing column of the table in the structure file. This option is useful when columns containing data have been deleted from the table (without the data being erased), to prevent existing data from being reassigned to the new column.
Passing DROP sql_name removes the column named sql_name from the table.
Passing ADD primary_key_definition adds a PRIMARY KEY to the table.
Passing DROP PRIMARY KEY removes the PRIMARY KEY of the table.
Passing ADD foreign_key_definition adds a FOREIGN KEY to the table.
Passing DROP CONSTRAINT sql_name removes the specified constraint from the table.
Passing ENABLE REPLICATE or DISABLE REPLICATE enables or disables the mechanism allowing replication of the table (see the Replication via SQL section).
Passing ENABLE LOG or DISABLE LOG enables or disables journaling for the table.
Passing ENABLE AUTO_INCREMENT or DISABLE AUTO_INCREMENT enables or disables the "Autoincrement" option for Longint type fields.
Passing ENABLE AUTO_GENERATE or DISABLE AUTO_GENERATE enables or disables the "Auto UUID" option for Alpha fields of the UUID type.
Passing SET SCHEMA sql_name transfers the table to the sql_name schema.

The command returns an error:

  • when the optional ENABLE LOG parameter is passed and no valid primary key is defined,
  • if you attempt to modify or delete the definition of the table’s primary key without disabling journaling by means of DISABLE LOG.

Example  

This example creates a table, inserts a set of values into it, then adds a Phone_Number column, adds another set of values and then removes the ID column:

CREATE TABLE ACTOR_FANS
(ID INT32, Name VARCHAR);

INSERT INTO ACTOR_FANS
(ID, Name)
VALUES(1, 'Francis');

ALTER TABLE ACTOR_FANS
ADD Phone_Number VARCHAR;

INSERT INTO ACTOR_FANS
(ID, Name, Phone_Number)
VALUES (2, 'Florence', '01446677888');

ALTER TABLE ACTOR_FANS
DROP ID;

 
PROPERTIES 

Product: 4D
Theme: SQL Commands

 
SEE ALSO 

column_definition
CREATE TABLE
DROP TABLE
foreign_key_definition
primary_key_definition

 
ARTICLE USAGE

4D SQL Reference ( 4D v14)
4D SQL Reference ( 4D v14 R2)

Inherited from : ALTER TABLE ( 4D v12.1)
Parent of : ALTER TABLE ( 4D v14 R3)