Web www.tizag.com

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:

idLastnameFirstnameTitledob
1JohnsonDavidcrew

Now we will remove the same column from the table.

SQL Code:

ALTER TABLE employees
DROP dob;

SQL Table:

idLastnameFirstnameTitle
1JohnsonDavidcrew

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.





New - Tizag.com Forums!
Recent Forum Topics: