由于lower_case_table_names = 1时,mysql会先把表名转为小写,如果建表时表名是大写的,设置完大小写不敏感后就会查不到。感觉类似Oracle建表写create table "test" 之后用select * from test就查不到,因为Oracle自动把test转成了大写。
所以我们需要先把库里所有表名改为小写。mysql中没有自带方法,可以写个存储过程修改。
DELIMITER //
DROP PROCEDURE IF EXISTS lowercase // CREATE PROCEDURE lowercase(IN dbname VARCHAR(200)) BEGIN DECLARE done INT DEFAULT 0; DECLARE oldname VARCHAR(200); DECLARE cur CURSOR FOR SELECT table_name FROM information_schema.TABLES WHERE table_schema = dbname; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur; REPEAT FETCH cur INTO oldname; SET @newname = LOWER(oldname);
#IF newname equals to oldname, do nothing; #select 'a' <> 'A'; -> 0 #select 'a' <> BINARY 'A'; -> 1 SET @isNotSame = @newname <> BINARY oldname; IF NOT done && @isNotSame THEN SET @SQL = CONCAT('rename table ',oldname,' to ',@newname); PREPARE tmpstmt FROM @SQL; EXECUTE tmpstmt; DEALLOCATE PREPARE tmpstmt; END IF; UNTIL done END REPEAT; CLOSE cur; END // DELIMITER ;