SELECT
tab.name AS [表名],
idx.name AS [主键名称],
col.name AS [主键列名]
FROM
sys.indexes idx
JOIN sys.index_columns idxCol
ON (idx.object_id = idxCol.object_id
AND idx.index_id = idxCol.index_id
AND idx.is_primary_key = 1)
JOIN sys.tables tab
ON (idx.object_id = tab.object_id)
JOIN sys.columns col
ON (idx.object_id = col.object_id
AND idxCol.column_id = col.column_id)
唯一约束
SELECT
tab.name AS [表名],
idx.name AS [约束名称],
col.name AS [约束列名]
FROM
sys.indexes idx
JOIN sys.index_columns idxCol
ON (idx.object_id = idxCol.object_id
AND idx.index_id = idxCol.index_id
AND idx.is_unique_constraint = 1)
JOIN sys.tables tab
ON (idx.object_id = tab.object_id)
JOIN sys.columns col
ON (idx.object_id = col.object_id
AND idxCol.column_id = col.column_id)
外键约束
select
oSub.name AS [子表名称],
fk.name AS [外键名称],
SubCol.name AS [子表列名],
oMain.name AS [主表名称],
MainCol.name AS [主表列名]
from
sys.foreign_keys fk
JOIN sys.all_objects oSub
ON (fk.parent_object_id = oSub.object_id)
JOIN sys.all_objects oMain
ON (fk.referenced_object_id = oMain.object_id)
JOIN sys.foreign_key_columns fkCols
ON (fk.object_id = fkCols.constraint_object_id)
JOIN sys.columns SubCol
ON (oSub.object_id = SubCol.object_id
AND fkCols.parent_column_id = SubCol.column_id)
JOIN sys.columns MainCol
ON (oMain.object_id = MainCol.object_id
AND fkCols.referenced_column_id = MainCol.column_id)
Check约束
SELECT
tab.name AS [表名],
chk.name AS [Check约束名],
col.name AS [列名],
chk.definition
FROM
sys.check_constraints chk
JOIN sys.tables tab
ON (chk.parent_object_id = tab.object_id)
JOIN sys.columns col
ON (chk.parent_object_id = col.object_id
AND chk.parent_column_id = col.column_id)
主键约束(Primary Key constraint):要求主键列数据唯一,并且不允许为空。
唯一约束(Unique constraint):要求该列唯一,允许为空,但只能出现一个空值。
检查约束(Check constraint):某列取值范围限制,格式限制等,如有关年龄、邮箱(必须有@)的约束。
默认约束(Default constraint):某列的默认值,如在数据库里有一项数据很多重复,可以设为默认值。
外键约束(Foreign Key constraint):用于在两个表之间建立关系,需要指定引用主表的哪一列。
扩展资料
主键约束在表中定义一个主键来唯一确定表中每一行数据的标识符。
(非空,唯一)
例如:
alter table member
add
constraint PK_member_member_no primary key clustered (member_no)
主键列的数据类型不限,但此列必须是唯一并且非空。
如该表中已有主键为1000的行,则不能再添加主键为1000。
人工或程序不好控制的时候,也可以设置主键列为自动增长列。
主键主要用在查询单调数据,修改单调数据和删除单调数据上。做程序的时候,都将表的主键设置为int型的可自增的列,这样在编程的时候,很容易区分数据。
参考资料来源:百度百科-主键约束
参考资料来源:百度百科-约束
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)