Removing "The " with an IF statement in MYSQL Before Insert Trigger - mysql

Hello helpful world of Stack Overflow!
I am brand new to Triggers, but am having fun using them in my new project.
I have already gotten a few done, but am struggling with setting up a sort field by removing "The " from the beginning of a name.
Being new to this, I am using PHP MyAdmin to build my triggers. So I have it set to my 'aartist' table, Before Insert. The 2 fields are artist_name & artist_sort. This is the full code I've tried with no luck.
BEGIN
IF (left(new.artist_name,4) != "The ")
THEN
set new.artist_sort = new.artist_name
ELSE
set new.artist_sort = right(new.artist_name,len(new.artist_name)-4)
END IF;
END
I've also tried stripping it back for something more simple, still without luck. The IF statement seems to not work in the TRIGGER.
BEGIN
IF (new.artist_name != new.artist_sort) THEN
set new.artist_sort = new.artist_name
END IF;
END
The Error PHPMyAdmin gives is:
One or more errors have occurred while processing your request:
The following query has failed: "CREATE DEFINER=`####`#`localhost` TRIGGER `AddArtist` BEFORE INSERT ON `aartist` FOR EACH ROW BEGIN IF (new.artist_name != new.artist_sort) THEN set new.artist_sort = new.artist_name END IF; END"
MySQL said: #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 IF; END' at line 4
Thank you, as always, for any help in figure out what I might be doing wrong with this.

Did some more digging around and found some SQL code that I modified, so instead of using the PHPMyAdmin Trigger Form, I just adjusted the SQL code and it worked! Not 100% sure why it worked with this, but the main change was the Delimiter piece in the code and additional ;s, so my guess is that had something to do with it.
MySQL trigger if condition exists
DROP TRIGGER IF EXISTS AddArtist;
DELIMITER $$
CREATE TRIGGER AddArtist BEFORE UPDATE ON `aartist`
FOR EACH ROW BEGIN
IF (left(NEW.artist_name,4) = 'The ') THEN
SET NEW.artist_sort = right(NEW.artist_name, CHAR_LENGTH(NEW.artist_name) - 4);
ELSE
SET NEW.artist_sort = NEW.artist_name;
END IF;
END$$
DELIMITER ;

Related

MySql Syntax error when creating trigger to insert calculated value

This is my first time trying to create a MySql trigger but I'm running into syntax errors that I can't identify. I am trying to have the trigger insert a calculated value when a row is updated. Below is my code, but I keep getting syntax errors when I try to execute it, and I cannot see where the error is. Can someone please look, what am I doing wrong?
DROP TRIGGER ins_cop_dhw;
CREATE TRIGGER ins_cop_dhw BEFORE INSERT ON `2017010001_data`
FOR EACH ROW
BEGIN
IF (NEW.`_007E` = 1 AND `_00A2` > 0) THEN
SET NEW.`_00B7` = NEW.`_0096` / NEW.`_00A2`;
END IF;
IF (NEW.`_007D` = 1 AND `_00A2` > 0) THEN
SET NEW.`_00B8` = (NEW.`_0096` / NEW.`_00A2`);
END IF;
END;
Here is the error I get in MySQL Workbench, but it's not much help. :/
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 5
MySQL Workbench highlights the end of line 6 as the start of the syntax error.
As said by #Akina I was missing the delimiter statements. Here is fixed code.
delimiter //
DROP TRIGGER ins_cop_dhw;
CREATE TRIGGER ins_cop_dhw BEFORE INSERT ON `2017010001_data`
FOR EACH ROW
BEGIN
IF (NEW.`_007E` = 1 AND NEW.`_00A2` > 0) THEN
SET NEW.`_00B7` = NEW.`_0096` / NEW.`_00A2`;
END IF;
IF (NEW.`_007D` = 1 AND NEW.`_00A2` > 0) THEN
SET NEW.`_00B8` = (NEW.`_0096` / NEW.`_00A2`);
END IF;
END//
delimiter ;
As per the advice of #Akina I read the Create Procedure documentation from the dev.mysql.com site and the below quote highlighted the importance of using delimiter
The example uses the mysql client delimiter command to change the statement delimiter from ; to // while the procedure is being defined. This enables the ; delimiter used in the procedure body to be passed through to the server rather than being interpreted by mysql itself. See Section 25.1, “Defining Stored Programs”.

Keep getting error #1064 when I try to create a trigger in phpmyadmin

I'm trying to create a trigger to import a field from another table when a new row is inserted but get the error message:
#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
I'm using the sql tab in phpmyadmin to enter the code.
I've read all the related questions on here, perused several other sites with articles on mysql triggers but can't find anything that helps.
I've tried with // at the end of the line prior to END//
I've tried with ; at the end of the line prior to END//
Nothing seems to work.
My trigger code:
DELIMITER //
CREATE TRIGGER towns2_before_insert
BEFORE INSERT ON towns2 FOR EACH ROW
BEGIN
SET NEW.regionid = (Select RegionID from world_map
where (world_map.X = new.mapx) AND (world_map.Y = new.mapy))
END//
DELIMITER ;
Both towns2 and world_map tables exist and have the required columns.
As far as I can tell this should fetch the RegionID field from any row in the world_map table that has X & Y fields matching the new mapx and mapy columns but all I can get is this error.
No doubt I've got a really simple error in this somewhere but for the life of me I can't find it and all help is greatfully recieved.
UPDATE
Re-created the code as shown by #Chris J below in the myphpadmin trigger form, ignored the big red X against the END statement and hit the GO button - it worked so I guess the problem was not with my code but with my chosen method of creating the trigger. The code as created by phpmyadmin is shown below:
CREATE TRIGGER `towns2_before_insert` BEFORE INSERT ON `towns2`
FOR EACH ROW BEGIN
SET NEW.regionid =
(Select RegionID from world_map
where (X = new.mapx) AND (Y = new.mapy));
END
Although you've mentioned it already, the following works perfectly for me:
DELIMITER //
CREATE TRIGGER towns2_before_insert
BEFORE INSERT ON towns2 FOR EACH ROW
BEGIN
SET NEW.regionid = (Select RegionID from world_map
where (world_map.X = new.mapx) AND (world_map.Y = new.mapy));
END//
This is based on a guess of your schema from your query. The trigger now exists and is visible in Heidi as follows:

MySQL before update trigger an insert syntax error using PhpMyAdmin

CREATE TRIGGER question_preserver BEFORE UPDATE ON bank
FOR EACH ROW
BEGIN
IF TRIM(NEW.question) != TRIM(OLD.question) THEN
INSERT INTO bank_question_history (id,old_question) VALUES (OLD.id,OLD.question)$$
END IF$$
END$$
I am inserting that query into Mysql using PHPMyAdmin's SQL window, using a delim of $$. I get an error 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 5
I'm sure it's something obvious and I'm just missing it, but no matter what I try I can't get it to work. The error is not helpful at all, and from what I have researched I am doing this exactly like 4-5 examples I found.
Any help would be greatly appreciated, thank you!
Go figure I figured it out right after asking.
CREATE TRIGGER question_preserver BEFORE UPDATE ON bank
FOR EACH ROW
BEGIN
IF TRIM(NEW.question) != TRIM(OLD.question) THEN
INSERT INTO bank_question_history (id,old_question) VALUES (OLD.`id`,OLD.`question`);
END IF;
END$$
You have to use ; to break each statement/command and your delim $$ to end the entire trigger.

MySQL Create Trigger Syntax Error near END

When I try and run my table/trigger creation script, I get the following error:
ERROR 1064 (42000) at line 19: 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 'SET NEW.fines_remaining = NEW.total_fines;
END IF;
END' at line 5
The following is the CREATE TRIGGER code which is causing the error:
DELIMITER //
CREATE TRIGGER fines_trigger BEFORE INSERT ON student
FOR EACH ROW
BEGIN
IF TUPLE.fines_remaining > TUPLE.total_fines
SET NEW.fines_remaining = NEW.total_fines;
END IF;
END; //
DELIMITER ;
I can't figure out why this is happening, I feel like the syntax is fine, but it's obviously not since there's some error being thrown.
EDIT: Immediately after posting this I notice that I still have these TUPLE 'variables' that I was using before I figured out about 'OLD' and 'NEW'. I'm changing them and will update momentarily.
You are missing then after the if condition
DELIMITER //
CREATE TRIGGER fines_trigger BEFORE INSERT ON student
FOR EACH ROW
BEGIN
IF new.fines_remaining > old.total_fines then
SET NEW.fines_remaining = NEW.total_fines;
END IF;
END; //
DELIMITER ;
Also I have changed TUPLE with new and old in the above trigger, you may need to adjust the logic as per your need.

I am using following PHP code for trigger creation but always get error, please help me to resolve it

I am using following PHP code for trigger creation but always get error, please help me to resolve it.
$link = mysql_connect('localhost','root','rainserver');
mysql_select_db('information_schema');
echo $trgquery = "DELIMITER $$ DROP TRIGGER `update_data` $$ CREATE TRIGGER `update_data` AFTER UPDATE on `jos_menu` FOR EACH ROW BEGIN
IF (NEW.menutype != OLD.menutype) THEN
INSERT INTO jos_menuaudit set menuid=OLD.id, oldvalue = OLD.menutype, newvalue = NEW.menutype, field = 'menutype';
END IF;
IF (NEW.name != OLD.name) THEN
INSERT INTO jos_menuaudit set menuid=OLD.id, oldvalue = OLD.name,
newvalue = NEW.name, field = 'name';
END IF;
IF (NEW.alias != OLD.alias) THEN
INSERT INTO jos_menuaudit set menuid=OLD.id, oldvalue = OLD.alias,
newvalue = NEW.alias, field = 'alias';
END IF;
END$$ DELIMITER ;";
echo "<br>";
//$trig = mysqli_query($link,$trgquery) or die("Error Exist".mysqli_error($link));
$trig = mysql_query($trgquery) or die("Error Exist".mysql_error());
I get the error as:
Error ExistYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$$ DROP TRIGGER `update_data` $$ CREATE TRIGGER `update_data` AFTER UPDATE on `j' at line 1
PLease help me to create my trigger...
The error tells you exactly what's wrong. DELIMITER $$ DROP ... isn't a valid statement. Part of the reason for this is delimiter is a MySQL command, not a statement. Another reason is the "$$" in "DELIMITER $$" is part of the command and thus doesn't terminate the command. You still need to include the old delimiter.
According to the documentation, mysql_query doesn't support multiple statements, so you'll have to issue separate queries for each one. Better yet, switch to the newer mysqli driver, whose multi_query function works for multiple statements in a single query. mysqli also supports prepared statements, which doesn't matter so much in your example, but does in most other situations. PDO is even newer and also supports prepared statements, and I believe the PDO MySQL driver supports multi-queries.
As an alternative to triggers, row-based binary logging will record changes made to tables in a database.