MySQL資料匯入匯出牛刀小試(r5筆記第3天)
阿新 • • 發佈:2022-05-04
最近學習了下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
[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
[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
<TABLE BORDER=1><TR><TH>id</TH><TH>name</TH></TR><TR><TD>1</TD><TD>aaaa</TD></TR><TR><TD>2</TD><TD>bbbb</TD></TR><TR><TD>3</TD><TD>c</TD></TR><TR><TD>4</TD><TD>NULL</TD></TR></TABLE>[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
<?xml version="1.0"?>
<resultset statement="select *from test
" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<field name="id">1</field>
<field name="name">aaaa</field>
</row>
<row>
<field name="id">2</field>
<field name="name">bbbb</field>
</row>
<row>
<field name="id">3</field>
<field name="name">c</field>
</row>
<row>
<field name="id">4</field>
<field name="name" xsi:nil="true" />
</row>
</resultset>
--資料匯出 方式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