我找到了一个正确的解决方案,MySQL中的Load Data Infile如何使用On Duplicate Key Update。这是我使用的代码(从堆栈溢出中获取):
CREATE TEMPORARY TABLE temporary_table LIKE employee_table;
SHOW INDEX FROM temporary_table;
DROP INDEX `PRIMARY` ON temporary_table;
LOAD DATA INFILE 'csv/employee_table'
INTO TABLE temporary_table FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n'
(
`AutoID`, `Name`, `Age`, `Salary`
)
SHOW COLUMNS FROM employee_table;
INSERT INTO employee_table
SELECT * FROM temporary_table
ON DUPLICATE KEY UPDATE AutoID = Values(AutoID);
DROP TEMPORARY TABLE temporary_table;
上面的代码可以正常使用仅具有不同ID的新记录来更新表。但是,当PRIMARY键是自动增量(AI)时,它无法工作。它表明下面的代码不能在自动增量PRIMARY键上运行。
DROP INDEX `PRIMARY` ON temporary_table
以下是错误消息:
#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
我很困惑,因为如果我不使用自动增量,它运行完美。有谁知道如何解决这个问题?提前谢谢。
答案 0 :(得分:0)
有两个程序。
1)你的问题解决方案。
mysql> DROP INDEX `PRIMARY` ON test5;
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
mysql> show create table test5;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test5 | CREATE TEMPORARY TABLE `test5` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`address` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> alter table test5 modify `id` int(11) NOT NULL;
Query OK, 0 rows affected (0.21 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
mysql> DROP INDEX `PRIMARY` ON test5;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
mysql> show create table test5;
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test5 | CREATE TEMPORARY TABLE `test5` (
`id` int(11) NOT NULL,
`name` varchar(50) NOT NULL,
`address` varchar(100) DEFAULT NULL,
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
2)最佳解决方案..
我们可以在程序中使用以下单个查询替换第一个(两个步骤)。
1)和2)我们可以创建具有相同参考结构且没有任何索引的新表。
CREATE TEMPORARY TABLE temporary_table SELECT * FROM target_table WHERE 1=0;
而不是..
1)创建一个新的临时表。
CREATE TEMPORARY TABLE temporary_table LIKE target_table;
2)(可选)从临时表中删除所有索引以加快速度。
SHOW INDEX FROM temporary_table; DROP INDEX PRIMARY ON temporary_table; DROP INDEX some_other_index ON temporary_table;
您也可以参考以下链接。