SQL Server table relationships - sql

SQL Server Mgmt Studio 2005: could someone please help me understand how to view and print the relationships between tables, while showing 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

Related

Loop through current table to find if column is a primary key?

I have a temp table called #ColumnList that holds a list of column names. I need to loop through each row in this table and look to see if the column is a primary key. If it is a key then I need to add that column name to a column called PRIMARYKEYCOLUMN.
This is what I have so far but it doesn't work. It throws an error
Cannot insert the value NULL into column 'DataType', table 'tempdb.dbo.#ColumnList__________________0000000B0D8C'; column does not allow nulls. INSERT fails.
ALTER TABLE #ColumnList
ADD PRIMARYKEYCOLUMN VARCHAR(50);
INSERT INTO #ColumnList ([PRIMARYKEYCOLUMN])
(SELECT DISTINCT KU.column_name as PRIMARYKEYCOLUMN
FROM
sys.columns c
JOIN sys.types t ON t.user_type_id = c.user_type_id
AND t.system_type_id = c.system_type_id
JOIN sys.tables tab ON c.object_id = tab.object_id
JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC ON TC.TABLE_NAME = tab.name
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KU
ON TC.CONSTRAINT_TYPE = 'PRIMARY KEY' AND
TC.CONSTRAINT_NAME = KU.CONSTRAINT_NAME AND
KU.table_name='myTableName'
JOIN #ColumnList cl ON cl.ColumnName = KU.COLUMN_NAME)
The subquery above (without the JOIN #ColumnList cl ON cl.ColumnName = KU.COLUMN_NAME) works fine and returns the results below. The insert doesn't work however.
[PRIMARYKEYCOLUMN]
Column1
Column2
Column3
I need those same columns above to be inserted in the temp table where those same column names exist
An update statement might work, something like :
update
#ColumnList
set
PRIMARYKEYCOLUMN = ColumnName
where
ColumnName in (
SELECT
DISTINCT KU.column_name
FROM
sys.columns c
JOIN
sys.types t
ON t.user_type_id = c.user_type_id
AND t.system_type_id = c.system_type_id
JOIN
sys.tables tab
ON c.object_id = tab.object_id
JOIN
INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC
ON TC.TABLE_NAME = tab.name
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KU
ON TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
AND TC.CONSTRAINT_NAME = KU.CONSTRAINT_NAME
AND KU.table_name='myTableName'
)

sql select column names where the column is a key

using this query,
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME <table>
how would i modify it to select only column names that are primary keys? or keys of any type?
Using the same type of select is prefered.
SELECT *
FROM INFORMATION_SCHEMA.Table_Constraints tc
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
ON tc.TABLE_NAME = kcu.TABLE_NAME
AND tc.TABLE_SCHEMA = kcu.TABLE_SCHEMA
AND tc.TABLE_CATALOG = kcu.TABLE_CATALOG
AND tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME
WHERE tc.TABLE_NAME = 'table_name'

Query to get all primary keys in a database

Merged with Get a List of all Primary Keys in a Database.
Is this the best way to - Get a List of all Primary Keys in a Database - or is there something better?
SELECT
KCU.TABLE_NAME AS Table_Name,
KCU.CONSTRAINT_NAME AS Constraint_Name,
KCU.COLUMN_NAME AS COLUMN_NAME
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU
ON KCU.CONSTRAINT_SCHEMA = TC.CONSTRAINT_SCHEMA
AND KCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
AND KCU.TABLE_SCHEMA = TC.TABLE_SCHEMA
AND KCU.TABLE_NAME = TC.TABLE_NAME
WHERE
TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
ORDER BY
KCU.TABLE_SCHEMA, KCU.TABLE_NAME, KCU.CONSTRAINT_NAME

How to find list of tables that are having foreign key contraint for primary key of Table-A?

How can i quickly find out the list of various tables that are having foreign key constraint to the primary key of a particular table.
Kindly guide...
Here's a query to do that using information_schema, adapted from this blog post:
SELECT FK_Table = FK.TABLE_NAME
, FK_Column = CU.COLUMN_NAME
, PK_Table = PK.TABLE_NAME
, PK_Column = PT.COLUMN_NAME
, Constraint_Name = C.CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK
ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK
ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU
ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
JOIN (
SELECT i1.TABLE_NAME, i2.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
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 = 'PrimaryKeyTable'

In SQL Server 2008 how can I query for tables with foreign key references to a specific table column?

In MSSQL 2008 how can I query for tables with foreign key references to a specific table column?
Let's say I have a table called Audit with an int column called 'ID".
How can I find all the tables in my database that have a foreign key to Audit.ID ?
SELECT
K_Table = FK.TABLE_NAME,
FK_Column = CU.COLUMN_NAME,
PK_Table = PK.TABLE_NAME,
PK_Column = PT.COLUMN_NAME,
Constraint_Name = C.CONSTRAINT_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
---- optional:
ORDER BY
1,2,3,4
WHERE PK.TABLE_NAME='something'WHERE FK.TABLE_NAME='something'
WHERE PK.TABLE_NAME IN ('one_thing', 'another')
WHERE FK.TABLE_NAME IN ('one_thing', 'another')
declare #keys table
(
ForeignKey varchar(100),
TableName varchar(100),
ColumnName varchar(100),
ReferenceTableName varchar(100),
ReferenceColumnName varchar(100)
)
insert #keys
SELECT f.name AS ForeignKey,
TableName = OBJECT_NAME(f.parent_object_id),
ColumnName = COL_NAME(fc.parent_object_id,
fc.parent_column_id),
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
COL_NAME(fc.referenced_object_id,
fc.referenced_column_id) AS ReferenceColumnName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id
select * from #Keys where TableName='X' and ColumnName='Y'