Skip site navigation (1) Skip section navigation (2)

Re: CHECK Constraints

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: "Van Ingen, Lane" <lvaningen(at)ESNCC(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: CHECK Constraints
Date: 2005-04-22 16:19:28
Message-ID: 20050422161928.GA50858@winnie.fuhr.org (view raw or flat)
Thread:
Lists: pgsql-novice
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/

In response to

pgsql-novice by date

Next:From: Bruno Wolff IIIDate: 2005-04-22 16:37:05
Subject: Re: CHECK Constraints
Previous:From: Walker, Jed SDate: 2005-04-22 16:11:00
Subject: Re: Authorized privileges when calling a procedure

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group