-- 各表外键信息 SELECT object_name(parent_object_id) 表名, object_name(constraint_object_id) 约束名称, col_name(parent_object_id, parent_column_id) 外键字段, object_name(referenced_object_id) 关联表名, col_name(referenced_object_id, referenced_column_id) 关联表字段 FROM sys.foreign_key_columns WHERE referenced_object_id IN ( SELECT object_id(T.table_name) FROM ( SELECT table_name FROM information_schema.columns WHERE TABLE_SCHEMA = 'dbo' ) T ) ORDERBY object_name(parent_object_id)
-- 各表索引信息 SELECT object_name(object_id) 表名, name 索引名, is_unique 是否唯一 FROM sys.indexes WHERE Object_id IN ( SELECT object_id(T.table_name) FROM ( SELECT table_name FROM information_schema.columns WHERE TABLE_SCHEMA = 'dbo' ) T ) ORDERBY 表名