问题
I have something like this:
CREATE TYPE oAuthors AS OBJECT (
... ,
contract CHAR(1),
CONSTRUCTOR FUNCTION oAuthors (...
);
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')),
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?
回答1:
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:
- As part of the definition of an individual column or attribute. This is called inline specification.
- As part of the table definition. This is called out-of-line specification
Constraint clauses can appear in the following statements:
- CREATE TABLE (see CREATE TABLE)
- ALTER TABLE (see ALTER TABLE)
- CREATE VIEW (see CREATE VIEW)
- ALTER VIEW (see ALTER VIEW)
Here is how you enforce constraints on objects: http://docs.oracle.com/cd/B28359_01/appdev.111/b28371/adobjdes.htm#i452285
来源:https://stackoverflow.com/questions/23954390/check-constraint-on-a-type