Check the code below to find out the mistake - mysql

Code in MySQL:
create trigger insert_adm
after
insert on table_student for each row BEGIN
INSERT INTO
Admission(ID_Student, registration_date)
VALUES
(new.ID_Student, CURDATE()) END;
Error output:
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 'END' at line 7.
I have no clue what to do
Whats wrong in this mysql code?

Try END$$ instead of END. Some explanation on delimiters here: Delimiters in MySQL
Final code:
CREATE TRIGGER insert_adm
AFTER INSERT ON table_student
FOR EACH ROW
BEGIN
INSERT INTO Admission (ID_Student, registration_date)
VALUES (new.ID_Student, CURDATE());
END$$

Related

create trigger syntax issue migrating mysql from 5.6 to 8

CREATE TRIGGER `set_avatar_null` BEFORE UPDATE ON `mybb_users`
FOR EACH ROW begin
if new.uid = -1 then
Insert Into Log (uid, Page, Params, Time) values (new.uid, 'set_avatar_null',new.avatar, NOW());
set new.avatar = '', new.avatardimensions='';
end if;
end
this syntax works fine in mysql 5.6, but in 8.0 throws this exception:
#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 4
Use the DELIMITER command to change the delimiter before the statement. See the db-fiddle.

Getting Error #1064 Near End When Trying to Create a Trigger

DELIMITER $$
CREATE TRIGGER DELETETRIGGER AFTER DELETE ON WORKLOG
FOR EACH ROW
BEGIN
INSERT INTO WORKLOGCOPY (Log_ID, Order_ID, Employee_ID, Client_ID,
Work_Completed, Hours_Taken, Date_Logged)
VALUES(OLD.Log_ID, OLD.Order_ID, OLD.Employee_ID, OLD.Client_ID,
OLD.Work_Completed, OLD.Hours_Taken, OLD.Date_Logged)
END$$
DELIMITER ;
I am getting the 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 6
Have tried messing around with the syntax to match examples I have seen but with no luck.
Using MySQL on phpmyadmin if that helps.
Thank you.
You are missing ; at the end of INSERT INTO
DELIMITER $$
CREATE TRIGGER DELETETRIGGER AFTER DELETE ON WORKLOG
FOR EACH ROW
BEGIN
INSERT INTO WORKLOGCOPY (Log_ID, Order_ID, Employee_ID, Client_ID,
Work_Completed, Hours_Taken, Date_Logged)
VALUES(OLD.Log_ID, OLD.Order_ID, OLD.Employee_ID, OLD.Client_ID,
OLD.Work_Completed, OLD.Hours_Taken, OLD.Date_Logged);
END$$
DELIMITER ;

create trigger not working in mysql

i am trying to write a trigger which validates insert statements before inserting in the table
table structure:
This is the code that i wrote
DELIMITER
$$
CREATE TRIGGER check_date_format
INSERT BEFORE ON
job_histry FOR EACH ROW
BEGIN
SET #bool=NEW.end_date LIKE '--/--/----';
IF #bool THEN
INSERT
INTO
job_histry
VALUES(
NULL,
NEW.start_date,
NEW.end_date,
NEW.job_id,
NEW.department_id
) ;
END IF ;
END
$$
This is the error that i received:
#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 'INSERT BEFORE ON job_histry FOR EACH ROW BEGIN SET #bool=NEW.end_date LIKE '' at line 2
why is this happening?
I am using localhost xampp server.
You have the trigger type (INSERT) and time (BEFORE) in the wrong order.
It should read
BEFORE INSERT ON job_histry

How to check SQL State in a if statement?

I'm trying to commit an insert in a trigger based on a sql state based on success of a previous query in a trigger. But I'm getting a syntax error.
Can anyone point out what's wrong I'm doing here.
SET #OLDTMP_SQL_MODE=##SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';
DELIMITER //
CREATE TRIGGER `insert_exp_register` AFTER INSERT ON `trnInvDtl` FOR EACH ROW begin
insert into trninvdtl_exportregister(comp_code,
inv_type,
inv_no,
item_srl_no) values(
new.comp_code,
new.inv_type,
new.inv_no,
new.item_srl_no);
if (sqlstate='0000') then
rollback;
end if;
end//
DELIMITER ;
SET SQL_MODE=#OLDTMP_SQL_MODE;
Here's the error message for syntax error.
[Window Title]
xxxx PC: Error
[Content]
SQL 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 'sqlstate='0000') then
rollback;
end if;
end' at line 11
[OK]
[Footer]
Find some help on this error

SQL syntax error in mysql when creating trigger

I have a problem when creating a trigger in mysql i get 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 '' at line 7
This is my first time creating triggers for mysql help
Here is the code:
CREATE TRIGGER insertAfterUser AFTER INSERT ON MEMBERS
FOR EACH ROW
BEGIN
INSERT INTO blagajna (MemberId,StanjeRacuna) VALUES(12,0.0);
END;
You need to set the delimiter
delimiter //
CREATE TRIGGER insertAfterUser AFTER INSERT ON MEMBERS
FOR EACH ROW
BEGIN
INSERT INTO blagajna (MemberId,StanjeRacuna) VALUES(12,0.0);
END;//
delimiter ;
CREATE TRIGGER `insertAfterUser ` BEFORE INSERT ON `members` FOR EACH ROW BEGIN INSERT INTO blagajna (MemberId,StanjeRacuna) VALUES('12','0.0'); END;