我需要查找有关特定表的列详细信息。这包括有关该表当前使用的外键的详细信息,例如引用的表名和引用的列名。目前我使用这样的查询,因为它返回表是否包含外键,但不显示任何有关引用表名等的信息。我尝试了很多方法,但没有得到准确的数据。这里查询我尝试参考:我试过使用sys.foreign_key_columns和sys.foreign_keys但我得到了错误。可能是我没有正确放置它。
SELECT sys.columns.column_id AS ColumnId,sys.columns.name AS ColumnName,
sys.types.name AS Datatype,(SELECT COUNT(column_name) FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
WHERE TABLE_NAME = sys.tables.name AND CONSTRAINT_NAME =( SELECT constraint_name FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = sys.tables.name AND constraint_type =
'PRIMARY KEY' AND COLUMN_NAME = sys.columns.name ) ) AS IsPrimaryKey, ( SELECT COUNT(column_name)
FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE WHERE TABLE_NAME = sys.tables.name AND CONSTRAINT_NAME
= ( SELECT constraint_name FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = sys.tables.name
AND constraint_type = 'Foreign key' AND COLUMN_NAME = sys.columns.name ) ) AS IsForeignkey,
sys.columns.max_length AS Lenght /*BUG*/
FROM sys.columns, sys.types, sys.tables WHERE
sys.tables.object_id = sys.columns.object_id AND sys.types.system_type_id = sys.columns.system_type_id
AND sys.types.user_type_id = sys.columns.user_type_id AND sys.tables.name ='tablename' ORDER BY columnid
我尝试使用sys.foreign_key_columns和sys.foreign_keys但我得到了错误。可能是我没有正确放置它。