Can't create a trigger into PHPMyAdmin - mysql

I just want to create a trigger for my phpmyadmin. Basically the intended function is whenever I update a credit column of my finance table, the balance column will be automatically updated for each row.
My program is:
CREATE TRIGGER UPDATE_BALANCE AFTER UPDATE financeofstudents
FOR EACH ROW
BEGIN
IF financeofstudents.credit > 0
AND
financeofstudents.credit <= financeofstudents.debit
THEN
UPDATE financeofstudents SET financeofstudents.balance = financeofstudents.debit- financeofstudents.credit;
END IF;
END
but it gives me this error:
Error
SQL query:
CREATE TRIGGER UPDATE_BALANCE AFTER UPDATE financeofstudents
FOR EACH ROW
BEGIN
IF financeofstudents.credit > 0
AND
financeofstudents.credit <= financeofstudents.debit
THEN
UPDATE financeofstudents SET financeofstudents.balance = financeofstudents.debit-financeofstudents.credit
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'financeofstudents
FOR EACH ROW
BEGIN
IF financeofstudents.credit > 0
AND
f' at line 1
I don't know where is the problem. Expert guys, please help me.

The error message is a useful hint here. It says .. for the right syntax to use near 'some text from your query starting with the first thing it didn't understand'.
In your case, you need the word ON before the name of the table.
CREATE TRIGGER update_balance
AFTER UPDATE
ON financeofstudents
FOR EACH ROW
If you observed that MySQL error messages are hard to interpret, you would be correct.

Related

Error adding update trigger to select columns in a table

I am trying to add a trigger, when a table is updated (except for two columns in that table), it updates another table. The query I used is given below:
CREATE TRIGGER my_trigger
BEFORE UPDATE ON town_1a2b3c4d
BEGIN
IF NOT UPDATE (is_killed) AND NOT UPDATE (is_executed)
BEGIN
UPDATE town_details SET game_index = game_index + 1 WHERE town_id = '1a2b3c4d';
END;
END;
But it gives me this error:
MySQL said
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'BEGIN
IF NOT UPDATE (is_killed) AND NOT UPDATE (is_executed)
BEGIN...' at line 3

MySQL: Trying to Create a Trigger Where Another Table is Updated

I'm trying to create a trigger whereby an insertion on one table updates another. This is my SQL Query:
CREATE TRIGGER makePayment AFTER INSERT ON Payments FOR EACH ROW
BEGIN
UPDATE Invoice
SET InvoiceClientPaid = SUM(InvoiceClientPaid + NEW.PaymentAmt)
WHERE InvoiceID = NEW.PaymentInvoiceID;
END;
No matter what I do I get the following error:
Error Code: 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 6
I don't think it's related to the SUM, because trying a basic = 1 on the SET command gives me the exact error. There is no '' at line 6 which is very confusing?
If you're entering this query directly into MySQL you will need to change the delimiter prior to the query using (e.g.) DELIMITER //, otherwise it thinks the query ends at the ; at the end of your UPDATE statement. MySQL then sees an END with nothing before it and complains about the nothing (''). So try this:
DELIMITER //
CREATE TRIGGER makePayment AFTER INSERT ON Payments FOR EACH ROW
BEGIN
UPDATE Invoice
SET InvoiceClientPaid = SUM(InvoiceClientPaid + NEW.PaymentAmt)
WHERE InvoiceID = NEW.PaymentInvoiceID;
END; //
DELIMITER ;

after-update trigger in mysql 5.5.40

It's my first time that I try to use trigger on mysql (and generally I don't use mysql so much).
The version of mysql that I'm using is 5.5.40 and the code I'm using to create the trigger is:
CREATE TRIGGER updateTrigger
AFTER UPDATE ON tab1
FOR EACH ROW
BEGIN
UPDATE tab2
SET field1 = NEW.field1
WHERE field2 = NEW.field2;
END;
Where field1 and field2 are two fields in both tables (tab1 and tab2) both archer(120).
When I try to execute this code I receive an 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 7
(line 7 is near "WHEN" keyword).
I checked several answer on stack overflow and other web sites, I tried with and without delimiters, and I still not able to create a trigger.
What's my syntax error?
Is there some way to have some more accurate mysql debugging? Actually mysql answer (like I just posted) is just "there is an error (1064)" but it doesn't let me know what is wrong.
you have to change the delimiter to use it in your sql statement :
DELIMITER //
CREATE TRIGGER updateTrigger
AFTER UPDATE ON tab1
FOR EACH ROW
BEGIN
UPDATE tab2
SET field1 = NEW.field1
WHERE field2 = NEW.field2;
END//
DELIMITER ;

query to create trigger in mysql

I have a table named sample_table and its columns are col_a, col_b, col_c.
I am trying to create a trigger in MySQL 5.6 which will update col_c(as concat(col_a,col_b)) on insertion of a row. Query which I have written for this is:
create trigger trg_sample_table
after insert on sample_table
for each row
begin
set col_c=concat(col_a,col_b)
end;
While running this sql, the error I am getting is:
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 'end' at line 6.
Could any one of you please help me to create a trigger as I need?
You need to make it a before insert. Try this:
create trigger trg_sample_table before insert on sample_table for each row
begin
set new.col_c=concat(new.col_a,new.col_b);
end;
You cannot modify the row that you just inserted in an after insert trigger. If you want to modify it, it needs to be done in a before insert.

Correct MySQL Syntax for this trigger?

Trying to create a simple trigger that after a table update, it checks if a count (InvalidLogins) is over 4, and if so, sets the field 'LockedOut' to 1. Below is how I am attempting to create the trigger, this is my first trigger for MySQL though and I can't get it to work.
CREATE TRIGGER update_user_lockout after update ON members
FOR EACH ROW SET LockedOut = 1 where invalidLogins < 4
The error that MySQL returns is:
Error Code: 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 '.LockedOut = 1 where invalidLogins < 4' at line 2
delimiter |
CREATE TRIGGER update_user_lockout before update ON members
FOR EACH ROW
IF NEW.invalidLogins < 4 THEN
SET NEW.LockedOut = 1;
END IF;
END
|
delimiter ;
The current record can be referenced with NEW
Choose another delimiter than ;. Otherwise the trigger definition will end at the first ; which is too early
Use a before trigger to change values before updating