Search code examples
sqloracle-databaseconstraints

Check constraint not reading [A-Z]?


I am having an issue with a check constraint reading [A-Z].

here is the code:

drop table test CASCADE CONSTRAINTS;

Create table test (
post Varchar2(6),
CHECK (post like '[A-Z]')
);

insert into test values ('A');

And I receive a "check constraint violated" error after trying to insert A. Any feedback would be appreciated. Even if it does work for you because everything is telling me it should work.


Solution

  • Use REGEXP_LIKE:

    CREATE TABLE test (
        post Varchar2(6),
        CHECK (REGEXP_LIKE(post, '^[A-Z]+$'))
    );
    

    The pattern ^[A-Z]+$ would match posts which only contain capital letters. If instead you want to flag posts which begin with a capital letter, then use ^[A-Z].