Using rename table in MySQL to quickly and efficiently RENAME a database (schema)

Source: Internet
Author: User
Tags mysql import

Using rename table in MySQL to quickly and efficiently RENAME a database (schema)

Once a new database (schema) is created in MySQL, it cannot be renamed, therefore, if you need to rename a database, the general practice is to dump the data in the target database and import it to the new database you want to rename. A large number of insert statements are used in MySQL import, DML is widely used. If it is a database with a large amount of data, it is very inefficient. In fact, in MySQL, you can use rename table as the DDL method to move TABLE data and then RENAME the database, the following is a brief introduction. First, rename table is easy to use:

RENAME  TABLE  old_table  TO  new_table;

In this way, you can query the information_schema.TABLES dictionary table of MySQL to obtain the data of the corresponding target database. For example, you need to rename a database named test in MySQL to test2:

CREATE  DATABASE  Test2; # create test2 first  SELECT     CONCAT( 'RENAME TABLE test.' ,TABLE_NAME, ' TO test2.' ,TABLE_NAME, ';' ) DDL FROM     information_schema.`TABLES` WHERE     TABLE_SCHEMA =  'test' ; # Generate the DDL for test to be transferred to test2 and then copy and execute DROP  DATABASE  Test; # Delete the old database

However, you must note that this method requires certain permissions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.