创建表后,在表中添加一列

时间:2010-05-13 04:08:10

标签: sql

我已经创建了一个名为starsin的表。现在,我想使用查询在表address中添加列starsin。我怎么能这样做?

4 个答案:

答案 0 :(得分:1)

在标准SQL中,您需要alter table X add column Y ...命令。

如果您想填充所有现有行的列,则只需编制查询即可:update X set Y = ...


这是一个示例会话,向您展示运行中的alter table add column(对于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

答案 1 :(得分:0)

如果这是mysql,您可以ALTER TABLE stasin ADD COLUMN <columnname> <type> <options>

答案 2 :(得分:0)

请参阅here

代码:

ALTER TABLE Sales 
ADD UnitPrice MONEY;

输出: 命令已成功完成。 说明: 此示例通过指定新字段名称和数据类型将单个字段添加到“Sales”表。 语言:MS SQL Server

答案 3 :(得分:0)

ALTER TABLE <table name> ADD(<coloumn name> <datatype>);

正常SQL查询以添加列。在oracle中,它会起作用。