Home > Ask the Oracle Experts > SQL Questions & Answers > Composite keys in M:N relationship tables
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Composite keys in M:N relationship tables

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 28 June 2004
What is the general procedure for dealing with M:N relationships encountered during the process of database design? Are composite keys always a straightforward solution?

>
EXPERT RESPONSE

There is so much to be said on this subject, it's hard to decide where to start. Let's look at a simple example.

create table users
( id    smallint not null primary key 
, name  varchar(9)
)
create table projects
( id    smallint not null primary key 
, name  varchar(9)
)
create table userprojects
( userid  smallint not null 
, projectid smallint not null 
, projectrole varchar(15) 
, primary key (userid,projectid)
, foreign key (userid) 
    references users (id)
, foreign key (projectid) 
    references projects (id)
)

This is the same example used in another recent SQL answer, Inserting and searching many-to-many relationships.

Note that the relationship table USERPROJECTS has a composite key. Is this a straightforward solution? Yes. Is this the recommended way? Yes. Many database developers will automatically give this table a separate surrogate key (i.e. SEQUENCE, IDENTITY, AUTO_INCREMENT, etc.), but I do not.

The only time it makes sense to use a surrogate key is when the relationship table itself has child tables, and the child tables have many rows, and the queries using the child tables are complex. Relationship tables with child tables do occur, but they are rare. Unless you have a compelling reason, use composite keys.

And if you do decide to use a surrogate key for the relationship table, don't forget to declare a unique constraint on the composite anyway.

For More Information


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
SQL
IN list or series of OR conditions?
Connecting tables in a database
SQL query for co-authored books
Querying complex derived tables
SQL string functions
Changing a NULL column to NOT NULL
SQL for hourly totals for the last 48 hours
LEFT OUTER JOIN to a MIN/MAX row
Normalizing a crosstab table
Querying metadata and data at the same time

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsWebcastsWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts