您好我正在制作一个测验系统,并且我在最后一张表user_quiz
上如何将其链接到其他表格?我想让它跟踪用户在测验中得到的分数以及用户已经完成的测验总数。
userid是主键,但我坚持外键应该是什么,所以我可以将表链接在一起。我一直在尝试规范化我的数据库,所以如果有人可以帮我解决外键应该是什么,并且如果数据库没有规范化,可以帮助我规范化它。谢谢:))
用户
+-----------+-------------+-----------+-------------+
|userid | email |username |password |
+-----------+-------------+-----------+-------------+
| 1 | | | |
| 2 | | | |
+-----------+-------------+-----------+-------------+
Module
+-----------+-------------+
| module_id | module_name |
+-----------+-------------+
| 1 | Forces |
| 2 | Magnetism |
| 3 | Electricity |
| 4 | Gravitation |
+-----------+-------------+
Questions
+------------+-----------+-----------------+
| question_id| module_id | question |
+------------+-----------+-----------------+
| 1 | 1 | Define a Newton |
| 2 | 1 | Define Work Done|
| 3 | 2 | Define Magnetism|
| 4 | 2 | Define a Tesla |
| 5 | 3 | Define Current |
+------------+-----------+-----------------+
Choices
+----------+-------------+-------------------+----------+
| choice_id| question_id | choices |is_correct|
+----------+-------------+-------------------+----------+
| 1 | 1 | Answer 1 | 0 |
| 2 | 1 | Answer 2 | 0 |
| 3 | 1 | Answer 3 | 0 |
| 4 | 1 | Answer 4 | 1 |
| 5 | 2 | Answer 1 | 0 |
| 6 | 2 | Answer 2 | 0 |
| 7 | 2 | Answer 3 | 0 |
| 8 | 2 | Answer 4 | 1 |
| 9 | 3 | Answer 1 | 0 |
| 10 | 3 | Answer 2 | 0 |
| 11 | 3 | Answer 3 | 0 |
| 12 | 3 | Answer 4 | 1 |
+----------+-------------+-------------------+----------+
user_quiz
+-----------+-------------+---------------------+------------+
|userid | score | total_quizzes_taken | ? |
+-----------+-------------+---------------------|------------+
| 1 | | | |
| 2 | | | |
+-----------+-------------+---------------------+------------+
答案 0 :(得分:0)
FOREIGN KEY
。UserQuiz
是多个:很多映射表?然后它有user_id
,quiz_id
和score
。有关最佳架构的信息,请参阅this:许多表格。user_id = 123采取的测验数:
SELECT COUNT(*)
FROM UserQuiz
WHERE user_id = 123;
但是存在潜在的问题。我假设在问题被回答和评分时添加了条目? COUNT
可能会为您提供已启动的测验数量,而非已完成。如果这不能令人满意,您需要解释如何知道测验已完成。