向表sql server添加一列零[duplicate]

时间:2021-12-28 12:59:11

Possible Duplicate:
Add column, with default value, to existing table in SQL Server

可能重复:将具有默认值的列添加到SQL Server中的现有表

I have a table in sql server, but want to add a extra column full of zeros

我在sql server中有一个表,但是想要添加一个充满零的额外列

What would be the best approach to do this

什么是最好的方法来做到这一点

att1 att2
---------
1.0   5.8
2.7   3.8
5.1   6.8

becomes

att1 att2  extra
----------------
1.0   5.8   0.0
2.7   3.8   0.0
5.1   6.8   0.0

2 个解决方案

#1


11  

If I recall correctly, it should be something like:

如果我没记错的话,应该是这样的:

ALTER TABLE table_name 
ADD extra REAL DEFAULT 0

See: http://msdn.microsoft.com/en-us/library/ms190273.aspx

请参阅:http://msdn.microsoft.com/en-us/library/ms190273.aspx

See: Add a column with a default value to an existing table in SQL Server

请参阅:将具有默认值的列添加到SQL Server中的现有表

#2


3  

I understand this column will always have value of 0.0. Then it does not have to be a real column

我知道这个列的值总是为0.0。那么它不一定是真正的专栏

CREATE TABLE extraColumn (
     att1   float  NULL
    ,att2   float  NULL
    ,extra AS 0.0 
)

But if you need that, you can persist it

但如果你需要,你可以坚持下去

CREATE TABLE extraColumn (
     att1   float  NULL
    ,att2   float  NULL
    ,extra AS 0.0  PERSISTED 
)

#1


11  

If I recall correctly, it should be something like:

如果我没记错的话,应该是这样的:

ALTER TABLE table_name 
ADD extra REAL DEFAULT 0

See: http://msdn.microsoft.com/en-us/library/ms190273.aspx

请参阅:http://msdn.microsoft.com/en-us/library/ms190273.aspx

See: Add a column with a default value to an existing table in SQL Server

请参阅:将具有默认值的列添加到SQL Server中的现有表

#2


3  

I understand this column will always have value of 0.0. Then it does not have to be a real column

我知道这个列的值总是为0.0。那么它不一定是真正的专栏

CREATE TABLE extraColumn (
     att1   float  NULL
    ,att2   float  NULL
    ,extra AS 0.0 
)

But if you need that, you can persist it

但如果你需要,你可以坚持下去

CREATE TABLE extraColumn (
     att1   float  NULL
    ,att2   float  NULL
    ,extra AS 0.0  PERSISTED 
)