From: | Jim Nasby <Jim(dot)Nasby(at)BlueTreble(dot)com> |
---|---|
To: | Dara Unglaube <dara(at)glifwc(dot)org>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: Constraint allowing value up to 2 times but no more than 2 times |
Date: | 2015-10-12 17:08:22 |
Message-ID: | 561BE906.3050808@BlueTreble.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On 10/12/15 12:04 PM, Dara Unglaube wrote:
> Greeetings.
> I'm trying to come up with a way to allow one column to have the same
> value up to two times, but no more than two times. I attempted adding a
> constraint to check the count of the value in the field - count
> (trsqqq) <=2 but aggregate functions are not allowed in constraints. Is
> there another way to do this? Any help would be greatly appreciated.
The safest bet is some kind of unique constraint. That would require
having a separate count field, which you would limit to being 1 or 2 via
a CHECK constraint. You could use a trigger to set the value based on
what's already in the table.
By the way, the issue with doing a simple count is that it's not safe
from race conditions, like an insert and a delete happening together.
--
Jim Nasby, Data Architect, Blue Treble Consulting, Austin TX
Experts in Analytics, Data Architecture and PostgreSQL
Data in Trouble? Get it in Treble! http://BlueTreble.com
From | Date | Subject | |
---|---|---|---|
Next Message | Jim Nasby | 2015-10-12 17:14:10 | Re: Effecient time ranges in 9.4/9.5? |
Previous Message | Dara Unglaube | 2015-10-12 17:04:13 | Constraint allowing value up to 2 times but no more than 2 times |