Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 132 additions & 34 deletions mysql-test/main/check_constraint.result
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ t1 CREATE TABLE `t1` (
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
insert into t1 values (100,100);
insert into t1 values (1,1);
ERROR 23000: CONSTRAINT `t1.a` failed for `test`.`t1`
ERROR 23000: CONSTRAINT `a` failed for `test`.`t1`
insert into t1 values (20,1);
ERROR 23000: CONSTRAINT `t1.b` failed for `test`.`t1`
ERROR 23000: CONSTRAINT `b` failed for `test`.`t1`
insert into t1 values (20,30);
ERROR 23000: CONSTRAINT `min` failed for `test`.`t1`
insert into t1 values (500,500);
Expand Down Expand Up @@ -60,7 +60,7 @@ t1 CREATE TABLE `t1` (
CONSTRAINT `CONSTRAINT_1` CHECK (`a` + `b` + `c` < 500)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
insert into t1 values(105,105,105);
ERROR 23000: CONSTRAINT `t1.c` failed for `test`.`t1`
ERROR 23000: CONSTRAINT `c` failed for `test`.`t1`
insert into t1 values(249,249,9);
ERROR 23000: CONSTRAINT `CONSTRAINT_1` failed for `test`.`t1`
insert into t1 values(105,105,9);
Expand Down Expand Up @@ -145,7 +145,7 @@ ERROR HY000: Function or expression '@b' cannot be used in the CHECK clause of `
create table t1 (a int check (a = 1));
insert t1 values (1);
insert t1 values (2);
ERROR 23000: CONSTRAINT `t1.a` failed for `test`.`t1`
ERROR 23000: CONSTRAINT `a` failed for `test`.`t1`
insert t1 values (NULL);
select * from t1;
a
Expand All @@ -165,13 +165,13 @@ ERROR 22007: Truncated incorrect DECIMAL value: 'Ken'
SHOW WARNINGS;
Level Code Message
Error 1292 Truncated incorrect DECIMAL value: 'Ken'
Error 4025 CONSTRAINT `t1.FirstName` failed for `test`.`t1`
Error 4025 CONSTRAINT `FirstName` failed for `test`.`t1`
INSERT INTO t1 VALUES (NULL, 'Ken'),(NULL, 'Brian');
ERROR 22007: Truncated incorrect DECIMAL value: 'Ken'
SHOW WARNINGS;
Level Code Message
Error 1292 Truncated incorrect DECIMAL value: 'Ken'
Error 4025 CONSTRAINT `t1.FirstName` failed for `test`.`t1`
Error 4025 CONSTRAINT `FirstName` failed for `test`.`t1`
INSERT IGNORE INTO t1 VALUES (NULL, 'Ken');
Warnings:
Warning 1292 Truncated incorrect DECIMAL value: 'Ken'
Expand All @@ -197,31 +197,6 @@ EmployeeID FirstName
5 Ken
6 Brian
drop table t1;
#
# MDEV-16630: Ambiguous error message when check constraint
# matches table name
#
use test;
drop table if exists t;
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;
Table Create Table
t CREATE TABLE `t` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL CHECK (`b` > 0),
CONSTRAINT `b` CHECK (`a` < `b`),
CONSTRAINT `a` CHECK (`a` > 0),
CONSTRAINT `x` CHECK (`a` > 10)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
# Field constraint 'b' will fail
insert into t values (-1, 0);
ERROR 23000: CONSTRAINT `t.b` failed for `test`.`t`
# Table constraint 'b' will fail
insert into t values (1,1);
ERROR 23000: CONSTRAINT `b` failed for `test`.`t`
drop table t;
create table t1 (a int auto_increment primary key, b int, check (b > 5));
insert t1 (b) values (1);
ERROR 23000: CONSTRAINT `CONSTRAINT_1` failed for `test`.`t1`
Expand Down Expand Up @@ -298,13 +273,13 @@ drop table t1;
#
# MDEV-24274 ALTER TABLE with CHECK CONSTRAINTS gives "Out of Memory" error
#
create table t1 (id varchar(2), constraint id check (id regexp '[a-z]'));
create table t1 (id varchar(2), constraint id1 check (id regexp '[a-z]'));
alter table t1 force;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`id` varchar(2) DEFAULT NULL,
CONSTRAINT `id` CHECK (`id` regexp '[a-z]')
CONSTRAINT `id1` CHECK (`id` regexp '[a-z]')
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
drop table t1;
#
Expand All @@ -315,8 +290,131 @@ drop table t1;
create table t1 (a int, b int as (a) check (b > 0));
insert t1 (a) values (1);
insert t1 (a) values (-1);
ERROR 23000: CONSTRAINT `t1.b` failed for `test`.`t1`
ERROR 23000: CONSTRAINT `b` failed for `test`.`t1`
drop table t1;
#
# MDEV-24598: duplicate CHECK constraint name
#
create table t (a int check (a>2),
constraint a1 check (a < 5),
constraint a1 check (a > 5));
ERROR HY000: Duplicate CHECK constraint name 'a1'
create table t (a int check (a>2),
constraint a check (a < 5));
ERROR HY000: Duplicate CHECK constraint name 'a'
CREATE TABLE t (
path VARCHAR(100) NOT NULL COLLATE 'utf8_general_ci',
json_c1 JSON NOT NULL DEFAULT '' COLLATE 'utf8mb4_bin',
json_c2 JSON NOT NULL DEFAULT '' COLLATE 'utf8mb4_bin',
comment VARCHAR(100) NULL DEFAULT NULL COLLATE 'utf8_general_ci',
UNIQUE INDEX path (path),
CONSTRAINT json_c1 CHECK (path > 0)
);
ERROR HY000: Duplicate CHECK constraint name 'json_c1'
select * from information_schema.check_constraints where table_name='t';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME CHECK_CLAUSE
create table t(a int);
alter table t add constraint a check (a > 10);
ERROR HY000: Duplicate CHECK constraint name 'a'
drop table t;
#
# MDEV-24602: cannot specify a name for a column check constraint
#
create table t(t int,
check (t>0),
constraint check(t>1),
constraint t_named check(t>2));
show create table t;
Table Create Table
t CREATE TABLE `t` (
`t` int(11) DEFAULT NULL,
CONSTRAINT `CONSTRAINT_1` CHECK (`t` > 0),
CONSTRAINT `CONSTRAINT_2` CHECK (`t` > 1),
CONSTRAINT `t_named` CHECK (`t` > 2)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
SELECT CONSTRAINT_NAME, TABLE_NAME, CONSTRAINT_TYPE
FROM information_schema.TABLE_CONSTRAINTS WHERE
TABLE_NAME = 't' AND TABLE_SCHEMA = 'test';
CONSTRAINT_NAME TABLE_NAME CONSTRAINT_TYPE
CONSTRAINT_1 t CHECK
CONSTRAINT_2 t CHECK
t_named t CHECK
SELECT * from INFORMATION_SCHEMA.CHECK_CONSTRAINTS WHERE
CONSTRAINT_SCHEMA = 'test';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME CHECK_CLAUSE
def test t CONSTRAINT_1 `t` > 0
def test t CONSTRAINT_2 `t` > 1
def test t t_named `t` > 2
drop table t;
create table t(t0 int,
t1 int check (t1>0),
t2 int constraint check(t2>0),
t3 int constraint t_field check(t3>0));
show create table t;
Table Create Table
t CREATE TABLE `t` (
`t0` int(11) DEFAULT NULL,
`t1` int(11) DEFAULT NULL CHECK (`t1` > 0),
`t2` int(11) DEFAULT NULL CHECK (`t2` > 0),
`t3` int(11) DEFAULT NULL CHECK (`t3` > 0)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
SELECT CONSTRAINT_NAME, TABLE_NAME, CONSTRAINT_TYPE
FROM information_schema.TABLE_CONSTRAINTS WHERE
TABLE_NAME = 't' AND TABLE_SCHEMA = 'test';
CONSTRAINT_NAME TABLE_NAME CONSTRAINT_TYPE
t1 t CHECK
t2 t CHECK
t_field t CHECK
SELECT * from INFORMATION_SCHEMA.CHECK_CONSTRAINTS WHERE
CONSTRAINT_SCHEMA = 'test';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME CHECK_CLAUSE
def test t t1 `t1` > 0
def test t t2 `t2` > 0
def test t t_field `t3` > 0
drop table t;
create table t(t1 int constraint t1_check check(t1>0),
t2 int check(t2>0),
t3 int constraint t_field check(t3>0),
t4 int constraint t_field check(t4>0));
ERROR HY000: Duplicate CHECK constraint name 't_field'
create table t(t1 int constraint t1_check check(t1>0),
t2 int check(t2>0),
t3 int constraint t_field check(t3>0),
constraint table_check1 check(t1>0),
constraint table_check1 check(t2>0));
ERROR HY000: Duplicate CHECK constraint name 'table_check1'
create table t(t1 int constraint t1_check check(t1>0),
t2 int check(t2>0),
t3 int constraint t_field check(t3>0),
constraint t_field check(t1>0));
ERROR HY000: Duplicate CHECK constraint name 't_field'
create table t(t1 int constraint t1 check(t1>0));
SELECT * from INFORMATION_SCHEMA.CHECK_CONSTRAINTS WHERE
CONSTRAINT_SCHEMA = 'test';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME CHECK_CLAUSE
def test t t1 `t1` > 0
alter table t add constraint t1 check (t1<0);
ERROR HY000: Duplicate CHECK constraint name 't1'
SELECT * from INFORMATION_SCHEMA.CHECK_CONSTRAINTS WHERE
CONSTRAINT_SCHEMA = 'test';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME CHECK_CLAUSE
def test t t1 `t1` > 0
show create table t;
Table Create Table
t CREATE TABLE `t` (
`t1` int(11) DEFAULT NULL CHECK (`t1` > 0)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
alter table t modify column t1 int constraint t1_greater_pi_check check (t1>3.14);
show create table t;
Table Create Table
t CREATE TABLE `t` (
`t1` int(11) DEFAULT NULL CHECK (`t1` > 3.14)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
SELECT * from INFORMATION_SCHEMA.CHECK_CONSTRAINTS WHERE
CONSTRAINT_SCHEMA = 'test';
CONSTRAINT_CATALOG CONSTRAINT_SCHEMA TABLE_NAME CONSTRAINT_NAME CHECK_CLAUSE
def test t t1_greater_pi_check `t1` > 3.14
drop table t;
#
# End of 10.4 tests
#
133 changes: 104 additions & 29 deletions mysql-test/main/check_constraint.test
Original file line number Diff line number Diff line change
Expand Up @@ -135,34 +135,6 @@ 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
#
Expand Down Expand Up @@ -226,7 +198,7 @@ drop table t1;
--echo #
--echo # MDEV-24274 ALTER TABLE with CHECK CONSTRAINTS gives "Out of Memory" error
--echo #
create table t1 (id varchar(2), constraint id check (id regexp '[a-z]'));
create table t1 (id varchar(2), constraint id1 check (id regexp '[a-z]'));
alter table t1 force;
show create table t1;
drop table t1;
Expand All @@ -244,6 +216,109 @@ insert t1 (a) values (1);
insert t1 (a) values (-1);
drop table t1;

--echo #
--echo # MDEV-24598: duplicate CHECK constraint name
--echo #

# Field constraints have th name of columns, it is not possible to have duplicated
# constraints (ER_DUP_FIELDNAME)

# Table constraints with the same name will raise ER_DUP_CONSTRAINT_NAME
--error ER_DUP_CONSTRAINT_NAME
create table t (a int check (a>2),
constraint a1 check (a < 5),
constraint a1 check (a > 5));

# Table constraint that has the same name as field
--error ER_DUP_CONSTRAINT_NAME
create table t (a int check (a>2),
constraint a check (a < 5));

# Check native Json constraint
--error ER_DUP_CONSTRAINT_NAME
CREATE TABLE t (
path VARCHAR(100) NOT NULL COLLATE 'utf8_general_ci',
json_c1 JSON NOT NULL DEFAULT '' COLLATE 'utf8mb4_bin',
json_c2 JSON NOT NULL DEFAULT '' COLLATE 'utf8mb4_bin',
comment VARCHAR(100) NULL DEFAULT NULL COLLATE 'utf8_general_ci',
UNIQUE INDEX path (path),
CONSTRAINT json_c1 CHECK (path > 0)
);
select * from information_schema.check_constraints where table_name='t';

# Check Alter table
create table t(a int);
--error ER_DUP_CONSTRAINT_NAME
alter table t add constraint a check (a > 10);
drop table t;

--echo #
--echo # MDEV-24602: cannot specify a name for a column check constraint
--echo #
# Table constraints (unnamed and named)
create table t(t int,
check (t>0),
constraint check(t>1),
constraint t_named check(t>2));
show create table t;
SELECT CONSTRAINT_NAME, TABLE_NAME, CONSTRAINT_TYPE
FROM information_schema.TABLE_CONSTRAINTS WHERE
TABLE_NAME = 't' AND TABLE_SCHEMA = 'test';
SELECT * from INFORMATION_SCHEMA.CHECK_CONSTRAINTS WHERE
CONSTRAINT_SCHEMA = 'test';
drop table t;

# Field constraints (with and without the names)
create table t(t0 int,
t1 int check (t1>0),
t2 int constraint check(t2>0),
t3 int constraint t_field check(t3>0));
show create table t;
SELECT CONSTRAINT_NAME, TABLE_NAME, CONSTRAINT_TYPE
FROM information_schema.TABLE_CONSTRAINTS WHERE
TABLE_NAME = 't' AND TABLE_SCHEMA = 'test';
SELECT * from INFORMATION_SCHEMA.CHECK_CONSTRAINTS WHERE
CONSTRAINT_SCHEMA = 'test';
drop table t;

# Field constraint names are the same
--error ER_DUP_CONSTRAINT_NAME
create table t(t1 int constraint t1_check check(t1>0),
t2 int check(t2>0),
t3 int constraint t_field check(t3>0),
t4 int constraint t_field check(t4>0));

# Table constraint names are the same
--error ER_DUP_CONSTRAINT_NAME
create table t(t1 int constraint t1_check check(t1>0),
t2 int check(t2>0),
t3 int constraint t_field check(t3>0),
constraint table_check1 check(t1>0),
constraint table_check1 check(t2>0));

# Table and field constraint names are the same
--error ER_DUP_CONSTRAINT_NAME
create table t(t1 int constraint t1_check check(t1>0),
t2 int check(t2>0),
t3 int constraint t_field check(t3>0),
constraint t_field check(t1>0));

# Adding table constraint with the same name will result in error for alter table.
create table t(t1 int constraint t1 check(t1>0));
SELECT * from INFORMATION_SCHEMA.CHECK_CONSTRAINTS WHERE
CONSTRAINT_SCHEMA = 'test';
--error ER_DUP_CONSTRAINT_NAME
alter table t add constraint t1 check (t1<0);
SELECT * from INFORMATION_SCHEMA.CHECK_CONSTRAINTS WHERE
CONSTRAINT_SCHEMA = 'test';
# Show create table will show field constraint
show create table t;
alter table t modify column t1 int constraint t1_greater_pi_check check (t1>3.14);
# Show create table will show again field constraint
show create table t;
SELECT * from INFORMATION_SCHEMA.CHECK_CONSTRAINTS WHERE
CONSTRAINT_SCHEMA = 'test';
drop table t;
--echo #
--echo # End of 10.4 tests
--echo #
4 changes: 2 additions & 2 deletions mysql-test/main/constraints.result
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ t1 CREATE TABLE `t1` (
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
insert into t1 values (1);
insert into t1 values (0);
ERROR 23000: CONSTRAINT `t1.a` failed for `test`.`t1`
ERROR 23000: CONSTRAINT `a` failed for `test`.`t1`
drop table t1;
create table t1 (a int, b int, check (a>b));
show create table t1;
Expand Down Expand Up @@ -195,7 +195,7 @@ create table t1 (a text default(length(now())) check (length(a) > 1));
insert into t1 values ();
insert into t1 values ("ccc");
insert into t1 values ("");
ERROR 23000: CONSTRAINT `t1.a` failed for `test`.`t1`
ERROR 23000: CONSTRAINT `a` failed for `test`.`t1`
select * from t1;
a
19
Expand Down
Loading