Langsung ke konten utama

Continue after an error “execute failed: Duplicate entry” in MySQL

When an INSERT statement is executed to add a set of records to a table that has a unique key defined, it may happen that the value of the key in some of the records to be inserted is the same as that of records already in the table.
The default behaviour in this case is that the execution fails with an error message “execute failed: Duplicate entry”. When this happens, no record is inserted, not even those whose keys do not clash with the keys of previously existing records.
Example:
Let’s say we are developing an application that needs a “customers” table, with columns “clientid”,”name” and “email”. There will be a unique key defined on the “clientid” field.
We can create the table, and insert the first records successfully:
Next, we want to insert two more records in the table, but the client id of one of them is already used by one of the existing records. The sentence fails with the “duplicate entry” error message.
MySQL implements some extension to the standard SQL, that allow us to treat this event in a flexible way:

INSERT IGNORE, UPDATE IGNORE

Using the IGNORE modifier, those new records that clash with existing records are discarded, while non-conflicting records are inserted in the table:
The IGNORE modifier can also be used in a UPDATE sentence. The effect is again to discard those updates that would cause a unique key conflict.

REPLACE

A REPLACE statement can be used in place of an INSERT statement. Using REPLACE, new records that do not conflict with existing records are inserted in the table as usual. When a record to be inserted causes a duplicate key conflict, the previously existing record is deleted, and the new record is inserted:

INSERT … ON DUPLICATE KEY UPDATE

The INSERT command, with the “ON DUPLICATE KEY UPDATE update-statement”, when a record to be inserted conflicts with an existing records, does not insert the new record, but executes the “update-statement” instead. This can be used to flag the existence of duplicates.
For instance, a field “total” can be added to the table customers, to keep track of the number of duplicates, as follows:
As can be seen, the values of name and email in the record with clientid=2 have not been changed, but the value of the “total” column has been increased by one.

LOAD DATA INFILE ‘filename’ [ IGNORE | REPLACE ]

The LOAD DATA INFILE command can also be used with the modifiers IGNORE and REPLACE, resulting in the same behaviour as above explained for conflicting records

Komentar

Postingan populer dari blog ini

CREATE CROSS TAB QUERY IN MYSQL

MySQL Multi-Aggregated Rows in Crosstab Queries MySQL’s crosstabs contain aggregate functions on two or more fields, presented in a tabular format. In a multi-aggregate crosstab query, two different functions can be applied to the same field or the same function can be applied to multiple fields on the same (row or column) axis. Rob Gravelle shows you how to apply two different functions to the same field in order to create grouping levels in the row axis. Today’s topic of discussion is crosstabs, which contain multiple aggregate functions in the row axis of a tabular resultset. Recall from the the  All About the Crosstab Query  article that an aggregate function is one that summarizes a group of related data in some way. Examples of aggregate functions include COUNT, SUM, AVG, MIN, and MAX. In a multi-aggregate crosstab query, two different functions can be applied to the same field or the same function can be applied to two or more fields. Today we’ll create a query...

Mengatasi "This app can’t run on your PC Windows 10"

  Salah satu pesan error yang sering muncul saat aplikasi tidak bisa dibuka di Windows 10 adalah “ This app can’t run on your PC ,   to find a version for your PC check with the software publisher “. Masalah seperti ini cukup umum dan dialami banyak orang, terutama saat menjalankan aplikasi yang bukan dari Microsoft. Penyebab utama terjadinya masalah ini adalah karena masalah kompatibilitas antara aplikasi dengan versi Windows yang dianggap tidak sesuai oleh sistem. Bisa juga karena aplikasi atau game yang akan jalankan tersebut terkena filter oleh Windows sehingga prosesnya diblokir. Windows 10 memiliki fitur untuk memblokir aplikasi tidak dikenal yang berasal dari  unverified developers , fitur ini secara default akan aktif dengan tujuan untuk mencegah masuknya aplikasi yang mengandung malware dan virus. Penyebab lainnya bisa juga karena file aplikasi yang rusak, file sistem yang korup, atau masalah yang disebabkan oleh malware dan virus. Pada kesempatan kali ini  ...

Linux Basic Command Cheat Sheet

 https://www.guru99.com/linux-commands-cheat-sheet.html Linux Command Cheat Sheet In this Linux/Unix command line cheat sheet, you will learn: Basic Linux commands File Permission commands Environment Variables command User management commands of linux Networking command Process command VI Editing Commands Basic Linux commands Command Description ls Lists all files and directories in the present working directory ls -R Lists files in sub-directories as well ls -a Lists hidden files as well ls -al Lists files and directories with detailed information like permissions,size, owner, etc. cd or cd ~ Navigate to HOME directory cd .. Move one level up cd To change to a particular directory cd / Move to the root directory cat > filename Creates a new file cat filename Displays the file content cat file1 file2 > file3 Joins two files (file1, file2) and stores the output in a new file (file3) mv file "new file path" Moves the files to the new location mv filename new_file_name Re...