0

I know it's possible to have n amount of columns, but is it proper mysql "coding standard"?

Here is what I'm doing:

I am a table student which includes all the students info including testScores:

student
-------
studId
name
age
gender
testId

Instead of putting each individual test answer within the student table, I made a separate table called testAnswers that will hold each students test results:

testAnswers
-----------
testId
ques1
ques2
.
.
.
quesN

Each entry in the testAnswers table corresponds to a specific student in the table student.

Of course, there will be an admin that will be able to add questions and remove questions as each year the test questions may change. So, if the admin were to remove an answer, than that means one of the columns would be removed.

只是重申一下自己,我知道这可以在 mysql 中编辑和删除表中的列,但是好的“编码标准”吗?

4

3 回答 3

1

使用两张桌子!

您所描述的是一种one to many关系,因为可以有one学生到many考试成绩。您需要将一些id作为外键student_id并将其放在idtestAnswers中。然后,您可以设置约束,告诉数据库如何处理数据的删除。

正如一位评论者所提到的,使用一个表会导致中断1nf,或者first normal form这基本上是说在给定特定记录的情况下,您不能为单个列有多个值 - 您不能在给定表中为同一用户提供多个测试分数,而是将数据分成两个表。

于 2013-03-30T20:59:29.090 回答
1

The answer is a simple and clear: No. That's just not how you should do it except for very few corner cases.

The usual way to approach this is to normalize your database. Normalization follows a standard procedure that (among other things) avoids having a table with columns names ques1, ques2, ques3 ....

This process will lead you to a database with three tables:

students - id, name, and other stuff that applies to one student each

questions - id and question text for each question

answers - this is a N:M relation between students: student_id, question_id, answer_value

于 2013-03-30T21:04:13.550 回答
0

...of course 2 tables, also could use 3, just remember to insert a studId column also in the testAnswers table (with REFERENCE to the student table) and an INNER JOIN testAnswers ON student.studId=testAnswers.studId at the SELECT query (to read the data).

于 2013-03-30T21:37:16.157 回答