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
主键约束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.deFinitionFROM  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)
 
 
 获取表名及表的触发器
select  (select b.name from sysobjects as b where b.ID = a.parent_obj)  表名,   a.name  as 触发器    from sysobjects  as a where a.xtype='TR'order by 表名
 
备注:
xtype   char(2)   对象类型。可以是下列对象类型中的一种:       C   =   CHECK   约束     D   =   默认值或   DEFAulT   约束     F   =   FOREIGN   KEY   约束     L   =   日志     FN   =   标量函数     IF   =   内嵌表函数     P   =   存储过程     PK   =   PRIMARY   KEY   约束(类型是   K)     RF   =   复制筛选存储过程     S   =   系统表     TF   =   表函数     TR   =   触发器     U   =   用户表     UQ   =   UNIQUE   约束(类型是   K)     V   =   视图     X   =   扩展存储过程  
总结

以上是内存溢出为你收集整理的sqlserver数据库里sql 语句查看表里的约束,要查出 约束名、列名、约束类型、触发器全部内容,希望文章能够帮你解决sqlserver数据库里sql 语句查看表里的约束,要查出 约束名、列名、约束类型、触发器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

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

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

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存