Jump to content

Archived

This topic is now archived and is closed to further replies.

mikhl

Check Constraint SQL

Recommended Posts

I want to create a check constraint that allows the input of data like 'COMP%' and 'SYS%' and 'INFO%'

 

The code that I have tried is as follows:

alter table subject
add constraint validclassid check(subjectid like 'COMP%', 'INFO%', 'SYS%');

and...

alter table subject
add constraint validclassid check(subjectid like 'COMP%' AND 'INFO%' AND 'SYS%');

and...

alter table subject
add constraint validclassid check(subjectid like 'COMP%' OR 'INFO%' OR 'SYS%');

 

None of the above are working, I could really use your help. Thanks.

 

I am using Oracle 10g

Share this post


Link to post
Share on other sites

A check constraint is like a little piece of validation code, that must evaluation to true or false.  It is not a query.  I'm not really sure what you're going for, but if you literally expect that the subjectid will be the strings 'COMP%' or 'INFO%', then you should have add ... check( subjectid in ('COMP%', 'INFO%', 'SYS%'))

 

If you literally are trying to get "LIKE" behavior so that you only allow words that start with COMP, INFO and SYS, but are actually COMPUTER, INFORMATION etc., then you might try using SUBSTR() to get the first 3 characters, although I don't know if that will work in a constraint.

Share this post


Link to post
Share on other sites

Thanks for your comment.

 

I have figured out what I wanted:

 

add constraint validclassid check(subjectid like 'COMP%' or subjectid like 'INFO%' or subjectid like 'SYS%')

 

Sorry if I wasn't clear :)

Share this post


Link to post
Share on other sites

Thanks for reporting back your results. I didn't think that you could use LIKE in a check constraint, but then my Oracle skills are getting pretty rusty these days.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.