You are on page 1of 4

From Ternary Relationship to Relational Tables

A Case Against Common Beliefs
Rafael Camps Paré
Universitat Politècnica de Catalunya (Barcelona) RCAMPS@LSI.UPC.ES

ABSTRACT from A. Remember: each one of the instances of a ternary
relationship involves actually three entity instances.
The transformation from n-ary relationships to a
relational database schema has never been really fully We will study the ternary relationship with 1:N:N
analyzed. This paper presents one of the several ternary cardinalities in Figure 1. The entities are Product
cases ignored by the ER-to-RM literature. The case
shows that the following common belief is wrong: Given Dea-id
a set of FDs over a table resulting in a non-3NF Dea-adress
situation, it is always possible to obtain a fully 1
equivalent set of 3NF tables, without adding other Concession Con-date
restrictions than candidate keys and elementary
inclusion dependencies. N N
Pro-id Product State Sta-id
Pro-type Sta-capital

1. INTRODUCTION Figure 1: Example of 1:N:N relationship
The transformation from an n-ary relationship of the ER
model to a relational database schema is considered by (identified by Pro-id) State (identified by Sta-id) and
the literature as an elementary and well known problem. Dealer (identified by Dea-id). The semantics of the case is
But according to my knowledge this subject is as follows: a dealer company can distribute several of our
anywhere fully and systematically described. products and a product can be distributed by several
dealers, but in each state a product cannot be distributed
It exists two popular approaches for expressing the by more than one dealer. Each concession of distribution
cardinalities of the n-ary relationships: Chen approach to a dealer of a product into a state, each instance of the
[4] and Merise approach [10]. Chen approach is used by ternary relationship, has a date of concession (Con-date).
Teorey [12], Ullman-Widom [13], UML [11], IDEF1X The meaning of the cardinalities is; each pair of one dealer
[2], etc. Merise approach [10] is used by Elmasri- and one state, can be associated to many products. Each
Navathe [5], Batini-Ceri-Navathe [1], Yourdon-Method pair of one state and one product cannot be associated to
[14], etc. more than one dealer. Each pair product-dealer can be
associated to more than one state.
In the literature about database design, when the
translation from ER to relational tables is described, is In terms of functional dependencies, FDs, this semantics
usually limited to a very general description, and only can be expressed as follows (the obvious internal
one of the two popular approaches is assumed for the dependencies in each entity between their identification
cardinality. But none of the two approaches, nor the two and their attributes are not represented):
together, allow us to express all the possible
cardinalities (see McAllister [9]). So, transformations to (Pro-id , Sta-id) ➔ Dea-id
relational tables from some ternary patterns are ignored because the 1 in the side of Dea-id
in the literature. A detailed analysis of the (Pro-id, Sta-id, Dea-id) ➔ Con-date
transformation of all the patterns can be found in [3]. because Con-Date is an attribute of the relationship

As an example we will present here a ternary case These two dependencies can be reduced to one:
(section 2 and 3) with interesting characteristics (section
4) that challenge some very common beliefs (section 5). (Pro-id , Sta-id) ➔ (Dea-id, Con-date)

2. AN ELEMENTARY EXAMPLE A relational representation of this ternary relationship
We assume that if R is a relationship between the could consist on one table for each entity and one table for
entities A B and C, then the symbol 1 in the leg of A the Concession relationship (keys are underlined) :
means that each pair of instances, b from B and c from
C cannot be associated to more than one instance a State (Sta-id, Sta-capital)

R. Pro-id and Sta-id in some other patterns) using any of the most popular ER the table Concession should be declared as foreign key models. B The two imposed structural constraints are. (A-id. Dea-id. Similar considerations can be made for the pair states and in a state is still possible to distribute several Sprite/Idaho.C-id) ➔ B-id 3. then a set of 2-tuples can not be a two binary FDs.Z) ➔ Y constraint is not a subset of a relationship. Cocacola cannot have a dealer in Kansas because Cocacola has Figure 2: Two imposed FDs already a dealer -Smith&Sons (in Idaho and Texas). is expressed by the non-presence of a row in the Abstracting our case to a relationship R between the three Concession table for that pair of Pro-id Sta-id. The possible lack of a dealer for a given pair product- state. Product (Pro-id. a valid set of instances for the new where it exists concessions. according to This FD ternary pattern is exactly the pattern number 4 in the terminology of Jones-Song [6]. It could seem that with only two binary FDs we can In Figure 2 we try to show graphically the new ternary. without adding text (or special symbols) to and not null. In fact the subset term is not strictly correct because a if X ➔ Y then (X. products. and therefore they stereotype to express that an association is a subset of . the set of functional dependencies it is not possible to reduce the number of tables without between the keys is: loosing their normalization (BCNF). Dea-address) but the lack of a rigorous definition of UML in their reference manual [11] do not allow us to be sure. B and C. Dealer (Dea-id. because the binary imposed FDs: N N N N Cocacola Kansas Smith&Sons 2000 Product State Sprite Idaho Smith&Sons 2000 As a consequence of the additional constraints. Perhaps this UML stereotype can be used with the same meaning than the arrows in Figure 2. relationship Concession can be: Pro-id Sta-id Dea-id Con-date Dealer Cocacola Idaho Smith&Sons 1996 Cocacola Texas Smith&Sons 1994 1 1 1 Fanta Idaho Smith&Sons 1994 Sprite Kansas FreeDrink 1998 Now it should be impossible to additionally insert the Concession following instances. and b) in each state In our example. two Constraining the analysis of McAllister [9] or the pattern number 11 in Binary Relationships. The semantics of the new ternary the analysis of Jones-Song [7]. because applying the augmentation The dotted lines and arrows try to express the two rule (one of the Armstrong's rules [12]) that states imposed binary constraints as subsets of the ternary. characterize this case. express that the binary restrictions are part of the ternary relationship. Pro-type) another association. Con-date ) There is no way to represent the pattern in Figure 2 (and Each one of the attributes Dea-id. C. Sta-id.C-id) ➔ B-id from both of our binary relationships. But remember that the ternary subset of a set of 3-tuples. Moreover.and Kansas already has a different dealer. Note that entities A. if we see the constraints as they were a set of instances of we derive the FD (A-id. r. only one dealer exists. FreeDrink (for Note that a product can still be distributed in several Sprite). Concession ( Pro-id. ADDING MORE FDs TO THE A-id ➔ B-id EXAMPLE C-id ➔ B-id Now we will add to our elementary example two new functional dependencies: So the case is apparently simple and can be represented as: Pro-id ➔ Dea-id and Sta-id ➔ Dea-id A. pattern is now more restricted: a) each product is distributed only by one dealer. In UML it exist the {subset} relationship. has its own attributes.

is normalized in 3NF (and also in BCNF) without lost of data and preserving all the FDs.C-id) ➔ B-id (A-id. a) B (B-id. of R can be reduced to: This schema has intertable redundancy (because B-id) R (A-id.B-id C π C-id C⊆π C-id R iff C.B-id III) π C-id. directly representing the full set of FDs of the pattern. B-id.Table C: UNIQUE (C-id.B-id C The constrain a) is more complex than an inclusion dependency: it involves a natural join operation.Table R: FOREIGN KEY (A-id. FROM TERNARY TO RELATIONAL (SELECT A-id. B-id) REFERENCES A (A-id.In table A CHECK ( (A-id. But as before. B-id) MATCH 4. Obviously. we must define the attribute B-id of tables A and C. is not mandatory. B-id. A-id➔ B-id C-id➔ B-id. and accepting nulls. We are assuming that participation of entities A. the schema id of table R as foreign keys of tables A and C. (A-id. imply the dependency (A-id.In table C CHECK ( (C-id. for example. determinants: C and R.C-id) ➔ B-id. So. . B-id FROM R )) A (A-id.are functionally dependent on the pair of keys of A and C. We could enforce: inclusion dependencies [5]: I) π A-id. r) and the table R is not in 3NF (nor in 2NF) because of the following two dependencies that have non-key Now the data base schema formed by the four tables A.Table A: UNIQUE (A-id.B-id is not null IV) π C-id. The B-id attributes of tables A. B-id FROM R )) If we transform our ternary relationship to relations. C and R. and A-id and C. In order to nulls.B-id is not null A-id. r) possible update problems owning to the redundancy of B- id and to the lack of normalization of R. using the following expressed in SQL92. but they make easy the control of Key FDs: Non-key FDs: the inclusion restrictions. B-id) CHECK ( NOT EXISTS ( and FOREIGN KEY (C-id. the full set of FDs is: Note that the UNIQUE declarations are partly redundant with the primary keys. the schema is now fully expressing the semantics of our case and avoids the C (C-id. B-id) (SELECT A-id. B-id) The two constraints b) could be expressed as an assertion: . this data base schema is not representing the The two binary dependencies A-id➔ B-id and C-id➔ inclusion of the binary restrictions in the ternary B-id in tables A and C. C-id) MATCH . So we must add restrictions to enforce that R ternary. Moreover. B-id) MATCH we obtain the following database schema: (SELECT C-id. B. C-id FROM (A NATURAL JOIN C))) . The attributes Let us now start again with the original schema and B-id of tables A and C are to be declared as accepting assume that we desire to normalize table R. B-id .B-id R ⊆ π A-id. B and C in the relationship R.C-id R ⊆ π A-id. adding keys and foreign keys as restriction in table R: follows: CHECK ((A-id. should be defined as part of the relationship. It could The inclusion dependencies I and III can be easily be expressed in SQL92. because the non-null. B-id) REFERENCES C (C-id. the above relational schema is not fully representing the semantics of our ternary relationship. already to be declared foreign keys of table B. as foreign keys referencing table B.C-id A*C II) π R ⊇ π A b) π A-id π A⊆ A-id R iff A. We could add the following tables A and C.B-id R ⊆ π C-id. C-id. are additional FDs represented in tables A and C. and in our schema they are not (they are is not independent of the two binary FDs implied by the independent of R). c) R (A-id.B -id R ⊇ π C-id.B-id A a) π A-id. C-id.C-id) ➔ r A-id ➔ B-id A-id ➔ a The inclusion dependencies II and IV are a little more C-id ➔ B-id C-id ➔ c complex to express in SQL: B-id ➔ b . b) With all these restrictions. The attribute B-id of table R must be declared as normalize R we can take out its attribute B-id.B -id A-id. So. B-id) (SELECT A-id FROM A WHERE B-id IS NOT NULL) .

CHECK (UNIQUE (SELECT A-id FROM [2] T.P. [13] J. Research Repot LSI- Because R is ternary. dependencies. B-id . [5] R.B. REFERENCES table as foreign key accepting nulls. The case we have presented here is one of these cases. If a correct C (C-id. B-id FROM Information Models. As we have seen . Booch. Database Design: An A-id ➔ B-id is not enforced. a) B (B-id. 143-159 (1983).Y. and last. But it has an interesting behavior that have AND NOT EXISTS ( make us remember of Loizou[8] when he says that "the (SELECT C-id FROM C WHERE B-id IS NOT NULL) central idea in relational databases design is that all the EXCEPT (SELECT C-id FROM R) ) ) integrity constraints in the database should be describable in terms of keys and foreign keys" although we know it's A third possible transformation for our case is to take not always possible. r) Modeling Language Reference Manual.. with the additional FDs A-id➔ B-id and C-id➔ B-id. Database Modeling & Design. 5. alternative transformation for our [7] T. cannot be decomposed in a fully equivalent set of 3NF [12] T.Y. EXCEPT (SELECT A-id FROM R) ) FD pattern.D. Journal of Database Management. It is a very common belief that given out the foreign key B-id from table A (or. and B-id of the C 6.Ullman and J. Decomposition Approach".Widom.Navathe. That can be enforced by an [4] P. It looks very simple. These restrictions are not powerful enough.Teorey. S. Now we have no intertable redundancy but table R is April-June 2000. The Unified R (A-id. Addison-Wesley (1999). 1(1) 9- R is already defined as foreign key of C): 36 (1976). Information & Management.. 28(2) . Databases and Beyond. "An Approach for Decomposing N-ary Data Relationships" Software Practice & CHECK ((UNIQUE (SELECT A-id FROM Experience.Elmasri and S.Rumbaugh. Designing Quality Databases with IDEF1X (SELECT DISTINCT A-id.McAllister and D. it is always possible to obtain a fully equivalent set of 3NF symmetrically. Now the schema will be: tables.Navathe. B-id FROM R)))) system design and development methodology". a set of FDs over a table resulting in a non-3NF situation. C-id.B.Bruce. 125-154 (1998). 12-19 (2000). Morgan tables if we use only candidate keys and inclusion Kaufmann (1998). 39-64 (1996). A-id. instances of A must be exactly the same ones that are associated to instances of B. B-id and C-id in table R must be defined as foreign keys and not null.Batini. Prentice-Hall.Jacobson and G.Sharpe.Ceri and S. 3th ed. A Guided Tour of Relational enforce A-id➔ B-id and C-id➔ B-id in table R. Prentice-Hall (1997).. 6. table C).S. patterns. "MERISE: An information (SELECT DISTINCT A-id. Prentice-Hall (1993). We can enforce it by: Entity-Relationship Approach.Jones and I. without adding other restrictions than candidate keys and inclusion dependencies (key or non-key based). consist of having B-id only in table R. Dorset House Publishing. the instances of C associated to 5-02R of Universitat Politècnica de Catalunya (Spain). Rochfeld and H. We must declare A-id and C-id of table R as foreign keys of the corresponding tables. Springer-Verlag (1999). But we must [8] G. So. C-id. B-id. "Transforming N-ary Relationships to Database Schemas: An Old and Forgotten Problem". b) But as we have seen in our case. Yourdon Method. A (A-id.Loizou and M. not in 3NF. A First Course in Database Systems. (1992). A fourth. B-id FROM R))) AND (UNIQUE (SELECT A-id FROM [10] A. (SELECT DISTINCT A-id. 19 (1). Addison-Wesley (1999). But now the FD [1] C. "Analysis of binary/ternary WHERE C-id NOT IN cardinality combinations in entity-relationship modeling". Data (SELECT C-id FROM C WHERE B-id IS NULL))) & Knowledge Engineering. apparently a harmless . we can write: [9] A. AGAINST A COMMON BELIEF The ER-to-RM literature ignores some interesting FD [14] Yourdon. Fundamentals of Database CHECK ((SELECT COUNT(*) FROM C Systems. r) database state is to be guaranteed more complex restrictions have to be used.Camps.Song. ACM-Transactions on Database Systems.Jones and I. "The entity relationship model: toward a unified SQL check for equality (remember that C-id from table view of data". "Binary Equivalents of Ternary Relationships in Entity-Relationship Modeling: a Logical ternary case. WHERE B-id IS NOT NULL) = (SELECT COUNT(DISTINCT C-id) FROM R [6] T.J.Song.Chen.Inc. Tardieu. (R NATURAL JOIN C )))) [3] R. it is not actually true. (1991). I.Levene. c) R (A-id.J. the non-3NF table [11] J. 3th ed.