mariadb/mysql-test/t/check_constraint.test
Sergei Golubchik 4c6c352138 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.
2017-09-18 10:12:23 +02:00

105 lines
3.1 KiB
Text

#
# Check of check constraints
set @save_check_constraint=@@check_constraint_checks;
create table t1 (a int check(a>10), b int check (b > 20), constraint `min` check (a+b > 100), constraint `max` check (a+b <500)) engine=myisam;
show create table t1;
insert into t1 values (100,100);
--error ER_CONSTRAINT_FAILED
insert into t1 values (1,1);
--error ER_CONSTRAINT_FAILED
insert into t1 values (20,1);
--error ER_CONSTRAINT_FAILED
insert into t1 values (20,30);
--error ER_CONSTRAINT_FAILED
insert into t1 values (500,500);
--error ER_CONSTRAINT_FAILED
insert into t1 values (101,101),(102,102),(600,600),(103,103);
select * from t1;
truncate table t1;
insert ignore into t1 values (101,101),(102,102),(600,600),(103,103);
select * from t1;
set check_constraint_checks=0;
truncate table t1;
insert into t1 values (101,101),(102,102),(600,600),(103,103);
select * from t1;
set check_constraint_checks=@save_check_constraint;
--replace_regex /failed for.*/failed for table/
--error ER_CONSTRAINT_FAILED
alter table t1 add c int default 0 check (c < 10);
set check_constraint_checks=0;
alter table t1 add c int default 0 check (c < 10);
alter table t1 add check (a+b+c < 500);
set check_constraint_checks=@save_check_constraint;
show create table t1;
--error ER_CONSTRAINT_FAILED
insert into t1 values(105,105,105);
--error ER_CONSTRAINT_FAILED
insert into t1 values(249,249,9);
insert into t1 values(105,105,9);
select * from t1;
create table t2 like t1;
show create table t2;
--error ER_CANT_DROP_FIELD_OR_KEY
alter table t2 drop constraint c;
alter table t2 drop constraint if exists c;
alter table t2 drop constraint min;
show create table t2;
drop table t1,t2;
#
# check constraint name auto-generation:
#
create or replace table t1 (a int, b int, constraint check (a>b));
show create table t1;
create or replace table t1 (a int, b int,
constraint CONSTRAINT_1 check (a>1),
constraint check (b>1));
show create table t1;
create or replace table t1 (a int, b int,
constraint CONSTRAINT_1 check (a>1),
constraint check (b>1),
constraint CONSTRAINT_2 check (a>b));
show create table t1;
drop table t1;
#
# MDEV-10370 Check constraints on virtual columns fails on INSERT when column not specified
#
create table t1(c1 int, c2 int as (c1 + 1), check (c2 > 2));
--error ER_CONSTRAINT_FAILED
insert into t1(c1) values(1);
insert into t1(c1) values(2);
drop table t1;
#
# MDEV-11117 CHECK constraint fails on intermediate step of ALTER
#
-- error ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED
create or replace table t1( c1 int auto_increment primary key, check( c1 > 0 or c1 is null ) );
#
# MDEV-12421 Check constraint with query crashes server and renders DB unusable
#
--error ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED
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;