Tuesday, December 17, 2019

List primary key, foreign key and constraint name relationship in Database

----// For all tables in a database, below is the query.

SELECT o2.name AS Referenced_Table_Name,
       c2.name AS Referenced_Column_As_FK,
       o1.name AS Referencing_Table_Name,
       c1.name AS Referencing_Column_Name,
s.name AS Constraint_name
FROM  sysforeignkeys fk
INNER JOIN sysobjects o1 ON fk.fkeyid = o1.id
INNER JOIN sysobjects o2 ON fk.rkeyid = o2.id
INNER JOIN syscolumns c1 ON c1.id = o1.id AND c1.colid = fk.fkey
INNER JOIN syscolumns c2 ON c2.id = o2.id AND c2.colid = fk.rkey
INNER JOIN sysobjects s ON fk.constid = s.id
ORDER BY o2.name

----// There's also a command:

exec sp_fkeys @pktable_name ='Product',
        @pktable_owner ='Production'

----// For all tables in a database, below is the query.

select schema_name(tab.schema_id) + '.' + tab.name as [table],
        col.column_id,
        col.name as column_name,
        case when fk.object_id is not null then '>-' else null end as rel,
        schema_name(pk_tab.schema_id) + '.' + pk_tab.name as primary_table,
        pk_col.name as pk_column_name,
        fk_cols.constraint_column_id as no,
        fk.name as fk_constraint_name
from sys.tables tab
inner join sys.columns col on col.object_id = tab.object_id
left outer join sys.foreign_key_columns fk_cols on fk_cols.parent_object_id = tab.object_id
                                                and fk_cols.parent_column_id = col.column_id
left outer join sys.foreign_keys fk on fk.object_id = fk_cols.constraint_object_id
left outer join sys.tables pk_tab on pk_tab.object_id = fk_cols.referenced_object_id
left outer join sys.columns pk_col on pk_col.column_id = fk_cols.referenced_column_id
                                    and pk_col.object_id = fk_cols.referenced_object_id
order by schema_name(tab.schema_id) + '.' + tab.name,
    col.column_id

----// For all tables in a database, below is the query.

select 
    schema_name(pk_tab.schema_id) + '.' + pk_tab.name as primary_table
    , pk_col.name as pk_column
    ,schema_name(fk_tab.schema_id) + '.' + fk_tab.name as foreign_table,
    '>-' as rel,
    substring(D.[name], 1, len(D.[name])-1) as [fk_columns],
    fk.name as fk_constraint_name
from sys.foreign_keys fk
inner join sys.tables fk_tab  on fk_tab.object_id = fk.parent_object_id
inner join sys.tables pk_tab  on pk_tab.object_id = fk.referenced_object_id
cross apply
(
    select col.[name] ,fk_c.referenced_column_id, fk_c.referenced_object_id
    --+ ', '
    from sys.foreign_key_columns fk_c
    inner join sys.columns col    on fk_c.parent_object_id = col.object_id and fk_c.parent_column_id = col.column_id
    where fk_c.parent_object_id = fk_tab.object_id
    and fk_c.constraint_object_id = fk.object_id
    -- order by col.column_id
    --for xml path ('')
) D
inner join sys.columns pk_col on pk_col.column_id = D.referenced_column_id
                                    and pk_col.object_id = D.referenced_object_id
WHERE schema_name(pk_tab.schema_id) = 'BEY00013'
order by schema_name(fk_tab.schema_id) + '.' + fk_tab.name,
    schema_name(pk_tab.schema_id) + '.' + pk_tab.name


----// Modify foreign key constraints

ALTER TABLE TableB
DROP CONSTRAINT MyForeignKey
GO

ALTER TABLE TableB
ADD CONSTRAINT MyForeignKey FOREIGN KEY (ForeignKeyColumn)
REFERENCES TableA (PrimaryKeyColumn)
ON DELETE CASCADE
GO

No comments: