sql server 2008 - Integer columns with no foreign key - sql-server

Having inherited a legacy Sql server database, I'd like to list by table all integer columns where there is no foreign key, on the basis that some will be missing an FK, and some will just be integer columns. Can anyone suggest a query to display this information? Clearly there's some human intervention involved here.
Thanks.

You can get it from information_schema. I would start with something like this:
SELECT * from INFORMATION_SCHEMA.COLUMNS c
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE k
ON
c.TABLE_CATALOG = k.TABLE_CATALOG
AND c.TABLE_SCHEMA = k.TABLE_SCHEMA
AND c.TABLE_NAME = k.TABLE_NAME
AND c.COLUMN_NAME = k.COLUMN_NAME
WHERE c.DATA_TYPE in ('int') AND CONSTRAINT_NAME is null

this one uses sql-server-only views:
select so.name as TableName, col.name as ColumnName
from
sys.objects so
inner join sys.columns col on so.object_id = col.object_id and col.system_type_id in (48,52,56,127)
left outer join sys.foreign_key_columns fkc on fkc.parent_object_id = so.object_id and fkc.parent_column_id = col.column_id
where
so.type='U'
and fkc.constraint_column_id is null
order by so.name

Related

In SQL Server - How to list all foreign keys related tables and fields?

I am having a problem to list all the foreign keys in a database and show the related tables & the fields from each one.
I have tried this query:
SELECT OBJECT_NAME(parent_object_id)
,OBJECT_NAME(referenced_object_id)
,OBJECT_NAME(object_id)
,*
FROM sys.foreign_keys
But this returns just the parent table and field. I need more information. Can anybody help me here?
You can try the below query from the reference also the reference.
-- using sys tables to enumerate foreign keys
-- returns 45 constraint rows
SELECT
f.name constraint_name
,OBJECT_NAME(f.parent_object_id) referencing_table_name
,COL_NAME(fc.parent_object_id, fc.parent_column_id) referencing_column_name
,OBJECT_NAME (f.referenced_object_id) referenced_table_name
,COL_NAME(fc.referenced_object_id, fc.referenced_column_id) referenced_column_name
,delete_referential_action_desc
,update_referential_action_desc
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.object_id = fc.constraint_object_id
ORDER BY f.name
-- using INFORMATION_SCHEMA to enumerate foreign keys
-- returns 45 constraint rows
SELECT
C.CONSTRAINT_NAME [constraint_name]
,C.TABLE_NAME [referencing_table_name]
,KCU.COLUMN_NAME [referencing_column_name]
,C2.TABLE_NAME [referenced_table_name]
,KCU2.COLUMN_NAME [referenced_column_name]
,RC.DELETE_RULE delete_referential_action_desc
,RC.UPDATE_RULE update_referential_action_desc
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS C
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU
ON C.CONSTRAINT_SCHEMA = KCU.CONSTRAINT_SCHEMA
AND C.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC
ON C.CONSTRAINT_SCHEMA = RC.CONSTRAINT_SCHEMA
AND C.CONSTRAINT_NAME = RC.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS C2
ON RC.UNIQUE_CONSTRAINT_SCHEMA = C2.CONSTRAINT_SCHEMA
AND RC.UNIQUE_CONSTRAINT_NAME = C2.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU2
ON C2.CONSTRAINT_SCHEMA = KCU2.CONSTRAINT_SCHEMA
AND C2.CONSTRAINT_NAME = KCU2.CONSTRAINT_NAME
AND KCU.ORDINAL_POSITION = KCU2.ORDINAL_POSITION
WHERE C.CONSTRAINT_TYPE = 'FOREIGN KEY'
ORDER BY C.CONSTRAINT_NAME
To answer your (underlying) question, you can find everything you need in the following system tables:
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
INFORMATION_SCHEMA.TABLE_CONSTRAINTS
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
Good luck!

How to get column-wise constraints in SQL Server

I am using below query to get constraints on required table:
SELECT
OBJECT_NAME(o.object_id) AS ConstraintName,
SCHEMA_NAME(schema_id) AS SchemaName,
OBJECT_NAME(parent_object_id) AS TableName,
type_desc AS ConstraintType
FROM
sys.objects o
-- INNER JOIN
-- sys.columns c ON o.object_id = c.object_id
WHERE
type_desc LIKE '%CONSTRAINT'
AND OBJECT_NAME(parent_object_id)= 'All_Data_Types'
Successfully getting table-wise constraint details. But, I want column's information as well.
Could someone help me with this?
Thanks in advance
Fast solution for default constraint is
SELECT dc.object_id AS ConstraintID, DC.name AS ConstraintName
, O.object_id AS TableID, O.name AS TableName
, C.object_id AS ColumnID, C.name AS ColName
FROM sys.default_constraints AS DC
LEFT JOIN sys.objects AS O ON O.object_id = DC.parent_object_id
LEFT JOIN sys.columns c ON o.object_id = c.object_id AND DC.parent_column_id = c.column_id
For table constraints you could use something like this:
SELECT KCU.*, TC.CONSTRAINT_TYPE
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC ON TC.CONSTRAINT_SCHEMA = KCU.CONSTRAINT_SCHEMA AND TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME
If you are on SQL Server 2012 or later then you can use system view sys.sysconstraints:
Contains mappings of constraints to the objects that own the
constraints within the database.
So, this query:
SELECT *
FROM sys.sysconstraints s
INNER JOIN sys.objects o ON o.object_id = s.constid
theoretically returns the same number of rows as your query. But now you have the additional information about the ID of the column on which the constraint is defined. That is column colid of sys.sysconstraints:
ID of the column on which the constraint is defined.
0 = Table constraint
Thus, using this query:
SELECT OBJECT_NAME(o.object_id) AS ConstraintName,
SCHEMA_NAME(o.schema_id) AS SchemaName,
OBJECT_NAME(o.parent_object_id) AS TableName,
o.type_desc AS ConstraintType,
COALESCE(c.COLUMN_NAME, 'Table constraint') AS ColumnName
FROM sys.sysconstraints s
INNER JOIN sys.objects o ON o.object_id = s.constid
LEFT JOIN INFORMATION_SCHEMA.COLUMNS c ON c.ORDINAL_POSITION = s.colid AND s.colid <> 0
you also get the name the column related to the constraint.
Here is the query. You are using INNER JOIN on wrong column_name of tables.
You used this
INNER JOIN sys.columns c ON o.object_id = c.object_id
I replaced it with
INNER JOIN sys.columns c ON o.parent_object_id = c.object_id
Now you can see the column_name as well as table_name along with constraint_details.
SELECT OBJECT_NAME(o.object_id) AS ConstraintName,
SCHEMA_NAME(schema_id) AS SchemaName,
OBJECT_NAME(parent_object_id) AS TableName,
c.name as ColumnName,
type_desc AS ConstraintType
FROM sys.objects o
INNER JOIN sys.columns c ON o.parent_object_id = c.object_id
WHERE type_desc LIKE '%CONSTRAINT';
Also avoid using the below clause
AND OBJECT_NAME(parent_object_id)= 'All_Data_Types'

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

How to get table columns according its schema and table name?

I want to get ColumnName and it's datatype and is_identity for each column of different databases for specific table and schema.
I use following code but SCHEMA_NAME is for current database but I want to run this query from master or another database.
How to check schema name?
SELECT
c.name AS column_name, tp.name as data_type, c.is_identity iden
FROM
DatabaseName.sys.tables AS t
INNER JOIN
DatabaseName.sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
INNER JOIN
DatabaseName.sys.types tp ON c.user_type_id = tp.user_type_id
WHERE
t.name = 'PlaceType'
AND SCHEMA_NAME(t.schema_id) = 'Coding'
Instead of SCHEMA_NAME, use OBJECT_SCHEMA_NAME, like so:
SELECT c.name AS column_name,tp.name as data_type,c.is_identity iden FROM
DatabaseName.sys.tables AS t INNER JOIN
DatabaseName.sys.columns c ON t.OBJECT_ID = c.OBJECT_ID INNER JOIN
DatabaseName.sys.types tp ON c.user_type_id = tp.user_type_id where
t.name='PlaceType' and OBJECT_SCHEMA_NAME(t.object_id,DB_ID(Databasename))='Coding'
Just change the last condition like
and SCHEMA_NAME(t.schema_id) in (Select name from Sys.Databases where database_id > 4)

how to find out the column dependencies of a table

I have written a query to find out column names of tables in the database.
SELECT
t.NAME AS TABLEName,
SCHEMA_NAME(schema_id) + '.' + c.name AS ColumnName
FROM
sys.tables t
INNER JOIN sys.columns c
ON c.object_id = t.object_id
WHERE OBJECT_NAME(c.object_id) LIKE '%Message%'
ORDER BY t.NAME
So far I've got the expected result, but whatever column I am looking I want to find out the dependency of column related tables. By executing the above query I am getting table names and column names.
How can I get the column related dependent tables?
Try this:
SELECT KCU1.TABLE_CATALOG,
KCU1.TABLE_SCHEMA,
KCU1.TABLE_NAME,
KCU1.COLUMN_NAME,
KCU2.TABLE_NAME AS REFERENCED_TABLE_NAME,
KCU2.COLUMN_NAME AS REFERENCED_COLUMN_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU1
JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS RC
ON KCU1.CONSTRAINT_CATALOG = RC.CONSTRAINT_CATALOG
AND KCU1.CONSTRAINT_SCHEMA = RC.CONSTRAINT_SCHEMA
AND KCU1.CONSTRAINT_NAME = RC.CONSTRAINT_NAME
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU2
ON KCU2.CONSTRAINT_CATALOG = RC.UNIQUE_CONSTRAINT_CATALOG
AND KCU2.CONSTRAINT_SCHEMA = RC.UNIQUE_CONSTRAINT_SCHEMA
AND KCU2.CONSTRAINT_NAME = RC.UNIQUE_CONSTRAINT_NAME
AND KCU2.ORDINAL_POSITION = KCU1.ORDINAL_POSITION
WHERE KCU1.TABLE_NAME LIKE '%Message%'
ORDER BY
KCU1.TABLE_NAME,
KCU1.COLUMN_NAME
This might help. You will however need to tweak it a bit to suit your specific need:
SELECT OBJECT_NAME(referenced_major_id) AS ReferingObject,
(SELECT o.type_Desc FROM sys.objects o
WHERE o.object_id = d.referenced_major_id) AS ReferingObjType,
CASE WHEN referenced_minor_id <> 0 THEN COL_NAME(referenced_major_id, referenced_minor_id)
ELSE '' END AS RefColumnName,
OBJECT_NAME(object_ID) AS ReferencedObject,
(SELECT o.type_Desc FROM sys.objects o
WHERE o.object_id = d.object_id) AS ReferedObjType
fROM sys.sql_dependencies d
If you're looking for all tables/columns that reference a column in the table whose name contains token Message, you can use this:
;WITH PT AS (
SELECT
tc1.TABLE_NAME,
tc2.COLUMN_NAME
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc1
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE tc2
ON tc1.CONSTRAINT_NAME = tc2.CONSTRAINT_NAME
WHERE tc1.CONSTRAINT_TYPE = 'PRIMARY KEY')
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 PT
ON PT.TABLE_NAME = PK.TABLE_NAME
WHERE PK.TABLE_NAME LIKE '%Message%'
ORDER BY
FK.TABLE_NAME,
CU.COLUMN_NAME,
PK.TABLE_NAME,
PT.COLUMN_NAME

Resources