I need a T-SQL query to fetch all relations in a SQL Server database and understand that each relation is of multiple order like one-to-one, one-to-many and many-to-many relation.
I can fetch all relation's but can't know each relation is one-to-one, one-to-many.
My query is this :
SELECT
FK.[name] AS ForeignKeyConstraintName
,SCHEMA_NAME(FT.schema_id) + '.' + FT.[name] AS ForeignTable
,STUFF(ForeignColumns.ForeignColumns, 1, 2, '') AS ForeignColumns
,SCHEMA_NAME(RT.schema_id) + '.' + RT.[name] AS ReferencedTable
,STUFF(ReferencedColumns.ReferencedColumns, 1, 2, '') AS ReferencedColumns,
'one-to-one or one-to-many or many-to-many' as RelationType
FROM
sys.foreign_keys FK
INNER JOIN
sys.tables FT ON FT.object_id = FK.parent_object_id
INNER JOIN
sys.tables RT ON RT.object_id = FK.referenced_object_id
CROSS APPLY
(SELECT
', ' + iFC.[name] AS [text()]
FROM
sys.foreign_key_columns iFKC
INNER JOIN
sys.columns iFC ON iFC.object_id = iFKC.parent_object_id
AND iFC.column_id = iFKC.parent_column_id
WHERE
iFKC.constraint_object_id = FK.object_id
ORDER BY
iFC.[name]
FOR XML PATH('')) ForeignColumns (ForeignColumns)
CROSS APPLY
(SELECT
', ' + iRC.[name] AS [text()]
FROM
sys.foreign_key_columns iFKC
INNER JOIN
sys.columns iRC ON iRC.object_id = iFKC.referenced_object_id
AND iRC.column_id = iFKC.referenced_column_id
WHERE
iFKC.constraint_object_id = FK.object_id
ORDER BY
iRC.[name]
FOR XML PATH('')) ReferencedColumns (ReferencedColumns)
What I can do ?