Discussion:
[ADMIN] Is it possible to create a CHECK constraint for my use case?
(too old to reply)
Gnanakumar
2012-01-31 08:11:00 UTC
Permalink
Hi,

Our Production server is running PostgreSQL v8.2.22 on CentOS5.2. We want
to enable a specific CHECK constraint for our application table as explained
below:

There are 2 columns in the table: "managertype" numeric(1) and "managerid"
numeric(10). "managertype" accepts only 2 valid values, either '1' or '2'.

managertype | managerid
---------------------------------
1 | null values NOT allowed
2 | null values allowed
---------------------------------

We want to enable a CHECK constraint based on 2 columns data in the table in
such a way that if "managertype" is 1, then null values are not allowed in
"managerid" column. In other words, I want to create a CHECK constraint
something like this: IF (managertype = 1) THEN managerid IS NOT NULL.

Is it possible to create a CHECK constraint for my use case explained above?

Regards,
Gnanam
--
Sent via pgsql-admin mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin
Thomas Kellerer
2012-01-31 08:33:19 UTC
Permalink
Post by Gnanakumar
Hi,
Our Production server is running PostgreSQL v8.2.22 on CentOS5.2.
You know that 8.2 is "end-of-live"?
http://www.postgresql.org/support/versioning/
Post by Gnanakumar
There are 2 columns in the table: "managertype" numeric(1) and "managerid"
numeric(10). "managertype" accepts only 2 valid values, either '1' or '2'.
managertype | managerid
---------------------------------
1 | null values NOT allowed
2 | null values allowed
---------------------------------
We want to enable a CHECK constraint based on 2 columns data in the table in
such a way that if "managertype" is 1, then null values are not allowed in
"managerid" column. In other words, I want to create a CHECK constraint
something like this: IF (managertype = 1) THEN managerid IS NOT NULL.
Is it possible to create a CHECK constraint for my use case explained above?
Something like this:

check ( (managertype = 2) or (managertype = 1 and managerid is not null) )

Not sure if the condition (managertype = 2) is actually needed, if the managertype is already constrained to 1/2 but I can't test it right now.

Thomas
--
Sent via pgsql-admin mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin
Walter Hurry
2012-02-01 01:04:58 UTC
Permalink
Post by Gnanakumar
Hi,
Our Production server is running PostgreSQL v8.2.22 on CentOS5.2. We
want to enable a specific CHECK constraint for our application table as
There are 2 columns in the table: "managertype" numeric(1) and
"managerid" numeric(10). "managertype" accepts only 2 valid values,
either '1' or '2'.
managertype | managerid ---------------------------------
1 | null values NOT allowed 2 | null values
allowed
---------------------------------
We want to enable a CHECK constraint based on 2 columns data in the
table in such a way that if "managertype" is 1, then null values are not
allowed in "managerid" column. In other words, I want to create a CHECK
constraint something like this: IF (managertype = 1) THEN managerid IS
NOT NULL.
Is it possible to create a CHECK constraint for my use case explained above?
Production server? Bollocks. It's homework. Look it up.
--
Sent via pgsql-admin mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin
Continue reading on narkive:
Loading...