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
350 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.

Join & Write a Comment

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
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 shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now