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
[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
這些匯入匯出的方式還是有不少的細節的,在此先不做擴充套件和深入分析。
--資料匯出 方式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 |
--資料匯出 方式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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 資料庫 MySQL 資料匯入匯出資料庫MySql
- MySQL入門--匯出和匯入資料MySql
- Mysql 資料庫匯入與匯出MySql資料庫
- Mysql匯入&匯出MySql
- SQLServer匯出匯入資料到MySQLServerMySql
- sqoop用法之mysql與hive資料匯入匯出OOPMySqlHive
- sqoop資料匯入匯出OOP
- Oracle 資料匯入匯出Oracle
- 資料泵匯出匯入
- Oracle資料匯入匯出Oracle
- phpMyAdmin匯入/匯出資料PHP
- 【MySQL】白話說MySQL(五),資料的匯出與匯入MySql
- mysql匯出資料MySql
- MYSQL資料匯出備份、匯入的幾種方式MySql
- Navicat、into outfile、mysql命令、mysqldump、mysqlpump、mydumper匯出匯入資料MySql
- Oracle資料庫匯入匯出。imp匯入命令和exp匯出命令Oracle資料庫
- Mongodb資料的匯出與匯入MongoDB
- oracle資料匯出匯入(exp/imp)Oracle
- 匯入和匯出AWR的資料
- EasyPoi, Excel資料的匯入匯出Excel
- MySQL資料的匯出MySql
- MySQL資料的匯入MySql
- Linux雲伺服器手動匯入匯出mysql資料庫Linux伺服器MySql資料庫
- Oracle資料泵匯出匯入(expdp/impdp)Oracle
- 【最佳實踐】MongoDB匯出匯入資料MongoDB
- SQL資料庫的匯入和匯出SQL資料庫
- Oracle資料泵的匯入和匯出Oracle
- 複雜「場景」資料匯入匯出
- ClickHouse 資料表匯出和匯入(qbit)
- [Docker核心之容器、資料庫檔案的匯入匯出、容器映象的匯入匯出]Docker資料庫
- spark sql與mysql 資料載入與匯出資料到mysqlSparkMySql
- MongoDB--Mongodb 中資料匯出與匯入MongoDB
- PHP大資料xlswriter匯入匯出(最優資料化)PHP大資料
- MySQL匯出資料庫指令碼MySql資料庫指令碼
- MySQL匯入百萬資料實踐MySql
- Windows 下 MySQL 資料匯入 RedisWindowsMySqlRedis
- mongodb使用自帶命令工具匯出匯入資料MongoDB
- QZpython匯入匯出redis資料的實現deuPythonRedis
- 使用Dbeaver 進行資料的匯入和匯出