学习是一种信仰。
分类: SQLServer
2014-01-22 16:51:58
sqlserver 约束分类如下:
primarykey(主键)约束;
foreignkey(外键)约束;
unique(唯一)约束;
check(检查)约束;
cascade(级联)约束;
1、check约束
sp_hel
sp_helpconstraint
2、default约束
3、unique约束
4、primarykey约束
5、foreignkey约束
6、cascade约束
约束的命名:
主键约束:PK_[table_name]_[column_name]_[n];
外键约束:FK_[table_name]_[column_name]_[references_table_name]_[references_column_name]_[n];
唯一性约束:UK_[table_name]_[column_name]_[n];
默认值约束:DF_[table_name]_[column_name]_[n];
核查约束:CK_[table_name]_[column_name]_[n];
禁用/启用约束:
约束不是一成不变的,有时候需要改变;
需要输入不满足当前约束,但又是合理的数据,不过以后不会出现违反约束的数据,这个时候,只需要将约束禁用,待新数据插入以后,再重新启用约束就可以了。
alter table table_name
check|nocheck constraint constraint_name
对表原始数据不校验添加约束:
禁用/启用约束中的约束,是在数据库表中数据已经存在的情况下创建的;
对表原始数据不校验添加约束,是数据库中表需要创建约束,表中的老数据不满足约束,而新数据都满足约束。
alter table table_name
with check|nocheck constraint contraint_name constraint_content
SQL SERVER 2000中各表外键名,主键名的获取
SELECT
外键表ID = b.fkeyid ,
外键表名称 = object_name(b.fkeyid) ,
外键列ID = b.fkey ,
外键列名 = (SELECT name FROM syscolumns WHERE colid = b.fkey AND id = b.fkeyid) ,
主键表ID = b.rkeyid ,
主键表名 = object_name(b.rkeyid) ,
主键列ID = b.rkey ,
主键列名 = (SELECT name FROM syscolumns WHERE colid = b.rkey AND id = b.rkeyid) ,
级联更新 = ObjectProperty(a.id,'CnstIsUpdateCascade') ,
级联删除 = ObjectProperty(a.id,'CnstIsDeleteCascade')
FROM sysobjects a
join sysforeignkeys b on a.id = b.constid
join sysobjects c on a.parent_obj = c.id
where a.xtype ='f'AND c.xtype ='U'
外键表ID 外键表名称 外键列ID 外键列名 主键表ID 主键表名 主键列ID 主键列名 级联更新 级联删除
71007334 input 2 productsize_id 1322487790 ProductSize 1 productsize_id 0 0
71007334 input 5 supplier_id 1498488417 supplier 1 supplier_id 0 0
71007334 input 3 product_id 1834489614 product 1 product_id 0 0
167007676 store 2 product_id 1834489614 product 1 product_id 0 0
407008531 sale 7 productsize_id 1322487790 ProductSize 1 productsize_id 0 0
407008531 sale 4 product_id 1834489614 product 1 product_id 0 0
1199343337 tb08_SendInfo 2 SaleID 407008531 sale 1 sale_id 0 0
1727345218 change_info 13 saleID 407008531 sale 1 sale_id 0 0
906486308 returninfo 19 ReturnCauseID 1167343223 tb08_ReturnCause 1 ReturnCauseID 0 0
1048390804 kind2 3 kind1_id 984390576 kind1 1 kind1_id 0 0
1626488873 kind3 3 kind2_id 1048390804 kind2 1 kind2_id 0 0
1611868809 orderinfo 16 orderSource_id 699865560 ordersource 1 orderSource_id 0 0
1754489329 ProductSequeMax 3 kind1_id 984390576 kind1 1 kind1_id 0 0
1834489614 product 4 supplier_id 1498488417 supplier 1 supplier_id 1 1
1834489614 product 3 productkind_id 1754489329 ProductSequeMax 1 productkind_id 1 1
747865731 output 3 product_id 1834489614 product 1 product_id 0 0
1160391203 presents 2 product_id 1834489614 product 1 product_id 0 0
1199343337 tb08_SendInfo 3 Post_ID 1931869949 postage 1 post_id 0 0
2058490412 price 2 product_id 1834489614 product 1 product_id 0 0
132911545 tb08_AdjustPriceInfo 2 priceID 2058490412 price 1 price_id 0 0
查询表主键constraint和表外键constraint的SQL
Oracle:
select o.obj# as objectId, o.name AS tableName, oc.name AS constraintName,
decode(c.type#, 1, 'C', 2, 'P', 3, 'U',
4, 'R', 5, 'V', 6, 'O', 7,'C', '?') as constraintType,
col.name AS columnName
from sys.con$ oc, sys.con$ rc,
sys.obj$ ro,sys.obj$ o, sys.obj$ oi,
sys.cdef$ c,
sys.col$ col, sys.ccol$ cc, sys.attrcol$ ac
where oc.con# = c.con#
and c.obj# = o.obj#
and c.rcon# = rc.con#(+)
and c.enabled = oi.obj#(+)
and c.robj# = ro.obj#(+)
and c.type# != 8
and c.type# != 12
and c.con# = cc.con#
and cc.obj# = col.obj#
and cc.intcol# = col.intcol#
and cc.obj# = o.obj#
and col.obj# = ac.obj#(+)
and col.intcol# = ac.intcol#(+)
and o.name = '你的表名'
SQL Server:
SELECT sysobjects.id objectId,
OBJECT_NAME(sysobjects.parent_obj) tableName,
sysobjects.name constraintName,
sysobjects.xtype AS constraintType,
syscolumns.name AS columnName
FROM sysobjects
INNER JOIN sysconstraints ON sysobjects.xtype in('C', 'F', 'PK', 'UQ', 'D')
AND sysobjects.id = sysconstraints.constid
LEFT OUTER JOIN syscolumns ON sysconstraints.id = syscolumns.id
WHERE OBJECT_NAME(sysobjects.parent_obj)='你的表名'
删除约束:
ALTER TABLE 你的表名 drop CONSTRAINT 外键约束名
命令查询:
sp_help
sp_heloconstraint
--删除约束
exec('alter table 表名 drop constraint ' + @csname)
--禁用约束(不校验)
exec('alter table 表名 nocheck constraint ' + @csname)
--启用约束(校验)
exec('alter table 表名 check constraint ' + @csname)
--添加约束
alter table 表名 add constraint 主键约束名 primary key (列名)