简体   繁体   English

SQL表添加约束以检查数量限制

[英]Sql table adding constraint to check for number limit

I am creating a table in oracle DB and trying to add constraint so that the numbers allowed in the column are 1,2,3. 我正在oracle DB中创建一个表,并尝试添加约束,以使该列中允许的数字为1,2,3。

CREATE TABLE "TABLE_EXAMPLE"
(
.
.
"PROTOCOL" NUMBER (1,2,3),

....)

CONSTRAINT "CH1"
        CHECK ("PROTOCOL" BETWEEN 1 AND 3),

Am I doing right or any better way? 我做对了还是更好的办法?

CREATE TABLE TABLE_EXAMPLE 
(
 ...
  PROTOCOL NUMBER(1) NOT NULL CONSTRAINT CH1 CHECK (PROTOCOL IN (1,2,3))
 ...
);

BETWEEN 1 AND 3 includes 1.5, 1.6, etc. BETWEEN 1 AND 3包括BETWEEN 1 AND 3等。

And I'd recommend not to use quotes " unless you have special characters in table or column names... 而且我建议不要使用引号"除非您在表名或列名中包含特殊字符...

If you are going to check in the table level Check Constraint is the best way. 如果要检入表级别,“检查约束”是最好的方法。 because if you are inserting larger value then the check constraint ll throw the error. 因为如果您要插入较大的值,则检查约束将引发错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM