I am trying to raise an error within my trigger if a person's lastname is not in full caps or if the first letter of their firstname is not in caps . But my code doesn't seem to raise an alarm if those scenarios are met. Can someone please explain to me how I can do so ?
This is my table:
DROP TABLE IF EXISTS people;
CREATE TABLE IF NOT EXISTS people (
Id_People smallint(5) unsigned NOT NULL AUTO_INCREMENT,
Firstname varchar(20) NOT NULL,
Lastname varchar(20) NOT NULL,
Birth DATE,
PRIMARY KEY (Id_People)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1002 ;DROP TRIGGER IF EXISTS insert_people;
This is my trigger
DROP TRIGGER IF EXISTS insert_people;
DELIMITER $$
CREATE TRIGGER insert_people BEFORE INSERT ON people
FOR EACH ROW BEGIN
IF NOT (NEW.Lastname like UCASE ) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'the lastname must be in full caps ';
ELSE IF
NOT (NEW.Firstname like ucfirst ) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'the first letter of the firstname must be in caps';
END IF;
END IF;
END$$
You code to find those is not correct.
Please add DELIMITER
to the trigger code, as it isn't necessary in the example site
DROP TABLE IF EXISTS people; CREATE TABLE IF NOT EXISTS people ( Id_People smallint(5) unsigned NOT NULL AUTO_INCREMENT, Firstname varchar(20) NOT NULL, Lastname varchar(20) NOT NULL, Birth DATE, PRIMARY KEY (Id_People) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1002 ;DROP TRIGGER IF EXISTS insert_people;
CREATE TRIGGER insert_people BEFORE INSERT ON people FOR EACH ROW BEGIN IF NOT (NEW.Lastname=BINARY UPPER(NEW.Lastname)) THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'the lastname must be in full caps '; ELSE IF NOT (ASCII(NEW.Firstname) BETWEEN 65 AND 90 ) THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'the first letter of the firstname must be in caps'; END IF; END IF; END
INSERT INTO people VALUES (NULL,'Hallo','Kitty','1980-01-01')
the lastname must be in full caps
INSERT INTO people VALUES (NULL,'hallo','KITTY','1980-01-01')
the first letter of the firstname must be in caps
db<>fiddle here