Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql LOAD DATA INFILE with auto-increment primary key

I am trying to load a data file into mysql table using "LOAD DATA LOCAL INFILE 'filename' INTO TABLE 'tablename'".

The problem is the source data file contains data of every fields but the primary key is missing ('id' column). I add a unique id field while I create the database but now I need to import the data into the table starting from the next field and auto increment the id field while importing.

def create_table():
            cursor.execute ("""
                    CREATE TABLE variants
                    (
                    id integer(10) auto_increment primary key,
                    study_no CHAR(40),
                    other fields.....


                    )
                    """)

here is my LOAD query

query1= "LOAD DATA LOCAL INFILE '"+currentFile+"' INTO TABLE variants FIELDS TERMINATED BY '\\t' LINES TERMINATED BY '\\n'"

any ideas?

Summary: create a table with an additional id field that would auto increment load data (20 columns) into the table of 21 fields skipping the id field let the id field automatically populate with an auto increment index.

like image 787
biomed Avatar asked Apr 26 '10 18:04

biomed


People also ask

Does MySQL auto increment primary key?

The Auto Increment feature allows you to set the MySQL Auto Increment Primary Key field. This automatically generates a sequence of unique numbers whenever a new row of data is inserted into the table.

Can a primary key be auto increment?

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.

Can you auto increment a non primary key?

There can be only one AUTO_INCREMENT column per table, it must be indexed, and it cannot have a DEFAULT value. So you can indeed have an AUTO_INCREMENT column in a table that is not the primary key.

How do you set a field as auto increment in MySQL?

In MySQL, the syntax to change the starting value for an AUTO_INCREMENT column using the ALTER TABLE statement is: ALTER TABLE table_name AUTO_INCREMENT = start_value; table_name.


2 Answers

Specify a column list:

By default, when no column list is provided at the end of the LOAD DATA INFILE statement, input lines are expected to contain a field for each table column. If you want to load only some of a table's columns, specify a column list:

LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata (col1,col2,...);

http://dev.mysql.com/doc/refman/5.1/en/load-data.html

like image 58
Aaron Avatar answered Sep 17 '22 00:09

Aaron


LOAD DATA LOCAL INFILE '/var/www/.........../file.csv' INTO TABLE table_name FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\r\\n' (field1,field2,.........,fieldn)

This is in case of ubuntu

like image 35
shiv Avatar answered Sep 18 '22 00:09

shiv