MariaDB 获取名称长度时触发错误
MariaDB Triggers error for getting length of name
我正在尝试查看我的库中某个名称的长度 table 是否为允许的长度。
这是我的触发器,但它给了我一个错误。我有一个名为 membership 的 table,它有一个名为 name 的列,用于每个拥有会员资格的人的姓名。
如有任何帮助,我们将不胜感激!
这是我的触发器:
BEGIN
DECLARE msg varchar(100);
FOR EACH ROW BEGIN
IF len(membership.name) NOT between 4 and 20 THEN
set msg ='Name should be between 5 and 20 letters';
SET MESSAGE_TEXT = msg;
END IF;
END
这是我遇到的错误:
The following query has failed: "CREATE TRIGGER `Student_Name` BEFORE
INSERT ON `membership` FOR EACH ROW BEGIN DECLARE msg varchar(100);
FOR EACH ROW BEGIN IF len(membership.name) NOT between 4 and 20 THEN
set msg ='Name should be between 5 and 20 letters'; SET MESSAGE_TEXT =
msg; END IF; END "
MySQL said: #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 'EACH ROW BEGIN IF len(membership.name) NOT between
4 and 20 THEN set msg ='...' at line 3
查看 CREATE TRIGGER 的语法:https://mariadb.com/kb/en/create-trigger/
您只能在触发器主体之前使用 FOR EACH ROW 一次。
您还在 DECLARE 行之后第二次使用了 FOR EACH ROW。把那个拿出来。
CREATE TRIGGER Student_Name BEFORE INSERT ON membership
FOR EACH ROW
BEGIN
DECLARE msg varchar(100);
FOR EACH ROW BEGIN <---- remove this line
IF LENGTH(NEW.name) NOT between 4 and 20 THEN
SET msg ='Name should be between 5 and 20 letters';
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = msg;
END IF;
END
还有其他问题。
这一行:
IF len(membership.name) NOT between 4 and 20 THEN
没有 LEN() 函数。参见 LENGTH()。
如果要在要插入的新行中引用 name
列的值,请使用 NEW.name
。
如果要引用插入的新行的 name
列,请使用 NEW.name
,而不是 membership.name
。
您可能还想使用 MESSAGE_TEXT 以某种方式在与您的规则冲突的情况下中止插入。您可能打算使用 SIGNAL.
我正在尝试查看我的库中某个名称的长度 table 是否为允许的长度。 这是我的触发器,但它给了我一个错误。我有一个名为 membership 的 table,它有一个名为 name 的列,用于每个拥有会员资格的人的姓名。
如有任何帮助,我们将不胜感激!
这是我的触发器:
BEGIN
DECLARE msg varchar(100);
FOR EACH ROW BEGIN
IF len(membership.name) NOT between 4 and 20 THEN
set msg ='Name should be between 5 and 20 letters';
SET MESSAGE_TEXT = msg;
END IF;
END
这是我遇到的错误:
The following query has failed: "CREATE TRIGGER `Student_Name` BEFORE INSERT ON `membership` FOR EACH ROW BEGIN DECLARE msg varchar(100); FOR EACH ROW BEGIN IF len(membership.name) NOT between 4 and 20 THEN set msg ='Name should be between 5 and 20 letters'; SET MESSAGE_TEXT = msg; END IF; END "
MySQL said: #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 'EACH ROW BEGIN IF len(membership.name) NOT between 4 and 20 THEN set msg ='...' at line 3
查看 CREATE TRIGGER 的语法:https://mariadb.com/kb/en/create-trigger/
您只能在触发器主体之前使用 FOR EACH ROW 一次。
您还在 DECLARE 行之后第二次使用了 FOR EACH ROW。把那个拿出来。
CREATE TRIGGER Student_Name BEFORE INSERT ON membership
FOR EACH ROW
BEGIN
DECLARE msg varchar(100);
FOR EACH ROW BEGIN <---- remove this line
IF LENGTH(NEW.name) NOT between 4 and 20 THEN
SET msg ='Name should be between 5 and 20 letters';
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = msg;
END IF;
END
还有其他问题。
这一行:
IF len(membership.name) NOT between 4 and 20 THEN
没有 LEN() 函数。参见 LENGTH()。
如果要在要插入的新行中引用 name
列的值,请使用 NEW.name
。
如果要引用插入的新行的 name
列,请使用 NEW.name
,而不是 membership.name
。
您可能还想使用 MESSAGE_TEXT 以某种方式在与您的规则冲突的情况下中止插入。您可能打算使用 SIGNAL.