代码之家  ›  专栏  ›  技术社区  ›  galymzhan

在一个表中定义多个外键到多个表

  •  14
  • galymzhan  · 技术社区  · 14 年前

    我有三种型号:

    岗位 :

    • 身份证件
    • 身体

    照片

    • 身份证件
    • 文件路径

    评论 :

    • 身份证件
    • 身体

    以及数据库中的相应表。现在,如果我只想对我的文章发表评论,我可以简单地添加以下外键: ALTER TABLE comment ADD FOREIGN KEY (post_id) REFERENCES post (id) . 但我想对其他型号(照片、个人资料、视频等)发表评论,并保留所有评论

    5 回复  |  直到 14 年前
        1
  •  10
  •   Álvaro González    14 年前

    你可以这样做:

     post:
      * post_id (PK)
      * title
      * body
    
     photo:
      * photo_id (PK)
      * filepath
    
     comment:
      * comment_id (PK)
      * body
    
     comment_to_post
      * comment_id (PK) -> FK to comment.comment_id
      * post_id (PK) -> FK to post.post_id
    
     comment_to_photo
      * comment_id (PK) -> FK to comment.comment_id
      * photo_id (PK) -> FK to photo.photo_id
    

    仍然有可能得到属于两个不同项目的评论。如果你认为那是个问题,我可以试着改进设计。

        2
  •  10
  •   Damir Sudarevic    14 年前

    Entity 因为缺少更好的词,那么子类型。

    • 在这个模型中,一个实体可以有许多注释,一个注释只属于一个实体。

    alt text

        3
  •  2
  •   pavanred    14 年前

    如果你想知道一个列是否可以有多个外键,那么答案是否定的。

    如果你想的话,你可以有单独的外键。所以你可以这样修改你的评论表-

     comment:
      * comment_id (PK)
      * PostID (FK to Post.PostID)
      * PhotoID (FK to <Photo>.PhotoID)
      * ProfileID (FK to <Profile>.ProfileID)
      * Body
    

    这里是实现这一点的DDL-

    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
    
        4
  •  0
  •   MatTheCat    14 年前

        5
  •  0
  •   HLGEM    14 年前

    由于照片注释与post注释不同,我将它们存储在单独的相关表中。所以我会:

    职务:

    • 张贴
    • 标题
    • 身体

    • 注释ID
    • 后身

    照片:

    • 光面
    • 文件路径

    • 照片id
    • 身体