Solved

Need help getting LOAD Inflie to work for a table with the PK auto_incremented

Posted on 2016-09-03
1
31 Views
Last Modified: 2016-09-14
I can not get the LOAD infile statement to work to populate track which has the first column as the PK auto_increment.  The file has only 3 columns and I want to allow MySQL to create the trkid then have the LOAD infile populate columns 2,3, and 4 columns. When I run this the LOAD infile is populating columns 1,2, and 3.   How do I alter the LOAD infile to make it populate properly.

CREATE TABLE track (    
    trkid   INTEGER auto_increment,  
      trknum     INTEGER,    
      trktitle    VARCHAR(50),      
      trklength   DECIMAL(4,2),        
         PRIMARY KEY (trkid));
                     
LOAD DATA LOCAL INFILE       /*this file does not include trkid and starts with trknum bc trkid is auto_incremented*/
'/Users/chon.abraham/CD_data.txt'  
INTO TABLE track
FIELDS TERMINATED BY ','
ENCLOSED BY ''''
LINES TERMINATED BY '\r\n' ;

The file looks like

(1, 'Giant Steps', 4.72),
(2, 'Cousin Mary', 5.75),
(3, 'Countdown', 2.35),
(4, 'Spiral', 5.93),
(5, 'Syeeda''s song flute', 7),
(6, 'Naima', 4.35),(7, 'Mr. P.C.', 6.95),
(8, 'Giant Steps', 3.67),(9, 'Naima', 4.45),
(10, 'Cousin Mary', 5.9),
(11, 'Countdown', 4.55),
(12, 'Syeeda''s song flute', 7.03),
(1, 'Stomp of King Porter', 3.2),
(2, 'Sing a Study in Brown', 2.85),
(3, 'Sing Moten''s Swing', 3.6),
(4, 'A-tisket, A-tasket', 2.95),
(5, 'I Know Why', 3.57),
(6, 'Sing You Sinners', 2.75),
(7, 'Java Jive', 2.85),
(8, 'Down South Camp Meetin''', 3.25),
(9, 'Topsy', 3.23),
(10, 'Clouds', 7.2),
(11, 'Skyliner', 3.18),
(12, 'It''s Good Enough to Keep', 3.18),
(13, 'Choo Choo Ch'' Boogie', 3)
0
Comment
Question by:chonabraham
1 Comment
 
LVL 77

Accepted Solution

by:
arnold earned 500 total points
ID: 41783273
After into table track and before fields terminated by, add
(trknum,trktitle,trklength)

This will direct MySQL to apply the data from the file into the three columns of the table, the PK when not included will be assigned a value auto_increment......

see MySQL writeup on load Infile
https://dev.mysql.com/doc/refman/5.7/en/load-data.html
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Creating and Managing Databases with phpMyAdmin in cPanel.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now