Monday, August 11, 2014

Oracle DB check contraints based on regular expression

It's good to know that for "check constraint" you can use any arbitrary regular expression http://docs.oracle.com/cd/E11882_01/server.112/e17118/conditions007.htm#SQLRF52150 .

Example:

create table PIPPO (name varchar2 (32));
alter table PIPPO add constraint check_name check (REGEXP_LIKE(name,'bi|bo|ba','c')); 


If you try inserting some crap you get:

Error starting at line 5 in command:
INSERT INTO "CMDBUSER"."PIPPO" (NAME) VALUES ('bu')
Error report:
SQL Error: ORA-02290: check constraint (CMDBUSER.CHECK_NAME) violated
02290. 00000 -  "check constraint (%s.%s) violated"
*Cause:    The values being inserted do not satisfy the named check
           
*Action:   do not insert values that violate the constraint.


If you get something like this below it means that your regex is bad or that the last parameter of REGEXP_LIKE is not legal (e.g. if you enter it UPPER CASE instead of lower case):

INSERT INTO "CMDBUSER"."PIPPO" (NAME) VALUES ('bu')
ORA-01760: illegal argument for function
ORA-06512: at line 1


No comments: