mysql匯入資料load data infile用法,mysqlinfile

來源:互聯網
上載者:User

mysql匯入資料load data infile用法,mysqlinfile

mysql匯入資料load data infile用法

基本文法:load data [low_priority] [local] infile 'file_name txt' [replace | ignore]
into table tbl_name
[fields
[terminated by't']
[OPTIONALLY] enclosed by '']
[escaped by'\' ]]
[lines terminated by'n']
[ignore number lines]
[(col_name, )]

load data infile語句從一個文字檔中以很高的速度讀入一個表中。使用這個命令之前,mysqld進程(服務)必須已經在運行。為了安全原因,當讀取位於伺服器上的文字檔時,檔案必須處於資料庫目錄或可被所有人讀取。另外,為了對伺服器上檔案使用load data infile,在伺服器主機上你必須有file的許可權。1 如果你指定關鍵詞low_priority,那麼MySQL將會等到沒有其他人讀這個表的時候,才把插入資料。可以使用如下的命令:
load data low_priority infile "/home/mark/data sql" into table Orders;2 如果指定local關鍵詞,則表明從客戶主機讀檔案。如果local沒指定,檔案必須位於伺服器上。3 replace和ignore關鍵詞控制對現有的唯一鍵記錄的重複的處理。如果你指定replace,新行將代替有相同的唯一鍵值的現有行。如果你指定ignore,跳過有唯一鍵的現有行的重複行的輸入。如果你不指定任何一個選項,當找到重複鍵時,出現一個錯誤,並且文字檔的餘下部分被忽略。例如: load data low_priority infile "/home/mark/data sql" replace into table Orders;4 分隔字元(1) fields關鍵字指定了檔案記段的分割格式,如果用到這個關鍵字,MySQL剖析器希望看到至少有下面的一個選項:
terminated by分隔字元:意思是以什麼字元作為分隔字元
enclosed by欄位括起字元
escaped by逸出字元terminated by描述欄位的分隔字元,預設情況下是tab字元(\t)
enclosed by描述的是欄位的括起字元。
escaped by描述的逸出字元。預設的是反斜線(backslash:\ ) 例如:load data infile "/home/mark/Orders txt" replace into table Orders fields terminated by',' enclosed by '"';(2)lines 關鍵字指定了每條記錄的分隔字元預設為'\n'即為分行符號如果兩個欄位都指定了那fields必須在lines之前。如果不指定fields關鍵字預設值與如果你這樣寫的相同:fields terminated by'\t' enclosed by ’ '' ‘ escaped by'\\'如果你不指定一個lines子句,預設值與如果你這樣寫的相同:lines terminated by'\n' 例如:load data infile "/jiaoben/load.txt" replace into table test fields terminated by ',' lines terminated by '/n';5 load data infile 可以按指定的列把檔案匯入到資料庫中。 當我們要把資料的一部分內容匯入的時候,,需要加入一些欄目(列/欄位/field)到MySQL資料庫中,以適應一些額外的需要。比方說,我們要從Access資料庫升級到MySQL資料庫的時候下面的例子顯示了如何向指定的欄目(field)中匯入資料:
load data infile "/home/Order txt" into table Orders(Order_Number, Order_Date, Customer_ID);6 當在伺服器主機上尋找檔案時,伺服器使用下列規則:
(1)如果給出一個絕對路徑名,伺服器使用該路徑名。
(2)如果給出一個有一個或多個前置組件的相對路徑名,伺服器相對伺服器的資料目錄搜尋檔案。
(3)如果給出一個沒有前置組件的一個檔案名稱,伺服器在當前資料庫的資料庫目錄尋找檔案。
例如: /myfile txt”給出的檔案是從伺服器的資料目錄讀取,而作為“myfile txt”給出的一個檔案是從當前資料庫的資料庫目錄下讀取。 注意:欄位中的空值用\N表示

相關文章

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.