sql - Oracle: Check constraint for both alphabetical and numerical characters in varchar2 -
so want check varchar2 in format of 4 alphabetical characters 3 numerical characters e.g. aabb123 or lmno987
so far i've tried:
constraint code_check check (regexp_like(code,'[^a-z][^a-z][^a-z][^a-z][0-9][0-9][0-9]')) constraint check_code check (code '[^a-z][^a-z][^a-z][^a-z][0-9][0-9][0-9]' constraint check_code check (code '[a-z][a-z][a-z][a-z][0-9][0-9][0-9]') constraint check_code check (code regexp_like '[a-z][a-z][a-z][a-z][0-9][0-9][0-9]') constraint check_code check (code '[^a-z]{4}[0-9]{3}')
but error:
insert table1 (code) values ('help555') error report: sql error: ora-02290: check constraint (bob.table1_check_code) violated 02290. 00000 - "check constraint (%s.%s) violated" *cause: values being inserted not satisfy named check *action: not insert values violate constraint.
the regular expressions not right, , way you've used pretty cumbersome.
instead, can opt use posix character classes. in specific case, so:
[[:alpha:]]{4}[[:digit:]]{3}
so constraint check
constraint code_check check (regexp_like(paper_code,'[[:alpha:]]{4}[[:digit:]]{3}')
this checks there 4 alphabetic characters, followed 3 numeric.
example on sql fiddle & more info available @ regexp cheatsheet
Comments
Post a Comment