如何为行创建表并在它们之间建立链接? [英] How to create a table for a row and link between them?
问题描述
我有一个表,其中商店名称,用户的问题,我希望存储多个用户的多个答案的问题。
用户名
`````````
名称问题
----------- --------
alice为什么要使用java?
thomes c和c ++有什么区别?
i想要这样
名称问题答案
-------- ---------------------------------------------
alice为什么要用java? answer1,answer2,answer3
我的尝试:
i want存储使这个网站像这个问题有多个答案。
简单:你需要多个表格。
第一个表存储用户:
ID int,identity
UserName NVARCHAR第二个存储问题:
ID int,identity
问题NVARCHAR
AIDCorrect int,Answers.ID的外键
AIDError1 int,Answers.ID的外键
AIDError1 int ,Answers.ID的外键
然后答案:
ID int,identity
答案NAVRCHAR
然后链接表:
ID int,identity
QID int,外键到Questions.ID
UID int,Users.ID的外键
GivenAnswer int,Answers.ID的外键,Nullable
然后您可以使用SQL JOIN来检索所需的信息。
在您的示例中,您声明您想要的将多个答案从一个人存储到一行。那是不可取的。相反,你应该使用一个关联表。
所以设计应该像
user
----
- userid
- name
question
--------
- questionid
- questiontext
回答
------
- 用户ID
- questionid
- 回答
欲了解更多信息,请查看关联实体 - 维基百科,免费的百科全书 [ ^ ]
i have a table in which store name,question of users and i want store multiple answers from multiple user for a question.
username
``````````
name question
-------------------
alice why use java?
thomes what is difference between c and c++?
i want like this
name question answer
-----------------------------------------------------
alice why use java? answer1,answer2,answer3
What I have tried:
i want to store make a page like this site where for a question have multiple answers.
Simple: you need multiple tables.
The first table stores users:
ID int, identity UserName NVARCHARThe second stores questions:
ID int, identity Question NVARCHAR AIDCorrect int, foreign key to Answers.ID AIDError1 int, foreign key to Answers.ID AIDError1 int, foreign key to Answers.ID
Then answers:
ID int, identity Answer NAVRCHAR
Then the linking tables:
ID int, identity QID int, foreign key to Questions.ID UID int, foreign key to Users.ID GivenAnswer int, foreign key to Answers.ID, Nullable
You can then use SQL JOIN to retrieve the info you need.
In your example you state that you would like to store multiple answers from a person to a single row. That's not advisable. Instead you should use an associative table.
So the design shold be something like
user ---- - userid - name question -------- - questionid - questiontext answer ------ - userid - questionid - answer
For more information, have a look at Associative entity - Wikipedia, the free encyclopedia[^]
这篇关于如何为行创建表并在它们之间建立链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!