27

Inspired by a Django modeling question: Database Modeling with multiple many-to-many relations in Django. The db-design is something like:

CREATE TABLE Book
( BookID INT NOT NULL
, BookTitle VARCHAR(200) NOT NULL
, PRIMARY KEY (BookID)
) ;

CREATE TABLE Tag
( TagID INT NOT NULL
, TagName VARCHAR(50) NOT NULL
, PRIMARY KEY (TagID)
) ;

CREATE TABLE BookTag
( BookID INT NOT NULL
, TagID INT NOT NULL
, PRIMARY KEY (BookID, TagID)
, FOREIGN KEY (BookID)  REFERENCES Book (BookID)
, FOREIGN KEY (TagID)   REFERENCES Tag (TagID)
) ;

CREATE TABLE Aspect
( AspectID INT NOT NULL
, AspectName VARCHAR(50) NOT NULL
, PRIMARY KEY (AspectID)
) ;

CREATE TABLE TagAspect
( TagID INT NOT NULL
, AspectID INT NOT NULL
, PRIMARY KEY (TagID, AspectID) 
, FOREIGN KEY (TagID)   REFERENCES Tag (TagID)
, FOREIGN KEY (AspectID)  REFERENCES Aspect (AspectID)
) ;

db diagram

and the issue is how to define the BookAspectRating table and to enforce referential integrity, so one cannot add a rating for a (Book, Aspect) combination that is invalid.

AFAIK, complex CHECK constraints (or ASSERTIONS) that involve subqueries and more than one table, that could possibly solve this, are not available in any DBMS.

Another idea is to use (pseudocode) a view:

CREATE VIEW BookAspect_view
  AS
SELECT DISTINCT
    bt.BookId
  , ta.AspectId
FROM 
    BookTag AS bt
  JOIN 
    Tag AS t  ON t.TagID = bt.TagID
  JOIN 
    TagAspect AS ta  ON ta.TagID = bt.TagID 
WITH PRIMARY KEY (BookId, AspectId) ;

and a table that has a Foreign Key to the above View:

CREATE TABLE BookAspectRating
( BookID INT NOT NULL
, AspectID INT NOT NULL
, PersonID INT NOT NULL
, Rating INT NOT NULL
, PRIMARY KEY (BookID, AspectID, PersonID)
, FOREIGN KEY (PersonID)   REFERENCES Person (PersonID)
, FOREIGN KEY (BookID, AspectID) 
    REFERENCES BookAspect_view (BookID, AspectID)
) ;

Three questions:

  • Are there DBMS that allow a (possibly materialized) VIEW with a PRIMARY KEY?

  • Are there DBMS that allow a FOREIGN KEY that REFERENCES a VIEW (and not only a base TABLE)?

  • Could this integrity problem be solved otherwise - with available DBMS features?


Clarification:

Since there is probably no 100% satisfying solution - and the Django question is not even mine! - I'm more interested in a general strategy of possible attack on the problem, not a detailed solution. So, an answer like "in DBMS-X this can be done with triggers on table A" is perfectly acceptable.

0

5 Answers 5

9

This business rule can be enforced in the model using only constraints. The following table should solve your problem. Use it instead of your view:

    CREATE TABLE BookAspectCommonTagLink
    (  BookID INT NOT NULL
    , AspectID INT NOT NULL
    , TagID INT NOT NULL
--TagID is deliberately left out of PK
    , PRIMARY KEY (BookID, AspectID)
    , FOREIGN KEY (BookID, TagID) 
        REFERENCES BookTag (BookID, TagID)
    , FOREIGN KEY (AspectID, TagID) 
        REFERENCES AspectTag (AspectID, TagID)
    ) ;
2
  • Oh nice. The only issue I can think is the complexity introduced in inserting/deleting BookTags and TagAspects. Every time a new BookTag (or TagAspect) is removed for example, a search has to be done to remove corresponding rows in this table and/or change the TagID to another Tag that is related to the same BookAspect combination. And similar search would have to be done for inserting into those 2 tables. But complex rules require complex procedures, so this looks really good. Commented May 15, 2012 at 19:50
  • 1
    @ypercube When you delete a tag, you do need to check and possibly switch to another tag linking the same Book and Aspect. When you insert new tags, however, there is no need to do any checks until you need to insert a rating.
    – A-K
    Commented May 15, 2012 at 20:04
10

In Oracle, one way to enforce this sort of constraint in a declarative fashion would be to create a materialized view that is set to refresh fast on commit whose query identifies all the invalid rows (i.e. BookAspectRating rows that have no match in BookAspect_view). You can then create a trivial constraint on that materialized view that would be violated if there are any rows in the materialized view. This has the benefit of minimizing the amount of data that you have to duplicate in the materialized view. It can cause problems, however, since the constraint is only enforced at the point that you're committing the transaction-- many applications aren't written to expect that a commit operation might fail-- and because the constraint violation can be somewhat hard to associate with a particular row or a particular table.

0
8

I think you'll find that in a lot of cases, complex business rules cannot be enforced via the model alone. This is one of those cases where, at least in SQL Server, I think a trigger (preferably an instead of trigger) better serves your purpose.

1
  • 3
    Be careful about race conditions that a naive implementation of triggers might introduce. For example, one transaction might disconnect a book from the tag and another still think it is OK to connect to the corresponding aspect, simply because the first transaction hasn't committed yet. The complexities introduced by @AlexKuznetsov answer are probably less than the complexity and fragility of the locking "protocol" necessary to prevent race conditions in triggers, IMHO. Commented Jan 7, 2014 at 2:36
4

SIRA_PRISE allows that.

Although the FK isn't called "FK" anymore, but just "database constraint", and the "view" does in fact not even have to be defined as a view, you can just include the view defining expression inside the declaration of the database constraint.

Your constraint would look something like

SEMIMINUS(BOOKASPECT , JOIN(BOOKTAG , TAGASPECT))

and you're done.

In most SQL DBMSs however, you'd have to do the analysis work on your constraint, determine how it can be violated and implement all needed triggers.

0
3

In PostgreSQL, I cannot imagine a solution without involving triggers, but it certainly can be solved that way (be it maintaining a materialized view of some sort or a before trigger on BookAspectRating). There are no foreign keys referencing a view (ERROR: referenced relation "v_munkalap" is not a table), let alone a primary key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.