How to import text file to table with primary key as auto-increment
auto-increment, import, phpmyadmin
Solution
Not correct on import with the LOADTABLE INFILE, just create the auto-increment column as the LAST column/field... As its parsing, if your table is defined with 30 columns, but the text file only has 1 (or anything less), it will import the leading columns first, in direct sequence, so ensure your delimited with... is correct between fields (for any future imports). Again, put the auto-increment AFTER the number of columns you know are being imported.
create table YourMySQLTable
( FullName varchar(30) not null ,
SomeOtherFlds varchar(20) not null,
IDKey int not null AUTO_INCREMENT,
Primary KEY (IDKey)
);
Notice the IDKey is auto-increment in the last field of the table... regardless of your INPUT stream text file which may have less columns than your final table will actually hold.
Then, import the data via...
LOAD DATA
INFILE `C:\SomePath\WhereTextFileIs\ActualFile.txt`
INTO TABLE YourMySQLTable
COLUMNS TERMINATED BY `","`
LINES TERMINATED BY `\r\n` ;
Above example is based on comma seperated list with quotes around each field such as "myfield1","anotherField","LastField". Also, the terminated is the cr/lf that typical text files are delimited per row
In the sample of your text file having the full name as the single column, all the data would get loaded into the "YourMySQLTable" into the FullName column. Since the IDKey is at the END of the list, it will still be auto-increment assigned values from 1-? and not have any conflict with the columns from the inbound text.
Problem
I have some bulk data in a text file that I need to import into a MySQL table. The table consists of two fields .. - ID (integer with auto-increment) - Name (varchar) The text file is a large collection of names with one name per line ... (example) ``` John Doe Alex Smith Bob Denver ``` I know how to import a text file via phpMyAdmin however, as far as I understand, I need to import data that has the same number of fields as the target table. Is there a way to import the data from my text file into one field and have the ID field auto-increment automatically? Thank you in advance for any help.