求SQL语句: 添加int型新列 默认值为0,并修改已有数据为默认值0

gaogongge 2011-06-15 10:30:16
如题!求解!谢谢!
...全文
9293 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
gaogongge 2011-06-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 qianjin036a 的回复:]

SQL code
create table tb(id int,col int)
insert into tb select 1,3 union all select 2,6
go
alter table tb add col1 int not null default 0
go
select * from tb
/*
id col col1
-------……
[/Quote]

嗯 是的!加个not null 确实能搞定!
嘎嘎!谢谢!
-晴天 2011-06-15
  • 打赏
  • 举报
回复
上面这是一句话搞定的.
-晴天 2011-06-15
  • 打赏
  • 举报
回复
create table tb(id int,col int)
insert into tb select 1,3 union all select 2,6
go
alter table tb add col1 int not null default 0
go
select * from tb
/*
id col col1
----------- ----------- -----------
1 3 0
2 6 0

(2 行受影响)
*/
go
drop table tb
gaogongge 2011-06-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 qianjin036a 的回复:]

SQL code
update tbname set columnname=0
[/Quote]
有没有一句话搞定的啊?
哈哈 我也想到了你的办法!
我google到这么一句话alter table table_name add column_name nvarchar(20) default ('') with values 1.with values用于修改已有记录。

但是看不懂!
GoAwayZ 2011-06-15
  • 打赏
  • 举报
回复
alter table aa add 列名 int default  0 
update aa set 列名=0
gaogongge 2011-06-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qianjin036a 的回复:]

添加新列,会有已有数据吗?
[/Quote]
已有数据是指 之前表中存储的。添加这个列后,该列值为默认值

表T
id name
1 wang
变为
表T1
id name age
1 wang 0
-晴天 2011-06-15
  • 打赏
  • 举报
回复
update tbname set columnname=0
-晴天 2011-06-15
  • 打赏
  • 举报
回复
ALTER TABLE dbo.tbname ADD
columnname int NULL DEFAULT 0
GO
-晴天 2011-06-15
  • 打赏
  • 举报
回复
添加新列,会有已有数据吗?

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧