Problem Solution Discussion Skipping Datafile Columns

The - - colum ns opt ion for m ysqlim port was int roduced in MySQL 3.23.17. I f you have an older version, you m ust eit her use LOAD DATA direct ly or preprocess your dat afile t o rearrange t he files colum ns int o t he order in which t hey occur in t he t able. See Recipe 10.20 for a ut ilit y t hat can do t his.

10.13 Skipping Datafile Columns

10.13.1 Problem

Your dat afile cont ains colum ns t hat should be ignored rat her t han loaded int o t he t able.

10.13.2 Solution

That s not a problem if t he colum ns are at t he ends of t he input lines. Ot herwise, youll need t o preprocess t he dat afile before loading it .

10.13.3 Discussion

Ext ra colum ns t hat occur at t he end of input lines are easy t o handle. I f a line cont ains m ore colum ns t han are in t he t able, LOAD DATA j ust ignores t hem t hough it m ay indicat e a nonzero warning count . Skipping colum ns in t he m iddle of lines is a bit m ore involved. Suppose you want t o load inform at ion from a Unix password file et c passwd, which cont ains lines in t he following form at : account:password:UID:GID:GECOS:directory:shell Suppose also t hat you dont want t o bot her loading t he password colum n. A t able t o hold t he inform at ion in t he ot her colum ns looks like t his: CREATE TABLE passwd account CHAR8, login name uid INT, user ID gid INT, group ID gecos CHAR60, name, phone, office, etc. directory CHAR60, home directory shell CHAR60 command interpreter ; To load t he file, we need t o specify t hat t he colum n delim it er is a colon, which is easily handled wit h a FIELDS clause: FIELDS TERMINATED BY : However, we m ust also t ell LOAD DATA t o skip t he second field t hat cont ains t he password. That s a problem , because LOAD DATA always want s t o load successive colum ns from t he dat afile. You can t ell it which t able colum n each dat afile colum n corresponds t o, but you cant t ell it t o skip colum ns in t he file. To deal wit h t his difficult y, we can preprocess t he input file int o a t em porary file t hat doesnt cont ain t he password value, t hen load t he t em porary file. Under Unix, you can use t he cut ut ilit y t o ext ract t he colum ns t hat you want , like t his: cut -d: -f0,3- etcpasswd passwd.txt The - d opt ion specifies a field delim it er of : and t he - f opt ion indicat es t hat you want t o cut colum n one and all colum ns from t he t hird t o t he end of t he line. The effect is t o cut all but t he second colum n. Run m an cut for m ore inform at ion about t he cut com m and. Then use LOAD DATA t o im port t he result ing passwd.t xt file int o t he passwd t able like t his: mysql LOAD DATA LOCAL INFILE passwd.txt INTO TABLE passwd - FIELDS TERMINATED BY :; The corresponding m ysqlim port com m and is: mysqlimport --local --fields-terminated-by=: cookbook passwd.txt 10.13.4 See Also cut always displays out put colum ns in t he sam e order t hey occur in t he file, no m at t er what order you use when you list t hem wit h t he - f opt ion. For exam ple, cut - f1,2,3 and cut - f3,2,1 produce t he sam e out put . Recipe 10.20 discusses a ut ilit y t hat can pull out and display colum ns in any order.

10.14 Exporting Query Results from MySQL