'Oracle SQL: How to create a constraint such that only one row in a group can be 'Checked' [duplicate]

In Oracle SQL

Suppose a table with the following structure:

ID NAME DESCRIPTION ACTIVE_FLAG
1 A1234567 Item Desc 1 Y
2 A1234567 Item Desc 2 N

I'd like to be able to create a constraint upon this table such that the following operations are allowed:

INSERT INTO TBL (name, description, active_flag) VALUES('A1234567', 'Item Desc 3', 'N')
UPDATE TBL SET active_flag = 'N' WHERE ID = 1

but these are is not:

INSERT INTO TBL (name, description, active_flag) VALUES('A1234567', 'Item Desc 3', 'Y')
UPDATE TBL SET active_flag = 'Y' WHERE ID = 2

Essentially, I'm interested in a constraint such that for all rows which share a value for NAME, only one of those rows can have their active flag set to 'Y'

How might one go about achieving such a constraint?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source