Advanced Database Constraints: There Can Be Only One

One of the powers of RDBMS as we use them today are constraints. I use unique indexes, not null constraints and foreign keys on a regular basis and if you do any work with RDBMSs you probably do as well.

From time to time one comes at a point where you think “it would be nice to enforce this with some kind of constraint”, but you can’t. Well maybe you just didn’t try hard enough. This is the first in a little series of articles pointing out some tricks you can do with database constraints.

I’ll talk Oracle here since that is what I know best, but many of these things are possible in other systems as well.

Lets assume we have a person table and a hobby table which holds the hobbies of a person:

  1. CREATE TABLE person (
  2.   id number PRIMARY KEY NOT NULL,
  3.   name varchar2(200) NOT NULL
  4.   );
  5.  
  6. CREATE TABLE hobby (
  7.   id number PRIMARY KEY NOT NULL,
  8.   person_id number REFERENCES person NOT NULL,
  9.   name varchar2(200) NOT NULL
  10.   )

Just ignore that the hobby table probably needs some normalization. Now assume you want to flag the favorite hobby of a person and of course there can be only one favorite hobby per person.

You could create an additional table holding that information, linking person entries and hobby entries, with a unique constraint on the person_id. From a theoretical point of view this is the correct normalized way to do it. But it makes you add a complete table when you just need a flag. There is a somewhat denormalized way to do it, which is in cases like this one simpler to use.

  1. ALTER TABLE hobby
  2. ADD  is_favorite varchar2(1) UNIQUE;
  3.  
  4. ALTER TABLE hobby ADD constraint check_is_favorite
  5. CHECK (is_favorite = 'Y');

The idea is to add a is_favorite column and make it unique, but also only allow a single value different from null using a check constraint. Since null values don’t get indexed you can have as many null values in a unique column as you want. This does look probmising, but there is a problem. The is_favorite column should only be unique per person, so we have to include the person_id. But now all the null values get indexed, since a unique key only ignores null values when all indexed columns are null. So we have to replace the null values with something unique. We can do that by creating a function based column which is identical to is_favorite for not null values and replaces the null values with the primary key (which of course is unique by definition).

  1. ALTER TABLE hobby
  2. ADD is_favorite_unique AS (nvl(is_favorite, id));
  3.  
  4. ALTER TABLE hobby ADD CONSTRAINT unique_favorite_hobby
  5. UNIQUE(person_id, is_favorite_unique);

If you remove the unique constraint from the is_favorite column you finally have the desired behavior:

  1. CREATE TABLE person (
  2.   id number PRIMARY KEY NOT NULL,
  3.   name varchar2(200) NOT NULL
  4.   );
  5.  
  6. CREATE TABLE hobby (
  7.   id number PRIMARY KEY NOT NULL,
  8.   person_id number REFERENCES person NOT NULL,
  9.   name varchar2(200) NOT NULL
  10.   );
  11.  
  12. ALTER TABLE hobby
  13. ADD  is_favorite varchar2(1);
  14.  
  15. ALTER TABLE hobby ADD constraint check_is_favorite
  16. CHECK (is_favorite = 'Y');
  17.  
  18. ALTER TABLE hobby
  19. ADD is_favorite_unique AS (nvl(is_favorite, id));
  20.  
  21. ALTER TABLE hobby ADD CONSTRAINT unique_favorite_hobby
  22. UNIQUE(person_id, is_favorite_unique);
  23.  
  24. INSERT INTO person VALUES (1, 'Jens');
  25. INSERT INTO person VALUES (2, 'Alfred');
  26. INSERT INTO hobby (id, person_id, name, is_favorite) VALUES (10,1, 'coding', 'Y');
  27. INSERT INTO hobby (id, person_id, name, is_favorite) VALUES (20,2, 'soccer', NULL);
  28. INSERT INTO hobby (id, person_id, name, is_favorite) VALUES (21,2, 'reading', NULL);
  29. INSERT INTO hobby (id, person_id, name, is_favorite) VALUES (22,2, 'swimming', 'Y');
  30. – this fails with a unique exception
  31. – insert into hobby (id, person_id, name, is_favorite) values (23,2, 'watching highlander', 'Y');


Share:
  • DZone
  • Digg
  • del.icio.us
  • Reddit
  • Facebook
  • Twitter