sqlserver如何查询一个表的主键都是哪些表的外键
sqlserver如何查询一个表的主键都是哪些表的外键
------解决方案--------------------select Fk.name, Fk.object_id, Fk.is_disabled, object_name(Fk.parent_object_id) as Fk_table_name, schema_name(Fk.schema_id) as Fk_table_schema, TbR.name as Pk_table_name, schema_name(TbR.schema_id) Pk_table_schema, col_name(Fk.parent_object_id, Fk_Cl.parent_column_id) as Fk_col_name, col_name(Fk.referenced_object_id, Fk_Cl.referenced_column_id) as Pk_col_name, Fk_Cl.constraint_column_id from sys.foreign_keys Fk left outer join sys.tables TbR on TbR.object_id = Fk.referenced_object_id inner join sys.foreign_key_columns Fk_Cl on Fk_Cl.constraint_object_id = Fk.object_id
where Fk.referenced_object_id = object_id(N'tablename')
order by Fk.object_id, Fk_Cl.constraint_column_id
------解决方案--------------------
select object_name(a.parent_object_id) 'tables'
from sys.foreign_keys a
where a.referenced_object_id=object_id('[表名]')