user1914374
user1914374

Reputation: 1260

should data below be foreign key or primary key?

Session

SessionId (PK) SessionName  SessionDuration  TotalMarks  SessionWeight  ModuleId (FK)
1             AAA          01:00:00        30         20            1

Penalty

SessionId (PK)  PenaltyEnalbed
1               1

My question is that as SessionId in Penalty Table refers to the Session Table, should SessionId in penalty table be a primary key or a foriegn key?

Upvotes: 0

Views: 41

Answers (2)

John Woo
John Woo

Reputation: 263703

SessionID in table Penalty should be define as FOREIGN KEY since you have mentioned that it refers on column SessionID of table Session.

CREATE TABLE Session
(
    SessionID INT PRIMARY KEY NOT NULL,
    -- OTHER columns here...
);

CREATE TABLE Penalty
(
    SessionID INT NOT NULL,
    -- OTHER columns here...,
    CONSTRAINT penalty_fk FOREIGN KEY (SessionID)
        REFERENCES Session(SessionID)
);

Upvotes: 1

Chanckjh
Chanckjh

Reputation: 2597

it should be foreign key but in penalty there should also be an Id to set as primary key. So in penalty it should be ID(PK) SessionId (FK) PenaltyEnalbed

Upvotes: 1

Related Questions