Skip to content

Instantly share code, notes, and snippets.

@faloi
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faloi/a7ab0a872b5290aa3d2d to your computer and use it in GitHub Desktop.
Save faloi/a7ab0a872b5290aa3d2d to your computer and use it in GitHub Desktop.

Foreign keys

Rename

-- 1. Just the object name as seen in sys.objects:
sp_rename 'FK_Product_Caetgory', 'FK_Product_Category', 'OBJECT'
 
-- 2. Qualified with  the schema:
sp_rename 'Shopping.FK_Product_Caetgory', 'FK_Product_Category', 'OBJECT'
 
-- 3. Qualified with the schema and table:
sp_rename 'Shopping.Product.FK_Product_Caetgory', 'FK_Product_Category', 'OBJECT'

List all whose name doesn't have prefix "FK_"

select 
  fk.table_name, fk.constraint_name
from
  information_schema.referential_constraints rc
inner join information_schema.table_constraints fk
  on rc.constraint_name = fk.constraint_name
where 
  fk.constraint_name not like 'FK[_]%'

Schema of a table (columns, contraints, FKs, etc)

sp_help 'VariationLink'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment