Script to find the table with missing primary key in MySQL

Source: Internet
Author: User

Sometimes a primary key may be missing from an earlier table, which can easily lead to a slow query or master-slave replication.


Here is a small script to find a table without a primary key.


#!/bin/bash

# Find a table without a primary key

# date:2017/06/05


Source/etc/profile


Log= "/tmp/nopk.log_$ (date +%f)"


User= ' Root '

host= ' localhost '

pass= ' 123456 '

sock= '/tmp/mysql.sock '


Mysql_cmd= "Mysql-u$user-h$host-p$pass-s$sock"


dbs=$ ($MYSQL _cmd 2>/dev/null-bne "select Schema_name from INFORMATION_SCHEMA. Schemata where schema_name not in (' Information_schema ', ' Performance_schema ') ")


for DB in $dbs; Do

$MYSQL _cmd information_schema 2>/dev/null-nbe "SELECT distinct table_schema,table_name from columns where Table_sche MA = ' $db ' and table_name not IN (SELECT DISTINCT table_name from COLUMNS where Table_schema = ' $db ' and (Column_key = ' PRI ' or Column_key = ' UNI ') ' | Tee-a $LOG

Done



This article is from the "Vegetable Chicken" blog, please be sure to keep this source http://lee90.blog.51cto.com/10414478/1932495

Script to find the table with missing primary key in MySQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.