There are two tables with the same ID field (in my opinion this is called a one-to-one relationship). These fields are linked through links. Data is added to one table. It is necessary for this ID to immediately appear in another. How can this be done, other than the obvious, immediately add to two tables? Preferably by means of mySQL
1 answer
Create a TRIGGER that will automatically add an entry to another table.
CREATE TRIGGER insert_table AFTER INSERT ON table_name FOR EACH ROW INSERT INTO table_name_two VALUES (NEW.id) In general, it is correct to use several INSERT requests within a single transaction, which would be convenient and obvious for other developers who will support your code. Because there is such a share of developers who shove everything that is possible in stored procedures and triggers, and then you won’t understand why you only execute this request, and then another 100,500 locks happened somewhere and the records changed.
- Thank you, read. But do you think that it is better not to complicate and make a double INSERT? - Zhenya Vedenin
- Yes, there is nothing terrible in the fact that you are performing two INSERTs within one transaction, if your business logic requires it. - Firepro
- Already read the amended answer) Thank you. But thanks for the triggers too - Zhenya Vedenin
|
update table set counter=counter+x, tstamp=tstamp where ...Those. you explicitly assign the current value to the timestamp field, then it will not be updated :) - Mike