Import .csv to mySQL when there is an auto-increment on Primary Key

Posted on 2009-02-23
Last Modified: 2012-05-06
I want to import a .csv file into a mySQL database.  My first field is the ID field, which is the Primary Key, set to auto-increment.  I thought I could leave that field blank in the .csv and have the database take care of it.  I found out I was wrong.  How should that be handled?
Question by:DennisHacker
    LVL 39

    Expert Comment

    by:Roger Baklund
    How are you doing the import? If you use LOAD DATA, leave the ID field out of the field list, but list all the other fields.

    Author Comment

    Actually I was using the LOAD DATA feature built in to phpmyadmin.  You're right, I had that field listed in the list.  If the field is first in the database, and I leave it out of the .csv file, will the data import and just auto-increment the ID field, or do I have to put that field at the end?
    LVL 39

    Accepted Solution

    Just leave out that field from the LOAD DATA and from the csv file. No need to put it at the end. If the table has 10 columns including the ID, the csv file should have 9 columns.

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    755 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

    20 Experts available now in Live!

    Get 1:1 Help Now