I get an error (1064) when attempting to run the following... (MySql 5.5.9)
query:
CREATE TRIGGER clearChat AFTER INSERT ON chat
FOR EACH ROW
BEGIN
DELETE p.* FROM chat p LEFT JOIN (SELECT t.id FROM chat t ORDER BY t.id DESC LIMIT 50) x ON x.id = p.id WHERE x.id IS NULL
END;
the error is:
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 'END' at line 5
Any assistance would be great.
Last Edit: Updated to show the 'FOR EACH ROW' and 'BEGIN'
You're missing FOR EACH ROW before DELETE: http://dev.mysql.com/doc/refman/5.1/en/create-trigger.html
Edit: There are more issues. The correct syntax is below:
delimiter |
CREATE TRIGGER clearChat AFTER INSERT ON chat
FOR EACH ROW BEGIN
DELETE p.* FROM chat p LEFT JOIN (SELECT t.id FROM chat t ORDER BY t.id DESC LIMIT 50) x ON x.id = p.id WHERE x.id IS NULL;
END;
|
delimiter ;
Edit 2:
I don't think that query is allowed to be in a trigger at all based on this http://dev.mysql.com/doc/refman/5.1/en/faqs-triggers.html#qandaitem-B-5-1-9:
A trigger can access both old and new
data in its own table. A trigger can
also affect other tables, but it is
not permitted to modify a table that
is already being used (for reading or
writing) by the statement that invoked
the function or trigger.
Since you aren't using OLD or NEW, I don't think you can modify chat since the trigger is triggered on inserts to chat.
I had the same problem with the following statement, it ALWAYS gave me a syntax error on even this simplified delete statement (originally was DELETE FROM APP_CACHE_VIEW WHERE APP_UID = OLD.APP_UID;):
CREATE TRIGGER APPLICATION_DELETE BEFORE DELETE ON APPLICATION
FOR EACH ROW
BEGIN
DELETE FROM APP_CACHE_VIEW;
END
If I changed the SQL command to the following then it WORKED but I don't understand why:
DELIMITER $$
CREATE TRIGGER APPLICATION_DELETE BEFORE DELETE ON APPLICATION
FOR EACH ROW
BEGIN
DELETE FROM APP_CACHE_VIEW;
END$$
DELIMITER ;
Related
I have an error in my After Update Trigger. I can't figure out where I went wrong. Do you have any suggestions? The trigger is supposed to change the status of another related table (users) when I change the main table I applied my trigger (sites)
CREATE DEFINER=`root`#`localhost` TRIGGER `update_sites_contacts` AFTER UPDATE ON `sites`
FOR EACH ROW
IF NEW.is_mlgu_handled = 1 AND OLD.is_mlgu_handled != NEW.is_mlgu_handled THEN
UPDATE commons_db.users
SET commons_db.users.status = 0
WHERE user_id in (
SELECT user_id FROM
commons_db.sites
LEFT JOIN commons_db.user_organizations using (site_id)
LEFT JOIN commons_db.users using (user_id)
WHERE ewi_recipient = 1
AND org_name in ('lewc','blgu','mlgu')
);
END IF;
Compound statements such as IF...THEN...END IF can only be used inside a BEGIN...END block.
I am trying to run a small trigger with following query in Mysql :
create trigger sample_trigger after delete on my_network
for each row
begin
update (select active from my_network_ref where id=old.rid limit 1) vnetr set vnetr.active=0;
end;
After running these trigger in MySQL Workbench , i ma getting following error
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1288: The target table vnetr of the UPDATE is not updatable
SQL Statement:
CREATE DEFINER = CURRENT_USER TRIGGER `mam_db`.`sample_trigger ` AFTER DELETE ON `my_network` FOR EACH ROW
BEGIN
update (select active from my_network_ref where id=old.rid limit 1) vnetr set vnetr.active=0;
END
Any syntax error or structural problems ? Please help
In MySQL, you cannot update a subquery. Instead:
update my_network_ref
set active = 0
where id = old.rid
limit 1;
Normally, you would use order by when using limit.
I have 2 tables. The Data of profit table will come from the bet table. Therefore, automating INSERT to profit table whenever new record got added on bet table will be convenient.
How can I make sure the trigger will insert only data that doesn't exist on the profit table?
Here's what I did so far
DELIMITER $$
DROP TRIGGER IF EXISTS tsuika $$
CREATE TRIGGER keisan BEFORE INSERT ON profitdb
FOR EACH ROW BEGIN
INSERT INTO `profitdb`(`BetID`,`DateTime`, `PlayerID`,
`Profit`,`SubAgentID`,`SubAgentRisk`,`AgentID`,`AgentRisk`)
SELECT `betdb`.`BetID`,`betdb`.`DateTime`,`betdb`.`PlayerID`,
(`BetAmount`-Payout`),`playerdb`.`SubAgentID`,`subagentdb`.`Risk`,
`agentdb`.`AgentID`,`agentdb`.`Risk` FROM `betdb` LEFT JOIN `playerdb` ON
`betdb`.`PlayerID` = `playerdb`.`PlayerID` LEFT JOIN `subagentdb` ON
`subagentdb`.`SubAgentID` = `playerdb`.`SubAgentID` LEFT JOIN `agentdb` ON
`agentdb`.`AgentID` = `playerdb`.`AgentID`
END
$$
DELIMITER;
I referred to this link MySQL trigger On Insert/Update events regarding making triggers with INSERT but still getting this 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 'END' at line 5
You must end all executable statements with default delimiter, i.e. ';' semi-colon in a code block.
You missed a semi-colon after select statement.
And you are trying to drop an irrelevant trigger 'tsuika'. It should be the current trigger you are trying to create or re-define. Change it to 'keisan'.
DELIMITER $$
DROP TRIGGER IF EXISTS keisan; $$ -- <--- this was with wrong trigger name. corrected.
CREATE TRIGGER keisan BEFORE INSERT ON profitdb
FOR EACH ROW BEGIN
INSERT INTO `profitdb`(`BetID`,`DateTime`, `PlayerID`, `Profit`,
`SubAgentID`,`SubAgentRisk`,`AgentID`,`AgentRisk`)
SELECT `betdb`.`BetID`, `betdb`.`DateTime`, `betdb`.`PlayerID`,
( `BetAmount` - `Payout` ), `playerdb`.`SubAgentID`,
`subagentdb`.`Risk`, `agentdb`.`AgentID`, `agentdb`.`Risk`
FROM `betdb`
LEFT JOIN `playerdb`
ON `betdb`.`PlayerID` = `playerdb`.`PlayerID`
LEFT JOIN `subagentdb`
ON `subagentdb`.`SubAgentID` = `playerdb`.`SubAgentID`
LEFT JOIN `agentdb`
ON `agentdb`.`AgentID` = `playerdb`.`AgentID`; -- <--- missed
END;
$$
DELIMITER ;
I've recently started working on MySQL n right now i want to create a trigger but MySQL is returning an error in my syntax.
delimiter $$;
create trigger abc after insert on ratings
for each row
begin
set #n1 = select avg(rating) from ratings join users where ratings.uname=users.uname
and ratings.bookid=new.bookid users.`type`='admin' or users.`type`='critic';
update books set avgcriticrating = #n1 where bookid=new.bookid;
end;
The select statement runs perfectly when fired on its own but gives an error when i use it inside of the trigger.
Here's the error that MySQL gives
#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 'select avg(rating) from ratings join users where ratings.uname=users.uname an' at line 4
Both the books and ratings table contain a field called bookid.
please help
If you want a single value from a select statement, the statement has to be in (brackets).
set #n1 = (select avg(rating) from ratings join users where ratings.uname=users.uname
and ratings.bookid=new.bookid users.`type`='admin' or users.`type`='critic');
The next error will occur at new.bookid users - there might be an and or an or missing.
The program I am working on has a poorly designed backend and basically there are two different tables needed to hold the exact same information. I am trying to write a trigger that will update table B with the same information that was just updated in table A. Oh and this is using a MYSQL database
I am not sure if I am just having a syntax error or if I am missing concepts, any help would be much appreciated... here is what I have as of yet
DELIMITER $$
DROP TRIGGER IF EXISTS after_update_A;
CREATE TRIGGER `after_update_A`
AFTER UPDATE ON `A` FOR EACH ROW
BEGIN
UPDATE TABLE B
SET username = NEW.username
, password = NEW.password
, email = NEW.email
WHERE id = NEW.id
END
$$
DELIMITER ;
And the errors I get are all some what similar to this...
ERROR 1064 (42000): 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 '= users.username
, B.email = users.email
D' at line 4
You need to close every statement inside the trigger with a ;, and I do mean every.
CREATE TRIGGER `after_update_A` AFTER UPDATE ON `A` FOR EACH ROW
BEGIN
UPDATE TABLE B
SET username = NEW.username
, password = NEW.password
, email = NEW.email
WHERE id = NEW.id; //<<-----------
END $$
I have the feeling that you are compiling .sql as .sh
the trigger is .sql and should be executed using
mysql-batch-commands.html
p.s. What #Johan says is obviously correct as well
p.s.2 Now I see another error:
you will need a delimiter between the drop and create statements