How can I create multiple BEFORE INSERT triggers? - mysql

I'm trying to learn mysql more in depth with relationships, triggers and what not. I currently have these 2 triggers for my database:
I'm trying to add another one that will BEFORE INSERT a different table
But I keep getting this error:
I'm not sure how I can get around this, or if this is specific to Sequel Pro (The GUI I'm using), it seems to wrap the query internally so I only have to enter the basic stuff, is there any way I can get this to work? My schema is like:
Forum_Threads has many Forum_Posts
Forum_Posts has many Posts_Replies
Forum_Threads and Forum_Posts both have a reply counter, so I'm trying to increment / decrement the reply_count on both tables when I'm inserting or deleting replies. Thanks.

The issue is that the GUI is trying to do a create trigger on a table that already has a before insert type trigger. Check your "Forum_threads" table to see if there's a BEFORE UPDATE trigger already on it.

Prior to MySQL 5.7.2,
There cannot be multiple triggers for a given table that have the same
trigger event and action time. For example, you cannot have two BEFORE
UPDATE triggers for a table. But you can have a BEFORE UPDATE and a
BEFORE INSERT trigger, or a BEFORE UPDATE and an AFTER UPDATE trigger.
(see 13.1.19 CREATE TRIGGER Syntax from the 5.6 manual.)
You can, however, specify multiple actions within the body of a given trigger, although the one-trigger-per-event limit can make managing trigger actions inconvenient.

it is not possible to 'create' multiple BEFORE INSERT triggers, but a possible workaround for you would be setting a behavior for each type of event.
Something like this may help you:
DROP TRIGGER IF EXISTS `<your_trigger>`;
DELIMITER //
CREATE TRIGGER `<your_trigger>` BEFORE INSERT ON `<your_table>`
FOR EACH ROW BEGIN
CASE <your_identifier>
WHEN <first_type> THEN
/*
ACTION BLOCK
FOR THIS EVENT
*/
WHEN <second_type> THEN
/*
ACTION BLOCK
FOR THIS EVENT
*/
ELSE
/*
ACTION BLOCK
FOR THIS EVENT
*/
END;
END//
DELIMITER ;
You can add as many event type as you needed, by doing this you can create a trigger that has multiple action based on a given event type identifier.
I hope this can help you, cheers!

Related

Using a trigger to automatically assign a value to specific column on a new insert

I'm struggling to find proper information on SQL Triggers.
My previous question got removed for it not being specific enough so hopefully this will do better.
I am trying to create a trigger that will assign RoleID 1 to every newly inserted row to my users table.
But I can't seem to figure it out.
AFTER INSERT on users
on EACH ROW
insert into users.RoleID values(1);
This doesn't seem to work.
And the examples and or information regarding triggers all focus on alerts or sending emails after an insert/drop or update.
Can it actually be done?
Any help would be much appreciated.
Cheers!
It looks like you aren't creating you sql trigger with the correct keyword.
Try this
drop trigger if exists before_insert_users;
DELIMITER $$
CREATE TRIGGER before_insert_users
BEFORE INSERT ON users
FOR EACH ROW
BEGIN
SET NEW.RoleID = 1;
END$$
DELIMITER ;
Note: RoleID will need to actually be a column on your table for this to work.
However, a trigger is not the best way to do this... See this SO post : Add a column with a default value to an existing table in SQL Server

UPDATE Same Row After UPDATE in Trigger

I want the epc column to always be earnings/clicks. I am using an AFTER UPDATE trigger to accomplish this. So if I were to add 100 clicks to this table, I would want the EPC to update automatically.
I am trying this:
CREATE TRIGGER `records_integrity` AFTER UPDATE ON `records` FOR EACH ROW SET
NEW.epc=IFNULL(earnings/clicks,0);
And getting this error:
MySQL said: #1362 - Updating of NEW row is not allowed in after trigger
I tried using OLD as well but also got an error. I could do BEFORE but then if I added 100 clicks it would use the previous # clicks for the trigger (right?)
What should I do to accomplish this?
EDIT - An example of a query that would be run on this:
UPDATE records SET clicks=clicks+100
//EPC should update automatically
You can't update rows in the table in an after update trigger.
Perhaps you want something like this:
CREATE TRIGGER `records_integrity` BEFORE UPDATE
ON `records`
FOR EACH ROW
SET NEW.epc=IFNULL(new.earnings/new.clicks, 0);
EDIT:
Inside a trigger, you have have access to OLD and NEW. OLD are the old values in the record and NEW are the new values. In a before trigger, the NEW values are what get written to the table, so you can modify them. In an after trigger, the NEW values have already been written, so they cannot be modified. I think the MySQL documentation explains this pretty well.
Perhaps you could write two separate statements in that transaction
update record set clicks=...
update record set epc=...
or you could put them inside a function, say updateClick() and just call that function. By doing it this way you can easily alter your logic should the need arise.
Putting the logic inside a trigger might create a situation where debugging and tracing are made unnecessarily complex.

MySQL: trigger with clause "instead of update"

I'm trying to create a database with history in mind (experience shows you'll have to do this one day or another).
I've asked here database-design-how-to-handle-the-archive-problem but there's no better anser than the link here.
My problem is about where to do the code and technically, how (MySQL gives me headaches). First I've started doing this in Php: before doing any insert, duplicate the record mark it as "obsolete" then modify the record.
But there's a dependency problem (manytomany and manytoone associations must be updated as well) which implies coding (one way or another) all the dependancies and updates that come with the tables (which is not acceptable).
So I'm thinking about doing all the work on the database server side. This would greatly simplify my Php code.
The problem is that I have to "archive" the current record before modifying it. To do so, the code must be in a trigger "before update".
Here's my code:
DELIMITER ;;
DROP TRIGGER IF EXISTS produit_trigger_update_before;
CREATE TRIGGER produit_trigger_update_before
BEFORE UPDATE ON produit
FOR EACH ROW BEGIN
/* */
INSERT INTO produit SET
id_origine = OLD.id_origine,
date_v_creation = OLD.date_v_creation,
date_v_start = OLD.date_v_debut,
date_v_end = NOW(),
...
last_record = OLD.last_record;
/* Dependancies : */
SET #last=LAST_INSERT_ID();
UPDATE categorie_produit SET id_produit=#last
WHERE id_produit = OLD.id;
UPDATE produit_attribut SET id_produit=#last
WHERE id_produit = OLD.id;
END;;
DELIMITER ;;
If I get this code working, all my problems are gone. But damn it, it's not working:
mysql> update produit set importance=3;
ERROR 1442 (HY000): Can't update table 'produit' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
mysql> Bye
In this page there's a working sample, which uses INSTEAD OF UPDATE clause in the trigger. MySQL doesn't seem to support this.
So my question is both conceptual (= have you any other "principle" that could work) and/or technical (= can you make this trigger work).
If I get this code working, all my problems are gone. But damn it, it's not working:
As a rule you can't have a trigger on table A trigger inserts into table A - since that could cause an endless loop. (Trigger mutation in Oracle terms)
Personally I would not do this using triggers. Triggers can do "audit logging" - but this is not what you want here.
I suggest you solve it programatically - either with a PHP function or a MySQL stored procedure (whatever your preference) that you call something like "ModifyProduit".
The code would then do basically what you have the trigger above do. (It might be easier to just have the code set date_v_end on the current row, and then insert a completly new row. That way you don't have to mess around with updating your referenced tables)
you can do history of a table with an auxiliary table like this (i've done this for many tables on mysql and the speed is very good):
table produit_history has the same structure as produit + 2 additional columns: "history_start DATETIME NOT NULL" and "history_stop DATETIME DEFAULT NULL".
there are 3 triggers on produit table:
AFTER INSERT: in this trigger there is a simple insert into produit_history of the same data with history_start = NOW() and history_stop = NULL (NULL means the current row is valid)
AFTER UPDATE: this trigger performs two queries. The first is un update like this:
UPDATE produit_history set history_stop = NOW() WHERE id_origine = OLD.id_origine AND history_stop IS NULL;
The second query is an insert identical to the one in the AFTER INSERT trigger.
AFTER DELETE: this triggers there is a simple update which is identical to the one in the AFTER UPDATE.
You can then query this history table and obtain snapshots at whatever time you're interested in with the following where condition:
WHERE (history_start &lt= "interesting_time" AND (history_stop IS NULL OR history_stop &gt "interesting_time"))

how to create trigger on any change in table

I have a mysql table which on any change (i.e. insert, update and delete) I need to run the relevant trigger code.
Do I need to create three different triggers or is there a syntax for just one.
Using mysql 5.1
Three triggers may perform better and AFAIK - there is no possibility to create multi-action trigger in MySQL, but I hope the syntax for one trigger is:
CREATE TRIGGER Name AFTER INSERT ON Table
FOR EACH ROW
begin
...
END

MySQL trigger which triggers on either INSERT or UPDATE?

Is there a way to create MySQL trigger which triggers on either UPDATE or INSERT?
Something like
CREATE TRIGGER t_apps_affected BEFORE INSERT OR UPDATE ...
Obviously, the above don't work. So, any workarounds without creating two separate triggers?
I need this in order to update running counter on another table.
Unfortunately, there is no shorthand form - you must create multiple triggers - one for each event.
The doc says:
trigger_event indicates the kind of statement that activates the trigger. The trigger_event can be one of the following:
INSERT: The trigger is activated whenever a new row is inserted into
the table; for example, through INSERT, LOAD DATA, and REPLACE
statements.
UPDATE: The trigger is activated whenever a row is modified; for
example, through UPDATE statements.
DELETE: The trigger is activated whenever a row is deleted from the
table; for example, through DELETE and REPLACE statements. However,
DROP TABLE and TRUNCATE TABLE statements on the table do not activate
this trigger, because they do not use DELETE. Dropping a partition
does not activate DELETE triggers, either. See Section 12.1.27,
“TRUNCATE TABLE Syntax”.
While it is impossible to put a trigger on multiple events, you can define the two triggers to merely call another stored procedure and, with that, cut down on the amount of code you need to commit. Just create the separate triggers to do nothing but, say,
CALL update_counter();
and put all of your actual work into the procedure. The triggers would then be a simple
CREATE TRIGGER t_apps_affected BEFORE INSERT ON table
FOR EACH ROW
BEGIN
CALL update_counter();
END;