Composite Foreign Keys?

  • Hi All,

    I have a Primary Table called 'STATES' with primary keys (stateid) as below:

    stateid state_abb state_txt

    2AL ALABAMA

    3AK ALASKA

    4AZ ARIZONA

    5AR ARKANSAS

    6CA CALIFORNIA

    7CO COLORADO

    8CT CONNECTICUT

    and I have a Secondary Table called 'Subject' with multiple state columns.

    Home_State_id

    Birth_State_id

    Driver_License_State_id

    I need to know how to set up Primary and Foreign Key relationship on these 2 tables? is there any way to setup composite foreign key(single column primary key and multiple columns foreign key)?

  • #1 I wouldn't bother with the identity here. Abbreviation is a correct PK.

    #2 You need 1 FK for each column in the 2nd table.

  • Ninja's_RGR'us (10/26/2011)


    #1 I wouldn't bother with the identity here. Abbreviation is a correct PK.

    #2 You need 1 FK for each column in the 2nd table.

    Thanks Ninja's for a quick reply...

    OK 1 FK for each column!

    As I have to setup foreignkeys ON DELETE CADCADE...

    I get the following error...

    "Introducing FOREIGN KEY constraint 'FK74988DB24B3C886' on table 'Subjects' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints."

  • I wouldn't bother with cascade here.

    States usually don't go anywhere (too often).

    And delete subjects shouldn't require you to delete a state.

    The problem probably that you have a circular refference.

    Delete a => delete b => goes back to delete a.

    The server stops that from hapenning with the error you just posted.

    I've always found that this is error is coming from bad table design. As with any generalisations, there should be exceptions!

  • Thanks Ninja...

    I have 50GB database without referrential integrity and lot's of table is having the same issue.

    So, basically there is no solution... PK and FK should have same numbers of columns to setup RI constraint.

    I can only create 1 FK constraint for instance -- State.stateid = Subjects.home_state_id with ON DELETE CASCADE

    and I have to leave the other columns without setting up Referrential Integrity.

  • In house app or 3rd party?

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply