简体   繁体   English

如何将复合主键插入另一个表?

[英]How to insert a composite primary key into another table?

I have a composite primary key that I would like to insert into another table. 我有一个复合主键,我想插入另一个表。

create table courses_instructors
(
courseID int foreign key references Course(C_ID) not null,
instructorID int foreign key references Instructor(I_ID) not null,
primary key (courseID, instructorID), --coourseID and instructorID combined is the composite PK
courseTerm varchar(50) not null,
courseNumber int not null,
courseLocation varchar(50),
courseTime varchar(50),
courseMaxOccupancy int,
courseSeatAvailable int
)

create table courses_students
(
studentID int foreign key references student(S_ID) not null,
courseID int, -- foreign key -- I want this value to the be value that represents the composite PK from the courses_instructors
primary key(studentID, courseID), -- these 2 fields combined would make the composite PK, but with the courseID value I will be able to identify who is the instructor for a course and the other details from the course_instructor table
courseOutcome varchar(50)
)

All the course come from a course table which only contains the course name and the disciple along with a descrption. 所有课程都来自一个课程表,其中只包含课程名称和门徒以及描述。 The course table has a primary key that identifies each course uniquely. 课程表有一个主键,可以唯一标识每个课程。

To refer composit primary key, Courses_Students table should be having both the columns CourseID and InstructorID. 要引用composit主键,Courses_Students表应该同时包含CourseID和InstructorID列。

And then 接着

ALTER TABLE Courses_Students
ADD CONSTRAINT FK_Courses_Students
FOREIGN KEY(CourseID, InstructorID) REFERENCES Courses_Instructors(CourseID, InstructorID)

Or the table definitions should look like, 或者表定义应该是这样的,

create table courses_instructors
(
courseID int foreign key references Course(C_ID) not null,
instructorID int foreign key references Instructor(I_ID) not null,
primary key (courseID, instructorID), --coourseID and instructorID combined is the composite PK
courseTerm varchar(50) not null,
courseNumber int not null,
courseLocation varchar(50),
courseTime varchar(50),
courseMaxOccupancy int,
courseSeatAvailable int
)

create table courses_students
(
studentID int foreign key references student(S_ID) not null,
courseID int,
instructorId int,
FOREIGN KEY(CourseID, InstructorID) REFERENCES Courses_Instructors(CourseID, InstructorID),
primary key(studentID, courseID, InstructorId),
courseOutcome varchar(50)
)

您可以像前面提到的那样引用两列,或者将一个代理键(例如标识列或GUID)添加到主表中并引用它 - 它通常表现更好。

The course_instructors table is an intersection table implementing an mm relationship between, as may be easily guessed, course entities and instructor entities. course_instructors表是交叉表,其实现了可能容易猜到的课程实体和教师实体之间的mm关系。 Almost invariably, I don't add a surrogate key to such a table for the simple reason that such a key would never be used. 几乎无一例外,我没有为这样的表添加代理键,原因很简单,因为这样的密钥永远不会被使用。 A typical user has a reference to one entity or the other and wishes to see all the other entities it relates to. 典型用户具有对一个实体或另一个实体的引用,并希望看到与其相关的所有其他实体。 Or sometimes the user has references to both entities and wished to get the details of their relationship. 或者有时用户可以引用这两个实体,并希望获得他们关系的详细信息。

This rule is not without exceptions, however, and your use case is just such an example. 然而,这条规则并非没有例外,您的用例就是这样一个例子。 The table not just expresses a relationship between two entities but becomes an entity unto itself: a class offering. 该表不仅表达了两个实体之间的关系,而且成为了一个实体:一个类提供。 A student will select a class from a published schedule for the class and day/time desired. 学生将从已发布的课程表和所需的日期/时间中选择一个班级。 This will be identified by a class code number of some sort. 这将通过某种类代码编号来识别。

This code is what will be used to register for the desired class. 此代码将用于注册所需的类。 In cases such as these, it makes sense to create a surrogate key for the intersection table -- which then becomes the class code printed in the catalog. 在这种情况下,为交集表创建一个代理键是有意义的 - 然后它成为目录中打印的类代码。 Thus you would use this class code to refer to the relationship that defines the class offering and not use the composite key. 因此,您将使用此类代码来引用定义类提供的关系,而不是使用组合键。

It looks like you already have such a composite key: the course_number field. 看起来你已经有了这样一个复合键:course_number字段。 You don't have it defined as unique but doesn't it uniquely identity the combination of course, instructor, location and time that makes up each class offering? 您没有将其定义为唯一,但它不能唯一地标识组成每个课程的课程,讲师,地点和时间的组合吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM