Altering a Table

Use the ALTER keyword, along with the following to:

  • ADD adds a column to a table
  • CHANGE changes a column in a table
  • DROP deletes a column from a table

ADD a new field called Team to the table:

mysql> alter table Students ADD Team varchar(20) ;
Query OK, 8 rows affected (0.29 sec)
Records: 8 Duplicates: 0 Warnings: 0

 

Now let's CHANGE that field to a different name and type:

mysql> alter table Students CHANGE Team Fees int(10) ;
Query OK, 8 rows affected (0.30 sec)
Records: 8 Duplicates: 0 Warnings: 0

 

Finally, let's DROP the field:

mysql> alter table Students DROP Fees ;
Query OK, 8 rows affected (0.30 sec)
Records: 8 Duplicates: 0 Warnings: 0

 

 

A new field can be placed as the first column by adding FIRST at the end of the command:

mysql> alter table Students DROP Fees ;
Query OK, 8 rows affected (0.30 sec)
Records: 8 Duplicates: 0 Warnings: 0

Leave a Reply