现在的位置: 首页 > 综合 > 正文

t-sql数据库表的操作

2013年08月05日 ⁄ 综合 ⁄ 共 772字 ⁄ 字号 评论关闭
------------------------------列操作-----------------------------------
--
添加列
ALTER TABLE t ADD Mycolumn int identity(1,1)||not null default(0)
--删除列
alter table t drop column Mycolumn
--修改列
alter table t alter column Mycolumn varchar(20) not null

--将表中的数据改为为小数
update t
set groups=cast(groups as DECIMAL(10,3))
--将表字段的类型更改小数
alter table t alter column Yourcolumn DECIMAL(10,3))
------------------------------约束操作----------------------------------
--
加约束(默认值)
Alter table t Add constraint C_Mycolumn default(30) for Mycolumn
--加约束(check条件)
alter table dbo.t add constraint ck_t check ('字段'<>'字段')

alter table t with nocheck
add constraint t_sex check([name] in(N'',N''))
--加约束(主键)
alter table t add constraint t_id primary key(id)
--删除约束
alter table t drop constraint [DF_t_Mycolumn]
--加列加约束(默认值)
alter table t add Mycolumn int CONSTRAINT [DF_t_Mycolumn] not null default(0)

抱歉!评论已关闭.