外键为主键 [英] Foreign key as Primary key
本文介绍了外键为主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我设计了这样的表:
table1: students
---------------------
PK id
name
number
...
---------------------
table2: students_score
---------------------
PK FK student_id
math_score
english_score
...
---------------------
问题1
如果有些学生根本没有分数,好的表格设计?
If some students doesn't have scores at all, is it good table design?
问题2
如果是好的设计,那么如何在MySQL中使FK成为PK?我不知道如何。每次我尝试与上面的关系SQLYog说这个错误:无法创建表'学生#sql-a31_2c8e'(errno:150)
If it's good design, then how can I make FK as PK in MySQL? I can't find out how. Everytime I try to make a relation like above SQLYog says this error: Can't create table 'students.#sql-a31_2c8e' (errno: 150)
谢谢
更新
发现问题2的答案 from这里。这只是一个类型(int,signed int)的问题。
I found an answer of the question 2 from here. This was just a problem of type(int, signed int).
推荐答案
我会建议更多的这些方面: p>
I would suggest something more along these lines:
table1: students
---------------------
PK id
name
number
...
---------------------
table3: classes
---------------------
pk id
name
table2: students_score
---------------------
fk student_id
fk class_id
score
PK(student_id, class_id)
这篇关于外键为主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文