I keep getting error messages when I try to execute this statement in phpmyadmin. I'm running mysql 5.7
DELIMITER //
CREATE OR REPLACE TRIGGER update_counts_trigger AFTER INSERT OR UPDATE OR DELETE ON tickets
FOR EACH ROW
BEGIN
DECLARE V_uitvoeringId, V_ReserveringId varchar(50);
DECLARE V_tekoop, V_gereserveerd, V_wachtlijst int;
SET V_ReserveringId = NEW.reserveringId OR OLD.reserveringId;
SET V_uitvoeringId = ( SELECT uitvoeringId FROM reservering WHERE id=V_ReserveringId )
SET V_tekoop = (
SELECT count(*)
FROM tickets t
WHERE t.tekoop AND NOT t.geannuleerd AND NOT t.verkocht AND t.uitvoeringId = V_uitvoeringId);
SET V_gereserveerd = (
SELECT count(*)
FROM tickets t
WHERE NOT t.wachtlijst AND NOT t.geannuleerd AND NOT t.verkocht AND t.uitvoeringId = V_uitvoeringId);
SET V_wachtlijst = (
SELECT count(*) FROM tickets t WHERE t.wachtlijst AND NOT t.geannuleerd AND NOT t.verkocht AND t.uitvoeringId = V_uitvoeringId);
update uitvoering
set tekoop = V_tekoop,
gereserveerd = V_gereserveerd,
wachtlijst = V_wachtlijst,
vrije_plaatsen = aantal_plaatsen - V_gereserveerd + V_tekoop
WHERE id=V_uitvoeringId;
END //
DELIMITER ;
Can anyone see what's wrong?
Unrecognized statement type. (near "DECLARE" at position 176)
This type of clause was previously parsed. (near "SET" at position 398)
Unrecognized statement type. (near "END" at position 1192)
There are other syntax errors causing those complaints.
There is no OR REPLACE option with triggers in MySQL. The trigger must be dropped and then recreated.
There is no ORing with actions, only one, which means that you'll have to create 3 triggers even though the definition may be the same. However, it seems like you only need an AFTER UPDATE action? ... because AFTER INSERT does not have an OLD reference, and AFTER DELETE does not have a NEW reference.
Getting past those two items, there is also a ; missing after the query with SELECT uitvoeringId.
Putting all that together, becomes:
DELIMITER //
CREATE TRIGGER update_counts_trigger AFTER UPDATE ON tickets
FOR EACH ROW
BEGIN
DECLARE V_uitvoeringId, V_ReserveringId varchar(50);
DECLARE V_tekoop, V_gereserveerd, V_wachtlijst int;
SET V_ReserveringId = NEW.reserveringId OR OLD.reserveringId;
SET V_uitvoeringId = ( SELECT uitvoeringId FROM reservering WHERE id=V_ReserveringId );
SET V_tekoop = (
SELECT count(*)
FROM tickets t
WHERE t.tekoop AND NOT t.geannuleerd AND NOT t.verkocht AND t.uitvoeringId = V_uitvoeringId);
SET V_gereserveerd = (
SELECT count(*)
FROM tickets t
WHERE NOT t.wachtlijst AND NOT t.geannuleerd AND NOT t.verkocht AND t.uitvoeringId = V_uitvoeringId);
SET V_wachtlijst = (
SELECT count(*) FROM tickets t WHERE t.wachtlijst AND NOT t.geannuleerd AND NOT t.verkocht AND t.uitvoeringId = V_uitvoeringId);
update uitvoering
set tekoop = V_tekoop,
gereserveerd = V_gereserveerd,
wachtlijst = V_wachtlijst,
vrije_plaatsen = aantal_plaatsen - V_gereserveerd + V_tekoop
WHERE id=V_uitvoeringId;
END //
DELIMITER ;
Then with that in phpMyAdmin:
Then after executing the SQL:
Then checking the triggers tab:
...and clicking Edit at the trigger shows the definition:
Related
I've got this SQL code:
CREATE EVENT `update_statistics`
ON SCHEDULE EVERY 1 DAY STARTS '2015-08-24 02:00:00'
ON COMPLETION PRESERVE
DO BEGIN
UPDATE statistics
SET km_traveled = (SELECT sum(km)
FROM archived_trips),
passengers_driven = (SELECT sum(passengers)
FROM archived_trips),
trips_taken = (SELECT count(*)
FROM archived_trips)
WHERE id = 1;
UPDATE statistics
SET co_saved = ((SELECT km_traveled
FROM statistics) * 0.215 * (SELECT passengers_driven
FROM statistics))
WHERE id = 1;
END;
When I run it through the SQL console in PhpStorm - it runs fine and the scheduled task works and so on.
But if I try to run the query directly in phpmyadmin, I get the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 13
Line 13 is WHERE id=1;. Honestly, I don't see any syntax problem with the query. Any suggestions?
The delimiter of the query is ';' and ';' is also used in the stored procedure, causing the query to fail.
Add DELIMITER // before the statement and end with // to fix this.
DELIMITER //
CREATE EVENT `update_statistics`
ON SCHEDULE EVERY 1 DAY STARTS '2015-08-24 02:00:00'
ON COMPLETION PRESERVE
DO BEGIN
UPDATE statistics
SET km_traveled = (SELECT sum(km)
FROM archived_trips),
passengers_driven = (SELECT sum(passengers)
FROM archived_trips),
trips_taken = (SELECT count(*)
FROM archived_trips)
WHERE id = 1;
UPDATE statistics
SET co_saved = ((SELECT km_traveled
FROM statistics) * 0.215 * (SELECT passengers_driven
FROM statistics))
WHERE id = 1;
END; //
DELIMITER ;
With PHPstorm, sending multiple queries in one go is probably disabled, causing MySQL to ignore the delimiter.
You must set the DELIMITER to other char. else they cant find out the end of the EVENT.
DELIMITER //
CREATE EVENT `update_statistics`
ON SCHEDULE EVERY 1 DAY STARTS '2015-08-24 02:00:00'
ON COMPLETION PRESERVE
DO BEGIN
UPDATE statistics
SET km_traveled = (SELECT sum(km)
FROM archived_trips),
passengers_driven = (SELECT sum(passengers)
FROM archived_trips),
trips_taken = (SELECT count(*)
FROM archived_trips)
WHERE id = 1;
UPDATE statistics
SET co_saved = ((SELECT km_traveled
FROM statistics) * 0.215 * (SELECT passengers_driven
FROM statistics))
WHERE id = 1;
END//
Shot in the dark, but try removing the semicolon after END.
I have a MySQL trigger that is being used to call the rsaencrypt function to encrypt a particular value.
DELIMITER $$
CREATE TRIGGER ssninsertencrypt BEFORE INSERT ON redcap_data
FOR EACH ROW
BEGIN
IF new.project_id = (SELECT ProjectID FROM redcap_encryption)
AND new.field_name = (SELECT FieldName FROM redcap_encryption)
THEN
SET #PublicKey = (SELECT PublicKey FROM redcap_encryption WHERE ProjectID = new.project_id);
SET new.value = rsaencrypt(new.value,#PublicKey);
END IF;
END$$
DELIMITER ;
This seems to be inconsistently working/not working so i'd like to insert the completed statement the trigger produces into another table so can see what is being passed to the rsaencrypt or not passed. I thought i could just do a SET #SQL () like the following
SET #SQL =
(IF new.project_id = (SELECT ProjectID FROM redcap_encryption)
AND new.field_name = (SELECT FieldName FROM redcap_encryption)
THEN
SET #PublicKey = (SELECT PublicKey FROM redcap_encryption WHERE ProjectID = new.project_id);
SET new.value = rsaencrypt(new.value,#PublicKey);
END IF;)
I get syntax errors on this and i'm unsure how best to proceed
Thanks
Resolved this a while ago but forgot to post :-
BEGIN
IF new.field_name = (SELECT FieldName FROM redcap_encryption WHERE new.project_id = ProjectID)
THEN
SET #PublicKey = (SELECT PublicKey FROM redcap_encryption WHERE ProjectID = new.project_id);
SET new.value = rsaencrypt(new.value,#PublicKey);
END IF;
END$$
I have created a trigger on a table, and when I try to insert data into it I get MySql error "Error Code: 1054 'unknown column license_key in field list'"
The INSERT I try to do is this:
INSERT INTO LOAD_MASTER(license_key, state, total_pageviews, total_visitors, max_visitors, max_pageviews, ip, secret, read_time, url)
VALUES ("order55555hytgtrfderfredfredfredftyu8ikloi98nhygt6", "preparing", 400, 1000, 200, 400, "202,2,35,36", "Hemmeligheden", 120, "http://google.dk");
The Trigger I have created is supposed to check the TABLE LOAD_STATS for the presence of the IP entered in the LOAD_MASTER table, and then based on the results change a value in another table called LICENSE
My trigger is here:
DELIMITER //
CREATE TRIGGER ip_check AFTER INSERT ON LOAD_MASTER FOR EACH ROW
BEGIN
DECLARE MK varchar(50);
DECLARE MIP varchar(15);
DECLARE LID int(6);
SET MK = license_key;
SET MIP = ip;
SET LID = (
SELECT l.license_id
FROM license_keys k, license l
WHERE l.license_id = k.license_id
AND k.license_key = MK
);
IF (SELECT COUNT(DISTICT(master_ip)) FROM LOAD_STATS WHERE master_ip = MIP AND master_key = MK ) > 3 THEN
UPDATE LICENSE
SET STATE = 0
WHERE license_id = LID;
END IF;
END
//
DELIMITER ;
Any help on why I get this error would be much appriciated.
-Dan
In these lines
SET MK = license_key;
SET MIP = ip;
you probably meant to address NEW.license_key and NEW.ip respectively.
IMHO you can make more succinct. Try this
DELIMITER //
CREATE TRIGGER ip_check
AFTER INSERT ON load_master
FOR EACH ROW
BEGIN
IF (SELECT COUNT(DISTICT(master_ip))
FROM load_stats
WHERE master_ip = NEW.ip
AND master_key = NEW.license_key ) > 3 THEN
UPDATE license
SET state = 0
WHERE license_id =
(
SELECT l.license_id
FROM license_keys k JOIN license l
ON l.license_id = k.license_id
AND k.license_key = NEW.license_key
); -- make sure that this subquery returns only ONE record
END IF;
END //
DELIMITER ;
There is some room for improvement rewriting an UPDATE with JOIN instead of using a subquery
my question is too easy (I guess), below is an example of trigger that I am trying to remove the aliases. I don't know what I am doing wrong I just can't get it right.
DELIMITER #
CREATE TRIGGER StartOfShift BEFORE INSERT ON shift
FOR EACH ROW
BEGIN
IF(NEW.CashierCode NOT IN ( SELECT w.EmployeeID FROM WorksOn as w
JOIN shop AS s ON w.ShopID = s.ShopID
JOIN CashMachineCode AS c ON s.ShopID = c.ShopID
WHERE c.CashMachineCode = NEW.CashMachineCode ))
THEN SET NEW.CashierCode = NULL;
END IF;
END;
The following should be what you are looking for:
DELIMITER #
CREATE TRIGGER StartOfShift BEFORE INSERT ON shift
FOR EACH ROW
BEGIN
IF(NEW.CashierCode NOT IN (
SELECT WorksOn.EmployeeID FROM WorksOn
JOIN shop ON WorksOn.ShopID = shop.ShopID
JOIN CashMachineCode ON shop.ShopID = CashMachineCode.ShopID
WHERE CashMachineCode.CashMachineCode = NEW.CashMachineCode )
) THEN
SET NEW.CashierCode = NULL;
END IF;
END#
DELIMITER ;
I have a table which contains relative large data,
so that it takes too long for the statements below:
SELECT MIN(column) FROM table WHERE ...
SELECT MAX(column) FROM table WHERE ...
I tried index the column, but the performance still does not suffice my need.
I also thought of caching min and max value in another table by using trigger or event.
But my MySQL version is 5.0.51a which requires SUPER privilege for trigger and does not support event.
It is IMPOSSIBLE for me to have SUPER privilege or to upgrade MySQL.
(If possible, then no need to ask!)
How to solve this problem just inside MySQL?
That is, without the help of OS.
If your column is indexed, you should find min(column) near instantly, because that is the first value MySQL will find.
Same goes for max(column) on an indexed column.
If you cannot add an index for some reason the following triggers will cache the MIN and MAX value in a separate table.
Note that TRUE = 1 and FALSE = 0.
DELIMITER $$
CREATE TRIGGER ai_table1_each AFTER INSERT ON table1 FOR EACH ROW
BEGIN
UPDATE db_info i
SET i.minimum = LEAST(i.minimum, NEW.col)
,i.maximum = GREATEST(i.maximum, NEW.col)
,i.min_count = (i.min_count * (new.col < i.minumum))
+ (i.minimum = new.col) + (i.minimum < new.col)
,i.max_count = (i.max_count * (new.col > i.maximum))
+ (i.maximum = new.col) + (new.col > i.maximum)
WHERE i.tablename = 'table1';
END $$
CREATE TRIGGER ad_table1_each AFTER DELETE ON table1 FOR EACH ROW
BEGIN
DECLARE new_min_count INTEGER;
DECLARE new_max_count INTEGER;
UPDATE db_info i
SET i.min_count = i.min_count - (i.minimum = old.col)
,i.max_count = i.max_count - (i.maximum = old.col)
WHERE i.tablename = 'table1';
SELECT i.min_count INTO new_min_count, i.max_count INTO new_max_count
FROM db_info i
WHERE i.tablename = 'table1';
IF new_max_count = 0 THEN
UPDATE db_info i
CROSS JOIN (SELECT MAX(col) as new_max FROM table1) m
SET i.max_count = 1
,i.maximum = m.new_max;
END IF;
IF new_min_count = 0 THEN
UPDATE db_info i
CROSS JOIN (SELECT MIN(col) as new_min FROM table1) m
SET i.min_count = 1
,i.minimum = m.new_min;
END IF;
END $$
DELIMITER ;
The after update trigger will be some mix of the insert and delete triggers.