Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It shouldn't make a difference, it's the same syntax. Just be sure you have both keys specified as columns. For example:</p> <pre><code>REPLACE INTO `my_table` ( `key1`, `key2`, `othercolumn1` /* , ... */ ) VALUES ( 'widgets', 14, 'Blue widget with purple trim' ); </code></pre> <p><strong>EDIT</strong></p> <p>Here's my test I ran in my test database to make sure I wasn't about to destroy your data. Of course, I encourage you to try it out if you're unsure!</p> <pre><code>CREATE SCHEMA `my_testdb`; USE `my_testdb`; CREATE TABLE `my_table` ( `key1` VARCHAR(20) NOT NULL, `key2` INTEGER NOT NULL, `othercolumn1` VARCHAR(50), CONSTRAINT PRIMARY KEY (`key1`, `key2`) ); REPLACE INTO `my_table` ( `key1`, `key2`, `othercolumn1` ) VALUES ( 'widgets', 14, 'Green widget with fuchsia trim' ); REPLACE INTO `my_table` ( `key1`, `key2`, `othercolumn1` ) VALUES ( 'widgets', 15, 'Yellow widget with orange trim' ); REPLACE INTO `my_table` ( `key1`, `key2`, `othercolumn1` ) VALUES ( 'thingamabobs', 14, 'Red widget with brown trim' ); REPLACE INTO `my_table` ( `key1`, `key2`, `othercolumn1` ) VALUES ( 'widgets', 14, 'Blue widget with purple trim' ); SELECT * FROM `my_table`; </code></pre> <p>This is my result:</p> <pre><code>key1 key2 othercolumn1 widgets 14 Blue widget with purple trim widgets 15 Yellow widget with orange trim thingamabobs 14 Red widget with brown trim </code></pre> <p><strong>ANOTHER EDIT</strong></p> <p>I think I see what you're talking about in the documentation, the confusion over unique columns:</p> <blockquote> <p>It is possible for a single row to replace more than one old row if the table contains multiple unique indexes and the new row duplicates values for different old rows in different unique indexes. —<em>MySQL Documentation</em></p> </blockquote> <p>That's referring to a rather contrived circumstance in which the row you're replacing with conflicts not just with an existing primary key, but with other unique columns as well. Here's another example to illustrate this point:</p> <pre><code>CREATE SCHEMA `my_testdb2`; USE `my_testdb2`; CREATE TABLE `my_table` ( `key1` VARCHAR(20) NOT NULL, `key2` INTEGER NOT NULL, `color` VARCHAR(20) NOT NULL UNIQUE, `othercolumn1` VARCHAR(50), CONSTRAINT PRIMARY KEY (`key1`, `key2`) ); REPLACE INTO `my_table` ( `key1`, `key2`, `color`, `othercolumn1` ) VALUES ( 'widgets', 14, 'green', 'Green widget with fuchsia trim' ); REPLACE INTO `my_table` ( `key1`, `key2`, `color`, `othercolumn1` ) VALUES ( 'widgets', 15, 'yellow', 'Yellow widget with orange trim' ); REPLACE INTO `my_table` ( `key1`, `key2`, `color`, `othercolumn1` ) VALUES ( 'thingamabobs', 14, 'red', 'Red widget with brown trim' ); REPLACE INTO `my_table` ( `key1`, `key2`, `color`, `othercolumn1` ) VALUES ( 'widgets', 14, 'yellow', 'Yellow widget with purple trim' ); SELECT * FROM `my_table`; </code></pre> <p>Notice how the last REPLACE operation not only conflicts with the (<code>key1</code>, <code>key2</code>) primary key, of the first REPLACE, but also with the unique color of the second one. In this case, BOTH rows are deleted before the last REPLACE operation is performed so that the result is no conflict. You'll end up with just two rows:</p> <pre><code>key1 key2 color othercolumn1 widgets 14 yellow Yellow widget with purple trim thingamabobs 14 red Red widget with brown trim </code></pre> <p>Both the row with (<code>key1</code>, <code>key2</code>) equal to ('widgets', 14) <strong>AND</strong> the row with the color 'yellow' were blown away due to the new row conflicting with multiple unique constraints on the table.</p> <p>Hope this helps!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload