Determine SQL Server foreign key name - sql-server

I'm trying to determine the name of the foreign key on a table but I'm getting wrong values. If I use this example SQL
CREATE TABLE Address
(
id INT NOT NULL PRIMARY KEY,
street VARCHAR NOT NULL
);
CREATE TABLE Person
(
id INT NOT NULL PRIMARY KEY,
orgId INT NOT NULL REFERENCES dbo.Organization (id)
);
SELECT tbl.name TableName, col.name ColumnName, fk.name ForeignKey
FROM sys.tables tbl
JOIN sys.columns col ON tbl.object_id = col.object_id
LEFT OUTER JOIN sys.foreign_keys fk ON tbl.object_id = fk.parent_object_id
WHERE tbl.name = 'Person'
it's telling me the ForeignKey name is the same thing for both the id and the orgId columns of the Person table. Clearly I'm missing an extra join condition, but I can't for the life of me figure out what it is.

your query is missing some part,to see which columns have FK you can use sys.foreign_key_columns view.
here is what you need:
SELECT
tbl.name TableName
, col.name ColumnName
, fk2.name ForeignKey
FROM
sys.tables tbl
JOIN sys.columns col
ON tbl.object_id = col.object_id
LEFT OUTER JOIN sys.foreign_key_columns AS fk
ON col.object_id = fk.parent_object_id
AND fk.parent_column_id = col.column_id
LEFT OUTER JOIN sys.foreign_keys AS fk2
ON fk2.object_id = fk.constraint_object_id
WHERE
tbl.name = 'Person';

Related

All columns that refer to a column as foreign key

How can I have a list of all columns in other tables/schemas that refer to a certain column A as foreign key ?
Something like this?
SELECT
fk.Name,
'Referenced table' = refTbl.Name,
'Parent table' = parentTbl.Name,
'Parent column' = c.name
FROM
-- FK constraint
sys.foreign_keys fk
INNER JOIN
-- Referenced table (where the PK resides)
sys.tables refTbl ON fk.referenced_object_id = refTbl.object_id
INNER JOIN
-- Parent table (which has the foreign key column)
sys.tables parentTbl ON fk.parent_object_id = parentTbl .object_id
INNER JOIN
-- link to the columns involved in the FK contraint
sys.foreign_key_columns fkc ON fkc.constraint_object_id = fk.object_id
INNER JOIN
-- column in the parent table that's part of the FK constraint
sys.columns c ON c.object_id = parentTbl.object_id AND c.column_id = fkc.parent_column_id
WHERE
refTbl.name = 'YourTableNameHere'
ORDER BY
fk.Name, parentTbl.name

How do you list the primary key of a SQL Server User Defined Table Type?

My question is simple, how do you list the primary key (column name) of a SQL Server User Defined Table Type?
ex;
CREATE TYPE [dbo].[MyTableType] AS TABLE
(
[ID] int NOT NULL, PRIMARY KEY CLUSTERED ( [ID])
)
How to get the column [ID] with a query
It seem it is only possible to find primary key for real table, not table type.
This is stored in the catalog views:
SELECT c.Name
FROM sys.table_types AS tt
INNER JOIN sys.key_constraints AS kc
ON kc.parent_object_id = tt.type_table_object_id
INNER JOIN sys.indexes AS i
ON i.object_id = kc.parent_object_id
AND i.index_id = kc.unique_index_id
INNER JOIN sys.index_columns AS ic
ON ic.object_id = kc.parent_object_id
INNER JOIN sys.columns AS c
ON c.object_id = ic.object_id
AND c.column_id = ic.column_id
WHERE tt.Name = 'YourTypeName';
A user-defined table isn't an actual table so it has an entry in sys.table_types, not in sys.tables.
The key information can be retrieved from sys.key_constraints as with other tables, if the sys.table_types.type_table_object_id and sys.key_constraints.parent_object_id fields are used, eg:
create TYPE TestTableType AS TABLE
(
ID int primary key,
Name nVARCHAR(50)
)
declare #typeID int
select #typeId=type_table_object_id
from sys.table_types
where name='TestTableType'
select #typeId
-- Returns 1134627085
select *
from sys.key_constraints
where parent_object_id=#typeID
-- Returns
-- PK__TT_TestT__3214EC27BA14A4A6 1150627142 NULL 4 1134627085 PK PRIMARY_KEY_CONSTRAINT 2016-04-25 17:36:34.890 2016-04-25 17:36:34.890 1 0 0 1 1
After that, you can get the column name in the same way as with other primary keys, by joining with sys.index_columns and sys.columns:
select col.name
from sys.key_constraints kcon
inner join sys.index_columns indcol on indcol.object_id=kcon.parent_object_id
inner join sys.columns col on col.object_id = kcon.parent_object_id
and col.column_id = indcol.column_id
where parent_object_id=#typeID
Or
select col.name
from sys.table_types tt
inner join sys.key_constraints kcon on type_table_object_id=kcon.parent_object_id
inner join sys.index_columns indcol on indcol.object_id=kcon.parent_object_id
inner join sys.columns col on col.object_id = kcon.parent_object_id and col.column_id = indcol.column_id
where tt.name='TestTableType'

How to get tableName , columnName ,constraintsType and cascade type in sql server 2008

i need list of tableName , columnName ,constraintsType and cascade type in sql server 2008 so i tried below query but cascade type is null
SELECT TB.name AS "TableName",CL.name AS
"ColumnName",KC.type,FK.delete_referential_action,FK.update_referential_action
FROM SYS.key_constraints KC JOIN SYS.tables TB ON
KC.parent_object_id=TB.object_id JOIN SYS.columns CL ON
KC.parent_object_id=CL.object_id LEFT JOIN SYS.foreign_keys FK ON FK.name = KC.name
I believe this is what you are looking for
SELECT
o1.name AS FK_table,
c1.name AS FK_column,
fk.name AS FK_name,
fk.type as FK_TYPE,
pk.name AS PK_name,
pk.type as PK_TYPE,
fk.delete_referential_action_desc AS Delete_Action,
fk.update_referential_action_desc AS Update_Action
FROM sys.objects o1
INNER JOIN sys.foreign_keys fk
ON o1.object_id = fk.parent_object_id
INNER JOIN sys.foreign_key_columns fkc
ON fk.object_id = fkc.constraint_object_id
INNER JOIN sys.columns c1
ON fkc.parent_object_id = c1.object_id
AND fkc.parent_column_id = c1.column_id
INNER JOIN sys.key_constraints pk
ON fk.referenced_object_id = pk.parent_object_id
AND fk.key_index_id = pk.unique_index_id
ORDER BY o1.name, fkc.constraint_column_id
I didn't try out your query as I don't a sql server in front of me but I believe you are missing a DMV and that is why your left join is returning nulls.

How to find what table foreign key is from?

I have a table with a foreign key. How can I tell what table the FK is a primary key in? There's about 200 tables and I don't know how to find where that info is coming from/connected to.
Use this..
SELECT fk.name,
Object_name(fk.parent_object_id) [Parent table],
c1.name [Parent column]
FROM sys.foreign_keys fk
INNER JOIN sys.foreign_key_columns fkc
ON fkc.constraint_object_id = fk.object_id
INNER JOIN sys.columns c1
ON fkc.parent_column_id = c1.column_id
AND fkc.parent_object_id = c1.object_id
INNER JOIN sys.columns c2
ON fkc.referenced_column_id = c2.column_id
AND fkc.referenced_object_id = c2.object_id
WHERE Object_name(fk.referenced_object_id) = 'Tablename' -- Replace with your tablename
AND c2.name = 'Columname' -- Replace with your columname
Or simply use
sp_help Tablename or [Alt]+F1
This should help. Just run it in the DB you wish to query:
SELECT f.NAME AS ForeignKey
,SCHEMA_NAME(f.SCHEMA_ID) SchemaName
,OBJECT_NAME(f.parent_object_id) AS TableName
,COL_NAME(fc.parent_object_id, fc.parent_column_id) AS ColumnName
,SCHEMA_NAME(o.SCHEMA_ID) ReferenceSchemaName
,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
INNER JOIN sys.objects AS o ON o.OBJECT_ID = fc.referenced_object_id
GO
Source: http://blog.sqlauthority.com/2009/02/26/sql-server-2008-find-relationship-of-foreign-key-and-primary-key-using-t-sql-find-tables-with-foreign-key-constraint-in-database/
Basically, the first column is the FK, followed by the FK schema and object. Following those are the PK column name, its schema and object.
Either of the answers by NoDisplayName or Kris G. should work, but if you want something easier to remember while you're in SSMS, just right click the Foreign Key and choose Script As>Create To>New Window.
You will then get a script that can be used to (re-)create the FK, and you will be able to see what column it references in what table by reading the script.

Calculate number of primary and secondary key columns

Is there a tool or query that will give me output as the total number of primary and secondary keys in a database table?
UPDATE ANSWER
Please note that secondary key is different from foreign key.
So count of no. of primary keys is possible in 2 ways:-
SELECT *
FROM sys.indexes i
INNER JOIN sys.tables t ON i.object_id = t.object_id AND
t.type = 'U'
LEFT JOIN sys.extended_properties AS EP ON EP.major_id = T.[object_id]
where is_primary_key=1
OR
SELECT COUNT(*) AS 'PRIMARY_KEY_CONSTRAINT'
FROM sys.objects
WHERE type_desc IN ('PRIMARY_KEY_CONSTRAINT')
To count no. of secondary keys use:
SELECT *
FROM sys.indexes i
INNER JOIN sys.tables t ON i.object_id = t.object_id AND
t.type = 'U'
LEFT JOIN sys.extended_properties AS EP ON EP.major_id = T.[object_id]
where is_unique=1 and is_primary_key=0
USE database
SELECT OBJECT_NAME(OBJECT_ID) AS NameofConstraint,
SCHEMA_NAME(schema_id) AS SchemaName,
OBJECT_NAME(parent_object_id) AS TableName
FROM sys.objects
WHERE type_desc IN ('PRIMARY_KEY_CONSTRAINT')
SELECT OBJECT_NAME(OBJECT_ID) AS NameofConstraint,
SCHEMA_NAME(schema_id) AS SchemaName,
OBJECT_NAME(parent_object_id) AS TableName
FROM sys.objects
WHERE type_desc IN ('FOREIGN_KEY_CONSTRAINT')
Simple count
SELECT COUNT(*) AS 'PRIMARY_KEY_CONSTRAINT'
FROM sys.objects
WHERE type_desc IN ('PRIMARY_KEY_CONSTRAINT')
SELECT COUNT(*) AS 'FOREIGN_KEY_CONSTRAINT'
FROM sys.objects
WHERE type_desc IN ('FOREIGN_KEY_CONSTRAINT')
The tool is SQL and this is another method to get what you want. A bit more complex then #kevchadders, but this way you can also make a listing of column names, types, etc
Count Foreign keys
SELECT count(*) from
INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab,
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col
WHERE
Col.Constraint_Name = Tab.Constraint_Name
AND Col.Table_Name = Tab.Table_Name
AND Constraint_Type in ( 'FOREIGN KEY')
AND Col.Table_Name in (select name from sysobjects where xtype = 'U')
Count Pirmairy keys
SELECT count(*) from
INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab,
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col
WHERE
Col.Constraint_Name = Tab.Constraint_Name
AND Col.Table_Name = Tab.Table_Name
AND Constraint_Type in ('PRIMARY KEY')
AND Col.Table_Name in (select name from sysobjects where xtype = 'U')
For all tables, show the PK and FK
FK
SELECT *, Col.Column_Name from
INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab,
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col
WHERE
Col.Constraint_Name = Tab.Constraint_Name
AND Col.Table_Name = Tab.Table_Name
AND Constraint_Type in ('PRIMARY KEY', 'FOREIGN KEY')
AND Col.Table_Name in (select name from sysobjects where xtype = 'U')
You can try this
SELECT 'PRIMARY KEYS' AS KeyType, COUNT(*) AS Total
FROM sys.tables AS TB INNER JOIN
sys.key_constraints AS KC ON KC.parent_object_id = TB.object_id
GROUP BY KC.type
UNION
SELECT 'FOREIGN KEYS' AS KeyType, COUNT(*) AS Total
FROM sys.tables AS TB INNER JOIN
sys.foreign_keys AS FK ON FK.referenced_object_id = TB.object_id
GROUP BY FK.type

Resources