MySQL bulk delete table with same prefix and modify table name

Source: Internet
Author: User
Tags table name

If there are many tables that need to be deleted, and the table has the same prefix, we might need the following statement:

The code is as follows Copy Code
drop table pre_tablename1;
drop table pre_tablename2;
drop table Pre_tablename3;

....... If we write manually, we may need a lot of repetitive work and may not know the table name yet. So we can output the deletion table statement above through the SQL statement

Execute SQL statement:

The code is as follows Copy Code
Select CONCAT (' drop table ', table_name, '; ')
From Information_schema.tables
Where table_name like ' pre_% ';

Note: Like ' pre_% ' where pre_ is the table prefix you need to replace. Of course, you can also write the rules according to your own situation.

Executing the query automatically generates the matching table with SQL statements such as DROP table table_name.

Bulk copy to edit tools such as Notepad or ET to determine if your SQL statement is correct.

This can also be a safe audit of the statement to avoid misoperation.

Of course, this is only a thought, but also can be used in other issues.

For example, how to modify a table name in bulk:

The code is as follows Copy Code
Select CONCAT (' ALTER TABLE ', table_name, ' RENAME to ', table_name, '; ')
From Information_schema.tables
Where table_name like ' uc_% ';

Execute the query and get the result:

The code is as follows Copy Code
ALTER TABLE uc_aaa RENAME to UC_AAA;
ALTER TABLE uc_bbb RENAME to UC_BBB;

Bulk copy to Notepad or et such as editing tools, and then bulk replace RENAME to UC into RENAME to you want the table prefix
Completed before execution.

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.