SQL: Alter Table

How to use the alter table SQL statement to modify existing database tables.

606 views

Edited: 2021-02-05 23:45

Alter table, SQL

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:

  1. An in-dept look at the use of headings (h1-h6) and sections in HTML pages.
  2. Pagination can be a confusing thing to get right both practically and programmatically. I have put a lot of thought into this subject, and here I am giving you a few of the ideas I have been working with.
  3. The best way to deal with a trailing question mark is probably just to make it a bad request, because it is a very odd thing to find in a request URL.
  4. How to optimize image-loading and automatically include width and height attributes on img elements with PHP.
  5. HTTP headers are not case-sensitive, so we are free to convert them to all-lowercase in our applications.

More in: Web development