Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-09-03
1
Medium Priority
?
55 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 80

Accepted Solution

by:
arnold earned 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month7 days, 15 hours left to enroll

824 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