How to alter a mysql 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.

Syntax :

ALTER TABLE tbl_name alter_specification [, alter_specification] ...
The below table will describe the alter specification :
Alter SpecificationDescription
RenameRename a Table name
AddAdd a new column, key, index
Add FirstAdd a column First
Add AfterAdd a column After
DropDrop a column, Index, key
ChangeChange a column name
Change TypeChange a column type
ModifyModify a column type

The below descriptions shows how to alter mysql tables using queries.

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.
Next we move to the alterations in displaying the tables.

Ask Questions

Ask Question