How to add prefix of all tables in mysql

前端 未结 4 1305
广开言路
广开言路 2021-01-31 11:32

How can I add prefix to all tables in mysql using query.

For example:

I need to add \"dr_\" in all tables which are available in mysql database.

相关标签:
4条回答
  • 2021-01-31 12:11

    To add to Rolando's excellent and comprehensive answer. If you run the RENAME queries generated by his SELECT CONCAT query (I used phpMyAdmin and just copied the output queries into the SQL box and hit Go) then you shouldn't have any problems with foreign key constraints. I've just done this on a test database in XAMPP and all tables renamed ok, and the constraints remained intact. This may seem obvious to SQL experts but perhaps not so much to us amateurs.

    0 讨论(0)
  • 2021-01-31 12:12

    Add a prefix to all of your database tables.

    Supposing that your database was called "my_database" and the prefix you want to add is "my_prefix", execute the following query:

    SELECT Concat('ALTER TABLE ', TABLE_NAME, ' RENAME TO my_prefix_', TABLE_NAME, ';') FROM information_schema.tables WHERE table_schema = 'my_database'
    

    Your result set will be a bunch of queries that you can copy and paste into your favorite MySQL editor (Sequel Pro, phpMyAdmin, whatever). Just paste those in and execute, and you're all done.

    0 讨论(0)
  • 2021-01-31 12:13

    Run all queries that you get from running this query:

    SELECT Concat('ALTER TABLE `', TABLE_NAME, '` RENAME TO `dr_', TABLE_NAME, '`;') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '<name of your db>';
    
    0 讨论(0)
  • 2021-01-31 12:19

    For this example, I will create a database called prefixdb with 4 tables:

    mysql> drop database if exists prefixdb;
    Query OK, 4 rows affected (0.01 sec)
    
    mysql> create database prefixdb;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> use prefixdb
    Database changed
    mysql> create table tab1 (num int) ENGINE=MyISAM;
    Query OK, 0 rows affected (0.05 sec)
    
    mysql> create table tab2 like tab1;
    Query OK, 0 rows affected (0.05 sec)
    
    mysql> create table tab3 like tab1;
    Query OK, 0 rows affected (0.05 sec)
    
    mysql> create table tab4 like tab1;
    Query OK, 0 rows affected (0.04 sec)
    
    mysql> show tables;
    +--------------------+
    | Tables_in_prefixdb |
    +--------------------+
    | tab1               |
    | tab2               |
    | tab3               |
    | tab4               |
    +--------------------+
    4 rows in set (0.00 sec)
    

    The query to generate it would be

    select
        concat('alter table ',db,'.',tb,' rename ',db,'.',prfx,tb,';')
    from
        (select table_schema db,table_name tb
        from information_schema.tables where
        table_schema='prefixdb') A,
        (SELECT 'dr_' prfx) B
    ;
    

    Running it at the command line I get this:

    mysql> select concat('alter table ',db,'.',tb,' rename ',db,'.',prfx,tb,';') from (select table_schema db,table_name tb from information_schema.tables where table_schema='prefixdb') A,(SELECT 'dr_' prfx) B;
    +----------------------------------------------------------------+
    | concat('alter table ',db,'.',tb,' rename ',db,'.',prfx,tb,';') |
    +----------------------------------------------------------------+
    | alter table prefixdb.tab1 rename prefixdb.dr_tab1;             |
    | alter table prefixdb.tab2 rename prefixdb.dr_tab2;             |
    | alter table prefixdb.tab3 rename prefixdb.dr_tab3;             |
    | alter table prefixdb.tab4 rename prefixdb.dr_tab4;             |
    +----------------------------------------------------------------+
    4 rows in set (0.00 sec)
    

    Pass the result back into mysql like this:

    mysql -hhostip -uuser -pass -AN -e"select concat('alter table ',db,'.',tb,' rename ',db,'.',prfx,tb,';') from (select table_schema db,table_name tb from information_schema.tables where table_schema='prefixdb') A,(SELECT 'dr_' prfx) B" | mysql -hhostip -uuser -ppass -AN
    

    With regard to your question if you want to rename all tables, do not touch information_schema and mysql databases. Use this query instead:

    select
        concat('alter table ',db,'.',tb,' rename ',db,'.',prfx,tb,';')
    from
        (select table_schema db,table_name tb
        from information_schema.tables where
        table_schema not in ('information_schema','mysql')) A,
        (SELECT 'dr_' prfx) B
    ;
    

    Give it a Try !!!

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