Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Check constraint with Date

Tags:

date

sql

mysql

I am using MySQL and here is a simple query set:

create table dcheck (
    fdate date,
    sdate date,
    check (fdate <= sdate)
);

insert into dcheck values ('2006-12-12','2003-12-12');
insert into dcheck values ('2003-12-12', '2006-12-12');

Here I expect the first insert statement to fail. But surprisingly, both the queries passes and two rows are there in the table.

Can anyone please explain why?

Thanks

like image 260
bdhar Avatar asked Dec 06 '22 16:12

bdhar


1 Answers

MySQL doesn't implement CHECK constraints. From the latest (5.6) fine manual:

The CHECK clause is parsed but ignored by all storage engines.

So the syntax is parsed for compatibility other other SQLs but the checking is not implemented.

You could fake your CHECK constraint with BEFORE INSERT and BEFORE UPDATE triggers that threw an exception if the desired condition was not met.

like image 139
mu is too short Avatar answered Dec 10 '22 11:12

mu is too short