I've got a table id a PK ID and a varchar(20) with some text. That PK is also a FK in another table I'm trying to switch the ID between two rows but I've been unable to do so as MySQl prevents this with an error with the FK constrain as for atomically, that row will violate the FK constrain but only for a milisecond as I will make that ID exist again with the other row. for example
Table 1
ID varchar(10)
1 "Hello"
2 "Bye"
Table 2
1(PK,FK) "Another data"
2(PK,FK) "BLA"
If I do
UPDATE Table 1 set ID=1 where text="Bye";
UPDATE Table 1 set ID=2 where text="Hello";
It will fail as
- ID already exist
- It will violate the FK constrain.
How can I do this?
EDIT: I have to do this once but for a bunch of rows as someone didn't add some data that needs to be in a specific index. Using an intermediate value will fix 1 but will still violate the FK constrain. The posted answer doesn't talk about FK.