【发布时间】:2013-01-26 22:10:22
【问题描述】:
【问题讨论】:
-
所选答案对您有用吗?什么版本的 Magento?
【问题讨论】:
您可以使用此选择创建 sql 来重命名所有表:
SELECT 'rename table '||table_name||' to '||'newprefix'||table_name||';'
FROM information_schema.tables
【讨论】:
SELECT CONCAT('rename table ', table_name , ' to ', 'newprefix_', table_name, ';') FROM information_schema.tables 用于better compatibility。
<?php
$database_host="localhost";
$database_user="root";
$database_password="";
$magento_database="test1";
$table_prefix="magtest_";
?>
<?php
$db=mysql_connect($database_host,$database_user,$database_password);
mysql_select_db($magento_database);
$query="SHOW TABLES";
$result=mysql_query($query) or die('Err');
while($row=mysql_fetch_array($result)){$old_table=$row[0];
if(preg_match('/'.$table_prefix.'/',$old_table)){echo"Table $old_table already done<br/>\n";continue;}
$new_table=$table_prefix.$old_table;echo"Renaming $old_table to $new_table<br/>\n";
$query="RENAME TABLE `$old_table` TO `$new_table`";mysql_query($query);}
?>
步骤:
【讨论】:
我运行这个 php 脚本来更改 Magento DB 表前缀
// mege_rename_table_prefix.php
//New Prefix Name
$table_prefix = "test_";
//Magento Database Backup php script
error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);
ini_set('memory_limit', '1512M');
// Get Magento Application
require_once 'app/Mage.php';
Mage::app();
// Mage::app('default');
//Mage::app('main');
// get Magento config
$config = Mage::getConfig()->getResourceConnectionConfig("default_setup");
$dbinfo = array(
"host" => $config->host,
"user" => $config->username,
"pass" => $config->password,
"dbname" => $config->dbname
);
// Database Config
$db_host = $dbinfo["host"];
$db_user = $dbinfo["user"];
$db_pass = $dbinfo["pass"];
$db_name = $dbinfo["dbname"];
//conect db
$db = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$query = "SHOW TABLES";
$result = mysql_query($query) or die('Err');
while($row = mysql_fetch_array($result)) {
$old_table = $row[0];
if(preg_match('/'.$table_prefix.'/', $old_table)) {
echo "Table $old_table already done<br/>\n";
continue;
}
$new_table = $table_prefix.$old_table;
echo "Renaming $old_table to $new_table<br/>\n";
$query = "RENAME TABLE `$old_table` TO `$new_table`";
mysql_query($query);
}
【讨论】:
您可以在 PHPMyAdmin 中简单地更改它。
Click/open the database.
Click Structure at the top bar.
This will display all your tables. Note the existing prefix.
Scroll to the bottom, to the last table.
Click "Check all".
This will check all tables.
Click the drop down menu just next to it - the one with the default value "with selected".
Select "Replace table prefix:"
This will bring you to a new page with two text inputs.
Fill in your existing prefix, e.g. "oldPrefi_". Don't forget the underscore.
Fill in your new prefix, e.g. "newPrefi_". Don't forget the underscore.
Finally, click submit.
您将被重定向到带有新前缀的表格列表。
【讨论】:
第 1 步 - 更改 env.php 文件中的 table_prefix,如下面的屏幕截图所示
第 2 步 - 运行以下查询以获取 ALTER 语句以将前缀添加到现有表 -:
SELECT Concat('ALTER TABLE ', TABLE_NAME, ' RENAME TO qa_',
TABLE_NAME, ';') FROM information_schema.tables WHERE table_schema =
'DB_NAME'
第 3 步 - 通过在 Magento 根文件夹上运行以下命令来升级 Magento 2 数据库
php bin/magento setup:upgrade
如果出现问题,您可以从 env.php 中删除 table_prefix 并在您的数据库实例上运行以下查询以将表重命名为原始表
SELECT Concat('ALTER TABLE ', TABLE_NAME , ' RENAME TO ',
replace(TABLE_NAME,'qa_',''), ';') FROM information_schema.tables
WHERE table_schema = 'magento_2'
希望对某人有所帮助!
【讨论】:
解决该问题的步骤(100% 可行,我有同样的问题,我已解决)
上述解决方案大多行不通,因为您可以 UPDATE 表名,但是 FORIEGN 键映射会导致问题。所以下面是解决这个问题的最佳方法。
【讨论】: