MySQL 插入带有条件语句的查询

标签 mysql sql mysql-workbench

我正在创建一个包含两个表(学生和教师)的 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/

相关文章:

connection - 使用标准 TCP IP over SSH 在 mysql 工作台中设置新连接

MYSQL触发器更新表

php - 语法错误,意外的 '.',在 config.php 中期望 ']'

sql - 使用 HAVING 和 WHERE 查询

sql - 如何在SQL查询中不获取数据消息

mysql - 在触发器中创建 View

mysql 错误代码 1093 : You can't specify target table for update in FROM clause

c# - 在 MVC3 中使用 ADO.NET MySQL Provider(无法找到请求的 .Net Framework Data Provider。可能未安装。)

php - 自动完成 2 个查询

mysql 特定子字符串