Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql sql检查约束无法正常工作_Mysql_Sql_Check Constraints - Fatal编程技术网

Mysql sql检查约束无法正常工作

Mysql sql检查约束无法正常工作,mysql,sql,check-constraints,Mysql,Sql,Check Constraints,我已创建带有检查约束的表格明细表: mysql> create table schedule(order_date date, dely_date date check(dely_date>order_date)); Query OK, 0 rows affected (0.50 sec) 当我插入一个违反检查约束的值时,sql不会报告任何错误 mysql> insert into schedule values('

我已创建带有检查约束的表格明细表:

mysql> create table schedule(order_date date, dely_date date
                             check(dely_date>order_date));
Query OK, 0 rows affected (0.50 sec)
当我插入一个违反检查约束的值时,sql不会报告任何错误

 mysql> insert into schedule values('2015-11-20','2014-12-25');
Query OK, 1 row affected (0.10 sec)

mysql> select * from schedule;
+------------+------------+
| order_date | dely_date  |
+------------+------------+
| 2015-11-20 | 2014-12-25 |
+------------+------------+
1 row in set (0.00 sec)

我插入了订单日期之前的日期。

哦,它工作正常。根据报告:

CHECK子句已解析,但被所有存储引擎忽略


您可能想尝试一个更合理的数据库。

MySQL中的
检查
约束被忽略,如中所示

使用
SQL Server
检查
的示例:

create table #schedule(order_date date,
dely_date date,
check(dely_date>order_date));

insert into #schedule values('2015-11-20','2014-12-25');
-- The INSERT statement conflicted with the CHECK constraint "CK_#schedule_A59B8DED". 
-- The conflict occurred in database "tempdb", table "dbo.#schedule___
-- __________________00000000C9D8". The statement has been terminated.

INSERT INTO #schedule values('2015-12-24','2015-12-25');

SELECT *
FROM #schedule;

您可以使用触发器进行验证:

CREATE TABLE `schedule`(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
                        order_date DATETIME,
                        dely_date DATETIME);              

CREATE TRIGGER `schedule_trg_ins` BEFORE INSERT ON `schedule`
FOR EACH ROW
BEGIN
    IF NOT(New.dely_date>New.order_date) THEN
    SIGNAL SQLSTATE '10000'
        SET MESSAGE_TEXT = 'check constraint on schedule failed during insert';
    END IF;
END;

CREATE TRIGGER `schedule_trg_upd` BEFORE UPDATE ON `schedule`
FOR EACH ROW
BEGIN
    IF NOT(New.dely_date>New.order_date) THEN
    SIGNAL SQLSTATE '10000'
        SET MESSAGE_TEXT = 'check constraint on schedule failed during update';
    END IF;
END;

INSERT INTO `schedule`(order_date, dely_date)
VALUES ('2015-12-24','2015-12-25');

INSERT INTO `schedule`(order_date, dely_date)
VALUES ('2015-12-26','2015-12-25');
-- check constraint on schedule failed during insert

UPDATE `schedule`
SET order_date = '2015-12-26'
WHERE id = 1;
-- check constraint on schedule failed during update

我喜欢
sane
:)您应该提到一个解决方法是使用触发器您需要在检查前输入comman。我应该怎么做才能让sql报告错误?@lad2025:好主意。你应该考虑写一个答案,因为我限制了自己对MySQL的抨击。@“USER 329 764”代码>检查< /代码>在MySQL中根本不做任何事情。使用之前插入的触发器,LAD2025被提到似乎是实现你所需要的约束的一个好方法。@ JakubKania添加了应答器。但是为什么没有触发器它就不能工作呢?@user3297764因为MySQL没有实现
CHECK
语法。它只是一个占位符。