Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6141127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:12:26+00:00 2026-05-23T18:12:26+00:00

so MYSQL’s REPLACE command (not to be confused with the string replace function) replaces

  • 0

so MYSQL’s REPLACE command (not to be confused with the string replace function) replaces a row if there exist a column with the same primary key with the inserted data…

but what if I have two primary keys and I want to use both to specify the row to replace not just one of them….how do I specify mysql to use both keys rather than just one

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:12:27+00:00Added an answer on May 23, 2026 at 6:12 pm

    It shouldn’t make a difference, it’s the same syntax. Just be sure you have both keys specified as columns. For example:

    REPLACE INTO `my_table` ( `key1`, `key2`, `othercolumn1` /* , ... */ )
      VALUES ( 'widgets', 14, 'Blue widget with purple trim' );
    

    EDIT

    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!

    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`;
    

    This is my result:

    key1          key2  othercolumn1
    widgets       14    Blue widget with purple trim
    widgets       15    Yellow widget with orange trim
    thingamabobs  14    Red widget with brown trim
    

    ANOTHER EDIT

    I think I see what you’re talking about in the documentation, the confusion over unique columns:

    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. —MySQL Documentation

    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:

    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`;
    

    Notice how the last REPLACE operation not only conflicts with the (key1, key2) 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:

    key1          key2  color   othercolumn1
    widgets       14    yellow  Yellow widget with purple trim
    thingamabobs  14    red     Red widget with brown trim
    

    Both the row with (key1, key2) equal to (‘widgets’, 14) AND the row with the color ‘yellow’ were blown away due to the new row conflicting with multiple unique constraints on the table.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

MySQL has this incredibly useful yet proprietary REPLACE INTO SQL Command. Can this easily
_mysql_exceptions.Warning: Incorrect string value: '\xE7\xB9\x81\xE9\xAB\x94...' for column 'html' at row 1 def getSource(theurl, moved
MySQL provides a string function named FIELD() which accepts a variable number of arguments.
MySQL runs pretty much all string comparisons under the default collation... except the REPLACE
MySQL specifies the row format of a table as either fixed or dynamic, depending
MySQL 5.0.45 What is the syntax to alter a table to allow a column
MySQL column > sdate, edate ( its 2 column). sdate is start date for
Mysql uses B+ tree for implementing indexes. Lets say my primary index is of
MySQL table with couple of fields: id - PRIMARY KEY url - CHAR(255) .
MySQL - is it legal to do 'SELECT table1.*,table2.column FROM table1,table2' ?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.