我有一个来自美国人口普查的 CSV 文件,如下所示:
"ZIP5","ZIP4","ZIP9","STATE CODE","STATE","COUNTY CODE","COUNTY NAME","CBSA CODE","CBSA TITLE","CBSA LSAD","METRO DIVISION CODE","METRO DIVISION TITLE","METRO DIVISION LSAD","CSA CODE","CSA TITLE","CSA LSAD"
"04841",,"04841","23","ME","013","Knox County","40500","Rockland, ME","Micropolitan Statistical Area",,,,,,
"04843",,"04843","23","ME","013","Knox County","40500","Rockland, ME","Micropolitan Statistical Area",,,,,,
"04846",,"04846","23","ME","013","Knox County","40500","Rockland, ME","Micropolitan Statistical Area",,,,,,
"04847",,"04847","23","ME","013","Knox County","40500","Rockland, ME","Micropolitan Statistical Area",,,,,,
"04848",,"04848","23","ME","027","Waldo County",,,,,,,,,
"04849",,"04849","23","ME","027","Waldo County",,,,,,,,,
"04850",,"04850","23","ME","027","Waldo County",,,,,,,,,
"04851",,"04851","23","ME","013","Knox County","40500","Rockland, ME","Micropolitan Statistical Area",,,,,,
"04852",,"04852","23","ME","015","Lincoln County",,,,,,,,,
该文件有超过 200 万条记录。大多数记录的所有字段都没有数据。
这是我为上述 CSV 文件定义的 MySQL 记录布局:
+----------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| ZIP5 | varchar(5) | NO | | NULL | |
| ZIP4 | varchar(5) | NO | | NULL | |
| ZIP9 | varchar(10) | NO | | NULL | |
| STATE_CODE | varchar(2) | NO | | NULL | |
| STATE | varchar(2) | NO | | NULL | |
| COUNTY_CODE | varchar(3) | NO | | NULL | |
| COUNTY_NAME | varchar(50) | NO | | NULL | |
| CBSA_CODE | varchar(5) | NO | | NULL | |
| CBSA_TITLE | varchar(50) | NO | | NULL | |
| CBSA_LSAD | varchar(50) | NO | | NULL | |
| METRO_DIVISION_CODE | varchar(5) | NO | | NULL | |
| METRO_DIVISION_TITLE | varchar(50) | NO | | NULL | |
| METRO_DIVISION_LSAD | varchar(50) | NO | | NULL | |
| CSA_CODE | varchar(3) | NO | | NULL | |
| CSA_TITLE | varchar(50) | NO | | NULL | |
| CSA_LSAD | varchar(50) | NO | | NULL | |
+----------------------+------------------+------+-----+---------+----------------+
(我刚刚意识到我应该将 ZIP5 定义为主键?)
我已经读过,如果您在 CSV 文件中有一个空字段,您应该将其更改为 \N,但是有没有办法轻松做到这一点?我可以编写一个 PHP 程序来做到这一点,但是有超过 200 万条记录需要很长时间,而且我的服务器没有很多 RAM。
如何以最简单的方式成功将此 CSV 文件导入 MySQL?MySQL 中的 LOAD 命令上是否有一些参数可以做到这一点?它现在的工作方式,它抱怨 ZIP5 有数据截断,当我查看 MySQL 时,它在邮政编码中有引号,只有前 4 位数字。谢谢!