MySQL数据导入导出牛刀小试

最近学习了下MySQL中数据的导入导出,发现功能点真是丰富,很方便很快捷。
这些导入导出的方式还是有不少的细节的,在此先不做扩展和深入分析。
--数据导出 方式1
比如要实现数据的导出,直接可以指定生成的文件使用outfile即可。对于空值的处理是“\N"
mysql> select * from test into outfile '/u02/mysql/dump/a.sql';
Query OK, 4 rows affected (0.00 sec)
1       aaaa
2       bbbb
3       c
4       \N
--数据导出 方式2
如果需要导出某个数据库下的表结构和数据,使用mysqldump也是不错的选择。生成的文件中,.txt是对应的数据,.sql是对应的表结构sql。
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test           |
| test1          |
+----------------+
2 rows in set (0.00 sec)
mysqldump -T /u02/mysql/dump -u root test
导出后查看目录结构。
[mysql@oel1 dump]$ ll
total 8
-rw-r--r-- 1 mysql dba 1338 Apr 11 22:39 test1.sql
-rw-rw-rw- 1 mysql dba    0 Apr 11 22:39 test1.txt
-rw-r--r-- 1 mysql dba 1364 Apr 11 22:39 test.sql
-rw-rw-rw- 1 mysql dba    0 Apr 11 22:39 test.txt
--数据导出 方式3
这种方式,直接把sql语句通过参数传入,确实很赞。注意这个时候生成的空值是NULL,而不是"\N"
[mysql@oel1 dump]$ mysql -u root --execute="select *from test;" test > aa.sql
[mysql@oel1 dump]$ cat aa.sql
id      name
1       aaaa
2       bbbb
3       c
4       NULL
[mysql@oel1 dump]$ cat ../a.sql
1       aaaa
2       bbbb
3       c
4       \N
--数据导出 方式4
如果这个时候需要导出的表中列较多,可以通过键值对的方式纵向显示。可读性就大大增强了。比如下面的方式。
[mysql@oel1 dump]$ mysql -u root --vertical --execute="select *from test;" test > aa.sql
[mysql@oel1 dump]$ cat aa.sql
*************************** 1. row ***************************
  id: 1
name: aaaa
*************************** 2. row ***************************
  id: 2
name: bbbb
*************************** 3. row ***************************
  id: 3
name: c
*************************** 4. row ***************************
  id: 4
name: NULL

注意这个时候,我指定sql语句以”;"结尾,和不加结尾符的效果是一样的,可见这个时候还是有一定的健壮性。
[mysql@oel1 dump]$ mysql -u root --vertical --execute="select *from test" test > aa.sql
[mysql@oel1 dump]$ cat aa.sql
*************************** 1. row ***************************
  id: 1
name: aaaa
*************************** 2. row ***************************
  id: 2
name: bbbb
*************************** 3. row ***************************
  id: 3
name: c
*************************** 4. row ***************************
  id: 4
name: NULL

--数据导出 方式5
如果需要导出的数据为html格式,也很清晰。直接调用-html选项即可。
mysql -u root --html --execute="select *from test" test > aa.html
mysql@oel1 dump]$ cat aa.html
id name
1 aaaa
2 bbbb
3 c
4 NULL
[mysql@oel1 dump]$ 
--数据导出 方式6
如果指定导出数据格式为xml,直接调用-xml选项即可。
[mysql@oel1 dump]$ mysql -u root --xml --execute="select *from test" test > aa.html
[mysql@oel1 dump]$ cat aa.html


 
        1
        aaaa
 
 
        2
        bbbb
 
 
        3
        c
 
 
        4
       
 

--数据导出 方式7
如果希望把数据的结果缓存出来,使用-tee也是一种选择,这种方式会导出所有的操作结果,可以根据自己的需要来做导出。
mysql> tee a.log
Logging to file 'a.log'
mysql> select *from test;
Empty set (0.00 sec)
mysql> select *from test1;
Empty set (0.00 sec)
mysql> notee;
Outfile disabled.
这个时候会把sql和对应的数据结果都生成在日志里面。

--数据导入 方式1
数据的导入可以在mysql命令里面使用load data的方式来实现,也是比较轻巧的。
mysql> load data infile '/u02/mysql/dump/a.sql'  into table test.test ;
Query OK, 4 rows affected (0.01 sec)
Records: 4  Deleted: 0  Skipped: 0  Warnings: 0
mysql> select *from test;
+------+------+
| id   | name |
+------+------+
|    1 | aaaa |
|    2 | bbbb |
|    3 | c    |
|    4 | NULL |
|    1 | aaaa |
|    2 | bbbb |
|    3 | c    |
|    4 | NULL |
+------+------+
8 rows in set (0.00 sec)
--数据导入 方式2
数据导入还可以使用mysqlimport来实现,比如我们需要导入的表为test,数据文件为test.txt 
 [mysql@oel1 dump]$  mysqlimport -u root test '/u02/mysql/dump/test.txt'
test.test: Records: 0  Deleted: 0  Skipped: 0  Warnings: 0


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23718752/viewspace-1564990/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/23718752/viewspace-1564990/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值