mysql数据库中的数据导出来以文本文件显示,而不是sql语句的格式。同样有时也会用到load data来加载文本数据到数据库中。

1.select   ...   into  outfile  ....   命令导出数据。

MYSQL数据的导出和加载_第1张图片

举例:

 

mysql> select * from student;
+------+-------+
| id   | name  |
+------+-------+
|    1 | zhu   |
|    2 | jiang |
|    3 | tao   |
+------+-------+
3 rows in set (0.00 sec)
mysql>
mysql> select * from student into outfile '/tmp/zhu1.txt' fields terminated by ","
enclosed by '"';
Query OK, 3 rows affected (0.00 sec)
mysql> system cat /tmp/zhu1.txt
"1","zhu"
"2","jiang"
"3","tao"
mysql> select * from student into outfile '/tmp/zhu2.txt' fields terminated by "," optionally enclosed by '"';
Query OK, 3 rows affected (0.00 sec)
mysql> system cat /tmp/zhu2.txt
1,"zhu"
2,"jiang"
3,"tao"
mysql> select * from student into outfile '/tmp/zhu3.txt' fields terminated by "," optionally enclosed by "'";
Query OK, 3 rows affected (0.00 sec)
mysql> system cat /tmp/zhu3.txt
1,'zhu'
2,'jiang'
3,'tao'

导入

 

mysql> select * from student;
+------+-------+
| id   | name  |
+------+-------+
|    1 | zhu   |
|    2 | jiang |
|    3 | tao   |
+------+-------+
3 rows in set (0.00 sec)
mysql> delete from student;
Query OK, 3 rows affected (0.00 sec)
mysql> select * from student;
Empty set (0.01 sec)
mysql> load data infile '/tmp/zhu3.txt' into table student  fields terminated by ',' optionally enclosed by "'";
Query OK, 3 rows affected (0.00 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 0
mysql> select * from student;
+------+-------+
| id   | name  |
+------+-------+
|    1 | zhu   |
|    2 | jiang |
|    3 | tao   |
+------+-------+
3 rows in set (0.00 sec)

MYSQL数据的导出和加载_第2张图片