Bobcares

Load data infile MySQL auto increment

by | Feb 9, 2023

Wondering how to load data infile mysql auto increment? Our in-house experts are here to help you out with this article. Our MySQL support is here to offer a lending hand with your queries and issues.

How to load data infile mysql auto increment?

The best thing to do is just include the 2 non-auto-increment columns in the CSV, and then explicitly set the ID column to NULL in the load data infile statement.

Something like this:

LOAD DATA INFILE '/tmp/data.csv'
INTO TABLE your_table
FIELDS TERMINATED BY ','
(AField, BField)
SET ID = NULL;

you can “omit” ID field in data row by placing delimiter sign in the beginning of a row, like this (for table schema ID,AField,BField):

,afieldvalue,bfieldvalue
...

SQL engine will assign NULL to ID field while reading such csv file

Try to use NULL as the value in the csv file for the ID field.

NULL in CSV file is read as a STRING in MySQL. If you want to have null values in a MySQL column instead use \N in place of `NULL. This will fix the issue and you will get the required result.

If you have only a small number of columns, you can just name them: LOAD DATA LOCAL INFILE 'myfile.txt' INTO TABLE mytable (AField); (assuming only ID & AField)

LINES TERMINATED BY '\r\n' 

That was the key to success for me when using a text file

Loading AUTO_INCREMENT values or not

When loading information from a CSV file it is possible to load the auto incremented values or allow new values to generate and associated with the data load. In other words we can allow the auto increment default behaviour to automatically generate the values.

mysql> DROP TABLE IF EXISTS names;
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> CREATE TABLE names
    ->   (
    ->      id   INT AUTO_INCREMENT,
    ->      name VARCHAR(100),
    ->      PRIMARY KEY(id)
    ->   )
    -> ENGINE = InnoDB;
Query OK, 0 rows affected (0.03 sec)

Consider the table above. Imagine we need to populate this table with a set of names but without caring about the “id” and the value automatically generated for this field. For the following file we have just names:

$ cat name.txt
Unknown
Simpson
Walker
$

We want to load this into the table but allow the “id” field to auto generate the values. Then we issue the following command:

mysql> LOAD DATA INFILE 'name.txt' INTO TABLE names LINES TERMINATED BY '\n' (name);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 0

mysql> select * from names;
+----+----------+
| id | name     |
+----+----------+
|  1 | Unknown  | 
|  2 | Simpson  | 
|  3 | Walker   |   
+----+----------+
3 rows in set (0.00 sec)

Notice the “(name)” after the line terminated definition. This indicates that the data contained in the file is to associate with this particular field.

So far so good. But what happens if we need to populate values stored in file in addition to the name. For the most part this is still handled the same way as previously. This time there is no field names defined in the LOAD DATA INFILE statement:

The file contains the following data:

$ cat names.txt
1 Unknown
2 Simpson
3 Walker
$

So loading this up we get :

mysql> LOAD DATA INFILE '/home/rcashell/mysql/csv/names.txt' INTO TABLE names LINES TERMINATED BY '\n';
Query OK, 3 rows affected (0.00 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 0

mysql> select * from names;
+----+----------+
| id | name     |
+----+----------+
|  1 | Unknown  | 
|  2 | Simpson  | 
|  3 | Walker   |    
+----+----------+

The next value of the auto increment adjusts to the largest value of the AUTO_INCREMENT field and adds 1.

mysql> insert into names(name) values ('Tom');
Query OK, 1 row affected (0.00 sec)

mysql> select * from names;
+----+----------+
| id | name     |
+----+----------+
|  1 | Unknown  | 
|  2 | Simpson  | 
|  3 | Walker   | 
|  4 | Tom      | 
+----+----------+
4 rows in set (0.00 sec)

So far so good. But what happens if we add an AUTO_INCREMENT value of 0. 0 or NULL is used by MySQL AUTO_INCREMENT fields to auto generate a value. Consider the following:

$ cat name.txt
0 Peter
$

We would expect Peter to add into our table with the “id” of 0.

mysql> select * from names;
+----+----------+
| id | name     |
+----+----------+
|  1 | Unknown  | 
|  2 | Simpson  | 
|  3 | Walker   | 
|  4 | Tom      | 
+----+----------+
4 rows in set (0.00 sec)

mysql> LOAD DATA INFILE 'name.txt' INTO TABLE names;
Query OK, 1 row affected (0.02 sec)
Records: 1  Deleted: 0  Skipped: 0  Warnings: 0

mysql> select * from names;
+----+----------+
| id | name     |
+----+----------+
|  1 | Unknown  | 
|  2 | Simpson  | 
|  3 | Walker   | 
|  4 | tom    | 
|  5 | Peter    | 
+----+----------+
5 rows in set (0.00 sec)

But what if we actually need the value to be added with the “id” equal to 0. In this case we need to inform MySQL of the exception. This is done through their SQL_MODE parameter settings.

Setting the session SQL_MODE as follows will overcome this issue:

mysql> SET SESSION sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
Query OK, 0 rows affected (0.00 sec)

So reloading this same information as before we now get:

mysql> select * from names;
+----+----------+
| id | name     |
+----+----------+
|  1 | Unknown  | 
|  2 | Simpson  | 
|  3 | Walker   | 
|  4 | Tom      | 
|  5 | Peter    | 
+----+----------+
5 rows in set (0.00 sec)

mysql> SET SESSION sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
Query OK, 0 rows affected (0.00 sec)

mysql> LOAD DATA INFILE 'name.txt' INTO TABLE names;
Query OK, 1 row affected (0.00 sec)
Records: 1  Deleted: 0  Skipped: 0  Warnings: 0

mysql> select * from names;
+----+----------+
| id | name     |
+----+----------+
|  0 | Peter    | 
|  1 | Unknown  | 
|  2 | Simpson  | 
|  3 | Walker   |   
|  4 | Tom      | 
|  5 | Peter    | 
+----+----------+
6 rows in set (0.00 sec)

[Looking for a solution to another query? We’re happy to help.]

Conclusion

In this article, we provide a quick and simple solution from our Support team to how to load data infile mysql auto increment

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF