We can use a CHECK CONSTRAINT, but we have to hard-code the date because CURRENT_DATE is not allowed in a CHECK CONSTRAINT. Please note that check constraint only function on recents editions of mySQL. If this approach is not what you are looking for the other possibility is a TRIGGER BEFORE INSERT. A trigger has the advantage of being able to check other rows and other table, which CONSTRAINTS cannot do.
create table Supporter (
name varchar(25),
email varchar(25),
phone varchar(25),
address varchar(25),
zip_code varchar(25),
city varchar(25),
birthday date CHECK (birthday < '2004-03-17')
) ;
insert into Supporter (name, birthday) values ('Andrew','2000-01-01');
insert into Supporter (name, birthday) values ('Bernard','2010-01-01');
发布评论
评论(1)
我们可以使用 CHECK CONSTRAINT,但我们必须对日期进行硬编码,因为 CHECK CONSTRAINT 中不允许使用 CURRENT_DATE。请注意,检查约束仅在最新版本的 mySQL 上起作用。
如果这种方法不是您正在寻找的,另一种可能性是插入前触发。触发器的优点是能够检查其他行和其他表,这是 CONSTRAINTS 无法做到的。
db<>fiddle此处
We can use a CHECK CONSTRAINT, but we have to hard-code the date because CURRENT_DATE is not allowed in a CHECK CONSTRAINT. Please note that check constraint only function on recents editions of mySQL.
If this approach is not what you are looking for the other possibility is a TRIGGER BEFORE INSERT. A trigger has the advantage of being able to check other rows and other table, which CONSTRAINTS cannot do.
db<>fiddle here