简体   繁体   中英

SQL Query Not Adding New Entries With INSERT IGNORE INTO

So I have a script that gets data about 100 items at a time and inserts them into a MySQL database with a command like this:

INSERT IGNORE INTO beer(name, type, alcohol_by_volume, description, image_url) VALUES('Bourbon Barrel Porter', 2, '9.1', '', '')

I ran the script once, and it populated the DB with 100 entries. However, I ran the script again with the same SQL syntax, gathering all new data (ie, no duplicates), but the database is not reflecting any new entries -- it is the same 100 entries I inserted on the first iteration of the script.

I logged the queries, and I can confirm that the queries were making requests with the new data, so it's not a problem in the script not gathering new data.

The name field is a unique field, but no other fields are. Am I missing something?

If you use the IGNORE keyword, errors that occur while executing the INSERT statement are treated as warnings instead. For example, without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a duplicate-key error and the statement is aborted. With IGNORE , the row still is not inserted, but no error is issued.

If there is no primary key, there can't be duplicate key to ignore. you should always set a primary key, so please do that - and if you want to have additional colums that shouldn't be duplicate, set them as " unique ".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM