我需要維護一個SQL腳本(對於MySQL),它創建了許多具有相同前綴的表,如wp_parent,wp_teacher。我不想寫CREATE TABLE wp_parent
。在這種情況下,如果我需要更改前綴,我必須修改很多行。如何以更易維護的方式創建帶有前綴的表
我試圖使用MySQL user variable,但它沒有奏效。
有什麼辦法讓前綴變成變量然後在其他語句中使用變量?
還有其他方法可以使腳本更易維護嗎?
我需要維護一個SQL腳本(對於MySQL),它創建了許多具有相同前綴的表,如wp_parent,wp_teacher。我不想寫CREATE TABLE wp_parent
。在這種情況下,如果我需要更改前綴,我必須修改很多行。如何以更易維護的方式創建帶有前綴的表
我試圖使用MySQL user variable,但它沒有奏效。
有什麼辦法讓前綴變成變量然後在其他語句中使用變量?
還有其他方法可以使腳本更易維護嗎?
您可以使用此命令將添加前綴表中的數據庫
SELECT Concat('ALTER TABLE ', TABLE_NAME, ' RENAME TO dr_', TABLE_NAME, ';')
FROM INFORMATION_SCHEMA.TABLES;
試試看!
謝謝。雖然還不夠,但確實有幫助。我發現添加'WHERE TABLE_SCHEMA = YourDbName'可以獲得僅用於特定數據庫的alter語句。 – 2014-11-24 10:52:09
我更新了示例如何做到這一點..請檢查一次 – RamThakur 2014-11-24 11:11:48
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 ;
如果你需要這個,你的數據庫表設計幾乎肯定是錯誤的。 – 2014-11-24 09:15:10
@RobertHarvey在OP的防禦中,表前綴允許多個應用程序共享一個數據庫,例如,在一個客戶只有一個數據庫並且想要運行多個數據庫驅動的應用程序(例如同時使用WordPress和phpBB)的廉價webhosting服務中, 。 – Dai 2014-11-24 09:19:03
以正確的方式做這件事幾乎肯定會更便宜。 – 2014-11-24 09:20:03