天天看點

mysql 導入導出_mysql資料導入導出總結

mysql檔案導入導出有2種方式:

1. 導入導出操作SQL檔案,通過執行SQL語句來完成操作,檔案中包含建表語句和一系列的插入語句;

2. 導入導出操作特定格式的文本資料,該方式檔案中僅包含資料,注意檔案的編碼和資料庫編碼要相容,檔案編碼應該和character_set_database一緻,而與set names charaset的charset無任何關系;

這裡隻是方式2的操作方法:

導出到檔案:

select * from custom into outfile 'e:\custom.txt' FIELDS TERMINATED BY ',' optionally enclosed by '"';

從檔案導入:

load data infile 'e:\custom.txt' replace into table custom;

load data infile 'e:\custom.txt' replace into table custom fields terminated by ',' optionally enclosed by '"';

從庫中其他表導入:

insert into tablename1 select * from tablename2;

/ 導入導出給定列 ///

load data infile 'e:\custom.txt' replace into table custom fields terminated by ',' optionally enclosed by

'"'(customid,name,sex);

select customid,name,sex from custom into outfile 'e:\acustom.txt' fields terminated by ',' optionally enclosed

by '"';

MYSQL的主鍵是放主存的,第一次的時候執行max擷取最大編号,如果插入的時候沒有就自增,如果插入的時候指定了主鍵,則判

斷是否大于max,是則設定主鍵為max,并插入記錄,否則替換或出錯;如果自增主鍵為null,則仍是自增;

是以導入的時候,自增主鍵也可以直接導入;

如果導入的時候出現:truncated字樣,則是SQL_MODE問題,修改sql_mode就可以了;

show variables like '%sql_mode%';

set sql_mode='no_auto_create_user,no_engine_substitution';

如出現錯誤:ERROR 1262 (01000): Row 6737 was truncated; it contained more data than there were input columns.

如:檔案中出現,,空字元,正常情況下會出錯,需要修改sql_mode後才能導入(會有正常警告);