Setting up Field constraints

Started by Joseph Maxwellabout 24 years ago3 messagesgeneral
Jump to latest
#1Joseph Maxwell
jemaxwell@jaymax.com

Hello,
I have created a DB and TABLE but would like to constrain one field to
accept either "YES" or "NO" only. How can I set this up?
I have tried various syntactic constructs of

Prior_Rx varchar(3) NULL CHECK ('Yes', 'No'),
Prior_Rx varchar(3) NULL CHECK (VALUE 'Yes', 'No'),
Prior_Rx varchar(3) NULL CHECK ('Yes', 'No'),
Prior_Rx varchar(3) NULL CONSTRAINT (CHECK (VALUE 'Yes', 'No')),
Prior_Rx varchar(3) NULL CONSTRAINT (CHECK ('Yes', 'No')),
Prior_Rx varchar(3) NULL CONSTRAINT (CHECK (VALUE Yes, No)),
Prior_Rx varchar(3) NULL CONSTRAINT (CHECK (Yes, No)),
Prior_Rx varchar(3) NULL CONSTRAINT (CHECK (Yes OR No)),
Prior_Rx varchar(3) NULL CHECK (Yes OR No),
Prior_Rx varchar(3) NULL CHECK ('Yes' OR 'No'),

Obviously they were all wrong, could someone advise?
Thanks

-- Joe --
---------------------------------------------------------------------------------------
PS:
Just learned that the datatype should be 'bool' in this case

haven't tested it yet

But had I wanted to restrict it to say either mike or john, or even one
of a list say, 'tea', 'coffee', 'chocolate' or 'espresso', how should
that be constructed?

#2Stephan Szabo
sszabo@megazone23.bigpanda.com
In reply to: Joseph Maxwell (#1)
Re: Setting up Field constraints

On Wed, 6 Mar 2002, Joseph Maxwell wrote:

Hello,
I have created a DB and TABLE but would like to constrain one field to
accept either "YES" or "NO" only. How can I set this up?

Probably something like:
Prior_Rx varchar(3) CHECK (Prior_Rx in ('Yes','No'))

#3Jeff Self
jself@nngov.com
In reply to: Joseph Maxwell (#1)
Re: Setting up Field constraints

Why not use a Boolean type?

On Wed, 2002-03-06 at 18:51, Joseph Maxwell wrote:

Hello,
I have created a DB and TABLE but would like to constrain one field to
accept either "YES" or "NO" only. How can I set this up?
I have tried various syntactic constructs of

Prior_Rx varchar(3) NULL CHECK ('Yes', 'No'),
Prior_Rx varchar(3) NULL CHECK (VALUE 'Yes', 'No'),
Prior_Rx varchar(3) NULL CHECK ('Yes', 'No'),
Prior_Rx varchar(3) NULL CONSTRAINT (CHECK (VALUE 'Yes', 'No')),
Prior_Rx varchar(3) NULL CONSTRAINT (CHECK ('Yes', 'No')),
Prior_Rx varchar(3) NULL CONSTRAINT (CHECK (VALUE Yes, No)),
Prior_Rx varchar(3) NULL CONSTRAINT (CHECK (Yes, No)),
Prior_Rx varchar(3) NULL CONSTRAINT (CHECK (Yes OR No)),
Prior_Rx varchar(3) NULL CHECK (Yes OR No),
Prior_Rx varchar(3) NULL CHECK ('Yes' OR 'No'),

Obviously they were all wrong, could someone advise?
Thanks

-- Joe --
---------------------------------------------------------------------------------------
PS:
Just learned that the datatype should be 'bool' in this case

haven't tested it yet

But had I wanted to restrict it to say either mike or john, or even one
of a list say, 'tea', 'coffee', 'chocolate' or 'espresso', how should
that be constructed?

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

http://archives.postgresql.org

--
Jeff Self
Information Technology Analyst
Department of Personnel
City of Newport News
2400 Washington Ave.
Newport News, VA 23607
757-926-6930