Make an existing MySQL database case-insensitive

Summary: Anyone who has used MySQL should have noticed that by default, MySQL installed under Linux is case-sensitive, which means that Table1 and table1 can exist at the same time. On the other hand, MySQL under Windows is case-insensitive, and all table names and database names will become lowercase.

Anyone who has used MySQL should have noticed that by default, MySQL installed under Linux is case-sensitive, which means that Table1 and table1 can exist at the same time. On the other hand, MySQL under Windows is case-insensitive, and all table names and database names will become lowercase.

For how to enable or disable the case sensitivity of the MySQL database, you can find it anywhere on the Internet, just change the parameter lower_case_table_names, and then restart.

However, if we already have multiple case-sensitive databases in our database, and now we want to change them to case-insensitive ones, we will get an error: Table 'databasenamexxx.tablenamexxx' doesn't exist.

To do this, we need to set the MySQL changed to case-sensitive mode, and then went to rename each table name and database name.

The really amazing thing about MySQL is that it does not allow renaming the database, so if we want to rename Test1 to test1, then we only need to create a new database for test1, and then rename the tables in Test1 to the test1 database.

And in the process of rename, we also need to change the surface from upper and lower case to all lower case.

In order to do such a thing in batches, I wrote a stored procedure that reads the system table, obtains the database table name, and then executes the rename operation in turn by means of a cursor.

DELIMITER //

CREATE PROCEDURE renametables(olddb VARCHAR(50), newdb VARCHAR(50))
BEGIN
DECLARE done BOOLEAN DEFAULT 0;
DECLARE tmp VARCHAR(100); -- define local variable

DECLARE tbcur CURSOR
    FOR SELECT TABLE_NAME FROM `information_schema`.`TABLES` WHERE table_schema=olddb AND Table_Type='BASE TABLE';
    DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
    OPEN tbcur; -- open cursor

    REPEAT
     FETCH tbcur INTO tmp;
     IF done=0
     THEN
      SET @sqlstring=CONCAT( 'RENAME TABLE ',olddb,'.`',tmp,'` TO ' ,newdb,'.`',LOWER(tmp),'`;');
      SELECT @sqlstring; -- Don't need this sentence, just print what SQL we want to run after splicing
      PREPARE s1 FROM @sqlstring; -- Execute The spliced ​​SQL
      EXECUTE s1;   
     DEALLOCATE PREPARE s1;   
    END IF;
     UNTIL done END REPEAT;
    CLOSE tbcur; -- close the cursor, release all internal memory and resources used by the cursor
   
    END//


   
We have established the stored procedure in the new database, and then call:

CALL renametables('Test1' ,'test1')
so that all mixed-case tables in Test1 are all converted to the test1 database, and the table names are all lowercase.

Do this one by one database, and then change the MySQL parameters to case-insensitive, so that it can be used normally.

Here I just migrate the table, and then migrate the stored procedure and view. Since it does not involve data, it is relatively simple. Find the DDL of the year or export the definition of the View and the stored procedure when it is case-sensitive, and then Use a text editor to lowercase the entire SQL, then execute it in the new database, and recreate it.

This article is transferred from the blog of Shenlanju Blog Garden, the original link: http://www.cnblogs.com/studyzy/p/6122739.html, if you need to reprint, please contact the original author.


Copyright statement: The content of this article is contributed by Internet users, and the copyright belongs to It is owned by the author, and this community does not own the ownership and does not assume relevant legal responsibilities. If you find any content suspected of plagiarism in this community, please send an email to: [email protected] to report and provide relevant evidence. Once verified, this community will immediately delete the allegedly infringing content.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326069576&siteId=291194637