Problem Solution Discussion Handling Duplicate Index Values

10.7 Reading Files from Different Operating Systems

10.7.1 Problem

Different operat ing syst em s use different line-ending sequences.

10.7.2 Solution

That s why LOAD DATA has a LINES TERMINATED BY clause.

10.7.3 Discussion

The line-ending sequence used in a dat afile t ypically is det erm ined by t he syst em on which t he file originat es, not t he syst em on which you im port it . Keep t his in m ind when loading a file t hat is obt ained from a different syst em . Unix files norm ally have lines t erm inat ed by linefeeds, which you can indicat e in a LOAD DATA st at em ent like t his: LINES TERMINATED BY \n However, because \n happens t o be t he default line t erm inat or for LOAD DATA , you dont need t o specify a LINES TERMINATED BY clause in t his case unless you want t o indicat e explicit ly what t he line ending sequence is. Files creat ed under Mac OS or Windows usually have lines ending in carriage ret urns or carriage ret urn linefeed pairs. To handle t hese different kinds of line endings, use t he appropriat e LINES TERMINATED BY clause: LINES TERMINATED BY \r LINES TERMINATED BY \r\n For exam ple, t o load a Windows file t hat cont ains t ab- delim it ed fields and lines ending wit h CRLF pairs, use t his LOAD DATA st at em ent : mysql LOAD DATA LOCAL INFILE mytbl.txt INTO TABLE mytbl - LINES TERMINATED BY \r\n; The corresponding m ysqlim port com m and is: mysqlimport --local --lines-terminated-by=\r\n cookbook mytbl.txt

10.8 Handling Duplicate Index Values

10.8.1 Problem

Your input cont ains records t hat duplicat e t he values of unique keys in exist ing t able records.

10.8.2 Solution

Tell LOAD DATA t o ignore t he new records, or t o replace t he old ones.

10.8.3 Discussion

By default , an error occurs if you at t em pt t o load a record t hat duplicat es an exist ing record in t he colum n or colum ns t hat form a PRIMARY KEY or UNIQUE index. To cont rol t his behavior, specify IGNORE or REPLACE aft er t he filenam e t o t ell MySQL t o eit her ignore duplicat e records or t o replace old records wit h t he new ones. Suppose you periodically receive m et eorological dat a about current weat her condit ions from various m onit oring st at ions, and t hat you st ore m easurem ent s of various t ypes from t hese st at ions in a t able t hat looks like t his: CREATE TABLE weatherdata station INT UNSIGNED NOT NULL, type ENUMprecip,temp,cloudiness,humidity,barometer NOT NULL, value FLOAT, UNIQUE station, type ; To m ake sure t hat you have only one record for each st at ion for each t ype of m easurem ent , t he t able includes a unique key on t he com binat ion of st at ion I D and m easurem ent t ype. The t able is int ended t o hold only current condit ions, so when new m easurem ent s for a given st at ion are loaded int o t he t able, t hey should kick out t he st at ions previous m easurem ent s. To accom plish t his, use t he REPLACE keyword: mysql LOAD DATA LOCAL INFILE data.txt REPLACE INTO TABLE weatherdata;

10.9 Getting LOAD DATA to Cough Up More Information