MySQL - AFTER UPDATE Trigger between Two Databases - mysql

Been having some issues with this for a few hours. I'm by no means a MySQL guy and have been looking for a solution but no luck. Here's essentially what I have..
T1: CollectionDB.source
indexName | status_id
Test | 11
T2: ManagementConsole.MC_SCHEDULE
NAME | ACTIVE
Test | 0
Basically, the trigger I need should run after T1.status_id has changed from any number to 8. When it runs, it should find T2.NAME that matches the corresponding T1.indexName and change ACTIVE from 0 to 1. Here's what I have thus far.
DELIMITER &&
CREATE TRIGGER `UpdateSynch`
AFTER UPDATE ON `CollectionDB`.source FOR EACH ROW
BEGIN
UPDATE `ManagementConsole`.MC_SCHEDULE AS T1
SET T1.ACTIVE = 1
WHERE (NOW.indexName = T1.NAME) AND ((OLD.status_id != 8) AND (NOW.status_id = 8));
END&&
I'm getting a 'Trigger in wrong schema' error, which makes sense since I'm trying to make an update in a different schema. Is there any way around this? Thanks in advance.

You have to create the trigger in the same database as the target table, i.e. CollectionDB; however, you must currently have another database selected as your default schema.
Therefore, either:
Change your default schema to the CollectionDB database before attempting to create the trigger:
USE CollectionDB;
CREATE TRIGGER UpdateSynch ...
Explicitly specify in the CREATE TRIGGER statement the database in which you want it to be created:
CREATE TRIGGER CollectionDB.UpdateSynch ...

Related

MySQL Trigger on update

I have a database with 2 tables. One tables contains data regarding references (job refs) the other contains votes. In order to get to the vote stage, you need 5 references. In another database, i must change a group_id once they have 5 refs.
I was trying to use something like below:
CREATE TRIGGER update_group AFTER UPDATE ON refs
FOR EACH ROW
BEGIN
IF (SELECT recruit_id from refs where count(refs) = 5) THEN
UPDATE users.members
SET group_id=10
WHERE member_id=recruit_id;
END;
Is something like this even feasible? if this doesn't work, my fallback is to use a bash script in a cron job that runs every 5 minutes, but a trigger just seems so much more efficient (and instantaneous)
You may use NEW. to refer respectively affected recruit_id after change, do a count and update the foreign table accordingly.
CREATE TRIGGER update_group AFTER UPDATE ON refs
FOR EACH ROW
BEGIN
SELECT #refs_no := count(*) from refs where recruit_id = NEW.recruit_id
IF (#refs_no = 5) THEN
UPDATE users.members
SET group_id=10
WHERE member_id=NEW.recruit_id;
END;

Trigger - Error #1442 Can't update [MYSQL] [duplicate]

MySQL doesn't currently support updating rows in the same table the trigger is assigned to since the call could become recursive. Does anyone have suggestions on a good workaround/alternative? Right now my plan is to call a stored procedure that performs the logic I really wanted in a trigger, but I'd love to hear how others have gotten around this limitation.
Edit: A little more background as requested. I have a table that stores product attribute assignments. When a new parent product record is inserted, I'd like the trigger to perform a corresponding insert in the same table for each child record. This denormalization is necessary for performance. MySQL doesn't support this and throws:
Can't update table 'mytable' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. A long discussion on the issue on the MySQL forums basically lead to: Use a stored proc, which is what I went with for now.
Thanks in advance!
You can actually up the rows in the same table as the trigger. The thread you linked to even has the solution.
For example:
TestTable ( id / lastmodified / random )
create trigger insert_lastmod
before insert on TestTable
for each row
set NEW.lastmodified = NOW();
insert into TestTable ( `random` ) values ( 'Random' );
select * from TestTable;
+----+---------------------+---------------------+
| id | lastmodified | random |
+----+---------------------+---------------------+
| 1 | 2010-12-22 14:15:23 | Random |
+----+---------------------+---------------------+
I suppose you could call the stored proc in your trigger. HOwever, if you want to update some fields in the same records that you are changing (such as an updatedby or lastupdated column) then you can do this in a beofre trigger according to the refernce manual. http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html
This is a common operation for triggers and I find it difficult to believe it isn't supported.
If you want to update column that you don't read in trigger function, then as a workaround, you could put that column into separate table.
You can actually do that
The below is an example for same
DELIMITER $$
create trigger test2
before insert on ptrt
for each row
begin
if NEW.DType = "A" then
set NEW.PA = 500;
elseif NEW.DType = "B" then
set NEW.PA = 1000;
else
set NEW.PA = 0;
END IF;
END;$$
DELIMITER;
This worked for me :D
On Before / Update.
BEGIN
SET NEW.DateTimeUpdated = NOW();
END

InnoDB automatic actions

I'm using MySQL and InnoDB. I'm versioning all the table's records - the latest version of the record has a column called 'lv' set to 1 and the previous record for that ID that has an 'lv' column set to 1 has to be set to 0 now that there's a new version. I'm now doing two queries every time I insert a new record (version). Is there a way to make InnoDB do that automatically? Thank you for your input.
You could use a trigger?
This is untested, but I think something like this would be possible:
delimiter |
CREATE TRIGGER newversion BEFORE INSERT ON yourTable
FOR EACH ROW BEGIN
UPDATE yourTable SET lv = 0 WHERE lv = 1;
END;
|
delimiter ;
There could be some mistake in there, as it is a quick type, based on the manual ofcourse :)

MYSQL auto increase column entity by 1 on update?

I have a table: ID,name,count,varchar(255)
Now, what i'd like is to increase the "count" each time that row in the table is updated.
Of course, the easy way is to read first, get the value, increase by 1 in php, then update with the new value. BUT!
is there any quicker way to do it? is there a system in mysql that can do the ++ automatically? like autoincrement, but for a single entity on itself?
I see two options:
1.
Just add this logic to every update query
UPDATE `table` SET
`data` = 'new_data',
`update_counter` = `update_counter` + 1
WHERE `id` = 123
2.
Create a trigger that will do the work automatically:
CREATE TRIGGER trigger_name
AFTER UPDATE
ON `table`
FOR EACH ROW
BEGIN
UPDATE `table`
SET `update_counter` = `update_counter` + 1
WHERE `id` = NEW.id
END
Create a trigger:
http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html
Triggers are pieces of code that are "triggered" by the database on certain events. In your case, the event would be an update. Many RDBMS support triggers, so does MySQL. The advantage of using a trigger is that every piece of your PHP logic that updates this entity, will implicitly invoke the trigger logic, you don't have to remember that anymore, when you want to update your entity from a different piece of PHP logic.
you can look up at the trigger
or can do with the extra mysql query
update table set count=count+1 ;
UPDATE table SET name='new value', count=count+1 WHERE id=...
An SQL update can use fields in the record being updated as a source of data for the update itself.

MySQL triggers cannot update rows in same table the trigger is assigned to. Suggested workaround?

MySQL doesn't currently support updating rows in the same table the trigger is assigned to since the call could become recursive. Does anyone have suggestions on a good workaround/alternative? Right now my plan is to call a stored procedure that performs the logic I really wanted in a trigger, but I'd love to hear how others have gotten around this limitation.
Edit: A little more background as requested. I have a table that stores product attribute assignments. When a new parent product record is inserted, I'd like the trigger to perform a corresponding insert in the same table for each child record. This denormalization is necessary for performance. MySQL doesn't support this and throws:
Can't update table 'mytable' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. A long discussion on the issue on the MySQL forums basically lead to: Use a stored proc, which is what I went with for now.
Thanks in advance!
You can actually up the rows in the same table as the trigger. The thread you linked to even has the solution.
For example:
TestTable ( id / lastmodified / random )
create trigger insert_lastmod
before insert on TestTable
for each row
set NEW.lastmodified = NOW();
insert into TestTable ( `random` ) values ( 'Random' );
select * from TestTable;
+----+---------------------+---------------------+
| id | lastmodified | random |
+----+---------------------+---------------------+
| 1 | 2010-12-22 14:15:23 | Random |
+----+---------------------+---------------------+
I suppose you could call the stored proc in your trigger. HOwever, if you want to update some fields in the same records that you are changing (such as an updatedby or lastupdated column) then you can do this in a beofre trigger according to the refernce manual. http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html
This is a common operation for triggers and I find it difficult to believe it isn't supported.
If you want to update column that you don't read in trigger function, then as a workaround, you could put that column into separate table.
You can actually do that
The below is an example for same
DELIMITER $$
create trigger test2
before insert on ptrt
for each row
begin
if NEW.DType = "A" then
set NEW.PA = 500;
elseif NEW.DType = "B" then
set NEW.PA = 1000;
else
set NEW.PA = 0;
END IF;
END;$$
DELIMITER;
This worked for me :D
On Before / Update.
BEGIN
SET NEW.DateTimeUpdated = NOW();
END