How do I quickly rename a MySQL database (change schema name)?

前端 未结 30 2429
余生分开走
余生分开走 2020-11-22 14:54

The MySQL manual at MySQL covers this.

Usually I just dump the database and reimport it with a new name. This is not an option for very big databases. Apparently

相关标签:
30条回答
  • 2020-11-22 15:06

    in phpmyadmin you can easily rename the database

    select database 
    
      goto operations tab
    
      in that rename Database to :
    
      type your new database name and click go
    

    ask to drop old table and reload table data click OK in both

    Your database is renamed

    0 讨论(0)
  • 2020-11-22 15:09

    For mac users, you can use Sequel Pro (free), which just provide the option to rename Databases. Though it doesn't delete the old DB.

    once open the relevant DB just click: Database --> Rename database...

    0 讨论(0)
  • 2020-11-22 15:10

    It is possible to rename all tables within a database to be under another database without having to do a full dump and restore.

    DROP PROCEDURE IF EXISTS mysql.rename_db;
    DELIMITER ||
    CREATE PROCEDURE mysql.rename_db(IN old_db VARCHAR(100), IN new_db VARCHAR(100))
    BEGIN
    SELECT CONCAT('CREATE DATABASE ', new_db, ';') `# create new database`;
    SELECT CONCAT('RENAME TABLE `', old_db, '`.`', table_name, '` TO `', new_db, '`.`', table_name, '`;') `# alter table` FROM information_schema.tables WHERE table_schema = old_db;
    SELECT CONCAT('DROP DATABASE `', old_db, '`;') `# drop old database`;
    END||
    DELIMITER ;
    
    $ time mysql -uroot -e "call mysql.rename_db('db1', 'db2');" | mysql -uroot
    

    However any triggers in the target db will not be happy. You'll need to drop them first then recreate them after the rename.

    mysql -uroot -e "call mysql.rename_db('test', 'blah2');" | mysql -uroot
    ERROR 1435 (HY000) at line 4: Trigger in wrong schema
    
    0 讨论(0)
  • 2020-11-22 15:13

    This is what I use:

    $ mysqldump -u root -p olddb >~/olddb.sql
    $ mysql -u root -p
    mysql> create database newdb;
    mysql> use newdb
    mysql> source ~/olddb.sql
    mysql> drop database olddb;
    
    0 讨论(0)
  • 2020-11-22 15:13

    TodoInTX's stored procedure didn't quite work for me. Here's my stab at it:

    -- stored procedure rename_db: Rename a database my means of table copying.
    -- Caveats: 
    -- Will clobber any existing database with the same name as the 'new' database name.
    -- ONLY copies tables; stored procedures and other database objects are not copied.
    -- Tomer Altman (taltman@ai.sri.com)
    
    delimiter //
    DROP PROCEDURE IF EXISTS rename_db;
    CREATE PROCEDURE rename_db(IN old_db VARCHAR(100), IN new_db VARCHAR(100))
    BEGIN
        DECLARE current_table VARCHAR(100);
        DECLARE done INT DEFAULT 0;
        DECLARE old_tables CURSOR FOR select table_name from information_schema.tables where table_schema = old_db;
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
    
        SET @output = CONCAT('DROP SCHEMA IF EXISTS ', new_db, ';'); 
        PREPARE stmt FROM @output;
        EXECUTE stmt;
    
        SET @output = CONCAT('CREATE SCHEMA IF NOT EXISTS ', new_db, ';');
        PREPARE stmt FROM @output;
        EXECUTE stmt;
    
        OPEN old_tables;
        REPEAT
            FETCH old_tables INTO current_table;
            IF NOT done THEN
            SET @output = CONCAT('alter table ', old_db, '.', current_table, ' rename ', new_db, '.', current_table, ';');
            PREPARE stmt FROM @output;
            EXECUTE stmt;
    
            END IF;
        UNTIL done END REPEAT;
    
        CLOSE old_tables;
    
    END//
    delimiter ;
    
    0 讨论(0)
  • 2020-11-22 15:16

    ALTER DATABASE is the proposed way around this by MySQL and RENAME DATABASE is dropped.

    From 13.1.32 RENAME DATABASE Syntax:

    RENAME {DATABASE | SCHEMA} db_name TO new_db_name;
    

    This statement was added in MySQL 5.1.7, but it was found to be dangerous and was removed in MySQL 5.1.23.

    0 讨论(0)
提交回复
热议问题