我已经将“关于我们”字段的非空约束更改为可为空。
CREATE TABLE IF NOT EXISTS `te` (
`id` int(30) NOT NULL,
`name` text NOT NULL,
`address` text NOT NULL,
`Aboutus` text NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
这是你的触发器 BEFORE INSERT
CREATE TRIGGER new_insert
BEFORE INSERT ON `te`
FOR EACH ROW
SET NEW.`Aboutus` = CASE WHEN NEW.Aboutus IS NULL THEN 'Not Updated' ELSE NEW.Aboutus END
;
不使用Aboutus
进行插入
INSERT INTO `te` (`id`, `name`, `address`)
VALUES (1, 'name', 'address') ;
使用Aboutus
进行插入操作
INSERT INTO `te` (`id`, `name`, `address`, `Aboutus`)
VALUES (2, 'name', 'address', 'Aboutus') ;
通过传递 null 插入 Aboutus
INSERT INTO `te` (`id`, `name`, `address`, `Aboutus`)
VALUES (3, 'name', 'address', null) ;
编辑 正如@garethD指出的更新场景,你还需要另一个在BEFORE UPDATE
触发器上的条件,这样如果更新时出现空值,则aboutus应该被更新为Not Updated
。
CREATE TRIGGER update_trigger
BEFORE UPDATE ON `te`
FOR EACH ROW
SET NEW.`Aboutus` = CASE WHEN NEW.Aboutus IS NULL THEN 'Not Updated' ELSE NEW.Aboutus END
;
UPDATE te
SET AboutUs = NULL;