Library tutorials & articles

MySQL Tutorial

Altering Tables

The ALTER TABLE command allows you to alter tables, fields and indexes of a table.

Renaming a Table

The RENAME modifier allows you to rename a table. The following example renames the table search to site.

mysql> ALTER TABLE search RENAME site;

Adding Columns to a Table

The ADD COLUMN modifier allows you to add a column to a table. The following example adds a field called modified to the search table.

mysql> ALTER TABLE search ADD COLUMN modified TIMESTAMP;

The FIRST and AFTER modifiers may be used to position the field within the table. The following places the new field as the first field in the table.

mysql> ALTER TABLE search ADD COLUMN modified TIMESTAMP FIRST;

The next example could be used to add the new field immediately after the field Keywords.

mysql> ALTER TABLE search ADD COLUMN modified TIMESTAMP AFTER Keywords;

Dropping a Column from a Table

The DROP COLUMN modifier may be used to drop a column from the table. The following example drops the field modified.

mysql> ALTER TABLE search DROP COLUMN modified;

Dropping a Indexes from a Table

The DROP INDEX modifier may be used to drop an index from a table. The following example drops the index keyIndex from the search table.

mysql> ALTER TABLE search DROP INDEX keyIndex;

Modifying columns

When you modify a column, you must specify the attributes for the column again. The following example renames the Description field to PageDescription in the search table.

mysql> ALTER TABLE search CHANGE Description PageDescription VARCHAR(255) NOT NULL;

If you only want to change the attributes, use the same column name as in the following example:

mysql> ALTER TABLE search CHANGE Description Description VARCHAR(200) NOT NULL;

Deleting a Table

Tables are deleted from the database using the DROP statement. The following example drops the table search from the database.

mysql> DROP TABLE search;

Comments

  1. 18 May 2009 at 17:00

    More good mysql examples:

    http://www.examplenow.com/mysql/alter

    --John

  2. 05 Aug 2008 at 13:25

     

    Dear Sir/ Madam

        I need your help, how to done replication method in MySQL Server. or if any automatic schedule scripting awailble. pls send my mail id as soon as possible.

    i will waiting for your mail

    email : vinaymeetu@ymail.com, or vinaywaiting4u@gmail.com,

    Regards

    Vinay.G.Liyar

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of MySQL Tutorial.

Leave a comment

Sign in or Join us (it's free).

Gez Lemon I'm available for contract work. Please visit Juicify for details.
AddThis

Related podcasts

  • Scaling Large Web Sites with Joe Stump, Lead Architect at DIGG

    Have you ever wanted to learn how top 100 web sites are architected? Deep Fried Bytes hosts Keith Elder and Chris Woodruff sat down with Joe Stump, Lead Architect at DIGG to discuss scaling large web sites, his life, development experiences and team building. Listen to the showThanks to our gues...

Events coming up

  • Dec 3

    The Auckland PHP December meetup

    Auckland, New Zealand

    Topic: Magento E-Commerce platform Speaker: Robert Popovic, LERO9, Robert is the Technical Director and co-founder of LERO9. Robert attended the Electrotechnical Faculty at The University of Belgrade where he graduated with a Masters in Computer Science and Information Technology. Robert has worked exclusively in the field of web and software development throughout his career.

Want to stay in touch with what's going on? Follow us on twitter!