I have the table:
select * from person;
+----+------+---------+
| id | name | surname |
+----+------+---------+
| 1 | John | Doe |
| 2 | Mary | Smith |
+----+------+---------+
2 rows in set (0.00 sec)
I need to replace values in a MySQL database. All the values have the same id, so I´m trying:
replace into person (id,name) values (1,'Victor');
select * from person;
+----+--------+---------+
| id | name | surname |
+----+--------+---------+
| 1 | Victor | NULL |
| 2 | Mary | Smith |
+----+--------+---------+
2 rows in set (0.00 sec)
The problem is it´s setting the surname as NULL.
I know I can do it with an UPDATE person SET name='Victor' WHERE id=1 but I have a huge SQL file with a dump containing many INSERT INTO, so i´m thinking on useing REPLACE INTO because has the same syntaxis and it would be faster, just doing a search&replace (INSERT -> REPLACE INTO).
Edit:
The problem is I have a huge dump file like this:
INSERT INTO person (id,name) VALUES (1,'Victor');
INSERT INTO person (id,name) VALUES (2,'Mark');
INSERT INTO person (id,name) VALUES (3,'Steve');
INSERT INTO person (id,name) VALUES (4,'Lou');
INSERT INTO person (id,name) VALUES (5,'Jessica');
etc..
So I´m looking the easiest way to update all those names into the database. It would be great if I could do something like this: UPDATE person SET (id,name) VALUES (1,´Victor')
Ideally you would use something like this:
The reason
REPLACE INTOdoesn’t achieve the results you want is becauseREPLACE INTO, in terms of functionality, deletes the row and re-inserts it with the data you provided.If your database dump contains all the info already in your database, plus updated info, you’re better off just dropping your tables and running the import on a clean db. If there is data stored in your database that isn’t contained in the dump, then the import is too blunt an instrument to work with.
You may have luck whacking
ON DUPLICATE KEY UPDATE name = 'foo';at the end of every statement.