Unix Technical Forum

CHECK Constraints

This is a discussion on CHECK Constraints within the pgsql Novice forums, part of the PostgreSQL category; --> Hi all, the 8.0 manual says CHECK can be used in the following manner: CHECK (expression) The only stipulations ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Novice

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 09:44 PM
Van Ingen, Lane
 
Posts: n/a
Default CHECK Constraints

Hi all,
the 8.0 manual says CHECK can be used in the following manner:
CHECK (expression)
The only stipulations are that it must produce a boolean result (true or unknown) to succeed.

If I were to specify a the following on a field named price in one expression, would it succeed?
CHECK (price = 1 OR price = 2 OR price = 3)

In other words, I would want to raise an exception on any other value other than 1, 2, or 3. Can
this be done? All of the examples in the manual show a single check (like price > 0) .

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-17-2008, 09:44 PM
Bruno Wolff III
 
Posts: n/a
Default Re: CHECK Constraints

On Fri, Apr 22, 2005 at 12:02:41 -0400,
"Van Ingen, Lane" <lvaningen@ESNCC.com> wrote:
> Hi all,
> the 8.0 manual says CHECK can be used in the following manner:
> CHECK (expression)
> The only stipulations are that it must produce a boolean result (true or unknown) to succeed.
>
> If I were to specify a the following on a field named price in one expression, would it succeed?
> CHECK (price = 1 OR price = 2 OR price = 3)
>
> In other words, I would want to raise an exception on any other value other than 1, 2, or 3. Can
> this be done? All of the examples in the manual show a single check (like price > 0) .


Yes, you can do that.

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-17-2008, 09:44 PM
Michael Fuhr
 
Posts: n/a
Default Re: CHECK Constraints

On Fri, Apr 22, 2005 at 12:02:41PM -0400, Van Ingen, Lane wrote:
>
> If I were to specify a the following on a field named price in one
> expression, would it succeed?
> CHECK (price = 1 OR price = 2 OR price = 3)


What happened when you tried it? A simple test should answer the
question.

CREATE TABLE foo (
id serial PRIMARY KEY,
price integer NOT NULL CHECK (price = 1 OR price = 2 OR price = 3)
);

INSERT INTO foo (price) VALUES (1);
INSERT 0 1

INSERT INTO foo (price) VALUES (2);
INSERT 0 1

INSERT INTO foo (price) VALUES (3);
INSERT 0 1

INSERT INTO foo (price) VALUES (0);
ERROR: new row for relation "foo" violates check constraint "foo_price_check"

INSERT INTO foo (price) VALUES (4);
ERROR: new row for relation "foo" violates check constraint "foo_price_check"

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 12:07 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com