QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1296|回复: 8

关于MYSQL的数据导入问题.---数据错误!请高手指教

[复制链接]
发表于 2004-7-22 12:04:13 | 显示全部楼层 |阅读模式
以下是我的操作步骤:  (实现创建各表及各表的数据输入)
1.建立school.sql文件:
#创建数据表student ,student_id为主键
create table student(
           student_id int unsigned not null auto_increment primary key,
           name varchar(20) not null,
           department_id int,
           birth  date,
           city varchar(20)
           );
          
#创建数据表department,department_id 为主键
create table department(
           department_id int unsigned not null auto_increment primary key,
           name varchar(40) not null,
           address varchar(20),
           dean int unsigned);
          
#创建数据表thacher,teacher_id为主键
create table teacher(
            teacher_id int unsigned not null auto_increment primary key,
            name varchar(30),
            department_id int,
            tlephone varchar(20)
            );          
            
          
#创建数据表teaching,用于连接teacher 和student两表
create table teaching(
              student_id int unsigned not null,
              teacher_id int unsigned not null );
             
#创建数据表test,test_id为表的主键
create table test(
           test_id int unsigned not null auto_increment primary key,
           test_date date not null,
           test_type enum('F','M') not null);
                                   
#创建数据表mark,student_id和test_id 为表的主键
create table mark (
           student_id int unsigned not null,
           test_id int unsigned not null,
           mark int not null,
           primary key(student_id,test_id));
          
#从文本文件/mysql/test/school/student.txt中读取数据并装载进数据表student
load data local infile "student.txt" into table student
  fields
       terminated by '\t'
       enclosed by ''
       escaped by '\\'
       lines terminated by '\n';
                 
#从文本文件/mysql/test/school/teacher.txt中读取数据并装载进数据表teacher
load data local infile "teacher.txt" into table teacher
  fields
       terminated by '\t'
       enclosed by ''
       escaped by '\\'
       lines terminated by '\n';

#从文本文件/mysql/test/school/teaching.txt中读取数据并装载进数据表teaching
load data local infile "teaching.txt" into table teaching
  lines terminated by '\n';      

#文本文件/mysql/test/school/test.txt中读取数据并装载进数据表test
load data local infile "test.txt" into table test
    lines terminated by '\n';

load data local infile"mark.txt" into table mark
   lines terminated  by '\n';

load  data local infile"department.txt" into table department
   lines terminated by '\n';
-----------------------------------------------------------------------------
然后建立各数据表,如其中的student.txt等.                                           
其中的student.txt文件:

100 linshengzhi   2    1976-12-06   BEIJING
101 yanghua     2   1980-10-11    SHANGHAI
102 zhongxin     2   1977-12-01   CHENGDU
103 huanghe     2   1978-01-02    GUANGZHOU
104 changjiang  2   1978-01-02    BEIJING
105 chaocao    3     19750-01-02   SHANGHAI
106 zhouyu'     3    1975-01-02    FUIJAN
107 zhugeliang   3 1977-01-04    XIANGYANG
108 huanggai   3   1957-04-05    JIANGDONG
-------------------------------------------------------------------------------
之后在命令窗口: (当然是进入school.sql所在目录)
# mysql -u root school < school.sql    (不知这一个是否正确?)

完成表的创建和数据导入.
但发现表中的数据有问题:

root@fish school]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3 to server version: 3.23.54

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use school
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from student;
+------------+------+---------------+-------+------+
| student_id | name | department_id | birth | city |
+------------+------+---------------+-------+------+
|        100 |      |          NULL | NULL  | NULL |
|        101 |      |          NULL | NULL  | NULL |
|        102 |      |          NULL | NULL  | NULL |
|        103 |      |          NULL | NULL  | NULL |
|        104 |      |          NULL | NULL  | NULL |
|        105 |      |          NULL | NULL  | NULL |
|        106 |      |          NULL | NULL  | NULL |
|        107 |      |          NULL | NULL  | NULL |
|        108 |      |          NULL | NULL  | NULL |
+------------+------+---------------+-------+------+
9 rows in set (0.01 sec)

mysql>

所有的表中的数据导入都有问题!---->只能导入数据文件中的第一列!其它的列的数据不能导入!好像上面的student.txt 导入表student (如上面显示的只有student_id能导入数据)

----------------------------------------------------------------------------
请高手帮我看看是不是哪里出错!
还有关于MYSQL 中创建数据库和表时,可以先建一个文本文件存放所有创建和操作的SQL语句,然后用重定向这个文本文件 的命令有哪些??(我上面的"mysql -u root school <school.sql"是否正确?)
谢谢先~~~~~~~~~~~~~~~~~~~
发表于 2004-7-22 14:17:52 | 显示全部楼层
有个问题:MySQL 有 varchar 列类型么?
回复

使用道具 举报

 楼主| 发表于 2004-7-22 14:56:44 | 显示全部楼层
TO版主大人:
MYSQL没有varchar列类型?是什么意思?
你的意思是不是说MYSQL没有varchar数据类型还是???????????
回复

使用道具 举报

发表于 2004-7-22 15:08:25 | 显示全部楼层
抱歉,我刚才试了一下,有 varchar 列类型~~~~
回复

使用道具 举报

 楼主| 发表于 2004-7-22 15:17:01 | 显示全部楼层
我把varchar全部改为char后
mysql>select  * from student
显示:


+------------+------+---------------+-------+------+
| student_id | name | department_id | birth | city |
+------------+------+---------------+-------+------+
|        100 |      |          NULL | NULL  | NULL |
|        101 |      |          NULL | NULL  | NULL |
|        102 |      |          NULL | NULL  | NULL |
|        103 |      |          NULL | NULL  | NULL |
|        104 |      |          NULL | NULL  | NULL |
|        105 |      |          NULL | NULL  | NULL |
|        106 |      |          NULL | NULL  | NULL |
|        107 |      |          NULL | NULL  | NULL |
|        108 |      |          NULL | NULL  | NULL |
+------------+------+---------------+-------+------+
9 rows in set (0.01 sec)
还是不行呀!
怎么回事????
请教ING~~~~~~~~
回复

使用道具 举报

发表于 2004-7-22 15:20:40 | 显示全部楼层
似乎找到原因了:

load data local infile "student.txt" into table student
fields
terminated by '\t'
enclosed by ''
escaped by '\\'
lines terminated by '\n';


我把这个 '\t' 改成了 ' ' (单引号里面是个空格),就可以成功导入。(数据是<ctrl>+<c>到我机器上的)
看来是数据的分隔符错误了。

我导入后:

[code:1]    -> enclosed by ''
    -> escaped by '\\'
    -> lines terminated by '\n';
Query OK, 9 rows affected (0.00 sec)
Records: 9  Deleted: 0  Skipped: 0  Warnings: 1

mysql> select * from student;
+------------+-------------+---------------+------------+------------+
| student_id | name        | department_id | birth      | city       |
+------------+-------------+---------------+------------+------------+
   |     100 | linshengzhi |             2 | 1976-12-06 | BEIJING
  |      101 | yanghua     |             2 | 1980-10-11 | SHANGHAI
   |     102 | zhongxin    |             2 | 1977-12-01 | CHENGDU
|       103 | huanghe     |             2 | 1978-01-02 | GUANGZHOU
   |     104 | changjiang  |             2 | 1978-01-02 | BEIJING
  |      105 | chaocao     |             3 | 0000-00-00 | SHANGHAI
    |    106 | zhouyu'     |             3 | 1975-01-02 | FUIJAN
|       107 | zhugeliang  |             3 | 1977-01-04 | XIANGYANG
|       108 | huanggai    |             3 | 1957-04-05 | JIANGDONG
+------------+-------------+---------------+------------+------------+
9 rows in set (0.00 sec)

mysql>[/code:1]
回复

使用道具 举报

发表于 2004-7-23 21:02:06 | 显示全部楼层
#从文本文件/mysql/test/school/student.txt中读取数据并装载进数据表student
load data local infile "student.txt" into table student
fields
terminated by '\t'
enclosed by ''
escaped by '\\'
lines terminated by '\n';


改成:
LOAD DATA LOCAL INFILE "student.txt" INTO TABLE student
LINES TERMINATED BY "\r\n"

其中LINES TERMINATED BY "\r\n",如果你的student.txt是在Windows建立的才要,在Linux建立的不必

字段的分隔默认就是TAB键,不必特意指定

至于enclosed by 和escaped by我不知道是什么(我前天才开始学MYSQL),但在这里应该不用
回复

使用道具 举报

发表于 2004-7-23 21:13:53 | 显示全部楼层
[quote:56e22fa7ec="jiangtao9999"]似乎找到原因了:
我把这个 '\t' 改成了 ' ' (单引号里面是个空格),就可以成功导入。(数据是<ctrl>+<c>到我机器上的)
看来是数据的分隔符错误了。
[/quote]

楼主应该没搞错吧,数据文件和SQL语句都是楼主自已写的,他总不会连自己用了什么分隔符也不知道吧,它帖出来的student.txt表的字段间之所以只显示一个空格大概是因为论坛的排版功能太弱了
回复

使用道具 举报

发表于 2004-7-24 09:03:39 | 显示全部楼层
[quote:968d7cabb3="Sam1860"][quote:968d7cabb3="jiangtao9999"]似乎找到原因了:
我把这个 '\t' 改成了 ' ' (单引号里面是个空格),就可以成功导入。(数据是<ctrl>+<c>到我机器上的)
看来是数据的分隔符错误了。
[/quote]

楼主应该没搞错吧,数据文件和SQL语句都是楼主自已写的,他总不会连自己用了什么分隔符也不知道吧,它帖出来的student.txt表的字段间之所以只显示一个空格大概是因为论坛的排版功能太弱了[/quote]

我也希望我搞错了,问题是我用 '/t' 导入的时候,结果和他一样。
只有第一列导入成功。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-5-11 04:25 , Processed in 0.165671 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表