Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace Into Query Syntax

I want to be able to update a table of the same schema using a "replace into" statement. In the end, I need to be able to update a large table with values that may have changed.

Here is the query I am using to start off:

REPLACE INTO table_name (visual, inspection_status, inspector_name, gelpak_name, gelpak_location), VALUES (3, 'Partially Inspected', 'Me', 'GP1234', 'A01'); 

What I don't understand is how does the database engine know what is a duplicate row and what isn't? This data is extremely important and I can't risk the data being corrupted. Is it as simple as "if all columns listed have the same value, it is a duplicate row"?

I am just trying to figure out an efficient way of doing this so I can update > 45,000 rows in under a minute.

like image 697
kformeck Avatar asked Nov 06 '13 19:11

kformeck


People also ask

How does replace into work in SQL?

REPLACE works exactly like INSERT , except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted. See Section 13.2. 6, “INSERT Statement”. REPLACE is a MySQL extension to the SQL standard.

What does replace command do in MySQL?

The REPLACE() function replaces all occurrences of a substring within a string, with a new substring. Note: This function performs a case-sensitive replacement.

How do I replace a column in MySQL?

To replace, use the REPLACE() MySQL function. Since you need to update the table for this, use the UPDATE() function with the SET clause.


1 Answers

As the documentation says:

REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.

like image 87
Filipe Silva Avatar answered Sep 25 '22 10:09

Filipe Silva