dbTalk Databases Forums  

Check for no more than two entries of each value in a column?

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss Check for no more than two entries of each value in a column? in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
livefreeordie
 
Posts: n/a

Default Check for no more than two entries of each value in a column? - 04-17-2009 , 12:40 PM






Hi,

I need to create an app where any given employee may nominate up to
TWO other employees per year for an award. This number could change.

Now I could create a table like this:

PK, U1 nominator (varchar2(12))
PK, U1 year (int)
U1 nomination1_id (int)
U1 nomination2_id (int)

Then another table:
PK nomination_id (int)
nominee (varchar2(12))
nomination_data ....

However, this would force me to update any Views or Code every time
the number of allowed nominations changes.

I'd much rather do this:

PK nomination_id (int)
year (int)
nominator (varchar2(12))
nominee (varchar2(12))
nomination_data ...

And have some kind of Check Constraint to only allow two nominees per
nominator per year. But I don't know how to do that.

Thoughts?

~ Jamie

Reply With Quote
  #2  
Old   
Shakespeare
 
Posts: n/a

Default Re: Check for no more than two entries of each value in a column? - 04-17-2009 , 01:28 PM






livefreeordie schreef:
Quote:
Hi,

I need to create an app where any given employee may nominate up to
TWO other employees per year for an award. This number could change.

Now I could create a table like this:

PK, U1 nominator (varchar2(12))
PK, U1 year (int)
U1 nomination1_id (int)
U1 nomination2_id (int)

Then another table:
PK nomination_id (int)
nominee (varchar2(12))
nomination_data ....

However, this would force me to update any Views or Code every time
the number of allowed nominations changes.

I'd much rather do this:

PK nomination_id (int)
year (int)
nominator (varchar2(12))
nominee (varchar2(12))
nomination_data ...

And have some kind of Check Constraint to only allow two nominees per
nominator per year. But I don't know how to do that.

Thoughts?

~ Jamie
You could create a trigger, checking the number of records for one
nominator on insert of a new one, and let it fail if two already exist.

OR: create an extra column for nominee number, with a constraint that it
can be 1 or 2, and let the nominator fill that column (1 for first
nominee, 2 for second) and create a unique constraint on nominator,
nominee number, nomination year. This way you only have to change the
check constraint on nominee number when more nominees are allowed.

OR: other options....

Shakespeare


Reply With Quote
  #3  
Old   
ddf
 
Posts: n/a

Default Re: Check for no more than two entries of each value in a column? - 04-17-2009 , 02:20 PM



On Apr 17, 12:40*pm, livefreeordie <jpittm... (AT) gmail (DOT) com> wrote:
Quote:
Hi,

I need to create an app where any given employee may nominate up to
TWO other employees per year for an award. *This number could change.

Now I could create a table like this:

PK, U1 *nominator * * * * (varchar2(12))
PK, U1 *year * * * * * * * * (int)
U1 * * * *nomination1_id *(int)
U1 * * * *nomination2_id *(int)

Then another table:
PK * nomination_id * *(int)
* * * * nominee * * * * * (varchar2(12))
* * * * nomination_data * ....

However, this would force me to update any Views or Code every time
the number of allowed nominations changes.

I'd much rather do this:

PK *nomination_id * (int)
* * * *year * * * * * * * * (int)
* * * *nominator * * * * (varchar2(12))
* * * *nominee * * * * * (varchar2(12))
* * * *nomination_data ...

And have some kind of Check Constraint to only allow two nominees per
nominator per year. *But I don't know how to do that.

Thoughts?

~ Jamie
Answered in comp.databases.oracle.server. Posting to more than one
newsgroup doesn't always get faster response.



David Fitzjarrell


Reply With Quote
  #4  
Old   
livefreeordie
 
Posts: n/a

Default Re: Check for no more than two entries of each value in a column? - 04-18-2009 , 07:06 AM



On Apr 17, 2:28*pm, Shakespeare <what... (AT) xs4all (DOT) nl> wrote:
Quote:
livefreeordie schreef:





Hi,

I need to create an app where any given employee may nominate up to
TWO other employees per year for an award. *This number could change.

Now I could create a table like this:

PK, U1 *nominator * * * * (varchar2(12))
PK, U1 *year * * * * * * * * (int)
U1 * * * *nomination1_id *(int)
U1 * * * *nomination2_id *(int)

Then another table:
PK * nomination_id * *(int)
* * * * nominee * * * * * (varchar2(12))
* * * * nomination_data * ....

However, this would force me to update any Views or Code every time
the number of allowed nominations changes.

I'd much rather do this:

PK *nomination_id * (int)
* * * *year * * * * * * * * (int)
* * * *nominator * * * * (varchar2(12))
* * * *nominee * * * * * (varchar2(12))
* * * *nomination_data ...

And have some kind of Check Constraint to only allow two nominees per
nominator per year. *But I don't know how to do that.

Thoughts?

~ Jamie

You could create a trigger, checking the number of records for one
nominator on insert of a new one, and let it fail if two already exist.

OR: create an extra column for nominee number, with a constraint that it
can be 1 or 2, and let the nominator fill that column (1 for first
nominee, 2 for second) and create a unique constraint on nominator,
nominee number, nomination year. This way you only have to change the
check constraint on nominee number when more nominees are allowed.

OR: other options....

Shakespeare- Hide quoted text -

- Show quoted text -
Ah that's a good solution. Thanks.


Reply With Quote
  #5  
Old   
Robert Klemme
 
Posts: n/a

Default Re: Check for no more than two entries of each value in a column? - 04-18-2009 , 02:27 PM



On 18.04.2009 14:06, livefreeordie wrote:
Quote:
On Apr 17, 2:28 pm, Shakespeare <what... (AT) xs4all (DOT) nl> wrote:
livefreeordie schreef:





Hi,
I need to create an app where any given employee may nominate up to
TWO other employees per year for an award. This number could change.
Now I could create a table like this:
PK, U1 nominator (varchar2(12))
PK, U1 year (int)
U1 nomination1_id (int)
U1 nomination2_id (int)
Then another table:
PK nomination_id (int)
nominee (varchar2(12))
nomination_data ....
However, this would force me to update any Views or Code every time
the number of allowed nominations changes.
I'd much rather do this:
PK nomination_id (int)
year (int)
nominator (varchar2(12))
nominee (varchar2(12))
nomination_data ...
And have some kind of Check Constraint to only allow two nominees per
nominator per year. But I don't know how to do that.
Thoughts?
~ Jamie
You could create a trigger, checking the number of records for one
nominator on insert of a new one, and let it fail if two already exist.
If I am not mistaken this one will fall into the "mutating table" trap.

Quote:
OR: create an extra column for nominee number, with a constraint that it
can be 1 or 2, and let the nominator fill that column (1 for first
nominee, 2 for second) and create a unique constraint on nominator,
nominee number, nomination year. This way you only have to change the
check constraint on nominee number when more nominees are allowed.
This makes additional business logic necessary, especially determining
the appropriate number before the insert.

Quote:
OR: other options....

Shakespeare- Hide quoted text -

- Show quoted text -

Ah that's a good solution. Thanks.
Here's another option: create another table with PK (nominator, year)
and additional column count (default = 0, check constraint which limits
values to 0,1 or 2). Create an insert trigger on the first table which
inserts or updates the counter table. Create update and delete triggers
on the first table which update counter table.

This version is also concurrency safe because you have exclusive access
on the counter records per (nominator, year).

Kind regards

robert


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.