sql如何给一个字段设置默认值

zhongxingqiche 2009-04-08 09:59:47
我在SQL2000中的企业管理器中右键点击表,然后新建表,建立如下表:

id(自动编号的列) a(money类型的) b(int类型的)

我想着在建立表后就能使字段a 和 b就自动有默认值0了。我在建立表的时候在,我在表设计器中的默认值那加入0,结果还是不行。



我建立完后的结果是:
id(自动编号的列) a(money类型的) b(int类型的)
1 null null
2 null null

我希望建立完表后的结果是:
id(自动编号的列) a(money类型的) b(int类型的)
1 0 0
2 0 0
a和b字段就自动加入0了。请高手指点,我希望在表设计器中完成这个工作。谢谢!!!
...全文
13771 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
疾风铸境 2011-05-25
  • 打赏
  • 举报
回复
create table tb(id int not null identity(1,1),a money not null default 0,b int not null default 0)
alter table tb add username nvarchar(50);
alter table tb add constraint df_a default 'admin' for username;
select * from tb;
zhongxingqiche 2009-04-08
  • 打赏
  • 举报
回复
感谢指导。已经结贴给分了。谢谢!!!!!
zhongxingqiche 2009-04-08
  • 打赏
  • 举报
回复
问题解决,感谢指导啊。结贴给分了。
usher_gml 2009-04-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 mugua604 的回复:]
SQL code
create table tabeName(id varchar(10) not null default '',b int not null default 0)
[/Quote]

UP
zhongxingqiche 2009-04-08
  • 打赏
  • 举报
回复
create table tb(id int not null identity(1,1),a money not null default 0,b int not null default 0)

这个语句运行后并没有出现的这样的结果啊。

id(自动编号的列) a(money类型的) b(int类型的)
1 0 0
2 0 0
zhongxingqiche 2009-04-08
  • 打赏
  • 举报
回复
在表设计器中如何操作呢?
肥龙上天 2009-04-08
  • 打赏
  • 举报
回复

alter table tb
add constraint df_a default 默认值 for username
mugua604 2009-04-08
  • 打赏
  • 举报
回复

create table tabeName(id varchar(10) not null default '',b int not null default 0)

appleller 2009-04-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 jia_guijun 的回复:]
SQL codecreate table tb(id int not null identity(1,1),a money not null default 0,b int not null default 0)
[/Quote]

ok
jia_guijun 2009-04-08
  • 打赏
  • 举报
回复
create table tb(id int not null identity(1,1),a money not null default 0,b int not null default 0)
mugua604 2009-04-08
  • 打赏
  • 举报
回复
not null default 0
jia_guijun 2009-04-08
  • 打赏
  • 举报
回复
设置为not null

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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