34,590
社区成员
发帖
与我相关
我的任务
分享
--创建测试表test_tb
create table test_tb(id int identity,col int)
--定义test_tb表的ID字段为主键
alter table test_tb add primary key(id)
--查询test_tb表有哪些约束
sp_helpconstraint test_tb
/*
Object Name
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_tb
constraint_type constraint_name delete_action update_action status_enabled status_for_replication constraint_keys
-------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- ------------- ------------- -------------- ---------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PRIMARY KEY (clustered) PK__test_tb__3213E83F20C1E124 (n/a) (n/a) (n/a) (n/a) id
*/
alter table #tb
add constraint id_PK primary key(id);
设置默认值alter table <表名> add <列名> <默认值类型> default '<默认值>'
设置主键alter table <表名> add constraint <主键名> primary key(<列名>)
设置联合主键alter table <表名> add constraint <主键名> primary key(<列名1>,<列名2>,...,<列名N>)
约束alter table <表名> add constraint <约束名> check (<约束语句>)在输入语句的时候<>是不需要的
column type primary key [,...]
------------------------
constrain column_PK
primary key (column,..l)