3

I wonder to know why EmailAddress Table in Adventure work database uses composite primary key(BusinessEntityID,EmailAddressID(Identity))? if it's something related to set cluster index for both fields, I would appreciate if tell me how composite primary key physically store(in which order and how data insert)?

enter image description here

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
Mohammad Yousefi
  • 563
  • 2
  • 7
  • 21

2 Answers2

1

It is because this way, the same e-mail address can be used by multiple Persons as well as the same Person can use multiple e-mail addresses, i.e., it makes the relationship between person and e-mail address many-to-many.

If the need was merely to enforce that the e-mail address should belong to a person, it would have been enough to make it a foreign key and the column BusinessEntityID not nullable.

Update:

There are 2 tables involved here, Person and EmailAddress.

Each record in Person is identified by a BusinessEntityID. To associate a record from Person with a record in another table T, it is enough to include a column in this table T that refers to BusinessEntityID. Now, if we needed to ensure that all records in T must be associated with some record in Person, then we would place a foreign key constraint on T.BusinessEntityID and make it not nullable. If on top of this, we wanted to ensure that each record in T must be associated with one and only one record in Person, then we could place a uniqueness constraint on the column T.BusinessEntityID.

When we make 2 columns, A and B part of the primary key of a table, we are telling the database that the values of these two columns together must be unique for all records in that table. It has no bearing on the values in each of those columns and any foreign key relationships.

To illustrate:

Person (BusinessEntityID, Name) and PK is BusinessEntityID
---------------
1 | John
---------------
2 | Jane
---------------
3 | Sales Team



EmailAddress (BusinessEntityID, EmailAddressID, EmailAddress) and PK is [Business EntityID, EmailAddressID] where EmailAddress is auto-incremented
--------------
1 | 1 | john@example.com
------------------------
1 | 2 | john@contoso.com
------------------------
2 | 3 | jane@example.com
------------------------
2 | 4 | jane@contoso.com
------------------------
1 | 5 | sales@example.com
------------------------
2 | 6 | sales@example.com
------------------------
3 | 7 | sales@example.com

Data similar to the above is possible to put in the tables in your example. Now what is happening here?

There are 3 entities, John, Jane and Sales Team.

John has 2 personal e-mail addresses. Jane also has 2 personal e-mail addresses. In addition, the e-mail address sales@example.com belongs to the Sales Team but also to John and Jane.

This is a many-to-many relationship.

Additonally, if the composite key in EmailAddress is clustered, the keys are stored in the order in which they appear. Read this for more information.

Community
  • 1
  • 1
Umar Farooq Khawaja
  • 3,925
  • 1
  • 31
  • 52
  • thanks for you reply but if we didn't use BusinessEntityID as a primary key(just we have had EmailAddressID) I think still we could have same address for multiple person and vice versa. am i right? – Mohammad Yousefi Jan 30 '13 at 19:53
  • is it correct that we think, related two fields in a table as a many to many relationship? because if BusinessEntityID in EmailAddress wasn't primary key(was just a FK), we still would have many mail for one person and many person for a same mail. – Mohammad Yousefi Jan 31 '13 at 19:29
  • In fact you are absolutely correct about that. I actually cannot think of a good reason why they have constructed the `EmailAddress` table's PK this way. Usually If you have two tables `A` with `A.Id` and `B` with `B.Id`, and you want a many-to-many relationship between `A` and `B`, you make a third table `C` with `C.AId` and `C.BId` and you can make `(AId, BId) the PK for `C` and also set up FKs for `AId` and `BId` to enforce referential integrity. That's clearly not what's going on here. My aplogies for misguiding you in my answer. – Umar Farooq Khawaja Jan 31 '13 at 20:27
0

That's because of composition, one email address could not exist without a person. So BusinessEntityID is also primary key for EmailAddress table

Nick
  • 4,192
  • 1
  • 19
  • 30
  • Thanks for your reply, would you please tell me or give me some references that how composite primary keys physically store. is just one of the fields consider as a cluster or both of them? – Mohammad Yousefi Jan 30 '13 at 19:44
  • 1
    -1 for giving an incorrect reason for composite key setup and confusing it with a foreign key constraint. – Umar Farooq Khawaja Jan 30 '13 at 19:44