CHECK constraint in MySQL is not working(MySQL 中的 CHECK 约束不起作用)
问题描述
首先我创建了一个表格
CREATE TABLE Customer (
SD integer CHECK (SD > 0),
Last_Name varchar (30),
First_Name varchar(30)
);
然后在该表中插入值
INSERT INTO Customer values ('-2','abc','zz');
MySQL 没有显示错误,它接受了值.
MySQL doesn't show an error, it accepted the values.
推荐答案
MySQL 8.0.16 是第一个支持 CHECK 约束的版本.
MySQL 8.0.16 is the first version that supports CHECK constraints.
阅读https://dev.mysql.com/doc/refman/8.0/en/create-table-check-constraints.html
如果您使用 MySQL 8.0.15 或更早版本,MySQL参考手册说:
If you use MySQL 8.0.15 or earlier, the MySQL Reference Manual says:
CHECK
子句被解析但被所有存储引擎忽略.
The
CHECK
clause is parsed but ignored by all storage engines.
尝试触发器...
mysql> delimiter //
mysql> CREATE TRIGGER trig_sd_check BEFORE INSERT ON Customer
-> FOR EACH ROW
-> BEGIN
-> IF NEW.SD<0 THEN
-> SET NEW.SD=0;
-> END IF;
-> END
-> //
mysql> delimiter ;
希望有所帮助.
这篇关于MySQL 中的 CHECK 约束不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL 中的 CHECK 约束不起作用


基础教程推荐
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 在多列上分布任意行 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01