Mysql 如何以更易于维护的方式创建带有前缀的表
我需要维护一个SQL脚本(用于MySQL),它可以创建许多具有相同前缀的表,比如wp_parent、wp_teacher 我不想编写Mysql 如何以更易于维护的方式创建带有前缀的表,mysql,sql,Mysql,Sql,我需要维护一个SQL脚本(用于MySQL),它可以创建许多具有相同前缀的表,比如wp_parent、wp_teacher 我不想编写创建表wp\u parent。在这种情况下,如果我需要更改前缀,我必须修改许多行 我尝试过使用MySQL,但没有成功 有没有办法使prefix成为一个变量,然后在其他语句中使用该变量 或者是否有其他方法使脚本更易于维护?您可以使用此命令将前缀添加到数据库中的表中 SELECT Concat('ALTER TABLE ', TABLE_NAME, ' RENAME T
创建表wp\u parent
。在这种情况下,如果我需要更改前缀,我必须修改许多行
我尝试过使用MySQL,但没有成功
有没有办法使prefix成为一个变量,然后在其他语句中使用该变量
或者是否有其他方法使脚本更易于维护?您可以使用此命令将前缀添加到数据库中的表中
SELECT Concat('ALTER TABLE ', TABLE_NAME, ' RENAME TO dr_', TABLE_NAME, ';')
FROM INFORMATION_SCHEMA.TABLES;
试试看 如果您需要这样做,您的数据库表设计几乎肯定是错误的。@RobertHarvey在OP的辩护中,表前缀允许多个应用程序共享一个数据库,例如在廉价的webhosting服务中,客户只获得一个数据库,并希望运行多个数据库驱动的应用程序(例如,WordPress和phpBB同时使用)。用正确的方法使用几乎肯定会更便宜。谢谢。虽然这还不够,但确实有帮助。我发现添加
WHERE TABLE_SCHEMA=YourDbName
可以仅针对特定数据库获得alter语句。
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 ;