Summary:In this tutorial you will learn how to use MySQL ALTER TABLE statement to change the structure of existing tables.

MySQL ALTER TABLE syntax

MySQL ALTER TABLE statement is used to change the structure of existing tables. You can use MySQL ALTER TABLE to add or drop column, change column data type, add primary key, rename table and a lot more. The following illustrates the MySQL ALTER TABLE syntax:

1 ALTER TABLE table_name action1[,action2,…]

Followed by the keyword ALTER TABLE is name of table that you want to make the changes. After the table name is the action you want apply to the table. An action can be anything from add a new column, add primary key…. to rename table. MySQL allows you to do multiple actions at a time, separated by a comma.

Let’s create a new table for practicing MySQL ALTER TABLE statement. We’re going to create a new table called tasks in our sample database classicmodels as follows:

1 CREATE TABLE 'tasks' (
2 'task_id' INT NOT NULL ,
3 'subject' VARCHAR(45) NULL ,
4 'start_date' DATETIME NULL ,
5 'end_date' DATETIME NULL ,
6 'description' VARCHAR(200) NULL ,
7 PRIMARY KEY ('task_id') ,
8 UNIQUE INDEX 'task_id_UNIQUE' ('task_id' ASC) );

Changing columns using MySQL ALTER TABLE statement

Using MySQL ALTER TABLE to add auto-increment for a column

Suppose we want the task id is increased by one automatically whenever we insert a new task. In order to accomplish this, we need to use the MySQL ALTER TABLE statement to change the column task id to make it auto increment as follows:

1 ALTER TABLE tasks
2 CHANGE COLUMN task_id task_id INT(11) NOT NULL AUTO_INCREMENT;

Using MySQL ALTER TABLE to add a new column into a table

Because of the new business requirement, we need to add a new column called complete to store completion percentage for each task in the tasks table. In this case, we can use MySQL ALTER TABLE to add a new column as follows:

1 ALTER TABLE tasks ADD COLUMN 'complete' DECIMAL(2,1) NULL
2 AFTER 'description' ;

Using MySQL ALTER TABLE to drop a column from a table

Let’s say we don’t want to store task description in the task table anymore so we have to remove that column. Here is the SQL command to drop a column from a table:

1 ALTER TABLE tasks
2 DROP COLUMN description ;

Renaming table using MySQL ALTER TABLE statement

We can use MySQL ALTER table statement to rename a table. Note that before renaming a table you should take a serious consideration to see its dependencies from database to application level. We can rename our tasks table to work_items as follows:

1 ALTER TABLE 'tasks'
2 RENAME TO 'work_items' ;

In this tutorial, you’ve learned how to use MySQL ALTER TABLE statement to change existing table structure and rename table.

Related posts:

  1. Managing Databases in MySQL
  2. Working with Database Table – Part I
  3. Understanding MySQL TIMESTAMP
  4. Understanding MySQL Table Types

Leave your comment

You must be logged in to post a comment.


Link Exchange Spritual Place for Bhakti | bhaktiguru.com Business Franchise in India | businessfranchiseindia.com Free Directory Submission & Link Submission | ab-directory.com Big Dwarka Business Listing | bigdwarka.com Kolkata Online Business Listing | onlinekolkata.com Patna Online Business Listing | onlinepatna.com Dwarka Online Business Listing | onlinedwarka.com Bihar Online | First Bihar's Portal | bihar-online.com Big Bihar Business Listing | bigbihar.com Dynamic Website Development | dynamicwebsitedeveloper.com Noida Online Business Listing | onlinenoida.com SEO Service Delhi & India | seoservicedelhi.com SEO Experts Delhi & India | seoexpertsdelhi.com Big Patna Business Listing | bigpatna.com Bihar schools Listing Free Schools Ad | biharschools.com Creative Websoft Solutions | Web Development India | creativewebsoft.us Bulk SMS Providers India | bulksmsproviders.net Online cbse Guides | onlinecbseguides.com Big Kolkata Business Listing | bigkolkata.com Handicap Shaadi.com | handicapshaadi.com Handicap Marriage.com | handicapmarriage.com India Home Tutors.com | indiahometutors.com Indian Property Guides.com | indianpropertyguides.com Online Stocks Community.com | onlinestockscommunity.com Free Article Submission | ab-articles.com Secret Dating Online.com | secretdatingonline.com Private DatingOnline.com | privatedatingonline.com Indian businessguides.com | indianbusinessguides.com Software Engineer Delhi | Meenu Khanna Bollywood & Hot Images | hibdy.com Free Computer & Online PHP Training | php2php.com Free Ignou Project & Assignment | ignouinfo.com PHP Programmer India | Web Developer India | bageshsingh.com Creative Websoft Solutions | Web Development IndiaBlog Link Exchange http://www.php2php.com/tutorial-blog/http://bageshsingh.com/bagesh-blog/http://www.bhaktiguru.com/bhaktigurublog/http://www.hibdy.com/desi-masala-blog/