Can we use logical operators, Conditional operators other than relational operator in query of Constraints?, because I am having an error
Below is the query I tried
Alter table arbab ADD CONSTRAINT abc CHECK (gender 'Male' or 'female');
I am having below mentioned error:
Error starting at line 1 in command:
Alter table arbab ADD CONSTRAINT abc CHECK (gender 'Male' or 'female')
Error report:
SQL Error: ORA-00920: invalid relational operator
00920. 00000 - "invalid relational operator"
*Cause:
*Action:
This query:
Alter table arbab ADD CONSTRAINT abc CHECK (gender >10);
Worked fine.
Thanks in advance.
Mamta MPosted Apr 11, 2011, 6:37 AM
The purpose of constraints is to maintain data integrity. So whenever you create constraints such as CHECK constraints or even FOREIGN KEY constraints, the RDBMS will confirm whether existing data satisfies those constraints and then only allow you to create the constraint. If the existing data violates the constraint, the constraint will not be created at all.
Just think if you had a column Price with existing data such as 40,50 and 60 and you create a constraint that Price must always be > 100, then what's the point of having the earlier values which are < 100. So the constraint creation will fail in this case unless you delete those values.
Syed Arbab AhmedPosted Apr 11, 2011, 6:24 AM
Mamta MPosted Apr 5, 2011, 7:08 AM
So what you need to do now is first empty that column, remove the data that does not satisfy the constraint, then create the constraint. Then for any new rows that you try to add, the CHECK constraint will verify if gende value satisfies the condition and then only allow insertion.
Syed Arbab AhmedPosted Apr 5, 2011, 7:07 AM
Error starting at line 1 in command:
Alter table arbab ADD CONSTRAINT abcd CHECK (gender in ('Male', 'female'))
Error report:
SQL Error: ORA-02293: cannot validate (IRIS.ABCD) - check constraint violated
02293. 00000 - "cannot validate (%s.%s) - check constraint violated"
*Cause: an alter table operation tried to validate a check constraint to
populated table that had nocomplying values.
*Action: Obvious
there must be a problem related to check constraint.
VulpesPosted Apr 5, 2011, 6:47 AM
Alter table arbab ADD CONSTRAINT abc CHECK (gender >10);
So are you sure that gender is a string rather than a number?
Syed Arbab AhmedPosted Apr 5, 2011, 6:40 AM
VulpesPosted Apr 5, 2011, 4:16 AM
Mamta MPosted Apr 5, 2011, 4:11 AM
Alter table arbab ADD CONSTRAINT abc CHECK (gender='Male' or gender='female')
You have to mention gender twice to check for each value and the = operator was missing.