Home » SQL & PL/SQL » SQL & PL/SQL » Check Constraints having alphanumeric (Oracle 10g)
Check Constraints having alphanumeric [message #643156] Wed, 30 September 2015 12:46 Go to next message
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 #643161 is a reply to message #643156] Wed, 30 September 2015 13:31 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
I don't want to use Regular Expression.


Why?

Re: Check Constraints having alphanumeric [message #643162 is a reply to message #643156] Wed, 30 September 2015 13:34 Go to previous messageGo to next message
Littlefoot
Messages: 21807
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Quote:

I don't want to use Regular Expression

Why not? It works just fine and is easy to implement:
CREATE TABLE project
(
   pid   VARCHAR2 (50)
            CONSTRAINT ch_pid_format CHECK (REGEXP_LIKE (pid, '^PID[0-9]+$'))
);


If you don't want to use it, I don't know whether you can (or can not) do it through the check constraint. An option that would work is to enforce that constraint through a database trigger, but that's too much programming for something that can be solved in an easier manner.
Re: Check Constraints having alphanumeric [message #643167 is a reply to message #643156] Wed, 30 September 2015 14:57 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
alter table project
  add constraint project_chk1
    check(
              pid like 'PRJ_%'
          and
              rtrim(pid,'0123456789') = 'PRJ'
         )
/


SY.
Re: Check Constraints having alphanumeric [message #643168 is a reply to message #643156] Wed, 30 September 2015 15:06 Go to previous messageGo to next message
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 #643185 is a reply to message #643168] Thu, 01 October 2015 02:53 Go to previous messageGo to next message
lokimisc
Messages: 101
Registered: February 2008
Senior Member
Thanks a lot Little foot and Solomon Yakobson for your help.... Smile
I was trying to achieve the result without using regular expressions.

Your help is very much appreciated.... Smile

Regards,
Lokesh
Re: Check Constraints having alphanumeric [message #643189 is a reply to message #643185] Thu, 01 October 2015 03:00 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

But the question is why?
Maybe your answer will help other people, so please answer.

Re: Check Constraints having alphanumeric [message #643225 is a reply to message #643189] Thu, 01 October 2015 06:43 Go to previous messageGo to next message
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 #643227 is a reply to message #643225] Thu, 01 October 2015 06:49 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Yes, it could be, but what is OP's reason? (I do not doubt that his reason will be now performances.)

Re: Check Constraints having alphanumeric [message #643240 is a reply to message #643156] Thu, 01 October 2015 09:13 Go to previous message
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
Previous Topic: DBMS_AQ
Next Topic: HOW TO UPDATE ONE FIELD IN A TABLE BASED ON OTHER ?
Goto Forum:
  


Current Time: Tue Apr 23 18:18:50 CDT 2024