Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to know if you can have multiple foreign keys to a single column then the answer is no you cant.</p> <p>You can have separate foreign keys if you want to. So your can modify your comment table like this - </p> <pre><code> comment: * comment_id (PK) * PostID (FK to Post.PostID) * PhotoID (FK to &lt;Photo&gt;.PhotoID) * ProfileID (FK to &lt;Profile&gt;.ProfileID) * Body </code></pre> <p>And, you will have to ensure that you allow nulls in PostID,PhotoID and ProfileID columns in Comment table and also perhaps set the default value to null.</p> <p>Here is the DDL to achieve this - </p> <pre><code>Create table Photo ( PhotoID int, PhotoDesc varchar(10), Primary key (PhotoID) ) Create table Post ( PostID int, PostDesc varchar(10), Primary key (PostID) ) Create table Profiles ( ProfileId int, ProfileDesc varchar(10), Primary key (ProfileId) ) Create table Comment ( CommentID int, PhotoID int, PostID int, ProfileId int, body varchar(10), Primary key (CommentID), Foreign key (PhotoID) references Photo(PhotoID), Foreign key (PostID) references Post(PostID), Foreign key (ProfileId) references Profiles(ProfileId) ) insert into Photo values (1,'Photo1') insert into Photo values (2,'Photo2') insert into Photo values (3,'Photo3') insert into Post values (11,'Post1') insert into Post values (12,'Post2') insert into Post values (13,'Post3') insert into Profiles values (111,'Profiles1') insert into Profiles values (112,'Profiles2') insert into Profiles values (113,'Profiles3') insert into Comment (CommentID,PhotoID,body) values (21,1,'comment1') insert into Comment (CommentID,PhotoID,body) values (22,3,'comment2') insert into Comment (CommentID,PostID,body) values (23,11,'comment3') insert into Comment (CommentID,PostID,body) values (24,12,'comment4') insert into Comment (CommentID,ProfileId,body) values (25,112,'comment5') insert into Comment (CommentID,ProfileId,body) values (26,113,'comment6') -- to select comments seperately for Photos, profiles and posts select * from Comment where PhotoID is not null select * from Comment where ProfileId is not null select * from Comment where PostID is not null </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload