如何在MySQL中创建需要负值的CHECK约束? [英] How to create a CHECK constraint in MySQL that requires negative values?

查看:101
本文介绍了如何在MySQL中创建需要负值的CHECK约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改表上的整数列以添加检查约束,以防止该值成为零或正数.例如:

I want to alter an integer column on a table to add a check constraint preventing the value from being zero or a positive number. For example:

CREATE TABLE example (id INTEGER)
ALTER TABLE example ADD CONSTRAINT chk_negID CHECK (id<0)

MySQL很高兴符合这些要求,但允许以下内容:

MySQL happily complies with these yet then allows the following:

INSERT INTO example VALUES (-1);
INSERT INTO example VALUES (1);

实际上没有添加我的约束吗?创建表后,有没有办法列出已添加到表中的约束?

Are my constraints not actually being added? Is there a way to list constraints that have been added to a table after it was created?

推荐答案

mysql 不支持(即使它接受有效的语法)

It's not supported by mysql (even though it accepts it as a valid syntax)

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

The CHECK clause is parsed but ignored by all storage engines

http://dev.mysql.com/doc/refman/5.5/en/create-table.html

这篇关于如何在MySQL中创建需要负值的CHECK约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆