查詢出使用者自建表約束 (回帖內容)

2021-06-01 13:19:14 字數 2433 閱讀 1924

select object_name(object_id) as nameofconstraint, 

schema_name(schema_id) as schemaname,

object_name(parent_object_id) as tablename,

type_desc as constrainttype

from sys.objects

where type_desc like '%constraint'

查詢出使用者自建表約束

主鍵約束

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)

查詢出表中該列的約束名稱

1.查詢出表中該列的約束名稱 declare conname varchar 50 select conname b.name from sysobjects b join syscolumns a on b.id a.cdefault where a.id object id tbbrandsho...

mysql回表查詢

通俗的講就是,如果索引的列在 select 所需獲得的列中 因為在 mysql 中索引是根據索引列的值進行排序的,所以索引節點中存在該列中的部分值 或者根據一次索引查詢就能獲得記錄就不需要回表,如果 select 所需獲得列中有大量的非索引列,索引就需要到表中找到相應的列的資訊,這就叫回表。根據這個...

mysql 回表 什麼是MYSQL回表查詢

select id,name where name shenjian select id,name,where name shenjian 多查詢了乙個屬性,為何檢索過程完全不同?什麼是回表查詢?什麼是索引覆蓋?如何實現索引覆蓋?哪些場景,可以利用索引覆蓋來優化sql?這些,這是今天要分享的內容。畫...