|
ALTER TABLE :
ALTER TABLE is used to change the structure of an existing table. We can add or delete columns, change the type of existing columns, or rename columns or the table itself. We can also change the comment for the table and type of the table.
The Syntax is
ALTER TABLE tbl_name alter_specification [, alter_specification] ...
The below table will describe the alter specification :
Renaming a Table :
We can also RENAME the table using ALTER TABLE. The following example query renames the table student to class.
mysql> ALTER TABLE student RENAME class;
The above query will change the table name.
Adding a column to a table :
The ADD COLUMN modifier is used to add a column to a table. The following example query adds a field called marks to the student table.
mysql> ALTER TABLE student ADD COLUMN marks INT(10);
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| studid | int(10) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| address | varchar(40) | YES | | NULL | |
| phone | int(10) | YES | | NULL | |
| marks | int(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
Next we move to the alterations in displaying the tables.
|