mysql使用技巧

2021-10-11 04:21:48 字數 2887 閱讀 1031

刪除表中資料

刪除整張表的資料!!!

delete from 表名;

匯入資料基本語法原文

一、loaddata 匯入資料的語法 load data infile

基本語法

複製**

load data [low_priority] [local] infile 『file_name.txt』 [replace|ignore]

into table table_name

fields

[terminated by 『\t』]

[[optionally] enclosed by 『』]

[escaped by 『』 ]

[lines terminated by 『\n』]

[ignore number lines]

[(col_name, )]

複製**

引數說明

參 數 說 明 備 注

low_priority mysql將會等到沒有其他人讀這個表的時候,才把資料插入 load data low_priority infile 「/tmp/harara.sql」 into table orders;

local 表明從客戶主機讀檔案。如果local沒指定,檔案必須位於伺服器上 load data low_priority local infile 「/tmp/harara.sql」 into table orders;

replace 如果你指定replace,新行將代替有相同的唯一健值的現有行 load data low_priority local infile 「/tmp/harara.sql」 replace into table orders;

ignore 如果你指定ignore,跳過有唯一鍵的現有行的重複行輸入 load data low_priority local infile 「/tmp/harara.sql」 ignore into table orders;

fields terminated by 描述欄位的分隔符,預設情況下是tab字元(\t) load data infile 「/tmp/harara.sql」 replace into table orders fields terminated by 『,』 enclosed by 『"』 escaped by 『』;

fields enclosed by 描述的是字段的括起字元。

fields escaped by escaped by 描述的轉義字元。預設的是反斜槓: \

lines terminated by』\n』 指定每條記錄的分隔符預設為"\n" 即為換行符 load data infile 「/tmp/harara.sql」 replace into table test fields terminated by 『,』 lines terminated by 『\n』;

(col_name, ) 可以按指定的列把檔案匯入到資料庫中

當我們要把資料的一部分內容匯入,需要加入一些欄目(列/字段/field)到mysql資料庫中,以適應一些額外的需要。

比如,我們要從access資料庫公升級到mysql資料庫的時候,下面的例子顯示了如何向指定的欄目(field)中匯入資料:

load data infile 「/home/harara.sql」 into table orders(order_number,order_date,customer_id)

匯入資料完整示例

load data local infile 『/var/lib/mysql/emp8/customers.txt』 replace into table lf_lanx_tmplp1 character set gbk

fields terminated by 『,』 enclosed by 『"』

lines terminated by 『\n』

(tmpl_name);

二、loaddata 匯出資料的語法 select into outfile

基本語法

複製**

select … into outfile 『file_name』

[character set charset_name]

[export_options]

export_options:

[[terminated by 『string』]

[[optionally] enclosed by 『char』]

[escaped by 『char』]

][lines

[starting by 『string』]

[terminated by 『string』]

]複製**

引數說明

參 數 說 明

fields terminated by 『str』 設定字段之間的分隔符,預設是"\t"

fields enclosed by 『char』 設定包括住字段的值的符號,如單引號、雙引號等,預設情況下不使用任何符號

fields optionally enclosed by 『char』 設定括住char、varchar和text等字元型字段的分隔符,預設情況下不使用任何符號

fields escaped by 『char』 設定轉義字元,預設值為""

lines starting by 『str』 設定每行資料開頭的字元,可以為單個或多個字元。預設情況下不使用任何字元

lines terminated by 『char』 設定每行資料結尾的字元,可以為單個或多個字元。預設值是"\n"

完整例項

select tmpl_name into outfile 『u/customers.txt』

fields terminated by 『,』 optionally enclosed by 『"』

lines terminated by 『\n』

from lf_lanx_tmplp1;

mysql使用技巧

1 分組查詢,並將組內的某個欄位值用逗號連線起來作為乙個新的字段,例如 select url,group concat type name as type names from music type group by url 即把同乙個url下面的type name欄位值用逗號連線起來。2 if函式...

mysql使用技巧

mysql使用者名稱和密碼是root 所以登入 u接使用者名稱,p接密碼,由於沒有密碼所以直接回車 root web01 mysql mysql uroot p 所以mysql不安全,需要給加密碼。mysql設定密碼分兩種情況 第一種情況 沒有密碼,設定密碼 設定密碼 1 root web01 my...

MySQL使用小技巧

1 忘記密碼 參考 如果你忘記了密碼,怎麼辦?我們可以開啟 etc my.cnf 檔案,在檔案最下面裡寫入 skip grant tables 儲存退出就可以了,然後記得重啟!然後就可以不用輸入密碼就進入了,然後進入之後修改密碼!如果你修改密碼的時候報這個錯 operation alter user...