`HashValue` char(32) COLLATE utf8_unicode_ci DEFAULT NULL, `SentCount` int(10) unsigned DEFAULT NULL, `Email` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL, `ID` int(10) unsigned NOT NULL AUTO_INCREMENT, *Table structure for table `email_sent_count` */ Use script shown below to create the table and populate our test data. The table tracks how many times an email address has been contacted. ![]() In this tutorial, we'll use a table called email_sent_count to demonstrate the various features of INSERT.ON DUPLICATE KEY UPDATE. Syntax of ON DUPLICATE KEY UPDATE in INSERT statement: This is much more efficient in terms ofĬoding than checking (SELECT query) whether a record exists, then launch either an UPDATE or an INSERT query depending on the value returned from the Occur (as it would violate unique constraint) but instead MySQL will perform an UPDATE of the existing row. The table must have a UNIQUE key or PRIMARY key constraint defined which MySQL can use to check if duplicate value exists. ON DUPLICATE KEY UPDATE is a useful feature in MySQL that allows you to update an existing row in the table when you do INSERT. To update the record instead of inserting. That the record already exists and it will fail anyway, you can deal with it in the insert statement by issuing ON DUPLICATE KEY UPDATE When inserting a record, if the record already exists in the table (based on the unique or primary key), the insert will fail. Combine update and insert into one statement
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |