Is merge statement available in MySQL - mysql

I have to use INSERT and UPDATE in single query. For that SQL having MERGE statement.
Is MERGE statement supported in MySQL. If supported, please provide sample.

MERGE is not supported by MySQL, However, there is other possible way of doing the same:
INSERT...ON DUPLICATE KEY UPDATE
If you specify the ON DUPLICATE KEY UPDATE option in the INSERT
statement and the new row causes a duplicate value in the UNIQUE or
PRIMARY KEY index, MySQL performs an update to the old row based on
the new values.

Related

A simple MERGE MySQL query. what is the error in line 1? need assistance [duplicate]

I have to use INSERT and UPDATE in single query. For that SQL having MERGE statement.
Is MERGE statement supported in MySQL. If supported, please provide sample.
MERGE is not supported by MySQL, However, there is other possible way of doing the same:
INSERT...ON DUPLICATE KEY UPDATE
If you specify the ON DUPLICATE KEY UPDATE option in the INSERT
statement and the new row causes a duplicate value in the UNIQUE or
PRIMARY KEY index, MySQL performs an update to the old row based on
the new values.

What is the difference between “INSERT IGNORE” and “INSERT … ON DUPLICATE KEY UPDATE” in MySQL? [duplicate]

This question already has answers here:
"INSERT IGNORE" vs "INSERT ... ON DUPLICATE KEY UPDATE"
(12 answers)
Closed last month.
What is the difference between INSERT IGNORE and INSERT…ON DUPLICATE KEY UPDATE. I would also like to know which one is preferred over the other. Can someone please help?
If you use INSERT IGNORE, then the row won't actually be inserted if it results in a duplicate key. But the statement won't generate an error. It generates a warning instead. These cases include:
•Inserting a duplicate key in columns with PRIMARY KEY or UNIQUE constraints.
•Inserting a NULL into a column with a NOT NULL constraint.
•Inserting a row to a partitioned table, but the values you insert don't map to a partition.
If you use REPLACE, MySQL actually does a DELETE followed by an INSERT internally, which has some unexpected side effects:
•A new auto-increment ID is allocated.
•Dependent rows with foreign keys may be deleted (if you use cascading foreign keys) or else prevent the REPLACE.
•Triggers that fire on DELETE are executed unnecessarily.
•Side effects are propagated to replication slaves too.
Both REPLACE and INSERT...ON DUPLICATE KEY UPDATE are non-standard, proprietary inventions specific to MySQL. ANSI SQL 2003 defines a MERGE statement that can solve the same need (and more), but MySQL does not support the MERGE statement.
If you try to avoid duplication violation, then the difference is: INSERT IGNORE does not allow to specify an action (to alter something in a row) if unique constraint violation occures whereas for INSERT...ON DUPLICATE KEY UPDATE some action specifying is compulsory. Of course, you can specify fake update in INSERT...ON DUPLICATE KEY UPDATE (for example, SET id=id, where id is primary/unique key) - in this case there is no difference.
INSERT IGNORE also may ignore some another ignorable errors, rather than INSERT...ON DUPLICATE KEY UPDATE.

"Insert if not exists" in both MySQL and HSQLDB

Is there a SQL statement (or atomic sequence of statements) supported by both MySQL and HSQLDB to insert values if they aren't already there?
I'm working on an app that uses MySQL as its production database and HSQLDB for unit tests; I'd like to have a single "initial data import when the tables are empty" script.
MySQL supports INSERT IGNORE, REPLACE INTO and INSERT INTO ... ON DUPLICATE KEY UPDATE ..., but HSQLDB doesn't; conversely, HSQLDB supports MERGE but MySQL doesn't.
HSQLDb from version 2.3.4 adds support for insert ignore.
http://hsqldb.org/
Version 2.3.4 added the UUID type for columns, SYNONYM for tables and
functions, PERIOD predicates, and auto-updated TIMESTAMP columns on
row updates. Other new features included the ability to cancel
long-running statements from JDBC as well as from admin sessions, and
UTF-16 file support for text table sources, in addition to 8-bit text
files. MySQL compatibility for REPLACE, INSERT IGNORE and ON
DUPLICATE KEY UPDATE statements.
And
http://hsqldb.org/doc/guide/guide.pdf (page 260).
HyperSQL supports and translates INSERT IGNORE, REPLACE and ON
DUPLICATE KEY UPDATE variations of INSERT into predictable and
error-free operations. When INSERT IGNORE is used, if any of the
inserted rows would violate a PRIMARY KEY or UNIQUE constraint, that
row is not inserted. With multi-row inserts, the rest of the rows are
then inserted only if there is no other violation such as long strings
or type mismatch, otherwise the appropriate error is returned. When
REPLACE or ON DUPLICATE KEY UPDATE is used, the rows that need
replacing or updating are updated with the given values. This works
exactly like an UPDATE statement for those rows. Referential
constraints and other integrity checks are enforced and update
triggers are activated. The row count returned is simply the total
number of rows inserted and updated.
If someone still has this problem you can enable syntax support for MySQl by adding the following to your script
SET DATABASE SQL SYNTAX MYS TRUE

How to get the PK of a ignored row in an INSERT IGNORE statement

I have an INSERT statement with an IGNORE option, because I have a unique field in the insert statement, that is not the primary key. I am using the getGeneratedKeys() command on a PreparedStatement object to get the keys of newly generated rows. Is it possible to configure JDBC in a way so that it returns the id of the ignored row in the case of a query where the IGNORE triggers?
The answer mysql - after insert ignore get primary key seems to indicate that a multi-step workaround is required to retrieve the details of the ignored rows.
With the optional alternative of using REPLACE INTO if you can afford the additional overhead of replacing the rows rather than ignoring them.

Is it possible to declare to mysql queries?

I'm trying to create a code for a single button where it will perform either of two actions where it will add to the database if the user currently don't have the record while it will update the user's record if the user has records already. I've done it like this:
if() {
mysql_query("INSERT INTO table...");
}
else {
mysql_query("UPDATE table SET...");
}
Is it possible?
Yes, what you've written will work. If you have a way to know if there already exists a row or not without making an additional query just for this bit of code, then do exactly as you wrote.
If, however, you planned to first SELECT from the table to see if a row exists, then conditionally INSERT or UPDATE, you will perform more queries than necessary.
It would be better to either:
Have a PRIMARY KEY or other constraint on the table prevent duplicate INSERTs. Then issue an INSERT ... ON DUPLICATE KEY UPDATE query. This will attempt to INSERT the row, and if it is a duplicate, automatically perform the specified UPDATE to that row instead.
Issue the UPDATE query and check mysql_affected_rows to see if it updated an existing row. If not, then issue the INSERT query to create the new row.
Which one is more appropriate depends on your application.
you can use INSERT ... ON DUPLICATE KEY UPDATE Syntax like:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
If you have properly set unique keys, you should use REPLACE so you could remove the if.
REPLACE INTO table VALUE (...);
Pay attention that this is a MySQL extension, thus not portable to other DBs.
Yes, you could try the insert then if it fails try the update.
But you could use the MYSQL sql "REPLACE" keyword, which will insert a new record if it doesn't exist or delete the existing record and insert your new one if it does.
You could also use the INSERT ... ON DUPLICATE KEY UPDATE syntax
(explained here - Link to MYSQL ref which seems to be the closest fit to your requirement.
yes it is possible
first write a query for check that record is already exist or not.
Yes it is possible , it will work