Skip to content

Instantly share code, notes, and snippets.

@wlievens
Created June 20, 2018 20:04
Show Gist options
  • Save wlievens/3570a5e9674afcac8fd169c3f6699448 to your computer and use it in GitHub Desktop.
Save wlievens/3570a5e9674afcac8fd169c3f6699448 to your computer and use it in GitHub Desktop.
Automatically generated SQL for renaming single-column unique constraints
SELECT
constraint_type,
tc.constraint_name,
tc.table_name,
kcu.column_name,
format('ALTER TABLE "%s" RENAME CONSTRAINT %s TO unq_%s_%s;',
tc.table_name, tc.constraint_name, tc.table_name, kcu.column_name)
FROM
information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
WHERE constraint_type = 'UNIQUE'
AND tc.constraint_name like 'uk\_%'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment