开发者

how to use foreign keys as primary key

开发者 https://www.devze.com 2023-04-05 12:15 出处:网络
i have a table with two foreign keys as composite key. ActivityTbl - (activityNbr(PK), supervisor(FK), status, type, startDate, endDate, location )

i have a table with two foreign keys as composite key.

ActivityTbl -

(activityNbr(PK), supervisor(FK), status, type, startDate, endDate, location )

VolunteerTbl -

(volunteerNbr(PK), name, address, contact)

Now I created a table where volunteer's choices can be 开发者_Go百科stored as per their prefereance rating.

ActivityChoice

(activityNbr(FK), VolunteerNbr(FK), Rating)

So the combination of those 2 FKs make a composite key. I am using sql Server to create a table.

Create ActivityChoiceTbl(
     VolunteerNbr   int NOT NULL,
     ActivityNbr    int NOT NULL,
     Rank           int NOT NULL,
     CONSTRAINT PKActivityChoice    PRIMARY KEY (VolunteerNbr,ActivityNbr), 
     CONSTRAINT CKRank CHECK (Rank>0 AND Rank<=9));

So in this case do I need to add another foreign key constrain for both to mention that they are foreign keys?? Am I doing it right?? Thanks


Yes, you need two foreign key constraints. The most direct way in standard SQL is to immediately reference the table.

Create ActivityChoiceTbl(
     VolunteerNbr   int NOT NULL REFERENCES VolunteerTbl (volunteerNbr),
     ActivityNbr    int NOT NULL REFERENCES ActivityTbl (activityNbr),
     Rank           int NOT NULL,
     CONSTRAINT PKActivityChoice    PRIMARY KEY (VolunteerNbr,ActivityNbr), 
     CONSTRAINT CKRank CHECK (Rank>0 AND Rank<=9));

But adding two more constraint clauses lets you name the constraints, which is a better practice.

Create ActivityChoiceTbl(
     VolunteerNbr   int NOT NULL,
     ActivityNbr    int NOT NULL,
     Rank           int NOT NULL,
     CONSTRAINT PKActivityChoice    PRIMARY KEY (VolunteerNbr,ActivityNbr), 
     CONSTRAINT FKActivityChoiceVolunteerNbr 
         FOREIGN KEY (VolunteerNbr) REFERENCES VolunteerTbl (VolunteerNbr),
     CONSTRAINT FKActivityChoiceActivityNbr 
         FOREIGN KEY (ActivityNbr) REFERENCES ActivityTbl (ActivityNbr),
     CONSTRAINT CKRank CHECK (Rank>0 AND Rank<=9));

If ActivityChoice is a separate table that needs to reference ActivityChoiceTbl, then you also need something along these lines.

CREATE TABLE ActivityChoice (
    VolunteerNbr INTEGER NOT NULL,
    ActivityNbr INTEGER NOT NULL,
    Rating  DECIMAL (2,1) NOT NULL CHECK (Rating between 0 and 9),  -- Pure guess
    PRIMARY KEY (VolunteerNbr, ActivityNbr),
    FOREIGN KEY (VolunteerNbr, ActivityNbr) 
      REFERENCES ActivityChoiceTbl (VolunteerNbr, ActivityNbr)
);


I would suggest you have a separate single PrimaryKey in ActivityChoice table. Make VolunteerNbr and ActivityNbr foreign keys and add a composite unique key constraint for two columns VolunteerNbr, ActivityNbr.

Some of the viewpoints you would like to take a look at, about composite foreign key. http://www.upsizing.co.uk/Art10_MultiPartkeys.aspx

http://social.msdn.microsoft.com/Forums/en/transactsql/thread/158d77f7-3029-43bc-bba6-a8a12374f00c

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号