1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
#
# 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;
--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;
#
# MDEV-15141 Check constraint validation on a datetime field crashes the process
#
create table t1 (id int auto_increment primary key, datecol datetime, check (datecol>'0001-01-01 00:00:00'));
insert into t1 (datecol) values (now());
insert into t1 (datecol) values (now());
drop table t1;
#
# MDEV-15461 Check Constraints with binary logging makes insert inconsistent
#
CREATE TABLE t1 (
EmployeeID SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
FirstName VARCHAR(30) NOT NULL CHECK (CHAR_LENGTH(FirstName > 2))
);
--error ER_TRUNCATED_WRONG_VALUE
INSERT INTO t1 VALUES (NULL, 'Ken');
SHOW WARNINGS;
--error ER_TRUNCATED_WRONG_VALUE
INSERT INTO t1 VALUES (NULL, 'Ken'),(NULL, 'Brian');
SHOW WARNINGS;
INSERT IGNORE INTO t1 VALUES (NULL, 'Ken');
INSERT IGNORE INTO t1 VALUES (NULL, 'Ken'),(NULL, 'Brian');
set sql_mode="";
INSERT INTO t1 VALUES (NULL, 'Ken');
INSERT INTO t1 VALUES (NULL, 'Ken'),(NULL, 'Brian');
set sql_mode=default;
select * from t1;
drop table t1;
--echo #
--echo # MDEV-16630: Ambiguous error message when check constraint
--echo # matches table name
--echo #
use test;
--disable_warnings
drop table if exists t;
--enable_warnings
create table t(a int, b int check(b>0),
constraint b check(a<b), constraint a check(a>0),
constraint x check (a>10));
show create table t;
# Generate error when field constraint 'b' is violated
--echo # Field constraint 'b' will fail
--error ER_CONSTRAINT_FAILED
insert into t values (-1, 0);
# Generate error when table constraint 'b' is violated.
--echo # Table constraint 'b' will fail
--error ER_CONSTRAINT_FAILED
insert into t values (1,1);
drop table t;
#
# check constraints and auto_is_null typo
#
create table t1 (a int auto_increment primary key, b int, check (b > 5));
--error ER_CONSTRAINT_FAILED
insert t1 (b) values (1);
insert t1 (b) values (10);
select * from t1 where a is null;
set sql_auto_is_null=1;
select * from t1 where a is null;
--error ER_CONSTRAINT_FAILED
insert t1 (b) values (1);
drop table t1;
|