如何检查 created_in 列是否为 none 或不在触发器内?
How can I check if the created_in column is none or not inside triggers?
用户table
中的列和触发器结构
CREATE TABLE `users` (
`created_in` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Trigger
Event
Table
Statement
Timing
...
insert
users
IF NEW.created_in = "" THEN
SIGNAL SQLSTATE "45000";
END IF
before
如果 created_in 列是 none 但不起作用,我正在尝试取消插入。
如何检查 created_in 列是否为 none?
Version of MySQL is 5.7
不需要触发器,会话严格模式就足够了:
set session sql_mode = '';
CREATE TABLE `users` (
`created_in` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
insert into users values ('');
select * from users;
| created_in |
| :--------- |
| 0000-00-00 |
set session sql_mode = 'STRICT_ALL_TABLES';
insert into users values ('');
Incorrect date value: '' for column 'created_in' at row 1
db<>fiddle here
用户table
中的列和触发器结构CREATE TABLE `users` (
`created_in` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Trigger | Event | Table | Statement | Timing |
---|---|---|---|---|
... | insert | users | IF NEW.created_in = "" THEN SIGNAL SQLSTATE "45000"; END IF |
before |
如果 created_in 列是 none 但不起作用,我正在尝试取消插入。
如何检查 created_in 列是否为 none?
Version of MySQL is 5.7
不需要触发器,会话严格模式就足够了:
set session sql_mode = '';
CREATE TABLE `users` ( `created_in` date NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; insert into users values (''); select * from users;
| created_in | | :--------- | | 0000-00-00 |
set session sql_mode = 'STRICT_ALL_TABLES';
insert into users values ('');
Incorrect date value: '' for column 'created_in' at row 1
db<>fiddle here