MySQL知識(二十三) 表的匯出和匯入

2021-07-10 05:05:53 字數 4476 閱讀 5991

mysql資料庫中的資料可以匯出成.sql文字檔案、xml檔案或html檔案。

select columnlist from

table

where condition into outfile 'filename' [option]

--options 選項

fields terminated by

'value' /*設定字段之間分隔符,單個或多個字元,預設為'\t'*/

fields [optionally] encloseed by

'value' /*設定字段包圍分隔符,單個字元*/

fields escaped by

'value' /*如何寫入或讀取特殊字元,單個字元*/

lines starting by

'value' /*每行資料開頭的字元,單個或多個*/

lines terminated by

'value' /*每行資料結尾的字元,單個或多個*/

(1)例子

select * from test.person into outfile "c:/person0.txt";
person0.txt

1

green

21 student

2 suse 2 dancer

3 mary 24 musician

4 willam 20 student

5 laura 0

6 evans 27 secretary

7 dale 22 student

8 edison 28 cook

9 harry 21 student

(2)例子

select * from test.person into outfile "c:/person1.txt"

fields

terminated by

',' enclosed by

'\"'

escaped by

'\''

lines

terminated by

'\r\n';

person1.txt

"1","green","21","student"

"2","suse","2","dancer"

"3","mary","24","musician"

"4","willam","20","student"

"5","laura","0",""

"6","evans","27","secretary"

"7","dale","22","student"

"8","edison","28","cook"

"9","harry","21","student"

mysqldump -t path -u root -p dbname [tables][options]

--option 選項

--fields-terminated-by=value

--fields-enclosed-by=value

--fields-optionally-enclosed-by=value

--fields-escaped-by=value

--lines-terminated-by=value

(1)例子

mysqldump -t c:/ test person -u root -p
語句執行後生成兩個檔案,person.sql(包含create語句等)和person.txt(包含資料資訊)。

mysql -u root -p

--execute=

"select 語句" dbname>filename.txt

相比mysqldump,mysql工具匯出的結果可讀性更強。

(1)例子

mysql -u root -p

--execute=

"select * from person;" test > c:\person3.txt

(2)例子,使用- -vertical引數顯示結果

mysql -u root -p

--vertical --execute=

"select * from person;" test > c:\person4.txt

(3)例子,匯出為html檔案

mysql -u root -p

--html --execute=

"select * from person;" test > c:\person5.html

(4)例子,匯出為xml檔案

mysql -u root -p

--xml

--execute=

"select * from person;" test > c:\person6.

xml

load data infile 'filename.txt'

into

table tablename [option][ignore number lines]

--options 選項

fields terminated by

'value' /*設定字段之間分隔符,單個或多個字元,預設為'\t'*/

fields [optionally] encloseed by

'value' /*設定字段包圍分隔符,單個字元*/

fields escaped by

'value' /*如何寫入或讀取特殊字元,單個字元*/

lines starting by

'value' /*每行資料開頭的字元,單個或多個*/

lines terminated by

'value' /*每行資料結尾的字元,單個或多個*/

(1)例子

mysql> select *  from test.person into outfile "c:/person0.txt";

query ok, 9 rows affected

mysql> use test;

database changed

mysql> delete

from person;

query ok, 9 rows affected

mysql> load data infile 'c:/person0.txt'

into

table test.person;

query ok, 9 rows affected

records: 9 deleted: 0 skipped: 0 warnings: 0

mysql> select * from person;

+----+--------+-----+-----------+

| id | name | age | info |

+----+--------+-----+-----------+

| 1 | green | 21 | student |

| 2 | suse | 2 | dancer |

| 3 | mary | 24 | musician |

| 4 | willam | 20 | student |

| 5 | laura | 0 | |

| 6 | evans | 27 | secretary |

| 7 | dale | 22 | student |

| 8 | edison | 28 | cook |

| 9 | harry | 21 | student |

+----+--------+-----+-----------+

9 rows in set

mysqlimport -u root -p dbname filename.txt [options]

--option 選項

--fields-terminated-by=value

--fields-enclosed-by=value

--fields-optionally-enclosed-by=value

--fields-escaped-by=value

--lines-terminated-by=value

--ignore-lines=n

二十三 MySQL 事務

mysql 事務主要用於處理操作量大,複雜度高的資料。比如說,在人員管理系統中,你刪除乙個人員,你即需要刪除人員的基本資料,也要刪除和該人員相關的資訊,如信箱,文章等等,這樣,這些資料庫操作語句就構成乙個事務!一般來說,事務是必須滿足4個條件 acid 原子性 atomicity,或稱不可分割性 一...

二十三 MySQL賬戶管理

在生產環境下運算元據庫時,絕對不可以使用root賬戶連線,而是建立特定的賬戶,授予這個賬戶特定的操作許可權,然後連線進行操作,主要的操作就是資料的crud。mysql賬戶體系 根據賬戶所具有的許可權的不同,mysql的賬戶可以分為以下幾種 賬戶的操作主要包括建立賬戶 刪除賬戶 修改密碼 授權許可權等...

(二十三)CISC和RISC

一 概念釋義 按指令長度是否可變,將cpu中執行的指令集分為 cisc complex instruction set computing 複雜指令集,指令長度可變 和 risc reduced instruction set computing 精簡指令集,指令程度不可變 二 cisc 和 ris...