When creating a Stored Procedure, you do not need COMMIT;
.
I have never used it for Stored Procedures.
No one should have to because Stored Procedures are stored in a MyISAM table (mysql.proc
)
Please remove the COMMIT;
before the END //
and try again.
I would also change line to say
DELIMITER $$
and the end clause to
END $$
and try again
I thought you were working in the mysql client. If your problem is SQLFiddle, here is post from StackOverflow by its Author (Execute triggers stored procedures on SqlFiddle. Mysql)
Based on it, stop using DELIMITER
altogether. Look for the fourth button under the query pane.
I just installed MySQL 5.6.22 for Windows on my laptop and ran your code as is. It Works !!!
The only culprit left would be phpmyadmin. It cannot handle delimiters against the latest version of MySQL, which is 5.6. You may need an upgrade of phpmyadmin.
The immediate issue with your first statement is that it has BEGIN without a corresponding END. In this case, END should be at the very end of the CREATE TRIGGER statement.
Once you add the END keyword, the statement becomes technically correct, but you will have trouble executing it in MySQL. The reason is that semicolons, while being standard statement delimiters in SQL, are also treated by MySQL in a special way. MySQL splits the input text at semicolons and sends each part of the text to the server separately. The server thus receives incomplete, syntactically incorrect pieces and returns an error. See here for more details:
So, to prevent MySQL from doing that, you need to instruct it to use a different symbol as a delimiter – that is what the DELIMITER command is for. Consequently, your second attempt should be something like this:
DELIMITER $$
CREATE TRIGGER `estatecat_piece`
AFTER INSERT ON `estate`
FOR EACH ROW
BEGIN
UPDATE estate_category set piece = (Select count(*) from estate where estate.estatecat_id=new.estatecat_id);
END$$
DELIMITER ;
The first DELIMITER command tells MySQL to parse the input text until $$
is encountered from that point on. Your CREATE TRIGGER should, therefore, end with the $$
symbol so that MySQL can consume it in its entirety and send the whole statement to the server. Finally, the second DELIMITER command restores the standard delimiter for MySQL to resume normal processing of your commands.
There is also a simpler solution, but it works only in cases similar to yours. The trigger body in your case consists of a single statement. That allows you to omit the keywords BEGIN and END from your statement and, as a result, avoid using the DELIMITER command at all:
CREATE TRIGGER `estatecat_piece`
AFTER INSERT ON `estate`
FOR EACH ROW
UPDATE estate_category set piece = (Select count(*) from estate where estate.estatecat_id=new.estatecat_id);
The above trigger will work exactly the same.
On a different note, your UPDATE statement may have a flaw: as currently written, it will update the piece
column in every row with the same row count – that of the category matching the inserted row. If the table has multiple rows, each for a different category, you probably need to introduce a filter on new.estatecat_id
to your UPDATE statement, similar to the filter in the subquery, in order to update only the corresponding piece
value:
UPDATE
estate_category
SET
piece = (
SELECT
COUNT(*)
FROM
estate
WHERE
estate.estatecat_id=new.estatecat_id
)
WHERE
... /* specify the necessary column name */ = new.estatecat_id
;
Best Answer
Based on your own answer, the issue appears to have been with this line:
The problem is the
`triedTime`
reference. According to the manual:That is to say, without either
NEW
orOLD
you cannot access columns of the affected rows. So, in the above shown line you probably want to use theNEW
keyword:Your own method of looking the value up in the table works as well, only it means an extra table hit, which may affect performance.