Skip to content

Instantly share code, notes, and snippets.

@adamelliotfields
Last active March 24, 2019 20:08
Show Gist options
  • Save adamelliotfields/8b27629c2a0ef2b68d86474b3f178815 to your computer and use it in GitHub Desktop.
Save adamelliotfields/8b27629c2a0ef2b68d86474b3f178815 to your computer and use it in GitHub Desktop.
SQL Select Tables without Primary Key in Database
SELECT tables.table_schema, tables.table_name
FROM tables
LEFT JOIN key_column_usage AS kcu ON (
kcu.table_name = tables.table_name AND
kcu.constraint_schema = tables.table_schema AND
kcu.constraint_name = 'PRIMARY')
WHERE tables.table_schema = @db_name AND
kcu.constraint_name IS NULL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment