Check Constraints having alphanumeric [message #643156] |
Wed, 30 September 2015 12:46 |
lokimisc
Messages: 101 Registered: February 2008
|
Senior Member |
|
|
Hi,
I'm trying to create table having check constraints on Project Id column such that it will have 'PRJ' + 0 to 9 values i.e first 3 character should be 'PRJ' followed by numeric values and I don't want to use Regular Expression.
Eg: PRJ0001, PRJ0012, PRJ1234....
Could you please anyone help to achieve above scenario
Create table Project
(
Pid VACHAR2(50), -- CHECK Pid LIKE 'PRJ'||[0-9]
PName VARCHAR(1000),
PLocation VARCHAR2(1000)
)
Regards,
Lokesh
|
|
|
|
|
|
Re: Check Constraints having alphanumeric [message #643168 is a reply to message #643156] |
Wed, 30 September 2015 15:06 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Actually, all values you provided have PRJ followed by 4 digit number. Is number of digits always the same? If so, then:
alter table project
add constraint project_chk1
check(
translate(pid,'0123456789','0000000000') = 'PRJ0000'
)
/
And if, for example, number of digits is always 6:
alter table project
add constraint project_chk1
check(
translate(pid,'0123456789','0000000000') = 'PRJ000000'
)
/
SY.
|
|
|
|
|
Re: Check Constraints having alphanumeric [message #643225 is a reply to message #643189] |
Thu, 01 October 2015 06:43 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
Speed could be a factor:
SQL> drop table tbl purge
2 /
Table dropped.
SQL> create table tbl(pid varchar2(9))
2 /
Table created.
SQL> alter table tbl
2 add constraint tbl_chk1
3 check(
4 translate(pid,'0123456789','0000000000') = 'PRJ000000'
5 )
6 /
Table altered.
SQL> set timing on
SQL> insert
2 into tbl
3 select 'PRJ' || lpad(level,6,'0')
4 from dual
5 connect by level <= 10000
6 /
10000 rows created.
Elapsed: 00:00:00.02
SQL> set timing off
SQL> drop table tbl purge
2 /
Table dropped.
SQL> create table tbl(pid varchar2(9))
2 /
Table created.
SQL> alter table tbl
2 add constraint tbl_chk1
3 check(
4 regexp_like(pid,'^PRJ\d{6}$')
5 )
6 /
Table altered.
SQL> set timing on
SQL> insert
2 into tbl
3 select 'PRJ' || lpad(level,6,'0')
4 from dual
5 connect by level <= 10000
6 /
10000 rows created.
Elapsed: 00:00:00.04
SQL> set timing off
SQL>
SY.
|
|
|
|
Re: Check Constraints having alphanumeric [message #643240 is a reply to message #643156] |
Thu, 01 October 2015 09:13 |
lokimisc
Messages: 101 Registered: February 2008
|
Senior Member |
|
|
Hello All,
I just wanted to know alternative method to achieve result other than Regular expression (no specific reason).
But I was not knowing, Solomon Yakobson's solution gives better performance than using regular expression.
Thanks Solomon Yakobson for your analysis.
Regards,
Lokesh
|
|
|