Jump to content

Composite Foreign Key in Database Diagram


Lc3

Recommended Posts

Hi, I'm designing a MySQL database. I have an entity with a composite primary key, and I'm referencing this entiry in another so I need to use a composite foreign key, e.g.

 

KEY `fk` (`site_name`,`company_name`),
CONSTRAINT `fk`
    FOREIGN KEY (`site_name`, `company_name`)
    REFERENCES `site` (`site_name`, `company_name`)

 

How is such a relationship represented in a Database Diagram? I haven't seen any examples of this. Is it unusual design?

Link to comment
Share on other sites

In terms of how the one entity relates to the other it's just a 1:1 relation. I would really recommend you use an ID instead of a text PK, let alone 2, for a whole scala of reasons.

 

actually it's a 1:M not 1:1 because the table with the foreign keys has primary keys

product_barcode, site_name, company_name

as it's PK, the table with the composite primary key was

site_name, company_name

there were going to be many products on one site...

 

... but that's an aside anyway I didn't give oyu enough information to know that so it's not important, in regards to what you were actually suggesting (that I should have a surrogate key), i have heard that using a combination of natural attributes has it's advantages. How do I know when to use a surrogate and when not to?

Link to comment
Share on other sites

There are only very few cases where I would recommend a natural/meaningful key. I personally would not use a composite primary key for anything other than intersection tables. A surrogate key outperforms a text based key and gives greater flexibility (what if I misspelled my company name?).

Link to comment
Share on other sites

A surrogate key outperforms a text based key and gives greater flexibility (what if I misspelled my company name?).

 

There's always cascade updates

 

I personally would not use a composite primary key for anything other than intersection tables.

 

you probably have a point though i'll have to think about it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.