Why wouldn’t MySQL have this feature?
INSERT INTO abc (a) VALUES ('bla')
ON DUPLICATE KEY
INSERT INTO ...
Do I need a stored procedure to do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
Insert into where exactly? The same table? You already know that’s impossible because the primary key already exists.
If you’re talking about inserting the row into a different table, that’s another thing altogether, not to be done with a single insert statement. That’s because the general idea of the
insertstatement is to get a specific row into a specific table. That can usually be done with “instead-of”-type triggers.That idea also means that you probably shouldn’t try to use a single
insertstatement to insert a different row (such as changing the primary key) in the case where the original already exists. In that case, the primary key is almost certainly an artificial one in which case an auto-increment key would probably be the way to go.The idea behind the
insert ... on duplicate key updateis get that row in whether it exists already or not. It does get a specific row into a specific table.