sqlserver数据库里怎么查找一个表里的约束,要查出 约束名、列名、约束类型?用sql语句查询

sqlserver数据库里怎么查找一个表里的约束,要查出 约束名、列名、约束类型?用sql语句查询,第1张

主键约束

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)

我正在开发一个网站,它使用SQL Server 2008 R2 Express作为其数据库。而在测试中,有很多数据和图像存储在这个数据库中。 According to wiki, the SQL Server Express edition has a 10 GB size limit.当我插入数据并达到极限时,会抛出什么异常?或者,如何通过代码检测接近极限问题? 我使用代码优先的方法插入大型数据集。

这些都是建表是的一些 *** 作。其中的关系是指这张表和其他表的联系是什么,比如表a和表b之间:表a的主键在表b中为外键。这就需要在建表b的时候将外键列参照到表a的主键列。索引是为了给经常进行检索的列设置索引项,提高数据库检索性能。约束就更好理解了,比如给某一列添加什么什么的约束,就是用check约束了。比如:性别一列,值域只能是(男,女) 就是check(sex in (男,女))。大差不多,只要用的多了,自然而然的就知道这些东西了。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6764410.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-28
下一篇 2023-03-28

发表评论

登录后才能评论

评论列表(0条)

保存