Use ALTER TABLE query on MySQL database to change the table structure. For example you need to add extra column, delete some column or change the database type, size of any column.
Syntax:
ALTER TABLE table_name [alter_specification [, alter_specification] ...]
ALTER TABLE – Add Column
Use following example command to add new column to existing table at first column.
1 2 | ALTER TABLE table_name ADD new_column_name datatype FIRST; |
Adding new column after specific column.
1 2 | ALTER TABLE table_name ADD new_column_name datatype AFTER column_name; |
ALTER TABLE – Drop Column
You can delete any existing column from MySQL database table. You can face issue with deletion of column due to foreign key or other constraints.
1 2 | ALTER TABLE table_name DROP COLUMN column_name; |
ALTER TABLE – Modify Column
You can also modify the existing column of database table using alter table query. Remember that changing the column may loss of existing data. For example to shorten the size of filed may truncate the extra content from columns.
1 2 | ALTER TABLE table_name MODIFY COLUMN column_name datatype; |