ASP.NET C# 表和 SQL 逻辑问题

发布于 2024-10-02 02:12:29 字数 580 浏览 3 评论 0原文

嘿,我有 3 个表,分别为“学生”、“课程”和“入学”。 STUDENT 的 PK 为 S_ID(学生 ID),COURSE 的 PK 为 C_ID(课程 ID)。在注册时它只有S_ID和C_ID。我有一个对象数据源,用于在下拉菜单中显示所有学生的姓名(以文本形式,S_ID 作为值),并且使用数据网格和另一个对象数据源,单击时将显示他注册的课程。我不想让学生也注册多门课程,但我不能这样做,因为课程表中不能有相同的 ID,所以每个学生只能注册一门课程。

是否有某种选项可以在表中具有相同的 ID?

如果没有,那么我必须了解如何操作 COURSE 表中 C_ID 中的字符串,因为所有课程都以 ISDVXXX 或 ITSXXXX 或 HGFXXXX 开头。这可能很难理解,但希望有人能提供帮助。

一个例子可能会有所帮助 因此,如果一个名为 Joe、S_ID 为 123 的学生注册到 ISDV,他将注册到所有以 ISDV 开头的课程。但我的问题是,我的课程表必须为每门课程提供唯一的 ID,例如 ISDV123、ISDV346、ISDV395 等...所以这也破坏了我的注册表,因为我不能简单地将 ISDV 放在那里,它需要特定的课程,但他已注册到所有这些。任何更多的澄清将给出:P谢谢......

Hey I have 3 tables called STUDENT, COURSE and ENROLLMENT. STUDENT has a PK of S_ID(student ID) and COURSE has PK of C_ID(course ID). In enrollment it only has S_ID and C_ID. I have an object data source to show all the students name (in text, and S_ID as the value) in a drop down menu and it will show which courses he is registered in when clicked, using a datagrid and another object data source. I wont to have the student to have multiple courses to be registered too, but I cant do that because you cannot have the same ID in the COURSE table, so every student is only registered to one course.

Is there some sort of option to have same ID's in a table?

If not, then I must some how manipulate the string in C_ID in the COURSE table because all the courses start with ISDVXXX or ITSXXXX or HGFXXXX. This may be hard to understand but hopefully someone will help.

An example may help
So if a student named Joe with a S_ID of 123 is registered to ISDV, he will be registered to all the courses that start with ISDV. But my problem is that my COURSE table has to have unique ID for each course such as ISDV123, ISDV346, ISDV395 etc... so this also ruins my enrollment table because I cannot simply have ISDV in there, it needs a specific course but he is registered to all of them. Any more clarification will be given :P Thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘿看小鸭子会跑 2024-10-09 02:12:29

您要解决的是多值属性问题。基本上,您有两个表,其中一个表(students)的主键是另一个表(classes)中的外键。您不希望在 classes 表中拥有多个同一班级,但您确实希望一个 student 能够拥有多个classes

因此,有一个非常简单的修复方法,您创建另一个表,其中至少包含这两列:student_id 和 class_id。这样,您就可以拥有多个学生链接到的单个,以及学生链接到的多个代码>可以链接到。

What you're trying to solve is a multi-valued attribute problem. Basically, you have two tables where one (students) has a primary key which is a foreign key in another table (classes). You don't want to have multiples of the same class in the classes table, but you do want a student to be able to have multiple classes.

So, there is a very simple fix, you create another table which contains at least these two columns: student_id and class_id. This way, you could have a single class that multiple students are linked to, and also multiple classes to which a student can be linked to.

烟若柳尘 2024-10-09 02:12:29

您正在寻找的是多对多的关系 - 即一个学生可以有多个课程,而一个课程可以有多个学生。因此,您的链接表(这是您打算注册的目的吗?)应该有两列,一列用于课程 ID,一列用于学生 ID。

因此,如果您有学生 123 和学生 234,以及课程 ABC 和 XYZ,您的表将类似于:

S_ID   C_ID
123    ABC
123    XYZ
234    ABC

现在,对于您的注册 PK,您可以使用复合键,或添加唯一整数 RowId(Identity 或 HiLo 算法) 。

在这种情况下,您的注册表将如下所示:

S_ID    C_ID     RowID
123     ABC      1
123     XYZ      2
234     ABC      3

然后,要查看学生所在的班级,您可以执行以下操作

Select * from courses c 
 inner join enrollments e 
 on c.C_ID = e.C_ID 
 AND e.S_ID = @StudentId

What you are looking for is a many to many relationship - i.e. a single student can have multiple courses, and a single course can have multiple students. So your link table (is this what you intended enrollment for?) should have two columns, one for the course ID and one for the student ID.

So if you had student 123 and student 234, and courses ABC and XYZ, your table would look something like:

S_ID   C_ID
123    ABC
123    XYZ
234    ABC

Now, for your PK on enrollments you could either use a composite key, or add a unique integer RowId (Identity or HiLo algorithm).

In that case, your enrollment table would look something like this:

S_ID    C_ID     RowID
123     ABC      1
123     XYZ      2
234     ABC      3

Then, to see what classes a student was in, you could do something like

Select * from courses c 
 inner join enrollments e 
 on c.C_ID = e.C_ID 
 AND e.S_ID = @StudentId
美人如玉 2024-10-09 02:12:29

您的 Enrollment 表代表 Studend 和 Course 表之间的 m:n 关系。

例如,您的 S_ID = 69 的学生注册了 C_ID = ISDVXXX 、C_ID = ITSXXXX、C_ID = HGFXXXX 的课程
一个学生可以注册更多的课程,更多的学生可以注册同一门课程,这就是您拥有注册表的原因。在我们的示例中,注册将包含以下行:
(69,ISDVXXX),(69,ITSXXXX),(69,HGFXXXX)。
如果稍后 S_ID = 96 的学生加入 C_ID = ISDVXXX 的课程,则注册表中将出现以下新行:
(69,ISDVXXX),(69,ITSXXXX),(69,HGFXXXX),(96,ISDVXXX)。
这里要理解的重要一点是,注册表中的每一行代表注册课程的学生,除了学生 ID 和课程 ID 之外不需要其他字段,因为这两个字段一起标识一个学生注册。

Your Enrollment table stands for the m:n relationship between the Studend and Course tables.

For instance your student with S_ID = 69 is Enrolled to the Courses with C_ID = ISDVXXX , C_ID = ITSXXXX, C_ID = HGFXXXX
A Student can be Enrolled to more Courses and more Students can be Enrolled to the same Course, that's why you have your Enrollment table. In our example, the Enrollment will have the following rows:
(69, ISDVXXX), (69, ITSXXXX), (69, HGFXXXX).
If later a student with the S_ID = 96 joins to the Course with C_ID = ISDVXXX, the following will be the new rows of the Enrollment table:
(69, ISDVXXX), (69, ITSXXXX), (69, HGFXXXX), (96, ISDVXXX).
The important thing to understand here is that each row in the Enrollment table stands for a Student Enrolled to a Course and there is no need to other fields than the ID of the Student and the ID of the Course, for these two fields together identify an Enrollment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文