Need help with simple mysql triggers - mysql

I have one table (TABLE A) in my mysql database which has the listed fields:
row1_id - PK
row2_id - PK
row3_data
What I would like to do is, every time a record is inserted or updated in table A, I would like to call a trigger to write me the new information of table A in another table, TABLE A_LOG, which has the following fields:
row0-modification-id - PK
row1_id (from table A) - PK
row2_id (from table A) - PK
row3_data (from table A)
the Table A_LOG should then have 3 PKeys, two from the new inserted/updated record of table A and other that indicates me the number of the modification (1, 2, 3..)
Thank you very much for your help.

Create TableA_Log to autoincrement row0-modification-id.
I bet you don't exactly need this table to have row0-modification-id AND row1_id and row2_id all as the PK.
CREATE TRIGGER dbo.trg_TableA_ins_upd
ON dbo.DatabaseName
AFTER INSERT, UPDATE
AS
BEGIN
INSERT INTO TableA_Log (row1_id, row2_id, row3_data)
SELECT row1_id, row2_id, row3_data FROM inserted
END

Related

MS Access after insert event

I have 2 tables in access, A and B, both with a case ID field and several other fields. I am trying to set up a process where if I insert a record in A with a new case ID, only the case ID of that record inserted in table A gets populated in table B.
I have tried using an After Insert event macro of table A with a create a record into table B action which should create a record with the case ID in table B. This work fine if none of the fields apart from the Case ID in table B are required. However, if I have other fields that are required in Table B, the create a record into table B does not happen upon insert into Table A.
How do I fix this issue? Thanks in advance!

How to merge two db with same structure but different data

I'm working with phpmyadmin and I have to merge two db with same structure but different data.
The db have relation between tables (foreign key).
The data in two db may have same id, and so their foreign key.
I would like to know if it's possible merge the two db keeping all data, so, if a row already "exist", insert it with new id and update its foreign key.
thanks a lot
No easy way unfortunately. If you have TableA as a foreign key to TableB, you will need to
1) Insert data from source tableA to target tableA
2) create a (temp) table to store the mapping between source tableA ids and target tableA ids
3) Use this mapping table when inserting data from tableB to convert the tableA ids to the new ones in the target db
... and so on. It can get quite hairy if you have a deep hierarchy of tables, but hopefully you get the idea. Take backups before you start.
Another idea that you might want to consider is using a cursor:
Assume table A is the one that you want to keep and table B is the one you want to remove.
Declare a cursor for table B and select all the records.
Loop each record selected from the cursor and check.
Case 1: If the ID is exists on table A, insert the record to table A with same details.
Case 2: If the ID is exists on table B, insert the record and modify the ID and foreign key.
Once all the records have been checked, drop table B.
Sorry, I just can give an idea at the moment.

enter data into 2 tables with foriegn key relation based on one attribute of primary table

I have a table 'project' that has attributes:name, UID(PK), section(distinguisher)... and 3 tables A,B,C based on section with specific properties of each section.i want to be able to enter data into project and the section specific data into A/B/C TABLE based on entry in section attribute of project table of that row. all tables have Foreign key with UID as UID_A, UID_B, UID_C...any ideas on how i can do it? all help appreciated as i am quite a novice...thanks! I am working with Mysql workbench.
Well, create a trigger on the table project:
CREATE TRIGGER `fill_abc` AFTER INSERT ON `project`
FOR EACH ROW BEGIN
INSERT INTO A <some specific data...>;
INSERT INTO B <some specific data...>;
INSERT INTO C <some specific data...>;
END;
Probably, you want to handle not only inserts into this table, but updates and deletes as well - it is nearly the same idea. See trigger definition here and examples here

Automatic Update for database rows based on changes in another table

I have a scenario that requires a value in a row of a table to be updated automatically whenever a row has been added or deleted in another table. I'm not sure how to do it.BTW I'm using phpmyadmin in order to manage my database. Thanks in advance.
pages Table
------------
page_no
no_of_choices
choices Table
-------------
page_no
choice_no
When I add a choice with choice number 1 and page_no, then the table page which has the row, page_no=1 should be updated with no_of_choices=no_of_choices+1
You can use triggers.
For example:
CREATE TRIGGER `test1`
AFTER INSERT ON `tbl1`
FOR EACH ROW SET NEW.upd_fld = new_value
Similarly could be done for delete.
You can also create triggers from phpMyAdmin
TABLE A: page_no, no_of_choices
TABLE B: page_no, choice_no...
With a relational database you very rarely want to have duplicate data. If something breaks at some point, you won't know which to trust - the rows in Table B, or the no_of_choices in Table A. A better solution is to do one of the following (depending on which table you are querying):
SELECT COUNT(no_of_choices) FROM B WHERE page_no = 1
or
SELECT A.*, COUNT(choice_no) AS choice_no FROM A LEFT JOIN B USING(page_no)
You get the same result, but now you have one record to go off of, so you won't have inconsistent data.

Mysql table sync or trigger?

What is the best way to do this with mysql :
I have two tables in the same database (a table : Gene, and a table Gcur).
In table Gene, I have a column last_updated. In table Gcur, I have a column last_modified.
I'd like to synchronize column last_modified with column last_updated.
For example, I made an update of column last_modified (from table Gcur), and automatically column last_updated (from table Gene) is updated. Two tables are linked by an ID key.
It should be possible with triggers ? An idea ?
Thanks !
Yes, it is possible with triggers, and fairly trivial. The result would look like
CREATE TRIGGER au_Gcur AFTER UPDATE ON Gcur
FOR EACH ROW
UPDATE Gene SET last_updated = NEW.last_modified WHERE id = NEW.id;