SQL: Alter Table
How to use the alter table SQL statement to modify existing database tables.
Edited: 2021-02-05 23:45
The alter table statement is used to change the composition of a table; a common usage is to change column names and data types, but it also allows adding and dropping columns to a table.
The below query will change the datatype of the test column in the my_test_table table:
alter table my_test_table modify column test varchar(200);
Adding and removing columns
To add a new column to an existing table:
alter table my_table_name add name_of_new_column varchar(255);
To remove a column from an existing table:
alter table my_table_name drop column name_of_column;
Changing the datatype or the length of a column
Sometimes we might need to change the datatype or length of an existing column, to do that we should use the modify column statement:
alter table my_test_table modify column test varchar(200);
We should carefully choose a datatype and length that suits the needs of our application.
Note. The maximum size of a varchar column is 65.535 characters.
Tell us what you think: