segunda-feira, 10 de agosto de 2015

Query all tables without a primary key

While supporting a bad performing MySQL Database, one of the first things I looked was for tables without a Primary Key. Althought adding a PK sounds like a basic thing, it's surprising how many developers overlook that.

Here is a query that does just that: a list of tables without a primary key.

SELECT t.table_schema, 
       t.table_name, 
       engine 
FROM   information_schema.tables t 
       INNER JOIN information_schema.columns c 
               ON t.table_schema = c.table_schema 
                  AND t.table_name = c.table_name 
WHERE  t.table_schema = 'yourDatabaseHere' 
GROUP  BY t.table_schema, 
          t.table_name 
HAVING Sum(IF(column_key IN ( 'PRI', 'UNI' ), 1, 0)) = 0; 


I found this query over here.

Check out my new book about R Language http://www.amazon.com/dp/B00SX6WA06

Nenhum comentário:

Postar um comentário

Leave your comment here!