英文:
MYSQL - Unable to resolve duplicate error
问题
尝试完成项目的第一步,其中我创建了一个新表格:
CREATE TABLE Customers (customerID SMALLINT, firstName VARCHAR(25), lastName VARCHAR(25), street VARCHAR(50), city VARCHAR(50), state VARCHAR(25), zipCode VARCHAR(10), telephone VARCHAR(15), PRIMARY KEY (customerID));
然后,我应该从应用程序上传一个 CSV 文件:
LOAD DATA INFILE "/home/codio/workspace/customers.csv" INTO TABLE Customers FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
或者
LOAD DATA INFILE '/home/codio/workspace/customers.csv' INTO TABLE Customers FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n';
但是这两者都提供了错误代码:
ERROR 1062 (23000): Duplicate entry '32767' for key 'PRIMARY'
打开文件并搜索 32767 只在电话行中找到了一个结果。没有重复项。我已经执行了 DELETE FROM Customers
和 DROP TABLE Customers
。两次都确认表格为空:
mysql> SELECT * FROM Customers;
Empty set (0.00 sec)
我已经与这个问题争论了好几个小时,不确定还能做什么。最糟糕的是,我觉得自己没有做错什么。但这就是你可以帮忙的地方。祈祷吧。
我已经删除了表格,清空了表格,重新创建了表格,有一次甚至创建了一个全新的文件。
英文:
Trying to complete step one of a project where I create a new table with
CREATE TABLE Customers (customerID SMALLINT,firstName VARCHAR(25),lastName VARCHAR(25),street VARCHAR(50),city VARCHAR(50),state VARCHAR(25),zipCode VARCHAR(10),telephone VARCHAR(15),PRIMARY KEY (customerID));
Then, I am supposed to upload a csv file from the application.
LOAD DATA INFILE "/home/codio/workspace/customers.csv" INTO TABLE Customers FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
or
LOAD DATA INFILE '/home/codio/workspace/customers.csv' INTO TABLE Customers FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n';
but both of these provide the error code
ERROR 1062 (23000): Duplicate entry '32767' for key 'PRIMARY'
Opening the file and searching for 32767 only yields one result in the telephone line. There is no duplicate. I have DELETE FROM Customers
and DROP TABLE Customers
. Both times confirming the table is empty by
mysql> SELECT * FROM Customers;
Empty set (0.00 sec)
I have been arguing with this for hours and am unsure what else to do. The worst part is I feel like I am not doing anything wrong. But that is where you come in. Fingers crossed.
I have deleted the table, cleared the table, recreated the table, at one point, went so far as creating a brand new file.
答案1
得分: 1
你已将主键定义为
customerID SMALLINT
这意味着它只能容纳在-32768和32767之间的值。当你使用 LOAD DATA
语句时,任何超出此范围的值都会被截断,如LOAD DATA
语句手册中所述:
> * 无效值会产生警告而不是错误,并转换为列数据类型的“最接近”的有效值。
> 例子:
>
> * [...]
>
> * 超出范围的数字或时间值会被剪裁到列数据类型范围的最接近端点。
这意味着你的第一行新记录,如果其 customerId
值大于32767,将会被剪裁为32767。第二行新记录,如果其 customerId
值大于32767,也将被剪裁为32767,从而导致主键冲突。
解决方法是为 customerId
列使用更大的整数类型,如 INT
或 BIGINT
。
英文:
You have defined your primary key as
customerID SMALLINT
which means that it can only hold values between -32768 and 32767. When you use the LOAD DATA
statement any value outside this range gets truncated as mentioned in the manual for the LOAD DATA
statement:
> * Invalid values produce warnings rather than errors, and are converted to the “closest” valid value for the column data type.
> Examples:
>
> * [...]
>
> * An out-of-range numeric or temporal value is clipped to the
> closest endpoint of the range for the column data type.
This means that your first new row with a larger customerId
value than 32767 gets clipped to 32767. The second new row with a larger customerId
value than 32767 gets clipped to 32767 as well, resulting in a primary key conflict.
The solution is to use a bigger integer type for the customerId
column like INT
or BIGINT
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论