2013-08-07 72 views
2

因此,我想檢查varchar2的格式是4個字母字符,然後是3個數字字符,例如AABB123或LMNO987Oracle:檢查varchar2中的字母和數字字符的約束條件

到目前爲止,我已經試過:

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 LIKE '[^A-Z][^A-Z][^A-Z][^A-Z][0-9][0-9][0-9]' 

CONSTRAINT Check_Code check (Code LIKE '[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 LIKE '[^A-Z]{4}[0-9]{3}') 

但僅僅得到錯誤:

insert into 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: The values being inserted do not satisfy the named check 
*Action: do not insert values that violate the constraint. 

回答

2

的正則表達式是不正確的,你使用的方式是相當繁瑣。

相反,您可以選擇使用POSIX character classes。在特定情況下,它會像這樣:

[[:alpha:]]{4}[[:digit:]]{3} 

所以約束檢查是

CONSTRAINT Code_Check check (REGEXP_LIKE(Paper_Code,'[[:alpha:]]{4}[[:digit:]]{3}') 

此檢查有4個字母字符,隨後3的數字。

SQL fiddle上的示例&更多信息可在regexp cheatsheet