I have to display a database diagram on a web application. When the user selects a database table I need to display relation tables and relations types as "one to one", "one to many" or many to many".
So far I was able to get all the details I need except the relationship type using the query below.
But I couldn't find a way to get a relationship type. Can you help me guys?
SELECT
t.name AS Parent_TableName,
COL_NAME(fc.referenced_object_id, fc.referenced_column_id) Parent_Id,
f.Name AS foreign_key_Name,
OBJECT_NAME(f.parent_object_id) ReferenceTableName,
COL_NAME(fc.parent_object_id, fc.parent_column_id) ColName
FROM
sys.foreign_keys AS f
INNER JOIN
sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id
INNER JOIN
sys.tables t ON t.OBJECT_ID = fc.referenced_object_id
WHERE
OBJECT_NAME (f.referenced_object_id) ='Category'
