I'm trying to create a couple of triggers for a homework assignment that seems simple enough, but I've run into one problem after another. Myself and my classmates have been tasked with creating an Online Food Ordering Database in PHPMyAdmin. It's pretty basic stuff as an introductory course and I did take an SQL course a couple of years ago at a previous institution, so it's all coming back to me.
Despite my previous experience, I've never dealt with triggers before nor have I used PHPMyAdmin but I'm getting used to it. My database has 8 tables but the only two that matter for this situation are tbl_employee and tbl_employee_audit. Our assignment had us create tbl_employee_audit with an auto-incrementing "ID" which is the primary key and a varchar(100) for footnotes. We are supposed to create two triggers: one that indicates the newest addition to the employee table and one that indicates when an employee has been deleted from the table. We record these changes in the tbl_employee_audit table in the format of "New employee with ID = (insert ID) was added on (insert date)". Seems simple enough...
As I stated in the title of this post, I've developed a piece of code that works when I run it in the SQL tab of PHPMyAdmin, but when I try to put it into a trigger, I get an error message saying the syntax for line 2 (which is @varID) is incorrect. Here is what I have:
SET @varDate = CURRENT_DATE();
SET @varID = (SELECT emp_ID FROM tbl_employee WHERE emp_ID = (SELECT MAX(emp_ID) FROM tbl_employee));
SET @string1 = 'New employee with ID = ';
SET @string2 = ' was added on ';
SET @var1 = CONCAT(@string1, @varID, @string2, @varDate);
INSERT INTO tbl_employee_audit(audit_data) VALUES (@var1);
I tried creating my own trigger in the SQL tab but didn't get that to work so I've been trying to use the "Create New Trigger" function found in the Triggers tab. I called the trigger "after_employee_insert", set the Table to "tbl_employee", set the Time to "AFTER", set the Event to "INSERT", and tried to get my code to work in the definition box.