我似乎无法弄清楚这里出了什么问题。
我正在尝试在R中运行SQL查询,我收到以下错误:
Error in .local(conn, statement, ...) : could not run statement: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DROP INDEX `PRIMARY` ON temporary_table' at line 2
以下是我的代码:
library(RMySQL)
con <- dbConnect(MySQL(),
user="XXX", password="XXX",
dbname="test", host="localhost")
query <- "CREATE TEMPORARY TABLE temporary_table LIKE test.helloworld;
DROP INDEX `PRIMARY` ON temporary_table;"
rs <- dbSendQuery(con, query) #Where the error occurs
我在mySQL中运行了完全相同的查询,它完全正常工作。
任何人都可以指出我做错了吗?
非常感谢!
答案 0 :(得分:0)
问题似乎是由于您尝试删除主键,即使主键列具有auto_increment属性,因此只需修改列以删除auto_increment属性,然后您可以删除主键,因为auto_increment需要唯一键。
您可以使用以下语法 -
CREATE TEMPORARY TABLE temporary_table LIKE test.helloworld;
alter table temporary_table modify column primary_key_column_with_all_properties_except_auto_increment,
DROP KEY `PRIMARY`;
答案 1 :(得分:0)
分两部分执行查询。第一部分创建表,第二部分删除索引。对于前 -
query <- "CREATE TEMPORARY TABLE temporary_table LIKE test.helloworld;"
rs <- dbSendQuery(con, query) #Where the error occurs
query <- "DROP INDEX `PRIMARY` ON temporary_table;"
rs <- dbSendQuery(con, query) #Where the error occurs
我在php中测试过。这在php中有同样的问题。它运行良好。
<?php
$mysqli = new mysqli("localhost", "root", "root", "test");
$result = $mysqli->query("CREATE TEMPORARY TABLE `temporary_table` LIKE t;");
printf("Errormessage: %s\n", $mysqli->error);
$result = $mysqli->query(" DROP INDEX `PRIMARY` ON temporary_table;");
#$result = $mysqli->query("DROP table temporary_table;");
printf("Errormessage: %s\n", $mysqli->error);
?>
root@smart:/var/www# php test.php
Errormessage:
Errormessage:
当我同时运行两个查询时,就像你说的那样说错误。
<?php
// mysqli
$mysqli = new mysqli("localhost", "root", "root", "test");
$result = $mysqli->query("CREATE TEMPORARY TABLE `temporary_table` LIKE t;DROP INDEX `PRIMARY` ON temporary_table;");
printf("Errormessage: %s\n", $mysqli->error);
?>
root@smart:/var/www# php test.php
Errormessage: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DROP INDEX `PRIMARY` ON temporary_table' at line 1