Вы пытаетесь получить доступ к объекту, который не существует (Таблица 'tbl'), вы пытаетесь создать его:
MariaDB [_]> DROP TABLE IF EXISTS `tbl`;
Query OK, 0 rows affected (0.01 sec)
MariaDB [_]> CREATE TABLE IF NOT EXISTS `tbl` (
-> `id` INT NOT NULL,
-> `start` DATETIME NOT NULL,
-> `end` DATETIME NOT NULL,
-> CONSTRAINT `C1` CHECK (`id` NOT IN
-> (SELECT `id`
-> FROM `tbl` AS `tt`
-> WHERE `tt`.`start` BETWEEN `start` AND `end` OR
-> `tt`.`end` BETWEEN `start` AND `end`)
-> ),
-> CONSTRAINT `C2` CHECK (`end` > `start`)
-> );
ERROR 1146 (42S02): Table '_.tbl' doesn't exist
В случае доступа к объекту, который существует:
MariaDB [_]> DROP TABLE IF EXISTS `tbl_TEMP`, `tbl`;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
MariaDB [_]> CREATE TABLE IF NOT EXISTS `tbl_TEMP` (
-> `id` INT NOT NULL,
-> `start` DATETIME NOT NULL,
-> `end` DATETIME NOT NULL
-> );
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> CREATE TABLE IF NOT EXISTS `tbl` (
-> `id` INT NOT NULL,
-> `start` DATETIME NOT NULL,
-> `end` DATETIME NOT NULL,
-> CONSTRAINT `C1` CHECK (
-> `id` NOT IN (SELECT `id`
-> FROM `tbl_TEMP` AS `tt`
-> WHERE `tt`.`start` BETWEEN `start` AND `end` OR
-> `tt`.`end` BETWEEN `start` AND `end`)
-> ),
-> CONSTRAINT `C2` CHECK (`end` > `start`)
-> );
ERROR 1901 (HY000): Function or expression 'select ...'
cannot be used in the CHECK clause of `C1`
Опция, которая может быть полезна:
MariaDB [_]> DROP TABLE IF EXISTS `tbl`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
MariaDB [_]> CREATE TABLE IF NOT EXISTS `tbl` (
-> `id` INT NOT NULL,
-> `start` DATETIME NOT NULL,
-> `end` DATETIME NOT NULL,
-> CONSTRAINT `C2` CHECK (`end` > `start`)
-> );
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DELIMITER //
MariaDB [_]> CREATE TRIGGER `trg_bi_idduplicate` BEFORE INSERT ON `tbl`
-> FOR EACH ROW
-> BEGIN
-> IF EXISTS (SELECT NULL
-> FROM `tbl`
-> WHERE `id` = NEW.`id`) THEN
-> SIGNAL SQLSTATE '45000' SET
-> MYSQL_ERRNO = 31001,
-> MESSAGE_TEXT = '`id` DUPLICATE';
-> END IF;
-> END//
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DELIMITER ;
MariaDB [_]> INSERT INTO `tbl`
-> SELECT 1, '2000-01-01 00:00:01', '2000-01-01 00:00:01';
ERROR 4025 (23000): CONSTRAINT `C2` failed for `_`.`tbl`
MariaDB [_]> INSERT INTO `tbl`
-> SELECT 1, '2000-01-01 00:00:01', '2000-01-01 00:00:02';
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
MariaDB [_]> SELECT
-> `id`,
-> `start`,
-> `end`
-> FROM
-> `tbl`;
+----+---------------------+---------------------+
| id | start | end |
+----+---------------------+---------------------+
| 1 | 2000-01-01 00:00:01 | 2000-01-01 00:00:02 |
+----+---------------------+---------------------+
1 row in set (0.00 sec)
MariaDB [_]> INSERT INTO `tbl`
-> SELECT 1, '2000-01-01 00:00:01', '2000-01-01 00:00:02';
ERROR 31001 (45000): `id` DUPLICATE
MariaDB [_]> SELECT
-> `id`,
-> `start`,
-> `end`
-> FROM
-> `tbl`;
+----+---------------------+---------------------+
| id | start | end |
+----+---------------------+---------------------+
| 1 | 2000-01-01 00:00:01 | 2000-01-01 00:00:02 |
+----+---------------------+---------------------+
1 row in set (0.00 sec)