要求为表中的transdate设一个约束,要它默认显示当前的日期,我写了程序
constraint transdate_deful
check default datetime for transdate
错误信息如下:
服务器: 消息 156,级别 15,状态 1,行 1
在关键字 'constraint' 附近有语法错误。 应该怎么写呢?
constraint transdate_deful
check default datetime for transdate
错误信息如下:
服务器: 消息 156,级别 15,状态 1,行 1
在关键字 'constraint' 附近有语法错误。 应该怎么写呢?
create table index_tb(id int constraint ck_id default 0,name varchar(20),xb varchar(2))
--查看表
exec sp_helpindex orders--索引
exec sp_helpconstraint index_tb --约束
exec sp_fkeys 'dbo.Person'--外键
--键不唯一,非聚集索引
create index id_index on index_tb(id)
--删除索引
drop index index_tb.id_index
--键聚集索引
create clustered index id_index on index_tb(id)
--删除索引
drop index index_tb.id_index
--创建check约束
alter table index_tb add constraint ck_index CHECK (name like '[0-9][0-9][0-9][0-9][0-9]')
alter table index_tb add constraint ck2_index check(xb in ('MM','GG'))
--删除check约束
alter table index_tb drop constraint ck2_index
--禁止表约束
alter table index_tb nocheck constraint ck_index
--恢复表约束
alter table index_tb check constraint ck_index
--禁止和恢复所有check-all关键字
select 'alter table '+name+' nocheck or check constraint all' from sysobjects where type='U'
--添加新默认值和约束
ALTER TABLE index_tb ADD CONSTRAINT de_name DEFAULT ('11111') FOR name
--删除表约束
alter table index_tb drop constraint de_name
-- 字段 UNIQUE NOT NULL = PRIMARY KEY 差不多
--创建unique
alter table index_tb add constraint uniqueindex unique(id)
--删除unique
alter table index_tb drop constraint uniqueindexinsert into index_tb(name,xb) select '12345','mm'select * from index_tbdelete index_tb
ALTER TABLE 你的表 ADD CONSTRAINT de_name DEFAULT (getdate()) FOR transdate
ALTER TABLE index_tb ADD CONSTRAINT de_name DEFAULT ('11111') FOR name
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tb](
[id] [int] NULL,
[date_dt] [datetime] DEFAULT (getdate())
) ON [PRIMARY]insert into tb(id) select 2
select * from tb
--
是不是上面想要的效果