开发者

SQL Server table relationships

开发者 https://www.devze.com 2023-04-03 11:01 出处:网络
SQL Server Mgmt Studio 2005: could someone please help me understand how to view and print the relationships between tables, while show开发者_如何学Pythoning the columns in the tables. I did it many y

SQL Server Mgmt Studio 2005: could someone please help me understand how to view and print the relationships between tables, while show开发者_如何学Pythoning the columns in the tables. I did it many years ago and have been struggling for days to do it again.


Right click on the database name, Expand "Database Diagrams" and Select: "New Database Diagram".


You need to create and edit a database diagram, see this Getting started with SQL Server database diagrams and/or Designing Database Diagrams

you can do it with a query as well: SQL SERVER – Query to Display Foreign Key Relationships and Name of the Constraint for Each Table in Database

here is a query that will get the PKs, Check Constraints, and FKs to and from @TableName, with multi column constraints in a comma separated list:

DECLARE @TableName varchar(250)
SET @TableName='YourTable'
;WITH AllInfo AS
(
SELECT
    tc.TABLE_NAME,tc.CONSTRAINT_NAME, ccu.COLUMN_NAME
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS                  tc
        INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE  ccu ON ccu.TABLE_NAME = tc.TABLE_NAME AND ccu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
    WHERE tc.TABLE_NAME =@TableName
UNION
SELECT
    FK.TABLE_NAME,C.CONSTRAINT_NAME,CU.COLUMN_NAME
    FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS      C
        INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
        INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
        INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE  CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
        INNER JOIN (SELECT i1.TABLE_NAME, i2.COLUMN_NAME
                        FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS          i1
                            INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
                            WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY'
                   ) PT ON PT.TABLE_NAME = PK.TABLE_NAME
        WHERE PK.TABLE_NAME=@TableName
)
SELECT DISTINCT
    t1.TABLE_NAME,t1.CONSTRAINT_NAME
        ,STUFF(
                   (SELECT
                        ', ' + t2.COLUMN_NAME
                        FROM AllInfo t2
                        WHERE t1.TABLE_NAME=t2.TABLE_NAME AND t1.CONSTRAINT_NAME=t2.CONSTRAINT_NAME
                        ORDER BY t2.COLUMN_NAME
                        FOR XML PATH(''), TYPE
                   ).value('.','varchar(max)')
                   ,1,2, ''
              ) AS ColumnNames
    FROM AllInfo t1
    ORDER BY 1,2,3


You can create a Database Diagram and add all your tables to it. It will be a graphical representation if that's what you want.


you can use the following script in order to find all the fk,pk relationship for specific table

DECLARE @tablename VARCHAR(100)
SET @tablename='xxxxxxx'
Select 'Referenced by FK table' AS Type,  FK.TABLE_SCHEMA, FK.TABLE_NAME AS 'FK_TABLE_NAME' ,cu.COLUMN_NAME AS 'FK_ReferencingColumn',PK.TABLE_NAME AS 'PK_TABLE_NAME',
 ku.COLUMN_NAME AS 'PK_ReferencedColumn'
From INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS As RC
    Join INFORMATION_SCHEMA.TABLE_CONSTRAINTS As PK
        On PK.CONSTRAINT_NAME = RC.UNIQUE_CONSTRAINT_NAME
    Join INFORMATION_SCHEMA.TABLE_CONSTRAINTS As FK
        On FK.CONSTRAINT_NAME = RC.CONSTRAINT_NAME
   JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE cu
   ON cu.CONSTRAINT_NAME = Rc.CONSTRAINT_NAME
     JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE ku
ON ku.CONSTRAINT_NAME = RC.UNIQUE_CONSTRAINT_NAME
Where 
    PK.TABLE_NAME = @tablename
UNION  
SELECT 'Referencing PK table' AS Type, FK.TABLE_SCHEMA, FK.TABLE_NAME AS 'FK_TABLE_NAME' ,cu.COLUMN_NAME AS 'FK_ReferencingColumn',PK.TABLE_NAME AS 'PK_TABLE_NAME',
 ku.COLUMN_NAME AS 'PK_ReferencedColumn'
From INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS As RC
    Join INFORMATION_SCHEMA.TABLE_CONSTRAINTS As PK
        On PK.CONSTRAINT_NAME = RC.UNIQUE_CONSTRAINT_NAME
    Join INFORMATION_SCHEMA.TABLE_CONSTRAINTS As FK
        On FK.CONSTRAINT_NAME = RC.CONSTRAINT_NAME
   JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE cu
   ON cu.CONSTRAINT_NAME = Rc.CONSTRAINT_NAME
     JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE ku
ON ku.CONSTRAINT_NAME = RC.UNIQUE_CONSTRAINT_NAME
Where 
     fk.TABLE_NAME = @tablename
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号