Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

some questions ...

Status
Not open for further replies.

totti24

Programmer
Aug 2, 2002
32
MY

hi all, can i know how to set a column in a table to consist only certain values? i've use the check contraint when creating the table. but once i have created the table, i cant describe it. it says that my pg_relcheck doesn't exist.


i've use the ddl below

create table test
(field1 varchar(1),
field2 varchar(1) DEFAULT 'N' CHECK (field2 = 'A' OR field2 = 'R'));

please advice....
 

cannot coz that column can only consist of 3 values which are N,A and R. and the default value should be N. any updates will make the field change to either A or R. hope u understand what i'm trying to say here. =)
 
then the check should be
default 'N'
CHECK (field2 = 'A' OR field2 = 'R' OR field2 ='N')

however this still means that if someone changes N to A(or R) , he can still change it later to N
We make sure from the front end that the values are not rechanged to N
 
Well, of course you can't set a table predicate that violates itself. The CHECK constraint is applied against all possible values in that column, including the default value.

So, you need to include the 3-value logic in your CHECK statement. I am not sure just how much logic can be encapsulated in a CHECK statement, but you might try some IF ... THEN logic.

Or, a more typical approach is to use a trigger, which runs a stored procedure. Stored procedures can handle any kind of logic you want, and can easily deal with comparing existing values to new values the user tries to enter. -------------------------------------------

My PostgreSQL FAQ --
 

i've created a table based on the logic provided. but i can't seem to describe my table. the error i got is "Relation pg_relcheck does not exist".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top