sql CHECK 约束无法正常工作
sql CHECK constraint not working properly
我已经创建了一个带有检查约束的 table 时间表:
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)
我在 order_date 之前插入了一个 dely_date。
哦,它工作正常。根据 manual:
The CHECK clause is parsed but ignored by all storage engines
您可能想尝试更理智的数据库。
MySQL
中的 CHECK
约束与
中一样被忽略
使用 SQL Server
工作 CHECK
的示例:
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
我已经创建了一个带有检查约束的 table 时间表:
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)
我在 order_date 之前插入了一个 dely_date。
哦,它工作正常。根据 manual:
The CHECK clause is parsed but ignored by all storage engines
您可能想尝试更理智的数据库。
MySQL
中的 CHECK
约束与
使用 SQL Server
工作 CHECK
的示例:
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