有时候早期建的表上可能缺少主键,这样容易导致查询或者主从复制比较慢。
下面是一个小的脚本,用于找出没有主键的表。
#!/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
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有