8.3.x database.inc db_merge($table, array $options = [])
8.0.x database.inc db_merge($table, array $options = array())
8.1.x database.inc db_merge($table, array $options = array())
8.2.x database.inc db_merge($table, array $options = array())
8.4.x database.inc db_merge($table, array $options = [])
7.x database.inc db_merge($table, array $options = array())

Returns a new MergeQuery object for the active database.


$table: The table into which to merge.

$options: An array of options to control how the query operates.

Return value

MergeQuery A new MergeQuery object for this connection.

Related topics

44 calls to db_merge()
actions_save in includes/actions.inc
Saves an action and its user-supplied parameter values to the database.
aggregator_refresh in modules/aggregator/aggregator.module
Checks a news feed for new items.
aggregator_save_category in modules/aggregator/aggregator.module
Adds/edits/deletes aggregator categories.
aggregator_save_item in modules/aggregator/aggregator.processor.inc
Adds/edits/deletes an aggregator item.
BlockInvalidRegionTestCase::testBlockInInvalidRegion in modules/block/block.test
Tests that blocks assigned to invalid regions work correctly.

... See full list


includes/database/database.inc, line 2500
Core systems for the database layer.


function db_merge($table, array $options = array()) {
  if (empty($options['target']) || $options['target'] == 'slave') {
    $options['target'] = 'default';
  return Database::getConnection($options['target'])->merge($table, $options);


mlncn’s picture

hgneng’s picture

I have spent some hours to figure out a db_merge command fails because of I use "group" as column name of the table. In this situation in MySQL, the column name needs to be quote by `group` but this seems not done in Drupal.

I got similar issues when I named a column "desc".

Hope this post saves someone's time.

danillonunes’s picture

Got the same trouble with "repeat". Here is a list of MySQL reserved words.

amagdy’s picture

I found no examples here, though i though of adding mine

notice that the key function should take the table primary key or any unique key of your choosing, in this case (uid, mid) are my table primary keys

  	->key(array('uid' => $user->uid, 'mid' => $mid))
  	      'uid' => $user->uid,
  	      'mid' => $mid,
  	      'status' => 0,
  	      'start_timestamp' => time()
grasmash’s picture

You may have some trouble using db_merge() if you're passing decimal values into keys() if your data is stored as FLOAT. From MYSQL manual:

"Using FLOAT might give you some unexpected problems because all calculations in MySQL are done with double precision."

If you do run into such an integrity contraint violation, try switching storage to type DOUBLE.


j0rd’s picture

db_merge('thetable')->key(array('name' => "Sao-Paulo"))->fields($fields)->execute();
db_merge('thetable')->key(array('name' => "São-Paulo"))->fields($fields)->execute();

This will fail, as MySQL considers them the same keys.


Chris Charlton’s picture