有时候早期建的表上可能缺少主键,这样容易导致查询或者主从复制比较慢。
下面是一个小的脚本,用于找出没有主键的表。
#!/bin/bash
# 找出没有主键的表
# 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_SCHEMA = '$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
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。