Commit 4c6c3521 authored by Sergei Golubchik's avatar Sergei Golubchik

MDEV-13596 CHECK constraints disallow NULL to pass through, violating SQL

SQL Standard (4.23.3.4 Table check constraints, part 2, SQL:2016) says
that CHECK constraint rejects rows *only* if the condition is FALSE.
That is, both TRUE and NULL should be allowed.
parent e6ce97a5
......@@ -142,3 +142,13 @@ create table t1 (a int check (@b in (select user from mysql.user)));
ERROR HY000: Function or expression 'select ...' cannot be used in the CHECK clause of `a`
create table t1 (a int check (a > @b));
ERROR HY000: Function or expression '@b' cannot be used in the CHECK clause of `a`
create table t1 (a int check (a = 1));
insert t1 values (1);
insert t1 values (2);
ERROR 23000: CONSTRAINT `a` failed for `test`.`t1`
insert t1 values (NULL);
select * from t1;
a
1
NULL
drop table t1;
......@@ -92,3 +92,14 @@ create or replace table t1( c1 int auto_increment primary key, check( c1 > 0 or
create table t1 (a int check (@b in (select user from mysql.user)));
--error ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED
create table t1 (a int check (a > @b));
#
# MDEV-13596 CHECK constraints disallow NULL to pass through, violating SQL
#
create table t1 (a int check (a = 1));
insert t1 values (1);
--error ER_CONSTRAINT_FAILED
insert t1 values (2);
insert t1 values (NULL);
select * from t1;
drop table t1;
......@@ -5090,7 +5090,8 @@ int TABLE::verify_constraints(bool ignore_failure)
{
for (Virtual_column_info **chk= check_constraints ; *chk ; chk++)
{
if ((*chk)->expr->val_int() == 0)
/* yes! NULL is ok, see 4.23.3.4 Table check constraints, part 2, SQL:2016 */
if ((*chk)->expr->val_int() == 0 && !(*chk)->expr->null_value)
{
my_error(ER_CONSTRAINT_FAILED,
MYF(ignore_failure ? ME_JUST_WARNING : 0), (*chk)->name.str,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment