Bug #10795 | REPLACE reallocates new AUTO_INCREMENT | ||
---|---|---|---|
Submitted: | 23 May 2005 6:58 | Modified: | 3 Oct 2008 19:11 |
Reporter: | B Jones | Email Updates: | |
Status: | Duplicate | Impact on me: | |
Category: | MySQL Server | Severity: | S4 (Feature request) |
Version: | 4.1.10 | OS: | Linux (Linux) |
Assigned to: | CPU Architecture: | Any |
[23 May 2005 6:58]
B Jones
[23 May 2005 12:08]
MySQL Verification Team
This is the expected behavior: http://dev.mysql.com/doc/mysql/en/replace.html REPLACE works exactly like INSERT, except that if an old record in the table has the same value as a new record for a PRIMARY KEY or a UNIQUE index, the old record is deleted before the new record is inserted. See Section 13.1.4, “INSERT Syntax”. Then a new auto_increment is applied.
[23 May 2005 12:48]
B Jones
I'm aware how REPLACE works. That wasn't the issue. To recap: The suggestion was that REPLACE lets you keep the same primary key. Monty said this was on their TODO list. Could you please tell me the status of that? Please don't close this bug report.
[24 May 2005 10:11]
Sergei Golubchik
I don't think it happened yet.
[24 May 2005 10:14]
Sergei Golubchik
besides, we have now INSERT ... ON DUPLICATE KEY UPDATE. It's what one should use to *update* the record, REPLACE is, indeed, just an DELETE+INSERT.
[26 May 2005 22:02]
B Jones
Hi Sergei, Thanks for your response. Had investigated INSERT ... ON DUPLICATE KEY but the problem is you have to hardcode the data assignments. That's okay if you're updating specific values, but not if you want to update your data from another table. By examples, you can do this: INSERT INTO donkey (name, city) VALUES ('Mr Ed', 'London') ON DUPLICATE KEY UPDATE city = 'London'; But this is the shortcoming; you can't do this (which has redundant code anyway): INSERT INTO donkey (name, city) SELECT name, city from mule M ON DUPLICATE KEY UPDATE donkey.city = M.city; It would be nicer if you could just do this: Ideal actually (ie. without having to specify hard-coded column values): INSERT INTO donkey (name, city) SELECT name, city from mule M ON DUPLICATE KEY UPDATE; Of course you can do the same thing with DELETEs, INSERTs, etc., or use REPLACE with a temporary table, but all these involve a speed penalty, because we're looking up the table twice. Which gets us back to REPLACE. Even if it did that, and let us keep the old AUTO_INCREMENT field, that would be fine. REPLACE is getting long in the tooth anyway and could use an overhaull; it is incompatibe with InnoDB's referential integrity (when REPLACE DELETE's a record, it breaks all the primary-foreign key relationships). REPLACE INTO donkey (name, city) SELECT name, city from mule M; To sum up, what we are asking for is an easier way to REPLACE or UPDATE records from a SELECT group. Just a bit of background; We've been using MySQL for online transactions for some time now. Lately we've been building an enterprise database with MySQL using InnoDB. Hope this suggestion is useful.
[29 May 2005 11:03]
Sergei Golubchik
You should be able to do INSERT INTO donkey (name, city) SELECT name, city from mule M ON DUPLICATE KEY UPDATE donkey.city = VALUES(city); VALUES() pseudo-function returns the value that would be inserted into the column if INSERT succeeded.
[30 May 2005 23:25]
B Jones
Hi Sergei, That looked promising; pretty much the sort of statement requested, but apparently MySQL doesn't let you you specify a column in both the INSERT SELECT clause and the DUPLICATE KEY UPDATE. INSERT INTO donkey (name, city) SELECT name, city from mule M ON DUPLICATE KEY UPDATE donkey.city = VALUES(city); -------------- ERROR 1110 (42000): Column 'city' specified twice -- If they're mutually exclusive, that's ok. -- INSERT INTO donkey (name) SELECT name from mule M ON DUPLICATE KEY UPDATE donkey.city = VALUES(M.city); -------------- Query OK, 3 rows affected (0.05 sec) Records: 2 Duplicates: 1 Warnings: 0 -- But if it's the same, it throws an error -- INSERT INTO donkey (name) SELECT name from mule M ON DUPLICATE KEY UPDATE donkey.name = VALUES(M.city); -------------- ERROR 1110 (42000): Column 'name' specified twice Not sure if this is considered a bug, but perhaps if INSERT ON DUPLICATE KEY was enhanced to allow you to provide the column to be specified in either place, that would provide the sort of functionality originally requested? It'd also be compatible with existing code.
[31 May 2005 11:29]
Sergei Golubchik
This was considered a bug (http://bugs.mysql.com/bug.php?id=8147), and is fixed in 4.1.11
[31 May 2005 22:19]
B Jones
Thanks very much, Sergei. Good Timing!