Solved

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

Posted on 2016-09-03
1
51 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 79

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 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