Solved

How would I write a Constraint to ensure that there is only one record that is active for any one templ_id.

Posted on 2011-03-10
7
354 Views
Last Modified: 2012-05-11
Hi, How would I write a Constraint to ensure that there is only one record that is active for any one templ_id.

This is valid... but two actives ones would not be valid.
templ_id       status
 1                 inactive
 1                 active
 1                 inactive



ALTER TABLE employee
    ADD CONSTRAINT unq_templ_id_active_status_ck
    CHECK (??????????????? );
0
Comment
Question by:bcarlis
  • 3
  • 3
7 Comments
 
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 25 total points
ID: 35098554
I don't believe to can do this with a simple constraint.  You definitely can with a trigger.

0
 
LVL 11

Expert Comment

by:Akenathon
ID: 35098592
Check constraint do NOT span more than one record. What you need is a unique function index:

create unique index emp_active_uk on employee (decode(status,'active',templ_id,null),decode(status,'active','active',null))
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35098648
>>What you need is a unique function index:

Very nice!!!

Yes, go with that.  It's better than my trigger suggestion.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 11

Accepted Solution

by:
Akenathon earned 100 total points
ID: 35098920
Thanks! A couple of notes for the author:

- A unique CONSTRAINT is no good here, since the syntax only takes columns (i.e. no functions allowed)
- The suggestion works because NULL keys are not indexed
- You can leave out the second field I'm indexing, since the point is indexing only the "active IDs" anyway:

create unique index emp_active_uk on employee (decode(status,'active',templ_id,null))
0
 
LVL 11

Expert Comment

by:Akenathon
ID: 35098956
Forgot to mention: the problem with triggers is concurrency. If two people simultaneously try to INSERT the same ID with 'active' status, the only way you can enforce uniqueness is having an autonomous transaction insert the ID into an auxiliary structure and fail if there's a duplicate. In effect, you'd be implementing the unique index anyway...
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35098983
>>If two people simultaneously try to INSERT

I stand corrected again.  ;)
0
 
LVL 2

Author Closing Comment

by:bcarlis
ID: 35101675
That Unique constraint worked awesome! Thanks!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to recover a database from a user managed backup

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question