- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
条件是
一科多师关系,即一科可以由多位老师教授,但一名老师只能教授一科
多学生与多科目的关系,即许多学生可以选修一门共同科目,而一个学生可以选修多门科目。
多个学生与多个批处理的关系。
不能同时保存两个批处理,因此批处理表中需要一个唯一的日期字段。
多批处理与一科目的关系,即每批处理仅教授一门科目。但在其他批处理中,可以重复上一批处理教授的相同科目。
最后一个条件给了我一个问题,即多个批处理与一个老师的关系,即一个老师可以分批授课,每批只能有一名老师。
我最初的方法如下
teacher table
-----------------------
id(PK) name email subject_id(FK to subject.id)
subject table
-----------------------
id(PK) name description
student table
----------------------
id(PK) name location
batch table
----------------------
id(PK) venue teacher_id(FK to teacher.id) date(unique)
student-batch table
-----------------------
id(PK) batch_id(FK to batch.id) student_id(FK to student.id)
并且需要获取特定日期的批处理 ID、教师姓名、学生姓名等信息。
但是在分配教师到批处理时会出现问题,每个批处理只能教授一门特定科目,而且一名教师不能教授多个科目。
最佳答案
您缺少关联科目和学生的表格(每点 2):
// student [student_id] takes subject [subject_id]
takes(student_id, subject_id)
请注意,每个基表都有一个关联的语句模板,用于有关业务情况的语句,并通过列名称进行参数化 - 它的(特征)谓词。使谓词为 true 的行进入表中。请注意,表定义看起来像是谓词的简写。
// teacher [id] named [name] with email [email] teaches subject [subject_id]
teacher(id, name, email, subject_id)
// subject [id] named [name] is [description]
subject(id, name, description)
// student [id] named [name] lives at [location])
student(id, name, location)
// batch [id] at venue [venue] was taught by teacher [teacher_id] on date [date]
batch(id, venue, teacher_id, date)
// student-batch [id] reports student [student_id] being in batch [batch_id]
student-batch(id, student_id, batch_id)
// CHECK student [student_id] takes the subject that is taught by the teacher of batch [batch_id]
But the problem is occuring when assigning teacher to batches, in each batch only one particular subject is being taught, and also one teacher can not teach more than one subject.
既然你似乎对此感到困惑,我将根据如何推理表、约束和查询设计来推导它。表达您想要的约束的一种方法似乎是上面注释的 CHECK。
要在 SQL 中表达任何表、约束或查询,我们首先决定其谓词。然后我们可以将谓词转换为简写。然后我们可以将简写转换为 SQL。
谓词:
student [student_id] takes the subject that is taught by the teacher of batch [batch_id]
使用基表谓词:
FOR SOME k.*, t.*, b.* (
student_id = k.student_id AND batch_id = b.bid
AND student [k.student_id] takes subject [k.subject_id]
AND teacher [t.id] named [t.name] with email [t.email] teaches subject [t.subject_id]
AND batch [b.id] at venue [b.venue] was taught by teacher [b.teacher_id] on date [b.date]
AND [k.subject_id] = [t.subject_id]
AND [t.id] = [b.teacher_id])
使用简写:
FOR SOME k.*, t.*, b.* (
student_id = k.student_id AND batch_id = b.bid
AND takes(k.student_id, k.subject_id)
AND teacher(t.id, t.name, t.email, t.subject_id)
AND batch(b.id, b.venue, b.teacher_id, date)
AND k.subject_id = t.subject_id
AND t.id = b.teacher_id)
在 FROM 中,每个(可能是隐式的)别名表示一个表,类似于给定的基表名称和/或子查询,但其值和谓词中的每一列均重命名为别名。列.
我们通过在 SQL 中 JOIN 谓词表来获取满足两个谓词 AND 的行。如果我们想要满足条件 AND 的行,那么我们在 SQL 中使用 ON 或 WHERE。
SELECT 子句返回的行中,对于点列的某些值,返回的(未点列)列等于满足 FROM 谓词的点列的函数。
SQL:用表替换语句,用 JOIN 或 ON 或 WHERE 替换语句,用 SELECT 替换外部 FOR SOME & THERE EXISTS:
SELECT t.student_id AS student_id, b.bid AS batch_id
FROM takes k JOIN teacher t JOIN batch b
WHERE k.subject_id = t.subject_id
AND t.id = b.teacher_id
AND student_id = t.student_id
AND batch_id = b.id
满足两个谓词 OR 的行表是它们表的 UNION。对于 AND NOT,我们使用 EXCEPT(又名 MINUS)(或 LEFT JOIN 惯用语)。 FOR SOME 或 THERE EXISTS 无法在 SQL 中查询所有列,但如果我们想知道是否有满足谓词的行,那么我们可以在带有该谓词的子查询周围使用 EXISTS。
假设我们想要约束一个基表,以便每一行都满足某些列上的谓词。即,对于所有列,如果它们满足基本谓词,那么它们满足查询谓词。即 FOR ALL 列 IF 它们形成的行位于基数中,那么它位于查询中。所以我们在 SQL 中要求 NOT EXISTS(从基本查询中选择列)。或者对于数据库中的每一行,我们需要在 SQL 中存在 EXISTS(query)。
在标准 SQL 中,您可以 CREATE ASSERTION CHECK(NOT EXISTS (SELECT Student_id, batch_id FROM Student-batch EXCEPT query)) 或在 CREATE TABLE Student-batch 中,您可以 CHECK(EXISTS(query))。不幸的是,MySQL 或大多数 DBMS 不支持这些。如果您在批处理后插入到学生批处理,那么您可以在触发器上要求存在(查询)。或者您可以添加某些列和复合 FK(外键)约束。
And need to get the information lets say batch's id, teacher's name, students' name for a particular day.
现在我们正在编写一个查询。我们想要的行位于:
FOR k.*, t.*, b.*, s.*, sb.* (
batch = b.id AND teacher = t.name AND student = s.name
AND takes(k.student_id, k.subject_id)
AND teacher(t.id, t.name, t.email, t.subject_id)
AND batch(b.id, b.venue, b.teacher_id, b.date)
AND student(s.id, s.name, s.location)
AND student-batch(sb.id, sb.student_id, sb.batch_id)
AND k.subject_id = t.subject_id
AND t.id = b.teacher_id
AND s.id = k.student_id
AND sb.student_id = k.student_id
AND sb.batch_id = b.id
AND @date = b.date)
这看起来像具有不同返回列和添加行的约束谓词。 SQL 也是直接翻译的。我们添加与学生的联接以获取学生姓名。我们添加了与 Student-batch 的连接,因为约束不处理它;使用约束查询的上下文检查学生批处理(student_id,batch_id)子行是否在其中。
SELECT b.id AS batch, t.name AS teacher, s.name AS student
FROM takes k JOIN teacher t JOIN batch b JOIN student s JOIN student-batch sb
WHERE ... AND @date = date
您可以尝试 ON 版本:
SELECT b.id AS Batch, t.name AS Teacher, s.name AS Student
FROM takes k
JOIN teacher t ON k.subject_id = t.subject_id
JOIN batch b ON t.id = b.teacher_id
JOIN ...
WHERE @date = b.date
关于mysql - 如何设计一个简单的教师-科目-学生批处理关系数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24724425/
我正在努力实现以下目标, 假设我有字符串: ( z ) ( A ( z ) ( A ( z ) ( A ( z ) ( A ( z ) ( A ) ) ) ) ) 我想编写一个正则
给定: 1 2 3 4 5 6
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
大家好,我卡颂。 Svelte问世很久了,一直想写一篇好懂的原理分析文章,拖了这么久终于写了。 本文会围绕一张流程图和两个Demo讲解,正确的食用方式是用电脑打开本文,跟着流程图、Demo一
身份证为15位或者18位,15位的全为数字,18位的前17位为数字,最后一位为数字或者大写字母”X“。 与之匹配的正则表达式: ?
我们先来最简单的,网页的登录窗口; 不过开始之前,大家先下载jquery的插件 本人习惯用了vs2008来做网页了,先添加一个空白页 这是最简单的的做法。。。先在body里面插入 <
1、MySQL自带的压力测试工具 Mysqlslap mysqlslap是mysql自带的基准测试工具,该工具查询数据,语法简单,灵活容易使用.该工具可以模拟多个客户端同时并发的向服务器发出
前言 今天大姚给大家分享一款.NET开源(MIT License)、免费、简单、实用的数据库文档(字典)生成工具,该工具支持CHM、Word、Excel、PDF、Html、XML、Markdown等
Go语言语法类似于C语言,因此熟悉C语言及其派生语言( C++、 C#、Objective-C 等)的人都会迅速熟悉这门语言。 C语言的有些语法会让代码可读性降低甚至发生歧义。Go语言在C语言的
我正在使用快速将 mkv 转换为 mp4 ffmpeg 命令 ffmpeg -i test.mkv -vcodec copy -acodec copy new.mp4 但不适用于任何 mkv 文件,当
我想计算我的工作簿中的工作表数量,然后从总数中减去特定的工作表。我错过了什么?这给了我一个对象错误: wsCount = ThisWorkbook.Sheets.Count - ThisWorkboo
我有一个 perl 文件,用于查看文件夹中是否存在 ini。如果是,它会从中读取,如果不是,它会根据我为它制作的模板创建一个。 我在 ini 部分使用 Config::Simple。 我的问题是,如果
尝试让一个 ViewController 通过标准 Cocoa 通知与另一个 ViewController 进行通信。 编写了一个简单的测试用例。在我最初的 VC 中,我将以下内容添加到 viewDi
我正在绘制高程剖面图,显示沿路径的高程增益/损失,类似于下面的: Sample Elevation Profile with hand-placed labels http://img38.image
嗨,所以我需要做的是最终让 regStart 和 regPage 根据点击事件交替可见性,我不太担心编写 JavaScript 函数,但我根本无法让我的 regPage 首先隐藏。这是我的代码。请简单
我有一个非常简单的程序来测量一个函数花费了多少时间。 #include #include #include struct Foo { void addSample(uint64_t s)
我需要为 JavaScript 制作简单的 C# BitConverter。我做了一个简单的BitConverter class BitConverter{ constructor(){} GetBy
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我是 Simple.Data 的新手。但我很难找到如何进行“分组依据”。 我想要的是非常基本的。 表格看起来像: +________+ | cards | +________+ | id |
我现在正在开发一个 JS UDF,它看起来遵循编码。 通常情况下,由于循环计数为 2,Alert Msg 会出现两次。我想要的是即使循环计数为 3,Alert Msg 也只会出现一次。任何想法都
我是一名优秀的程序员,十分优秀!