引发错误在 MySQL 触发器中不起作用
Raising Error not working in MySQL trigger
如果一个人的姓氏不是全大写或者他们名字的第一个字母不是大写,我试图在我的触发器中引发错误。但如果满足这些情况,我的代码似乎不会发出警报。有人可以向我解释一下我该怎么做吗?
这是我的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;
这是我的触发器
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$$
你找到那些的代码不正确。
请将 DELIMITER
添加到触发代码,因为在示例站点中不需要
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
如果一个人的姓氏不是全大写或者他们名字的第一个字母不是大写,我试图在我的触发器中引发错误。但如果满足这些情况,我的代码似乎不会发出警报。有人可以向我解释一下我该怎么做吗?
这是我的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;
这是我的触发器
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$$
你找到那些的代码不正确。
请将 DELIMITER
添加到触发代码,因为在示例站点中不需要
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