查询数据库总大小的SQL:
SELECT CONCAT(ROUND(SUM(DATA_LENGTH/1024/1024),2),'MB') AS data FROM information_schema.TABLES
查询数据库中某个库大小的SQL:
SELECT CONCAT(ROUND(SUM(DATA_LENGTH/1024/1024),2),'MB') AS DATA FROM information_schema.TABLES WHERE table_schema = 'dbName';
查询数据库中某个库中某个表大小的SQL:
SELECT CONCAT(ROUND(SUM(DATA_LENGTH/1024/1024),2),'MB') AS DATA FROM information_schema.TABLES WHERE table_schema = 'dbName' AND table_name = 'tableNme';
备份数据的SQL:
将db库中t1表中的数据按条件查询的数据插入到db_bak库的t1表中,前提t和t1表结构相同(不同库之间的备份)
INSERT INTO db_bak.t1 SELECT * FROM db.t1 WHERE AND id>id;
将db库中t1表中的数据按条件查询的数据插入到t1_bak表中,前提t1_bak和t1表结构相同(同库之间的备份)
INSERT INTO db.t1_bak SELECT * FROM db.t1 WHERE AND id>id;
将t1表中column1字段中的所有出现xxx11都改为yyy22
UPDATE db.t1 SET column1=REPLACE(column1,"xxx11","yyy22");
当一个表的某个列的值为另一个表的某个列的值,更新这个列的值,一般都是为了打断外键关系,再重新建立一种弱关系
UPDATE db.`t1` AS b INNER JOIN (SELECT id,instance_id FROM db.`t2`) n ON b.`t2_pk`=n.id SET b.`code`=n.instance_id;
好的sql可以事半功倍,以后会持续更新