# Modify the structure of a table - ALTER TABLE

## Modify the structure of a table - ALTER TABLE

ALTER TABLE is the command used to add, delete or modify columns in an existing table. This command is also used to add and remove various constraints on an existing table.

The basic syntax of an ALTER TABLE command to add a new column to an existing table is as follows

###### Syntax :
                                ALTER TABLE table_name
column2  data_type,
...
columnN  data_type);

###### Example 1 :

To add a column "address" to the table employees we use the following query

                                ALTER TABLE Employees ADD Address Varchar(100);


ADD can also be used to create a constraint on table columns

###### Syntax :
                                ALTER TABLE table_name

###### Example 1 :

The following query, will add a CHECK constraint to the Age column.

                                ALTER TABLE Employes
ADD CONSTRAINT ageConstraint CHECK(Age >= 18);


#### ALTER TABLE - DROP

##### Drop a column

DROP COLUMN is used to drop a column from a table. Remove unwanted columns from the table.

###### Syntax :
                                ALTER TABLE table_name
DROP COLUMN columnName;

###### Example 1 :

To remove the Age column from the Employees table, you can use the following query:

                                ALTER TABLE Employees
DROP COLUMN Age;

##### Remove a constraint

DROP CONSTRAINT can also be used to drop a constraint on table columns

###### Syntax :
                                ALTER TABLE table_name
DROP CONSTRAINT constraint_name;

###### Example 1 :

To remove the ageConstraint constraint on the Age column of the Employees table, you can use the following query:

                                ALTER TABLE Employees
DROP CONSTRAINT ageConstraint;


#### ALTER TABLE - MODIFY

It is used to modify existing columns in a table. Several columns can also be modified at the same time.

###### Syntax :
                                ALTER TABLE table_name
MODIFY columnName data_type;

###### Example 1 :

Suppose we want to change the data type of the Age column from int to Date. You can do this using the following query:

                                ALTER TABLE Employes
MODIFY Age Date;

                                    +---------+---------------+------+-----+---------+-------+
| Field   | Type          | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| Id      | int(11)       | NO   | PRI | NULL    |       |
| Nom     | varchar(20)   | NO   |     | NULL    |       |
| Age     | Date          | NO   |     | NULL    |       |
| Salaire | decimal(18,2) | YES  |     | NULL    |       |
+---------+---------------+------+-----+---------+-------+