MYSQL On Duplicate Key Do Nothing OR Insert If Not Exists
If you are looking for a MYSQL statement that essentially says 'on duplicate key do nothing' or 'insert if not exists', try:
INSERT IGNORE INTO ....
(from the manual)
If you use the
IGNOREkeyword, errors that occur while executing theINSERTstatement are treated as warnings instead. For example, withoutIGNORE, a row that duplicates an existingUNIQUEindex orPRIMARY KEYvalue in the table causes a duplicate-key error and the statement is aborted. WithIGNORE, the row still is not inserted, but no error is issued. Data conversions that would trigger errors abort the statement ifIGNOREis not specified. WithIGNORE, invalid values are adjusted to the closest values and inserted; warnings are produced but the statement does not abort. You can determine with themysql_info()C API function how many rows were actually inserted into the table.
You may also find http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html good reading on the subject.
Recent blog posts
- Too much to fit into a twitter update!
- Function Before Form
- Feedburner - The Newsletter Alternative?
- Paused Adsense, but the traffic keeps coming.
- Amazon S3 Is Nice
- listen to RSS feeds with Odiogo.com
- US Army warns of Twitter Terrorist Threat
- $350 generates $12,000+
- MYSQL Versus File System For Storage
- I'm a Drupal Contributor!


Post new comment