php 使用 SQL 语句重命名 MySQL 表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9549243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rename a MySQL table using SQL statement
提问by user1216858
I want to rename an existing table using SQL statement:
我想使用 SQL 语句重命名现有表:
I have already tried:
我已经尝试过:
mysql_query("RENAME '$renameFolder' TO '$newName'");
mysql_query("ALTER TABLE '$renameFolder' RENAME TO '$newName'");
mysql_query("RENAME TABLE '$renameFolder' TO '$newName'");
mysql_query("RENAME '$renameFolder' TO '$newName'");
mysql_query("ALTER TABLE '$renameFolder' RENAME TO '$newName'");
mysql_query("RENAME TABLE '$renameFolder' TO '$newName'");
Using any of the 3 statements I'm always getting the same error message:
使用 3 个语句中的任何一个,我总是收到相同的错误消息:
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax"
“您的 SQL 语法有错误;请检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法”
Please tell me what I'm doing wrong!
请告诉我我做错了什么!
回答by Overv
Try using backquotes instead, e.g.:
尝试使用反引号代替,例如:
mysql_query( "RENAME TABLE `" . $renameFolder . "` TO `" . $newname . "`" );
回答by Varuna
appectedanswer from mySQLi:
来自 mySQLi 的预期答案:
$db=mysqli_connect("localhost","root","password","database");
$oldFolder="old_table_name";
$newname="new_table_name";
mysqli_query($db,"RENAME TABLE `" . $oldFolder . "` TO `" . $newname . "`");
Good Luck!
祝你好运!
回答by David
Have you connected to the server properly?
您是否正确连接到服务器?
Have you selected the db the table is in?
您是否选择了表所在的数据库?
If you have, then you should be able to run this:
如果你有,那么你应该能够运行这个:
mysql_query("ALTER TABLE table_name RENAME TO new_table_name");
回答by php
The mysql query for rename table is RENAME TABLE old_name TO new_name
重命名表的mysql查询是 RENAME TABLE old_name TO new_name
回答by vanneto
Try it without the quotes so the final query will look like:
尝试不带引号,因此最终查询将如下所示:
mysql_query ("ALTER TABLE foo RENAME TO bar");
Hope this helps.
希望这可以帮助。
回答by vanneto
RENAME TABLE `jshop`.`mob_apple` TO `jshop`.`item_mobile`;