Mysql insert игнорировать ошибки

Summary: in this tutorial, you will learn how to use the MySQL INSERT IGNORE statement to insert data into a table.

Introduction to MySQL INSERT IGNORE statement

When you use the INSERT statement to add multiple rows to a table and if an error occurs during the processing, MySQL terminates the statement and returns an error. As the result, no rows are inserted into the table.

However, if you use the INSERT IGNORE statement, the rows with invalid data that cause the error are ignored and the rows with valid data are inserted into the table.

The syntax of the INSERT IGNORE statement is as follows:

INSERT IGNORE INTO table(column_list)
VALUES( value_list),
      ( value_list),
      ...
Code language: SQL (Structured Query Language) (sql)

Note that the IGNORE clause is an extension of MySQL to the SQL standard.

MySQL INSERT IGNORE example

We will create a new table called subscribers for the demonstration.

CREATE TABLE subscribers (
    id INT PRIMARY KEY AUTO_INCREMENT,
    email VARCHAR(50) NOT NULL UNIQUE
);
Code language: SQL (Structured Query Language) (sql)

The UNIQUE constraint ensures that no duplicate email exists in the email column.

The following statement inserts a new row into the  subscribers table:

INSERT INTO subscribers(email)
VALUES('[email protected]');
Code language: SQL (Structured Query Language) (sql)

It worked as expected.

Let’s execute another statement that inserts two rows into the  subscribers table:

INSERT INTO subscribers(email)
VALUES('[email protected]'), 
      ('[email protected]');
Code language: SQL (Structured Query Language) (sql)

It returns an error.

Error Code: 1062. Duplicate entry '[email protected]' for key 'email'
Code language: SQL (Structured Query Language) (sql)

As indicated in the error message, the email [email protected] violates the UNIQUE constraint.

However, if you use the INSERT IGNORE statement instead.

INSERT IGNORE INTO subscribers(email)
VALUES('[email protected]'), 
      ('[email protected]');
Code language: SQL (Structured Query Language) (sql)

MySQL returned a message indicating that one row was inserted and the other row was ignored.

1 row(s) affected, 1 warning(s): 1062 Duplicate entry '[email protected]' for key 'email' Records: 2  Duplicates: 1  Warnings: 1
Code language: SQL (Structured Query Language) (sql)

To find the detail of the warning, you can use the SHOW WARNINGS command as shown below:

SHOW WARNINGS;
Code language: SQL (Structured Query Language) (sql)

MySQL INSERT IGNORE - warning

In conclusion, when you use the INSERT IGNORE statement, instead of issuing an error, MySQL issued a warning in case an error occurs.

If you query data from subscribers table, you will find that only one row was actually inserted and the row that causes the error was not.

MySQL INSERT IGNORE - subscribers table

MySQL INSERT IGNORE and STRICT mode

When the strict mode is on, MySQL returns an error and aborts the INSERT statement if you try to insert invalid values into a table.

However, if you use the INSERT IGNORE statement, MySQL will issue a warning instead of an error. In addition, it will try to adjust the values to make them valid before adding the value to the table.

Consider the following example.

First, we create a new table named tokens:

CREATE TABLE tokens (
    s VARCHAR(6)
);
Code language: SQL (Structured Query Language) (sql)

In this table, the column s accepts only string whose lengths are less than or equal to six.

Second, insert a string whose length is seven into the tokens table.

INSERT INTO tokens VALUES('abcdefg');
Code language: SQL (Structured Query Language) (sql)

MySQL issued the following error because the strict mode is on.

Error Code: 1406. Data too long for column 's' at row 1
Code language: SQL (Structured Query Language) (sql)

Third, use the INSERT IGNORE statement to insert the same string.

INSERT IGNORE INTO tokens VALUES('abcdefg');
Code language: SQL (Structured Query Language) (sql)

MySQL truncated data before inserting it into the tokens table. In addition, it issues a warning.

MySQL INSERT IGNORE - strict mode

In this tutorial, you have learned how to use the MySQL INSERT IGNORE statement to insert rows into a table and ignore error for rows that cause errors.

Was this tutorial helpful?

You can use INSERT… IGNORE syntax if you want to take no action when there’s a duplicate record.

You can use REPLACE INTO syntax if you want to overwrite an old record with a new one with the same key.

Or, you can use INSERT… ON DUPLICATE KEY UPDATE syntax if you want to perform an update to the record instead when you encounter a duplicate.

Edit: Thought I’d add some examples.

Examples

Say you have a table named tbl with two columns, id and value. There is one entry, id=1 and value=1. If you run the following statements:

REPLACE INTO tbl VALUES(1,50);

You still have one record, with id=1 value=50. Note that the whole record was DELETED first however, and then re-inserted. Then:

INSERT IGNORE INTO tbl VALUES (1,10);

The operation executes successfully, but nothing is inserted. You still have id=1 and value=50. Finally:

INSERT INTO tbl VALUES (1,200) ON DUPLICATE KEY UPDATE value=200;

You now have a single record with id=1 and value=200.

Сегодня возникла проблема дублирования строк в базе MySQL, потратил много времени на поиски и нашел такую итересную статью. Надеюсь она вам поможет, как и мне. Источник статьи указал в конце страницы.

Задача

Вы создали таблицу с уникальным индексом для предотвращения появления дубликатов значений в индексированном столбце или столбцах. Но теперь при попытке вставки записи с повторяющимся значением генерируется ошибка, а вы хотели бы избежать обработки таких ошибок.

Создать индекс на несколько столбцов можно так:

ALTER TABLE `db_name`.`table_name` ADD UNIQUE `name_index` (`app`, `email`, `other_field`);

Где db_name — имя вашей базы данных, table_name — имя таблицы в базе, name_index — название индекса, `app`, `email`, `other_field` — группируемые поля в уникальный индекс.

Решение

Один из способов – просто игнорировать ошибку, другой – использовать предложение INSERT IGNORE или REPLACE, каждое из которых изменяет поведение MySQL в отношении обработки повторений. 

Обсуждение

По умолчанию MySQL генерирует ошибку при вставке записи, дублирующей существующий уникальный ключ. Например, если таблица person содержит уникальный индекс для столбцов last_name и first_name, то вы увидите следующее:

mysql> INSERT INTO person (last_name, first_name)
-> VALUES('X1','Y1');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO person (last_name, first_name)
-> VALUES('X1','Y1');
ERROR 1062 at line 1: Duplicate entry 'X1-Y1' for key 1

Если вы интерактивно запускаете предложения из программы mysql, то можете просто сказать: «Понял, не сработало», игнорировать ошибку и продолжать работу.

Но если вы пишете программу, вставляющую записи, то ошибка может привести к завершению ее работы. Один из способов избежать этого – изменить поведение программы за счет отлавливания ошибки и ее игнорирования.

Если вы хотите предотвратить появление ошибки, то, вероятно, подумываете о решении задачи обработки дубликатов с помощью двух запросов: запустите SELECT, чтобы определить, есть ли уже такая запись, а затем – INSERT, если записи еще нет. Но на самом деле ничего не получится. Другое клиентское приложение может вставить такую же запись в промежуток между вашими SELECT и INSERT, и тогда опять-таки сгенерируется ошибка. Чтобы это не произошло, можно заключить два предложения в транзакцию или заблокировать таблицы, но тогда вместо двух предложений у вас появится четыре. MySQL предлагает два решения задачи обработки дубликатов, каждое из которых состоит из единственного предложения:

Первый cпособ: Используйте предложение INSERT IGNORE вместо INSERT.

Если запись не дублирует существующую, то MySQL вставляет ее как обычно. Если же запись – это дубликат, то ключевое слово IGNORE указывает MySQL, что следует молча отбросить ее, не генерируя ошибку:

mysql> INSERT IGNORE INTO person (last_name, first_name)
-> VALUES('X2','Y2');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT IGNORE INTO person (last_name, first_name)
-> VALUES('X2','Y2');
Query OK, 0 rows affected (0.00 sec)

Значение счетчика строк показывает, была запись вставлена или проигнорирована.

В программе вы можете получить это значение, используя функцию подсчета обработанных строк, имеющуюся в вашем API.

Второй способ: Используйте предложение REPLACE вместо INSERT.

Если запись новая, она вставляется так, как если бы выполнялось предложение INSERT. Если же это дубликат, то новая запись замещает старую:

mysql> REPLACE INTO person (last_name, first_name)
-> VALUES('X3','Y3');
Query OK, 1 row affected (0.00 sec)
mysql> REPLACE INTO person (last_name, first_name)
-> VALUES('X3','Y3');
Query OK, 2 rows affected (0.00 sec)

Значение количества обработанных строк во втором случае равно 2, так как исходная запись удалена, а на ее место вставлена новая запись.

Выбор INSERT IGNORE или REPLACE зависит от того, какое поведение для вас предпочтительно. INSERT IGNORE хранит первую из множества повторяющихся записей и удаляет остальные. REPLACE хранит последний из дубликатов и удаляет все остальные. Предложение INSERT IGNORE эффективнее, чем REPLACE, так как дубликаты не вставляются в таблицу. То есть его лучше применять, когда вы просто хотите убедиться в том, что копия указанной записи содержится в таблице.

С другой стороны, REPLACE больше подходит для таблиц, в которых может потребоваться обновление других столбцов, не входящих в ключ. Предположим, что у вас есть таблица users, используемая в веб-приложении для хранения адресов электронной почты и паролей, в которой email является ключом:

CREATE TABLE users
(
email CHAR(60) NOT NULL,
password CHAR(20) BINARY NOT NULL,
PRIMARY KEY (email)
);

Как создавать записи для новых пользователей и изменять пароли для существующих? Без REPLACE, создание нового пользователя и изменение пароля существующего, обрабатывались бы по-разному. Стандартный алгоритм мог бы быть таким:

  1. Запустить SELECT, чтобы проверить, существует ли уже запись с указанным значением email.
  2. Если такой записи нет, добавить новую при помощи INSERT.
  3. Если запись существует, обновить ее при помощи UPDATE.

Все это можно выполнить внутри транзакции или заблокировав таблицы, чтобы запретить другим пользователям изменять таблицы в течение того времени, пока вы с ними работаете. Применив REPLACE, вы можете свести оба случая к одному предложению:

REPLACE INTO users (email,password) VALUES(адрес,пароль);

Если запись с указанным адресом электронной почты не существует, то MySQL создает новую. Если запись существует, MySQL заменяет ее. В результате обновляется столбец password записи, содержащей данный адрес.

Источник: 
https://oooportal.ru/?cat=arti…

« Все статьи

Updated May 29, 2023

MySQL-INSERT-IGNORE

Introduction to MySQL INSERT IGNORE

When we want to insert the data into the MySQL database tables, we use the INSERT statement for performing this operation. Mysql provides the facility to add IGNORE keyword in the syntax of the INSERT statement. You can call this function with any number of parameters, and it will return the desired value. This is because the INSERT IGNORE statement handles the error that arrives while the addition of the duplicate record and prevents the inconsistency and redundancy of the records in the tables of Mysql.

In this article, we will learn about the general syntax of the INSERT IGNORE statement, it works, how the strict mode affects the working of the INSERT IGNORE statement, and learn the usage with the help of a few examples to make the concept clearer.

How does INSERT IGNORE work in MySQL?

When we try to insert multiple records in a particular Mysql database table using the INSERT statement, the error occurs for some reason. Mysql will terminate the execution of the query and give the error without inserting any rows in the table that we tried to insert. But when we use INSERT IGNORE instead of just inserting a statement, then Mysql will give a warning and insert all the correct records, leaving and excluding the rows that caused the error.

Syntax

The syntax of the INSERT IGNORE statement is as follows –

INSERT IGNORE INTO table(list_of_columns)
VALUES(record1),
(record2),

Where list_of_columns are the comma-separated names of the column that you wish to insert in the record and the record1,record2,.. are the values of the columns that you have mentioned in the list_of_columns in the same order as they have been mentioned in the list.

Examples to Implement MySQL INSERT IGNORE

Let’s create a table named “developers” that contains one auto-incremented primary key column called “id”, one column named “name” with a unique constraint, and other necessary fields using the following query statement:

Code:

CREATE TABLE 'developers' (
'developer_id' int(11) AUTO_INCREMENT PRIMARY KEY,
'team_id' int(11) NOT NULL,
'name' varchar(100) DEFAULT NULL UNIQUE,
'position' varchar(100) DEFAULT NULL,
'technology' varchar(100) DEFAULT NULL,
'salary' int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Output:

related fields

Now, we will try to insert multiple records with a single record value repeated for the name column on which we have defined the unique constraint to avoid inserting records with the same name. As inserting such records is against the rules and breaking the constraint, MySQL will issue an error saying the record with a duplicate name column value cannot be inserted. In this case, our insert query accurately specified all other columns according to the constraints and did not insert any restricted values. We can try executing the following query statement –

Code:

INSERT INTO 'developers' ('team_id', 'name', 'position', 'technology', 'salary') VALUES
( 1, 'Payal', 'Developer', 'Angular', 30000),
( 1, 'Heena', 'Developer', 'Angular', 10000),
( 3, 'Vishnu', 'Manager', 'Maven', 25000),
( 3, 'Rahul', 'Support', 'Digital Marketing', 15000),
( 3, 'Siddhesh', 'Tester', 'Maven', 20000),
( 7, 'Siddharth', 'Manager', 'Java', 25000),
( 4, 'Brahma', 'Developer', 'Digital Marketing', 30000),
( 1, 'Arjun', 'Tester', 'Angular', 19000),
( 2, 'Nitin', 'Developer', 'MySQL', 20000),
( 2, 'Ramesh', 'Administrator', 'MySQL', 30000),
( 2, 'Rohan', 'Admin', NULL, 20000),
( 2, 'Raj', 'Designer', NULL, 30000),
( 1, 'Raj', 'Manager', NULL, 56000);

Output:

query statement

Let us retrieve the records of the developer’s table by using the following query statement

Code:

SELECT * FROM developers;

Output:

MySQL INSERT IGNORE - 3

We can see that none of the records got inserted even though other than the record with the Raj name was correct. Let us now try inserting the records using the INSERT IGNORE statement instead of the INSERT statement. Our query statement will be as follows –

Code:

INSERT IGNORE INTO 'developers' ('team_id', 'name', 'position', 'technology', 'salary') VALUES
( 1, 'Payal', 'Developer', 'Angular', 30000),
( 1, 'Heena', 'Developer', 'Angular', 10000),
( 3, 'Vishnu', 'Manager', 'Maven', 25000),
( 3, 'Rahul', 'Support', 'Digital Marketing', 15000),
( 3, 'Siddhesh', 'Tester', 'Maven', 20000),
( 7, 'Siddharth', 'Manager', 'Java', 25000),
( 4, 'Brahma', 'Developer', 'Digital Marketing', 30000),
( 1, 'Arjun', 'Tester', 'Angular', 19000),
( 2, 'Nitin', 'Developer', 'MySQL', 20000),
( 2, 'Ramesh', 'Administrator', 'MySQL', 30000),
( 2, 'Rohan', 'Admin', NULL, 20000),
( 2, 'Raj', 'Designer', NULL, 30000),
( 1, 'Raj', 'Manager', NULL, 56000);

Output:

MySQL INSERT IGNORE - 4

We can see that 12 rows were affected, and the query got executed with one warning. To see the warning, we can execute the following command –

Code:

SHOW WARNINGS;

Output:

duplicate

The warning shows that one record with the name Raj had a duplicate value for the name column having a unique constraint on it. Let us retrieve the records of the developer’s table and check which records got inserted using the following query –

Code:

SELECT * FROM developers;

Output:

12 rows

We can see that 12 rows got inserted when we tried to insert 13 rows, but as Raj’s record was duplicated, all the remaining rows got inserted successfully. Also, note that the autoincremented column of developer id has got values inserted beginning from 14,15 and so on because the previous 13 records that we tried to insert using just INSERT query caused an error, and then all the records that were being inserted got rollbacked leaving the sequence value associated with auto-incremented column updated to 14.

Value Exceeding the Range of the Column

When we try to insert the value that exceeds the limit of the defined column size, the INSERT statement results in an error. However, using INSERT IGNORE inserts the truncated value and warns that the range exceeded and hence value has been truncated for insertion. Consider the following example:

The length of salary columns is 11, and we try inserting 12-digit numbers using the INSERT statement gives the following output –

Code:

INSERT INTO 'developers' ('team_id', 'name', 'position', 'technology', 'salary') VALUES
( 1, 'Pihu', 'Developer', 'Angular', 300000000000);

Output:

MySQL INSERT IGNORE - 7

Let’s try using INSERT IGNORE statement –

Code:

INSERT IGNORE INTO 'developers' ('team_id', 'name', 'position', 'technology', 'salary') VALUES
( 1, 'Pihu', 'Developer', 'Angular', 300000000000);

Output:

MySQL INSERT IGNORE - 8

with warning –

Code:

SHOW WARNINGS;

Output:

MySQL INSERT IGNORE - 9

Let us retrieve and check the inserted value –

Code:

SELECT * FROM developers WHERE name='pihu';

Output:

MySQL INSERT IGNORE - 10

Conclusion

Using the INSERT IGNORE statement instead of just inserting statements is always a good practice. Mysql tries to adjust the values to arrange them in the correct format and inserts the correct records, excluding the ones that can cause an error.

Recommended Articles

We hope that this EDUCBA information on “MySQL INSERT IGNORE” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. MySQL count()
  2. SELECT in MySQL
  3. ORDER BY in MySQL
  4. MySQL Database Repair

MySQL INSERT IGNORE clause is used to insert rows in a table while ignoring any duplicate key error, the rows with the same PRIMARY KEY or UNIQUE KEY are skipped in the process of insertion.

Suppose we have a table containing data related to a firm.
The data consists of the following columns:

  1. EmpID
  2. Name
  3. Department
  4. Salary

When we try to insert details of some employees in bulk, there’s a chance of encountering records with the same primary key. In such cases, MySQL will throw an error and the whole insertion process will be halted.

To solve this issue, MySQL INSERT IGNORE clause is used.

The MySQL INSERT IGNORE clause ignores the entry of records in the following conditions:

  • Insertion of a row that has duplicate PRIMARY KEY or UNIQUE KEY
  • Insertion of NULL values when the column is defined with a NOT NULL constraint.
  • If we attempt to insert a record into a partitioned table with values that do not conform to the format specified for the available partitions.

When one of these three conditions occurs, the MySQL INSERT IGNORE generates a warning instead of throwing an error which prevents halting of the insertion process.

Note: The error encountered when a duplicated row is found in the INSERT IGNORE clause is error 1062.

Syntax

The following is the SYNTAX of the MySQL INSERT IGNORE clause:


Note that, column_1, column_2 and column_3 are the columns of the table where we want to insert the data.
Similarly, value_1, value_2 and value_3 represent the values of the specific fields, following the same order as the columns provided in the query.

Examples:

We can illustrate how to use the INSERT IGNORE clause with the following example:

First, let’s create a database named «db» with the following query:


To use this database, we’ll have to run the following query:


Now that we have our database «db«, we can create the «Employee» table in which we’ll insert the employee records:


Now, let’s add the following rows to the table.


Note:

  • The PRIMARY KEY (EmpID) of the first and the second entries are the same.
  • The Department of the fourth record is missing.

If we had used the INSERT clause for these four rows, it would probably throw an error.
However, when we use the INSERT IGNORE clause, it only generates a warning and the process of insertion is not halted.

This is the output of the query without the INSERT IGNORE clause:

Output of the query without the INSERT IGNORE clause

This is the output of the query with the INSERT IGNORE clause:
Output of the query with the INSERT IGNORE clause

This is how the table looks like after the insertion with the INSERT IGNORE clause:

Emp_ID Name Department Salary
111 John Doe IT 100000
113 Emily Blake HR 80000
114 Paul Wood 70000

Let’s discuss some of the advantages and disadvantages of the MySQL INSERT IGNORE clause.

Advantages:

  • Simplicity: The MySQL INSERT IGNORE clause is a simple way to deal with data redundancy. It is a straightforward way to insert data without checking for the duplicated values explicitly.
  • Query Optimization: Queries written by using the INSERT IGNORE clause are very efficient in terms of speed and performance because it ignores all the duplicate rows without any further checking. This is a great way of dealing with a huge chunk of data.
  • Error Handling: The MySQL INSERT IGNORE clause is very helpful in terms of handling errors, especially when dealing with large-scale data.

Disadvantages:

Apart from various advantages of the MySQL INSERT IGNORE clause, they are not preferred by database administrators because of the following reasons:

  • Data Integrity: Data Integrity is considered one of the most important features of a database management system. However, when using the INSERT IGNORE clause, we may have to make some compromises with it.
  • Lack of Control: When using the MySQL INSERT IGNORE clause, the database administrator only has limited control over the insertion. The database engine skips all the redundant rows without providing any information. This can cause some problems during troubleshooting.
  • Truncated Data: The MySQL INSERT IGNORE clause forces the data to fit in the given constraints. This results in the data being truncated and inserted into the table which can result in the insertion of a wrong or incomplete entry into the table.

MySQL INSERT IGNORE and STRICT Mode

STRICT MODE in MySQL prevents any illegal entry of records in the table. When the strict mode is ON and we try inserting rows that have some illegal value concerning the structure of the table then it will throw an error

However, when the INSERT IGNORE clause is used, MySQL ignores these illegal entries and generates a warning instead of an error. MySQL also tries to truncate the data according to the structure and the constraints of the table in which the records are being inserted.

Conclusion

  • MySQL INSERT IGNORE clause allows the administrator to insert the rows that have duplicate PRIMARY KEY or UNIQUE KEY.
  • Whenever such records are found, MySQL throws a warning instead of an error, which is very helpful in the case of adding bulk entries because it prevents the halting of the insertion process.
  • The INSERT IGNORE function also tries to modify the data in illegal entries by truncating the extra part in it.

Понравилась статья? Поделить с друзьями:
  • Mysql innodb проверка базы на ошибки
  • Mysql fetch assoc ошибка
  • Mysql 500 ошибка
  • Mysql 42000 ошибка
  • Mysql 134 ошибка