SQL - Alter Table
The alter clause changes a table by adding, removing, or modifying an existing table column.
The following example adds a new column to our table.
SQL Code:
ALTER TABLE employees
ADD dob varchar(10);
SQL Table:
| id | Lastname | Firstname | Title | dob |
| 1 | Johnson | David | crew | |
Now we will remove the same column from the table.
SQL Code:
ALTER TABLE employees
DROP dob;
SQL Table:
| id | Lastname | Firstname | Title |
| 1 | Johnson | David | crew |
SQL - Alter Columns
Alter is used again when changing attributes of the table cloumns. For instance if we wanted to change a table column name or change a column from a varchar to an interger type.
SQL Code:
ALTER TABLE 'employees' CHANGE 'ID' 'id' TINYINT ( 3 )
NOT NULL AUTO_INCREMENT;
Above we changed 'ID' to 'id' because SQL is case sensative and this will remove a potential bug issue. After we have selected a new name, we list the new attributes to be given to the altered column.
|