Right now I have the following table:
+----------------+
| Sale table |
+----------------+
| ID |
| sale |
| ... |
| receiver_name |
| receiver_email |
| ... |
+----------------+
The important columns are receiver_name
and receiver_email
. The receiver is the person 'receiving' the object of a sale. Until now it was only possible to receive an object by mail. But in the next update it will also be possible to receive that object by other communication methods like for example SMS.
So for each sale the receiver of the sold object can select a communication method. For this question let's only consider e-mail and SMS. The new database-structure will have a extra table.
+----------------+
| Receiver table |
+----------------+
| ID |
| receive_method |
| receiver_name |
| receiver_email |
| receiver_telnr |
+----------------+
The updated sale table will have a foreign key to the new Receiver table
:
+----------------+
| Sale table |
+----------------+
| ID |
| sale |
| ... |
| receiver_id |
| ... |
+----------------+
This is necessary because my business logic can't know what method the user selected. Depending on the value of receive_method
having the domain ("email", "SMS")
I'll collect the data from column receiver_email
(if receive_method == "email"
) or from column receiver_telnr
(if receive_method == "SMS"
).
I think this logic is pretty solid.
The problem, however, is that the Sale table is already filled with data! So I need a way to put the values of receiver_name
and receiver_email
from the old Sale table into the new version of the Sale table. And put the correct foreign key in place so the new Sale table still contains the information of the old sales before the update.
To complete this update, this are the steps I will take:
- Create the new Receiver table
- Put the values of
receiver_name
andreceiver_email
from the old Sale table into the new Receiver table. - Add the column
receiver_id
to the Sale table. - (Here I am stuck...)
- Delete
receiver_name
andreceiver_email
from Sale table because it is referenced by the new columnreceiver_id
.
I am stuck at step 4 though. How do I insert the foreign key in the new column receiver_id
so the data stays correct?
1 Answer 1
You have to update your Sale
table in three steps.
- First you have to add the
receiver_id
field as FK. - Next you have to fill up
Receiver
table with data fromreceiver_name
andreceiver_email
. Some denormalization emerge at this step because of data duplication. - Finally you have to drop
receiver_name
andreceiver_email
fromSale
.
-
But the question remains: In your second step, when I fill up
Receiver
with the data fromreceiver_name
andreceiver_email
, how do I link the ID of the newly inserted rows to columnreceiver_id
?O'Niel– O'Niel2022年04月02日 05:37:38 +00:00Commented Apr 2, 2022 at 5:37 -
Sale
andReceiver
tables are in 1:1 relation - each row ofSale
should have a single corresponding row inReceiver
and vv. So you can create an emptyReceiver
table with IDs fromSale
and use it for JOIN.Kondybas– Kondybas2022年04月02日 05:55:56 +00:00Commented Apr 2, 2022 at 5:55
method
anENUM
? Or aSET
? Your table names don't 'speak to me'. Seems like you have "customers" and "sales"? And the question is how to record that a particular customer has been sent notice of a sale?