Re: newbie: Column CHECK(col contains '@') ?

Lists: pgsql-novice
From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: newbie: Column CHECK(col contains '@') ?
Date: 2002-05-12 17:30:45
Message-ID: web-1457726@davinci.ethosmedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

JP,

> One column in my table contains email addresses - I want to check
> that any value entered contains a '@'. How do I create a CONSTRAINT
> or CHECK to ensure this when creating the table?

Here's the online documentation. It's very good, you should give it a
try!

http://www.us.postgresql.org/users-lounge/docs/7.2/postgres/sql-createtable.html

-Josh Berkus


From: john-paul delaney <jp(at)justatest(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: newbie: Column CHECK(col contains '@') ?
Date: 2002-05-12 19:11:00
Message-ID: Pine.LNX.4.21.0205121705340.1265-100000@justatest.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice


Hello list...

One column in my table contains email addresses - I want to check that any value entered contains a '@'. How do I create a CONSTRAINT or CHECK to ensure this when creating the table?

Thanks for any help.
/j-p.

-----------------------
JUSTATEST Art Online
www.justatest.com


From: "Joel Burton" <joel(at)joelburton(dot)com>
To: "john-paul delaney" <jp(at)justatest(dot)com>, <pgsql-novice(at)postgresql(dot)org>
Subject: Re: newbie: Column CHECK(col contains '@') ?
Date: 2002-05-12 19:50:50
Message-ID: JGEPJNMCKODMDHGOBKDNEEMCCNAA.joel@joelburton.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

> One column in my table contains email addresses - I want to check
> that any value entered contains a '@'. How do I create a
> CONSTRAINT or CHECK to ensure this when creating the table?

create table em (
em text constraint is_email check (em like '%(at)%(dot)%')
);

will work fine, assuming that this check (something @ something . something)
is acceptable in your context as "looks like an email address"

- J.

Joel BURTON | joel(at)joelburton(dot)com | joelburton.com | aim: wjoelburton
Knowledge Management & Technology Consultant


From: john-paul delaney <jp(at)justatest(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: newbie: Column CHECK(col contains '@') ?
Date: 2002-05-12 23:21:05
Message-ID: Pine.LNX.4.21.0205122114590.1508-100000@justatest.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

Thanks Joel... that did the trick (even better than I had asked for).
Forgive my ignorance, but it your solution a regular expression?

Can anyone suggest a good source where I can read up on these (regex's) in relation to postgresql?

thanks again,
/j-p.

On Sun, 12 May 2002, Joel Burton wrote:

> > One column in my table contains email addresses - I want to check
> > that any value entered contains a '@'. How do I create a
> > CONSTRAINT or CHECK to ensure this when creating the table?
>
> create table em (
> em text constraint is_email check (em like '%(at)%(dot)%')
> );
>
> will work fine, assuming that this check (something @ something . something)
> is acceptable in your context as "looks like an email address"
>
> - J.
>
> Joel BURTON | joel(at)joelburton(dot)com | joelburton.com | aim: wjoelburton
> Knowledge Management & Technology Consultant
>

-----------------------
JUSTATEST Art Online
www.justatest.com


From: Andrew McMillan <andrew(at)catalyst(dot)net(dot)nz>
To: john-paul delaney <jp(at)justatest(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: newbie: Column CHECK(col contains '@') ?
Date: 2002-05-12 23:32:21
Message-ID: 1021246341.22269.2651.camel@kant.mcmillan.net.nz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

On Mon, 2002-05-13 at 11:21, john-paul delaney wrote:
> Thanks Joel... that did the trick (even better than I had asked for).
> Forgive my ignorance, but it your solution a regular expression?
>
> Can anyone suggest a good source where I can read up on these (regex's)
> in relation to postgresql?

The PostgreSQL manual has a section (section 4.6) on pattern matching
using REGEX and pattern matching using the SQL 'LIKE' operator.

The LIKE operator (which was what Joel used in his solution for you)
uses '%' as a wildcard and _ as a single character match.

Regex is much more complicated, and there are many sources of help for
it out on the internet. A similar check using a regex operator would be
something like:

(em ~ '@.*\.')

since there is no need to specify leading and trailing wildcards within
a regex (instead you specify that you want to anchor the regex to the
beginning and/or ending of the string).

A search on the internet might provide a more thorough regex for
validation of e-mail addresses. A slightly more complex one I have used
is:

(em ~* '^[^(at)]+@[a-z0-9-]+\.[a-z]+')

which should validate (a) there is only a single '@' in the address and
(b) the first part of the domain name contains only valid domain-name
like characters. The ~* operator is the case insensitive regex match
which I didn't use in the one above since there was no alphabetic
matching involved.

How I do this in my own applications is actually to implement a function
for valid email addresses, viz:

CREATE FUNCTION valid_email(TEXT) RETURNS BOOLEAN AS '
DECLARE
email ALIAS FOR $1;
user TEXT;
domain TEXT;
BEGIN
IF email !~ ''(dot)(at)(dot)'' THEN
RETURN FALSE; -- One @ good
END IF;
IF email ~ ''@.*@'' THEN
RETURN FALSE; -- Two @s bad
END IF;
domain := substring( email from position( ''@'' in email) + 1 );
user := substring( email from 1 for position( ''@'' in email) - 1
);
IF domain ~* ''([a-z0-9-]+\.)+([a-z])?[a-z][a-z]$'' THEN
-- Only really worth validating the domain
RETURN TRUE;
END IF;
RETURN FALSE;
END;
' LANGUAGE 'plpgsql';

This checks for internal spaces as well, and means that the rules for
valid e-mail addresses happens in only one place.

Regards,
Andrew.
>
> thanks again,
> /j-p.
>
>
> On Sun, 12 May 2002, Joel Burton wrote:
>
> > > One column in my table contains email addresses - I want to check
> > > that any value entered contains a '@'. How do I create a
> > > CONSTRAINT or CHECK to ensure this when creating the table?
> >
> > create table em (
> > em text constraint is_email check (em like '%(at)%(dot)%')
> > );
> >
> > will work fine, assuming that this check (something @ something . something)
> > is acceptable in your context as "looks like an email address"
> >
> > - J.
> >
> > Joel BURTON | joel(at)joelburton(dot)com | joelburton.com | aim: wjoelburton
> > Knowledge Management & Technology Consultant
> >
>
>
> -----------------------
> JUSTATEST Art Online
> www.justatest.com
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
> message can get through to the mailing list cleanly
>
--
--------------------------------------------------------------------
Andrew @ Catalyst .Net.NZ Ltd, PO Box 11-053, Manners St, Wellington
WEB: http://catalyst.net.nz/ PHYS: Level 2, 150-154 Willis St
DDI: +64(4)916-7201 MOB: +64(21)635-694 OFFICE: +64(4)499-2267
Are you enrolled at http://schoolreunions.co.nz/ yet?


From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: john-paul delaney <jp(at)justatest(dot)com>, pgsql-novice(at)postgresql(dot)org
Subject: Re: newbie: Column CHECK(col contains '@') ?
Date: 2002-05-13 04:17:12
Message-ID: web-1458032@davinci.ethosmedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

JP,

> Thanks Joel... that did the trick (even better than I had asked for).
>
> Forgive my ignorance, but it your solution a regular expression?

Actually, no. The LIKE and ILIKE operators are a regular part of SQL.
They really only accept one "wildcard", the % in place of "anything".

Postgres has a Regexp operator, "~" (the tilde) which does Unix-style
pattern-matching.

> Can anyone suggest a good source where I can read up on these
> (regex's) in relation to postgresql?

2 Places: Functions and Operators, in the online docs.
Any beginner's guid to PostgreSQL, such as Bruce Momjian's book or the
Wrox Press book. (see http://techdocs.postgresql.org/ ----> book
reviews).

-Josh Berkus


From: john-paul delaney <jp(at)justatest(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: newbie: Column CHECK(col contains '@') ?
Date: 2002-05-13 08:51:06
Message-ID: Pine.LNX.4.21.0205130630400.2108-100000@justatest.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

Apologies Josh... I'm so dumb. I overlooked the Functions and Opterators chapter (it's taking me a little time to assimilate the structure of online documentation). Thanks for your patience and explanations.

/j-p.

On Sun, 12 May 2002, Josh Berkus wrote:

> JP,
>
> > Thanks Joel... that did the trick (even better than I had asked for).
> >
> > Forgive my ignorance, but it your solution a regular expression?
>
> Actually, no. The LIKE and ILIKE operators are a regular part of SQL.
> They really only accept one "wildcard", the % in place of "anything".
>
> Postgres has a Regexp operator, "~" (the tilde) which does Unix-style
> pattern-matching.
>
> > Can anyone suggest a good source where I can read up on these
> > (regex's) in relation to postgresql?
>
> 2 Places: Functions and Operators, in the online docs.
> Any beginner's guid to PostgreSQL, such as Bruce Momjian's book or the
> Wrox Press book. (see http://techdocs.postgresql.org/ ----> book
> reviews).
>
> -Josh Berkus
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
> (send "unregister YourEmailAddressHere" to majordomo(at)postgresql(dot)org)
>

-----------------------
JUSTATEST Art Online
www.justatest.com


From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: john-paul delaney <jp(at)justatest(dot)com>, pgsql-novice(at)postgresql(dot)org
Subject: Re: newbie: Column CHECK(col contains '@') ?
Date: 2002-05-13 14:51:01
Message-ID: web-1458287@davinci.ethosmedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

JP,

> Apologies Josh... I'm so dumb. I overlooked the Functions and
> Opterators chapter (it's taking me a little time to assimilate the
> structure of online documentation). Thanks for your patience and
> explanations.

Well, the online docs are a reference, not a learning tool. I heartily
reccommend that you get a PostgreSQL book, it'll save you a *lot* of
time asking questions of the list.

-Josh


From: "Juliet May" <jmay(at)speark(dot)com>
To: <pgsql-novice(at)postgresql(dot)org>
Subject: Re: newbie: Column CHECK(col contains '@') ?
Date: 2002-05-13 18:55:42
Message-ID: 007301c1faaf$c83371c0$a1d2a8c0@sweetbeet2k
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

I second that!! I bought the PostgreSQL Developer's Handbook and it has been
a G*D send. Between that and Professional PHP4 I've been able to make a lot
of progress without going in too many circles.

Although I still don't understand half of what is said on here :)

Julie
<snip>
----- Original Message -----
From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: "john-paul delaney" <jp(at)justatest(dot)com>; <pgsql-novice(at)postgresql(dot)org>

> Well, the online docs are a reference, not a learning tool. I heartily
> reccommend that you get a PostgreSQL book, it'll save you a *lot* of
> time asking questions of the list.
>
> -Josh


From: "Joel Burton" <joel(at)joelburton(dot)com>
To: "john-paul delaney" <jp(at)justatest(dot)com>, <pgsql-novice(at)postgresql(dot)org>
Subject: Re: newbie: Column CHECK(col contains '@') ?
Date: 2002-05-16 20:36:43
Message-ID: JGEPJNMCKODMDHGOBKDNOEHKCOAA.joel@joelburton.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

> > CREATE FUNCTION valid_email(TEXT) RETURNS BOOLEAN AS '
> > DECLARE
> > email ALIAS FOR $1;
> > user TEXT;
> > domain TEXT;
> > BEGIN
> > IF email !~ ''(dot)(at)(dot)'' THEN
> > RETURN FALSE; -- One @ good
> > END IF;
> > IF email ~ ''@.*@'' THEN
> > RETURN FALSE; -- Two @s bad
> > END IF;
> > domain := substring( email from position( ''@'' in email) + 1 );
> > user := substring( email from 1 for position( ''@'' in email) - 1
> > );
> > IF domain ~* ''([a-z0-9-]+\.)+([a-z])?[a-z][a-z]$'' THEN
> > -- Only really worth validating the domain
> > RETURN TRUE;
> > END IF;
> > RETURN FALSE;
> > END;
> > ' LANGUAGE 'plpgsql';
> >
> > This checks for internal spaces as well, and means that the rules for

Fine idea, but be careful about the regex for domains: it tries to ensure
that the TLD ending (.com, .us, etc) is 2-3 characters long. ".intl" and
".info" are both legal TLDs that are four characters long. A better replace
for the line is

> > IF domain ~* ''([a-z0-9-]+\.)+([a-z])*[a-z][a-z]$'' THEN
^- note was a ? before

For succintness' sake, though, this seems like overkill: 3 regex matches
that could be collapsed into one. I'd do:

em ~* '^[^(at)]+@[a-z0-9-]+\.[a-z]*[a-z][a-z]'

(slightly modified from Andrew's earlier suggestion to include the 2-or-more
chars in TLD)

Not sure how this will play with domains with non-US characters.

- J.

Joel BURTON | joel(at)joelburton(dot)com | joelburton.com | aim: wjoelburton
Knowledge Management & Technology Consultant


From: john-paul delaney <jp(at)justatest(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: newbie: Column CHECK(col contains '@') ?
Date: 2002-05-17 00:27:52
Message-ID: Pine.LNX.4.21.0205162220590.20402-100000@justatest.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice


Wow... and this is the Novice list? Thanks Andrew, you cover it all from a to z. It'll take me a bit of work to digest your message and put it into practice, as the (weird and) wonderful world of regex's is very new to me.

regards
/j-p.

On 13 May 2002, Andrew McMillan wrote:

> On Mon, 2002-05-13 at 11:21, john-paul delaney wrote:
> > Thanks Joel... that did the trick (even better than I had asked for).
> > Forgive my ignorance, but it your solution a regular expression?
> >
> > Can anyone suggest a good source where I can read up on these (regex's)
> > in relation to postgresql?
>
> The PostgreSQL manual has a section (section 4.6) on pattern matching
> using REGEX and pattern matching using the SQL 'LIKE' operator.
>
> The LIKE operator (which was what Joel used in his solution for you)
> uses '%' as a wildcard and _ as a single character match.
>
> Regex is much more complicated, and there are many sources of help for
> it out on the internet. A similar check using a regex operator would be
> something like:
>
> (em ~ '@.*\.')
>
> since there is no need to specify leading and trailing wildcards within
> a regex (instead you specify that you want to anchor the regex to the
> beginning and/or ending of the string).
>
> A search on the internet might provide a more thorough regex for
> validation of e-mail addresses. A slightly more complex one I have used
> is:
>
> (em ~* '^[^(at)]+@[a-z0-9-]+\.[a-z]+')
>
> which should validate (a) there is only a single '@' in the address and
> (b) the first part of the domain name contains only valid domain-name
> like characters. The ~* operator is the case insensitive regex match
> which I didn't use in the one above since there was no alphabetic
> matching involved.
>
> How I do this in my own applications is actually to implement a function
> for valid email addresses, viz:
>
> CREATE FUNCTION valid_email(TEXT) RETURNS BOOLEAN AS '
> DECLARE
> email ALIAS FOR $1;
> user TEXT;
> domain TEXT;
> BEGIN
> IF email !~ ''(dot)(at)(dot)'' THEN
> RETURN FALSE; -- One @ good
> END IF;
> IF email ~ ''@.*@'' THEN
> RETURN FALSE; -- Two @s bad
> END IF;
> domain := substring( email from position( ''@'' in email) + 1 );
> user := substring( email from 1 for position( ''@'' in email) - 1
> );
> IF domain ~* ''([a-z0-9-]+\.)+([a-z])?[a-z][a-z]$'' THEN
> -- Only really worth validating the domain
> RETURN TRUE;
> END IF;
> RETURN FALSE;
> END;
> ' LANGUAGE 'plpgsql';
>
> This checks for internal spaces as well, and means that the rules for
> valid e-mail addresses happens in only one place.
>
> Regards,
> Andrew.
> >
> > thanks again,
> > /j-p.
> >
> >
> > On Sun, 12 May 2002, Joel Burton wrote:
> >
> > > > One column in my table contains email addresses - I want to check
> > > > that any value entered contains a '@'. How do I create a
> > > > CONSTRAINT or CHECK to ensure this when creating the table?
> > >
> > > create table em (
> > > em text constraint is_email check (em like '%(at)%(dot)%')
> > > );
> > >
> > > will work fine, assuming that this check (something @ something . something)
> > > is acceptable in your context as "looks like an email address"
> > >
> > > - J.
> > >
> > > Joel BURTON | joel(at)joelburton(dot)com | joelburton.com | aim: wjoelburton
> > > Knowledge Management & Technology Consultant
> > >
> >
> >
> > -----------------------
> > JUSTATEST Art Online
> > www.justatest.com
> >
> >
> >
> >
> > ---------------------------(end of broadcast)---------------------------
> > TIP 3: if posting/reading through Usenet, please send an appropriate
> > subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
> > message can get through to the mailing list cleanly
> >
>

-----------------------
JUSTATEST Art Online
www.justatest.com