Trigger after Update based on condition - mysql

I have 4 columns in a table and this are.
when a new item is inserted it will look like this
I have a seperate table that look like this (based on the above image) it will appear like this
It looks like more of a logs. Now I have a code for that.
CREATE TRIGGER insert_on_tbl1
AFTER INSERT ON table1
FOR EACH ROW
INSERT INTO table2 (Person1,Item,tag) VALUES (New.Person1,New.Item,'Inserted');
now here is my question lets there is a modification happen something like this
and the responsilble for updating is on the Person1 Column (AAA) how can I insert that in my logs table? actually i have a code here for that and here it is
CREATE TRIGGER update_basedon_tbl1
AFTER UPDATE ON table1
FOR EACH ROW
IF IFNULL(NEW.Person2,"") = "" THEN
INSERT INTO table2 (Person1,Item,tag) VALUES (New.Person1,New.Item,'Modified');
END IF;
The problem here now is if the person who updates comes from Person2 still it saves the logs how can I fix that? I mean I want that all logs will be saved if changes are made from Person1 column only.

Related

Insert a new record in MySQL table at start

I am new to MySQL and learning it to my own. Actually I want to copy a column from a table into my existing table column! suppose that my existing table is:
where pid values are inserted by default!
now i want to copy a column from another table using:
INSERT INTO exist_tab(FirstLevel) SELECT some_col FROM another_table;
so that the values should come inside FirstLevel Column.
but the problem is that the copies values come below the pid values in FirstLevel Column as:
see that the firstlevel comes below! what is wrong with it? I need the "H" value against 19 but i dont want to use wild cards just want to copy the new data against old column data
thanks
I am new to this kind a work please can somebody give me any idea how to do it please!
thanks in advance
INSERT and UPDATE is different Command to Perform Different Task.
INSERT :Insert New Record into the table
Update:Update Existing Record in table If Exist.
NOT SURE ABOUT IT:(i'm Not Familiar With MYSQL)
Update a set
a.FirstLevel=b.some_col
from
exist_tab a join another_table b on a.Id=b.Id
Or You can Try :
update exist_tab a set a.FirstLevel=
(select top 1 some_col from another_table where Id=a.Id)
EDIT2:
update exist_tab a set a.FirstLevel=
(select top 1 some_col from another_table)
You Can Find Here.
You are using INSERT statement here. INSERT will create a new record in the table. You have to use UPDATE for updating a particular column in the existing table like this:
UPDATE exist_tab
SET FirstLevel = (SELECT some_col FROM another_table)
If you want any conditional update then you can use JOIN like this:
UPDATE exist_tab a
LEFT JOIN another_table b ON
a.pid = b.id
SET FirstLevel = a.some_col;

Is there a way to find out what was updated with the update query?

If I have 3 columns and my program can update all 3 i'll have an update query:
Update table set col1 = #value1, col2 = #value2, col3 = #value3
aside from storing all old values to variables then comparing them to the values in the tables once the update is committed, is there anyway to find out what changed and maybe, what didn't?
You can use a trigger (after or before) and do what you need in it. If you need that data at application level, this would not work unless storing the old values.
You can have history table with trigger set on main table on update to insert old values to history table before updating it.

MySQL, Synchronise two tables

I have two data base A & B, in each one I have a table called answer, I want to use the 2nd one as an archive table, I want to create a trigger that copies the last inserted row in A.answer to B.answer.
Here what I did
CREATE TRIGGER `a` AFTER INSERT ON `A`.`answer`
FOR EACH ROW INSERT INTO `B`.`answer` SELECT * FROM `answer`
This trigger works, but copy all the answers inserted in A.answer to B.answer.
The problem is : I dont want to copy all answers, but only the last one.
(remark : I dont know the id of the inserted answer, so dont tell me to add a ' WHERE answer.id = xx ').
Thanks for your help
You could write your trigger this way:
CREATE TRIGGER `a` AFTER INSERT ON `A`.`answer`
FOR EACH ROW
INSERT INTO `B`.`answer` VALUES (NEW.col1, NEW.col2, ..., NEW.colN)
where you have to specify all column names.
Please see fiddle here.

mysql trigger on insert many to many

I have two tables with many to many relationship.
The tables are
table user
idUser
name
table right
idRight
type
table user_has_right
idUser
idRight
When I insert a user with the rights I want him to have,
obviously the middle table (user_has_right) must have some inserts
too. Let's say I want to insert the user with id = 1 and name = 'test'
and right = 'boss' , where boss is already a row in table right with id = 1 .
The middle table must have an insert of (1,1).
I know how to do this programmatically, but can this be done with trigger on insert?
Thank you.
//The problem I'm facing is how to find the idRight for type 'boss'. Maybe a nested select inside the trigger?
//Is this task even possible?
I've come across this old question. If you're still interested in an answer then here it is
CREATE TRIGGER tg_user_insert
AFTER INSERT ON `user`
FOR EACH ROW
INSERT INTO user_has_right (idUser, idRight)
SELECT NEW.idUser, idRight
FROM `right`
WHERE type = 'boss';
Here is SQLFiddle demo

trigger updating all rows, not just inserted

I know I'm missing something obvious here. This trigger is updating all rows in the table (killing performance) when all I want it to do is perform the update on the new inserted row.
CREATE TRIGGER [dbo].[update_location_topo_name]
--fires at each row insert, queries topo map layer (must be present!) and inserts name of topo into new location record
on [dbo].[TBL_LOCATIONS]
after insert
AS
BEGIN
update TBL_LOCATIONS
set TOPO_NAME = dbo.QD24K_GRSM.NAME
FROM dbo.tbl_locations
inner join dbo.QD24K_GRSM
on TBL_LOCATIONS.Location_ID = TBL_LOCATIONS.Location_ID
WHERE (QD24K_GRSM.Shape.STContains(TBL_LOCATIONS.SHAPE) = 1)
END
You need to reference the INSERTED pseudo table to get only the rows that were inserted.
Additionally your join condition of TBL_LOCATIONS.Location_ID = TBL_LOCATIONS.Location_ID makes no sense at all.
Probably better to do this as an INSTEAD OF trigger to modify the rows before insert rather than after they are inserted.
CREATE TRIGGER [dbo].[update_location_topo_name]
ON [dbo].[TBL_LOCATIONS]
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO TBL_LOCATIONS
(foo,
bar,
TOPO_NAME)
SELECT foo,
bar,
dbo.QD24K_GRSM.NAME
FROM INSERTED I
LEFT JOIN dbo.QD24K_GRSM
ON QD24K_GRSM.Shape.STContains(I.SHAPE) = 1/* Will insert additional
rows if more than one match*/
END
I would suggest to create trigger "Instead of Insert" rather than "After Insert".
That way you can modify newly inserted rows as you need before actually inserting them in target table.
And there is absolutely no need to join "inserted" table with target table.
Something like this (syntax might be wrong as not actually run that code):
CREATE TRIGGER [dbo].[update_location_topo_name]
on [dbo].[TBL_LOCATIONS]
Instead Of insert
AS
BEGIN
Insert Into TBL_LOCATIONS (...., TOPO_NAME)
Select ..., CrTable.NAME as TOPO_NAME
From Inserted Cross Apply
(
Select top 1 QD24K_GRSM.NAME from QD24K_GRSM where
QD24K_GRSM.Shape.STContains(Inserted.SHAPE) = 1
) as CrTable
END