是否可以创建外键

是否可以创建外键

本文介绍了如果两个字段都不唯一,是否可以创建外键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑创建以下2个数据库表:

I have 2 database tables below I am thinking of creating:

问题表:

QuestionId(PK)  QuestionNo  QuestionContent  SessionId (FK)
11              1           Question1        3
12              2           Question2        3
13              3           Question3        3

Image_Question

Image_Question

ImageId (PK)  SessionId (Fk)  QuestionNo (FK)
1              3               1
2              3               2

由于我设置图像上传的方式,用户可以在将问题提交到数据库之前将图像上传到问题,因此,确定图像属于我的html的问题的唯一方法是在直到提交页面后,才输入要知道哪个问题作为QuestionId上传图像文件的html.

Because of the way I set up an image upload, the user can upload images to a question before the question is submitted into db, so only way to determine which question an image belong to in my html was to use the QuestionNo in the html to know which question uploaded the image file as QuestionId is not entered until page is submitted.

我的问题是,有可能有2个非唯一字段作为外键吗?

My question is that is it possible to have 2 non unique fields as foreign keys?

如果我仅将SessionId单独设置为(FK),并且QuestionNo允许(FK)设置,那也是我自己设置的问题,因为如果有多个具有相同QuestionNo的会话,请单击在上面的QuestionNo上,它可能在包含该QuestionNo的所有会话中显示同一QuestionNo的行.

Also if I just set SessionId as (FK) on its own and if QuestionNo allows (FK), that is set on its own thn prblem I have as that as there are multiple sessions with same QuestionNo, if I click on a QuestionNo above, it could displays rows for the same QuestionNo in all sessions that contain thatQuestionNo.

我的另一个问题是如何组合SessionIdQuestionNo(FK),以便如果我在QuestionNo下搜索,它将在相关的SessionId中搜索该QuestionNo?如果单击SessionId,则它会显示该SessionId的详细信息,这样就可以了.

My other question will be how to combine SessionId and QuestionNo (FK) so that if I search under QuestionNo, it searches for that QuestionNo in the relevant SessionId? If I click on the SessionId then it displays details for that SessionId so that is ok.

推荐答案

我发现您的问题有点难以理解,但是据我所知,您正在使用QuestionNo作为临时令牌将几个http请求链接在一起(图片上传) +问题帖).

I find your question a little hard to understand, but from what I can gather you are using QuestionNo as a temporary token to link several http requests together (image uploads + question post).

我建议类似:

SessionQuestionToken
{
  Token
  SessionId
}

Image
{
  ID
  QuestionId FK references Question(Id)
  ImageName
}

Question
{
  Id
  Content
}

TempImage
{
  Token FK references SessionQuestionToken(token)
  ImageName
}

逻辑如下:

  1. 用户请求问题表格.服务器设置令牌并包含令牌以响应用户.
  2. 用户上传包含令牌的图片.图像存储在临时表中.
  3. 第2步重复了n次.
  4. 如果用户使用令牌值提交问题,则会在问题表中放置一个条目并分配一个ID.共享令牌的TempImage表中的所有图像都使用现在已知的QuestionId插入到图像表中.然后删除SessionQuestionToken条目,并级联删除TempImage中的临时图像.
  5. 其他用户未提交问题,则文件被删除,SessionQuestionToken条目被删除.

对于您的问题,外键是否仍然可以引用非唯一列,答案是肯定的.

As for your question if foreign keys can still reference non unique columns, the answer is yes.

这篇关于如果两个字段都不唯一,是否可以创建外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 22:32