Saturday, July 23, 2011

Alter Table Add Column Syntax


The SQL syntax for ALTER TABLE Add Column is
ALTER TABLE "table_name"
ADD "column 1" "Data Type"

Let's look at the example. Assuming our starting point is the "customer" table created in the CREATE TABLEsection:
Table customer
Column NameData Type
First_Namechar(50)
Last_Namechar(50)
Addresschar(50)
Citychar(50)
Countrychar(25)
Birth_Datedate

Our goal is to add a column called "Gender". To do this, we key in:
MySQL:
ALTER TABLE customer ADD Gender char(1);
Oracle:
ALTER TABLE customer ADD Gender char(1);
SQL Server:
ALTER TABLE customer ADD Gender char(1);
Resulting table structure:
Table customer
Column NameData Type
First_Namechar(50)
Last_Namechar(50)
Addresschar(50)
Citychar(50)
Countrychar(25)
Birth_Datedate
Genderchar(1)

Note that the new column Gender becomes the last column in the customer table.
It is also possible to add multiple columns. For example, if we want to add a column called "Email" and another column called "Telephone", we will type the following:
MySQL:
ALTER TABLE customer ADD (Email char(30), Telephone char(20));
Oracle:
ALTER TABLE customer ADD (Email char(30), Telephone char(20));
SQL Server:
ALTER TABLE customer ADD (Email char(30), Telephone char(20));
The table now becomes:
Table customer
Column NameData Type
First_Namechar(50)
Last_Namechar(50)
Addresschar(50)
Citychar(50)
Countrychar(25)
Birth_Datedate
Genderchar(1)
Emailchar(30)
Telephonechar(20)



0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Deep's | Bloggerized by Deep - Deep's Templates | ElearSQL-Server