tags:

views:

61

answers:

4

there is already some columns added in my table starsin.. i want to add a column address in my table starsin by using a query..

A: 

If this is mysql, you can do ALTER TABLE stasin ADD COLUMN <columnname> <type> <options>

webdestroya
+1  A: 

In standard SQL, you want the alter table X add column Y ... command.

If you then want to populate the column for all existing rows, it's a simple matter of working out the query and: update X set Y = ....


Here's an example session which shows you the alter table add column in action (for DB2):

> CREATE TABLE XYZ (F1 INTEGER);                       
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0  

> INSERT INTO XYZ VALUES (1);                          
DSNE615I NUMBER OF ROWS AFFECTED IS 1                      
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0  

> INSERT INTO XYZ VALUES (2);                          
DSNE615I NUMBER OF ROWS AFFECTED IS 1                      
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0  

> SELECT * FROM XYZ;                                   
F1                                                
--
 1                                                
 2                                                
DSNE610I NUMBER OF ROWS DISPLAYED IS 2                     
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 100

> ALTER TABLE XYZ ADD COLUMN F2 INTEGER;               
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0  

> UPDATE XYZ SET F2 = F1 + 7;                          
DSNE615I NUMBER OF ROWS AFFECTED IS 2                      
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0  

> SELECT * FROM XYZ;                                   
F1   F2                                   
--   --
 1    8                                   
 2    9                                   
DSNE610I NUMBER OF ROWS DISPLAYED IS 2                     
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 100
paxdiablo
do i have to drop a table afrer altering it?
Abid
...not unless you want to get rid of it
Matti Virkkunen
Good one, Matti :-) No, altering doesn't create a new table, it modifies the current table.
paxdiablo
A: 

See here

Code:

ALTER TABLE Sales 
ADD UnitPrice MONEY;

Output: The command(s) completed successfully. Explanation: This example adds a single field to the 'Sales' table by specifying the new field name and data type. Language(s): MS SQL Server

David Basarab
thanks mate :))
Abid
A: 

alter table <tablename> add(<coloumn name> <datatype>);

normal sql query to add a coloumn... in oracle it will work....

navinbecse