问题描述
我有这样的东西:
CREATE TYPE oAuthors AS OBJECT (
... ,
contract CHAR(1),
CONSTRUCTOR FUNCTION oAuthors (...
);
问题是我想添加一个约束,以便合同中只能包含0或1.
我试图在添加列之后声明添加
The thing is that I wanted to add a constraint to allow only 0 or 1 in contract.
I tried to do so right after the declaration of the column by adding
CONSTRAINT contract_bit CHECK (contract IN ('0','1')),
我也尝试过使用ALTER语句来执行此操作,但是问题是这不是一个表,因此它无法识别它,并且使用ALTER TYPE也不起作用(尽管我只是在尝试,但我没有这样做)不知道该怎么做).
I also tried to do it in an ALTER sentence, but the problem is that this is not a table so it won't recognize it, and using ALTER TYPE doesn't work (although I was just trying, I don't know how to do it).
所以基本上我不知道是否可以通过使用约束来实现,我应该创建触发器还是其他东西?
So basically I have no idea if this is possible by using a constraint, should I create a trigger or something?
推荐答案
您不能定义对象类型的约束: http://docs.oracle.com/cd/B28359_01/server .111/b28286/clauses002.htm
You can't define constraint on object type:http://docs.oracle.com/cd/B28359_01/server.111/b28286/clauses002.htm
您可以通过两种方式在语法上定义约束:
You can define constraints syntactically in two ways:
- 作为单个列或属性的定义的一部分.这称为内联规范.
- 作为表定义的一部分.这称为离线规范
约束子句可以出现在以下语句中:
Constraint clauses can appear in the following statements:
- 创建表(请参阅创建表)
- ALTER TABLE(请参阅ALTER TABLE)
- 创建视图(请参阅创建视图)
- ALTER VIEW(请参阅ALTER VIEW)
以下是您对对象实施约束的方式: http://docs.oracle.com/cd/B28359_01 /appdev.111/b28371/adobjdes.htm#i452285
Here is how you enforce constraints on objects:http://docs.oracle.com/cd/B28359_01/appdev.111/b28371/adobjdes.htm#i452285
这篇关于检查类型的约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!