Re: Constraint: string length must be 32 chars

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Alexander Farber <alexander(dot)farber(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Constraint: string length must be 32 chars
Date: 2010-10-16 19:23:44
Message-ID: AANLkTimt_hQ_-HYekpdYdhK1otSq8N-3d-VKNh4uQS2y@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sat, Oct 16, 2010 at 12:15 PM, Alexander Farber
<alexander(dot)farber(at)gmail(dot)com> wrote:
> Hello,
>
> I'm trying to create a table, where md5 strings will serve as primary keys.
> So I'd like to add a constraing that the key length should be 32 chars long
> (and contain [a-fA-F0-9] only):

why don't you use the bytea type, and cut the key size down 50%? You
can always format it going out the door if you want it displayed hex.
Besides being faster, you get to skip the 'is hex' regex.

create table foo(id bytea check(length(id) = 16));
insert into foo values (decode(md5('a'), 'hex')); -- if not using pgcrypto
insert into foo values (digest('b', 'md5')); -- if using pgcrypto (preferred)

select encode(id, 'hex') from foo;

merlin

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Rob Sargent 2010-10-16 21:08:03 Re: Constraint: string length must be 32 chars
Previous Message Guy Rouillier 2010-10-16 17:15:18 Re: Constraint: string length must be 32 chars