mysql - 将文本文件中的数据加载到数据库中的表中

标签 mysql sql

有人可以告诉我我在这里做错了什么吗?这一定是一些简单的事情,首先我创建了一些带有外键的表,然后尝试从文本文件导入数据,但出现了各种错误,而没有获取数据,所以现在我尝试创建表加载数据,然后添加外键希望能起作用,但这不是我所做的

create table Books (
ISBN Char(10) not null,
Title Varchar(50) not null,
Price Decimal(5,2) null,
Authors Varchar(50) null,
Pages int null,
PubYear int null,
QTY int null,
Constraint Books_PK primary key(ISBN)
);



create table customers (
customerid int not null,
company varchar(30) null,
firstname varchar(30) null,
lastname varchar(30) null,
street varchar(50) null,
city varchar(30) null,
state char(2) null default 'NE',
zip char(5) null,
phone char(10) null,
constraint customer_pk primary key(customerid)
);

create table orders (
orderid int not null,
customerid int not null,
orderdate date null,
shipdate date null,
shipping decimal(5,2) null,
salestax decimal(5,2) null,
constraint order_pk primary key(orderid)
);

create table orderinfo (
orderid int not null,
isbn char(10) not null,
qty int not null,
price decimal(5,2) not null,
detailid int not null auto_increment,
constraint orderinfo_pk primary key(detailid)
);

load data infile 'C:/lab8/books.txt
into table books;

它给我一个错误,说 DATA TOO LONG FOR COLUMN ISBN IN ROW 1

文本文件的内容是

0929306279,  Bell labs,  29.95,  Gehani,  269,  2008,  121
0929306260,  Java,  49.95,  Sahni & Kumar,  465,  2008,  35
0670031844,  White Mughals,  34.95,  Dalrymple,  459,  2008,  78
0439357624,  Born Confused,  16.95,  Hidier,  432,  2007,  11

显然 ISBN 是 10 个字符,那么为什么它不进入表格呢?

最佳答案

如果您使用 , 作为分隔符,则必须如此说明。

load data infile 'C:/lab8/books.txt'
into table books
fields terminated by ',';

根据LOAD DATA INFILE Syntax ,默认为制表符\t

If you specify no FIELDS or LINES clause, the defaults are the same as if you had written this:

FIELDS TERMINATED BY '\t' ENCLOSED BY '' ESCAPED BY '\\'
LINES TERMINATED BY '\n' STARTING BY ''

关于mysql - 将文本文件中的数据加载到数据库中的表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13782612/

相关文章:

mysql - 是否可以在 mysql select 查询中添加 while 循环?

mysql - SQL查询: Indicate if a row had a value of 0

mysql - CASE 返回多行

mysql - 表连接顺序会影响查询结果吗?

php - 将表列中的 DEC 值转换为十六进制

php - xampp 和 mysql 表中的所有列在 php 中都是小写的

java - 如何检查数据库中是否存在表或列?

sql - 将两个表组合成一个新表,以便忽略另一个表中的选择行

sql - 使用 Postgres : Create a Column of Binary Data Conditional on Data in Another Table

mysql - 如何在MySql中查找具有最高优先级的行?