我正在创建一个包含两个表(学生和教师)的 MySQL 架构。
1. Students are given a student number when they join the school.
This is stored along with their name, date of birth, and the date they joined
the school.
2. All instructors are also students, but clearly, not all students are
instructors. In addition to the normal student information, for all
instructors, the date that they start working as an instructor must be
recorded, along with their instructor status (compensated or volunteer).
我的架构:
学生
+---------------+
| ID | PK
+---------------+
| LNAME |
+---------------+
| FNAME |
+---------------+
| MNAME |
+---------------+
| BDAY |
+---------------+
| JOIN_DATE |
+---------------+
| IS_INSTRUCTOR |
+---------------+
讲师
+---------------+
| ID | PK
+---------------+
| STUDENT_ID | FK
+---------------+
| INSTR_STATUS | (compensated or volunteer)
+---------------+
| DATE_WORKED |
+---------------+
当我插入 STUDENT TABLE 时,如果 IS_INSTRUCTOR = 1(意思是 true),INSTRUCTOR TABLE (STUDENT_ID) 会有 STUDENT.ID 吗?
或者你们有更好的表格设计吗? TIA
最佳答案
我不确定为什么学生表中需要 is_instructor
字段。每次从 instructor
表中添加或删除记录时,您都需要更新它。如果您需要查看特定学生是否是教师,则对 student_id
进行简单的哈希连接就足够了。但我看到的选项:第一个选项是使用存储过程,第二个选项只是将单独的插入包装到一个事务中。
我会删除计算字段is_instructor
,除非有我不知道的特定应用程序要求。尽管代理(或人工)键可能很有用,但对于当前的设计,我会从讲师表中删除主键 id ,并使 student_id 成为主键,它也是学生
表。
关于MySQL 插入带有条件语句的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22414672/