MySQL資料庫通過命令匯入匯出資料

來源:互聯網
上載者:User

學習總結基本的MySQL資料庫匯入匯出操作
 
 1 表tt的格式:
 
 CREATE TABLE `tt` (
  `ind` int NOT NULL auto_increment,
  `name` char(100) default NULL,
  PRIMARY KEY  (`ind`)
 )
 
2 檔案d.txt的內容樣本:
 1,a
 2,b
 3,c
 
 3 匯入命令:
 mysql> load data infile 'd.txt' into table tt
    -> fields terminated by','
    -> lines terminated by'\r\n'
 
 注意的地方:
 1)、檔案可以使用絕對路徑如'c:/d.txt',否則請將檔案放在資料庫根目錄中
 2)、因為欄位之間用了逗號隔開,所以必須fields terminated by',',否則匯入失敗
 3)、因為Winsows中行以“\r\n”隔開,所以必須lines terminated by'\r\n',
 如果不設定這個參數,也可匯入成功,但是會多匯入一個“\r”控制字元,可能在可視化
 MySQL工具中看不出欄位異樣,但是在MySQL命令列中顯示會明顯混亂。
 4)、如果表tt非空,且檔案中的ind值在表中有重複,會提示錯誤,並匯入失敗。
 
 只匯入name欄位,檔案d.txt的內容:
 a
 b
 c
 
 mysql> load data infile 'd.txt' into table tt
    -> lines terminated by'\r\n'
    -> (name);
 
 load data 命令還支援更複雜的文字格式設定、檔案編碼等,可參考官方文檔。
 
 5 匯出到資料到windows文字檔時,為了方便查看,也需要相同的設定
 mysql> select * from tt into outfile 'd.txt'
    -> fields terminated by','
    -> lines terminated by'\r\n'

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.