Skip to content

Instantly share code, notes, and snippets.

@NaserKhoshfetrat
Forked from timabell/FK-validation.sql
Created September 13, 2021 09:00
Show Gist options
  • Save NaserKhoshfetrat/6ccc859619db193dc81cca246bc3988e to your computer and use it in GitHub Desktop.
Save NaserKhoshfetrat/6ccc859619db193dc81cca246bc3988e to your computer and use it in GitHub Desktop.
misnamed sql server foreign key query
-- https://gist.github.com/timabell/d017038af96196b84e56
SELECT
obj.name,
fk.name
FROM sys.foreign_keys fk
inner join sys.objects obj on obj.object_id = fk.parent_object_id
-- use this where clause to find FKs that aren't named after their table:
where fk.name not like 'FK_' + obj.name + '_%'
order by
obj.name,
fk.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment