为什么我的数据被截断了?警告 |第1265章列的数据被截断
Why is my data being truncated? Warning | 1265 | Data truncated for column
我尝试将 NULL 添加到 date_of_birth 和 date_of_death,但这没有帮助。为什么数据被截断了? (特别是因为目前没有任何数据...)
另外,我完全不明白警告1261。第 5 行与第 1-4 行没有区别。
谢谢!
以下脚本:
-- create the people table
DROP TABLE IF EXISTS people;
CREATE TABLE people (
full_name varchar (50) NOT NULL,
people_ID varchar (15) NOT NULL,
date_of_birth DATE,
date_of_death DATE,
PRIMARY KEY(people_ID))
ENGINE = INNODB;
LOAD DATA LOCAL INFILE "People.csv"
INTO TABLE people
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;
给我这些错误:
Query OK, 5 rows affected, 10 warnings (0.00 sec)
Records: 5 Deleted: 0 Skipped: 0 Warnings: 10
mysql> show warnings;
+---------+------+----------------------------------------------------+
| Level | Code | Message |
+---------+------+----------------------------------------------------+
| Warning | 1265 | Data truncated for column 'date_of_birth' at row 1 |
| Warning | 1265 | Data truncated for column 'date_of_death' at row 1 |
| Warning | 1265 | Data truncated for column 'date_of_birth' at row 2 |
| Warning | 1265 | Data truncated for column 'date_of_death' at row 2 |
| Warning | 1265 | Data truncated for column 'date_of_birth' at row 3 |
| Warning | 1265 | Data truncated for column 'date_of_death' at row 3 |
| Warning | 1265 | Data truncated for column 'date_of_birth' at row 4 |
| Warning | 1265 | Data truncated for column 'date_of_death' at row 4 |
| Warning | 1265 | Data truncated for column 'date_of_birth' at row 5 |
| Warning | 1261 | Row 5 doesn't contain data for all columns |
+---------+------+----------------------------------------------------+
10 rows in set (0.00 sec)
mysql> select * from people;
+-------------------+-----------+---------------+---------------+
| full_name | people_ID | date_of_birth | date_of_death |
+-------------------+-----------+---------------+---------------+
| Harry Dunham | H_Dunham | 0000-00-00 | NULL |
| Julien Bryan | J_Bryan | 0000-00-00 | 0000-00-00 |
| Jules V.D. Bucher | J_Bucher | 0000-00-00 | 0000-00-00 |
| Miriam Bucher | M_Bucher | 0000-00-00 | 0000-00-00 |
| Paul Bowles | P_Bowles | 0000-00-00 | 0000-00-00 |
+-------------------+-----------+---------------+---------------+
5 rows in set (0.01 sec)
这是我的 .csv:
FullName,People_ID,DOB,DOD
Jules V.D. Bucher,J_Bucher,,
Miriam Bucher,M_Bucher,,
Julien Bryan,J_Bryan,,
Paul Bowles,P_Bowles,,
Harry Dunham,H_Dunham,,
也许正在发生的事情是 Mysql 试图 强制 空字符串 ""
进入您的日期列,但该列只接受 DATE
格式化数据,以便将其转换为正确的格式。尝试调整您的 CSV,使其中一行的日期列中包含 "0000-00-00"
,而不仅仅是 ""
(空白),然后查看您收到的警告是否更少。
1261
警告看起来更像是相同的 -- 在 Mysql 如何处理 CSV 导入方面可能无害但令人讨厌的怪癖 -- 我不会担心它,除非你看到证据表明你的导入的数据不完整。
综上所述,我推测您的 CSV 文件包含 DOS 样式的行结尾 (\r\n
),而您已指示 MySQL 使用 UNIX 样式的行结尾 ( \n
).
那些 \r
字符因此被视为数据,\r
在字符串到 DATE
的转换期间被 "truncated" 变为无,最后一行最后一列确实不包含任何数据(因为您没有在 CSV 文件的末尾写入空行)。这就是最后一行的不同之处。
你实际上可以 fix this by using a more appropriate LINES TERMINATED BY
directive:
LOAD DATA LOCAL INFILE "People.csv"
INTO TABLE people
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
MySQL 将允许 \r\n
序列被视为行尾。
您还有另一个问题,即您告诉 MySQL 您的字段被 "
括起来,而事实并非如此。您可以删除 ENCLOSED BY
指令,or precede it with OPTIONALLY
:
If the input values are not necessarily enclosed within quotation marks, use OPTIONALLY before the ENCLOSED BY keywords.
您应该阅读所用技术的文档。
我尝试将 NULL 添加到 date_of_birth 和 date_of_death,但这没有帮助。为什么数据被截断了? (特别是因为目前没有任何数据...)
另外,我完全不明白警告1261。第 5 行与第 1-4 行没有区别。
谢谢!
以下脚本:
-- create the people table
DROP TABLE IF EXISTS people;
CREATE TABLE people (
full_name varchar (50) NOT NULL,
people_ID varchar (15) NOT NULL,
date_of_birth DATE,
date_of_death DATE,
PRIMARY KEY(people_ID))
ENGINE = INNODB;
LOAD DATA LOCAL INFILE "People.csv"
INTO TABLE people
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;
给我这些错误:
Query OK, 5 rows affected, 10 warnings (0.00 sec)
Records: 5 Deleted: 0 Skipped: 0 Warnings: 10
mysql> show warnings;
+---------+------+----------------------------------------------------+
| Level | Code | Message |
+---------+------+----------------------------------------------------+
| Warning | 1265 | Data truncated for column 'date_of_birth' at row 1 |
| Warning | 1265 | Data truncated for column 'date_of_death' at row 1 |
| Warning | 1265 | Data truncated for column 'date_of_birth' at row 2 |
| Warning | 1265 | Data truncated for column 'date_of_death' at row 2 |
| Warning | 1265 | Data truncated for column 'date_of_birth' at row 3 |
| Warning | 1265 | Data truncated for column 'date_of_death' at row 3 |
| Warning | 1265 | Data truncated for column 'date_of_birth' at row 4 |
| Warning | 1265 | Data truncated for column 'date_of_death' at row 4 |
| Warning | 1265 | Data truncated for column 'date_of_birth' at row 5 |
| Warning | 1261 | Row 5 doesn't contain data for all columns |
+---------+------+----------------------------------------------------+
10 rows in set (0.00 sec)
mysql> select * from people;
+-------------------+-----------+---------------+---------------+
| full_name | people_ID | date_of_birth | date_of_death |
+-------------------+-----------+---------------+---------------+
| Harry Dunham | H_Dunham | 0000-00-00 | NULL |
| Julien Bryan | J_Bryan | 0000-00-00 | 0000-00-00 |
| Jules V.D. Bucher | J_Bucher | 0000-00-00 | 0000-00-00 |
| Miriam Bucher | M_Bucher | 0000-00-00 | 0000-00-00 |
| Paul Bowles | P_Bowles | 0000-00-00 | 0000-00-00 |
+-------------------+-----------+---------------+---------------+
5 rows in set (0.01 sec)
这是我的 .csv:
FullName,People_ID,DOB,DOD
Jules V.D. Bucher,J_Bucher,,
Miriam Bucher,M_Bucher,,
Julien Bryan,J_Bryan,,
Paul Bowles,P_Bowles,,
Harry Dunham,H_Dunham,,
也许正在发生的事情是 Mysql 试图 强制 空字符串 ""
进入您的日期列,但该列只接受 DATE
格式化数据,以便将其转换为正确的格式。尝试调整您的 CSV,使其中一行的日期列中包含 "0000-00-00"
,而不仅仅是 ""
(空白),然后查看您收到的警告是否更少。
1261
警告看起来更像是相同的 -- 在 Mysql 如何处理 CSV 导入方面可能无害但令人讨厌的怪癖 -- 我不会担心它,除非你看到证据表明你的导入的数据不完整。
综上所述,我推测您的 CSV 文件包含 DOS 样式的行结尾 (\r\n
),而您已指示 MySQL 使用 UNIX 样式的行结尾 ( \n
).
那些 \r
字符因此被视为数据,\r
在字符串到 DATE
的转换期间被 "truncated" 变为无,最后一行最后一列确实不包含任何数据(因为您没有在 CSV 文件的末尾写入空行)。这就是最后一行的不同之处。
你实际上可以 fix this by using a more appropriate LINES TERMINATED BY
directive:
LOAD DATA LOCAL INFILE "People.csv"
INTO TABLE people
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
MySQL 将允许 \r\n
序列被视为行尾。
您还有另一个问题,即您告诉 MySQL 您的字段被 "
括起来,而事实并非如此。您可以删除 ENCLOSED BY
指令,or precede it with OPTIONALLY
:
If the input values are not necessarily enclosed within quotation marks, use OPTIONALLY before the ENCLOSED BY keywords.
您应该阅读所用技术的文档。