继承了遗留的Sql server数据库之后,我想按表列出所有没有外键的整数列,其中一些是缺少FK的,有些只是整数列。有人可以建议查询来显示这些信息吗?显然,这里涉及一些人为干预。
感谢。
答案 0 :(得分:3)
您可以从information_schema获取它。我会从这样的事情开始:
SELECT * from INFORMATION_SCHEMA.COLUMNS c
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE k
ON
c.TABLE_CATALOG = k.TABLE_CATALOG
AND c.TABLE_SCHEMA = k.TABLE_SCHEMA
AND c.TABLE_NAME = k.TABLE_NAME
AND c.COLUMN_NAME = k.COLUMN_NAME
WHERE c.DATA_TYPE in ('int') AND CONSTRAINT_NAME is null
答案 1 :(得分:0)
这个使用仅限sql-server的视图:
select so.name as TableName, col.name as ColumnName
from
sys.objects so
inner join sys.columns col on so.object_id = col.object_id and col.system_type_id in (48,52,56,127)
left outer join sys.foreign_key_columns fkc on fkc.parent_object_id = so.object_id and fkc.parent_column_id = col.column_id
where
so.type='U'
and fkc.constraint_column_id is null
order by so.name