default的小问题

zhangyanli 2007-12-23 09:47:04
请问:
ALTER TABLE abc
ALTER COLUMN dt datetime default(getdate())
在default附近有语法错误

建库:
create table abc
(
name varchar(3) not null,
id int not null,
age int not null,
dt datetime not null default(getdate())
)
...全文
199 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
-狙击手- 2007-12-23
  • 打赏
  • 举报
回复
select
[约束名]=a.Name,
[默认值]=b.text
from
sysobjects a
join
syscomments b on a.ID=b.ID
where parent_obj=object_id('abc') and xtype='D'
zhangyanli 2007-12-23
  • 打赏
  • 举报
回复
问题是我创建时没有约束名,所以没法drop
-狙击手- 2007-12-23
  • 打赏
  • 举报
回复



create table abc
(
name varchar(3) not null,
id int not null,
age int not null,
dt datetime not null CONSTRAINT AddDateDflt default(getdate())
)

go

insert abc(name,id,age) select 'asd',1,10
select * from abc




ALTER TABLE abc drop constraint AddDateDflt


insert abc(name,id,age,dt) select 'asf',1,10,'2007-12-22'
select * from abc

drop table abc
/*
(所影响的行数为 1 行)

name id age dt
---- ----------- ----------- ------------------------------------------------------
asd 1 10 2007-12-23 22:31:01.237

(所影响的行数为 1 行)


(所影响的行数为 1 行)

name id age dt
---- ----------- ----------- ------------------------------------------------------
asd 1 10 2007-12-23 22:31:01.237
asf 1 10 2007-12-22 00:00:00.000

(所影响的行数为 2 行)
*/
中国风 2007-12-23
  • 打赏
  • 举报
回复
create table T(ID int default (1))

--
select
[约束名]=a.Name
[默认值]=b.text
from
sysobjects a
join
syscomments b on a.ID=b.ID
where parent_obj=object_id('T') and xtype='D'

--
alter table T drop constraint 约束名
zhangyanli 2007-12-23
  • 打赏
  • 举报
回复
我不是建的约束,怎么去掉默认值啊
dawugui 2007-12-23
  • 打赏
  • 举报
回复
如果列已有默认值,必须除去旧默认值后才能添加新默认值。
dawugui 2007-12-23
  • 打赏
  • 举报
回复
建库:
create table abc
(
name varchar(3) not null,
id int not null,
age int not null,
dt datetime not null default(getdate())
)
你不已经创建成功了吗?
wzy_love_sly 2007-12-23
  • 打赏
  • 举报
回复
alter table table add constraint px default getdate() for dt

34,838

社区成员

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

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