我正在尝试在oracle 11g中创建一个学校注册系统的对象关系数据库,我的sql代码如下。我正在尝试编写一个查询,提供课程名称和在该课程中注册的学生。但是当我尝试添加另一个化学类时,id为101.我收到一个错误。这是因为我已经将courseID作为主键,所以我不能插入多个课程的课程ID?所以任何人都可以告诉我如何创建具有相同ID但不同学生ID的多个化学插入语句?
CREATE TYPE Student_objtyp AS OBJECT (
FName VARCHAR2(20),
LName VARCHAR2(20),
StudentID NUMBER
);
/
CREATE TABLE Student_objtab OF Student_objtyp (StudentID PRIMARY KEY)
OBJECT IDENTIFIER IS PRIMARY KEY;
CREATE TYPE Course_objtyp AS OBJECT (
CourseName VARCHAR(20),
CourseID NUMBER,
StudentID REF Student_objtyp
);
/
CREATE TABLE Course_objtab OF Course_objtyp (
PRIMARY KEY (CourseID),
FOREIGN KEY (StudentID) REFERENCES Student_objtab)
OBJECT IDENTIFIER IS PRIMARY KEY;
INSERT INTO Student_objtab VALUES('bill','smitts',1);
INSERT INTO Student_objtab VALUES('bob','jo',2);
INSERT INTO Course_objtab
SELECT 'Chemistry',101,
REF(S)
FROM Student_objtab S
WHERE S.StudentID = 1;
INSERT INTO Course_objtab
SELECT 'Chemistry',101,
REF(S)
FROM Student_objtab S
WHERE S.StudentID = 2;
INSERT INTO Course_objtab
SELECT 'Physics',201,
REF(S)
FROM Student_objtab S
WHERE S.StudentID = 1;
select c.CourseName, c.StudentID
from Course_objtab c;
答案 0 :(得分:1)
您需要花一些时间考虑数据库的结构。根据您的描述,您至少需要三张桌子。课程,包括PK course_id,学生,PK student_id和注册,使用外键course_id和student_id,引用各自的表格。
答案 1 :(得分:1)
此设计违反了数据库规范化规则。课程应该只保留课程的信息,而不是学生注册课程的学生。 IMO应该至少有一个ENROLLMENT表,引用COURSE和STUDENT。这被称为"联合表"。
我按如下方式对其进行建模:
员工 - 有关员工(教师/教授等)的信息 学生 - 有关学生的信息 课程 - 有关可授课的课程的信息 CLASS - 有关课程特定实例的信息,包括课程开会的日期/时间,地点以及教授课程的工作人员。 CLASS_ENROLLMENT - 将学生与特定的CLASS联系起来。
这里可能还有一些关于ROOM和ROOM_SCHEDULE的内容,也许还有一个Academic_CALENDAR。
祝你好运。
答案 2 :(得分:0)
由于您的course_objtab表包含在课程中注册的学生,因此PK可能应该是courseID和StudentID:
import collections
import contextlib
import threading
lock = threading.Lock()
wait_tracker = collections.defaultdict(lambda: (False, 0, threading.Condition(lock)))
@contextlib.contextmanager
def critical(bar):
with lock:
busy, waiters, condition = wait_tracker[bar]
if busy:
# Someone with the same bar value is in the critical section.
# Record that we're waiting.
waiters += 1
wait_tracker[bar] = busy, waiters, condition
# Wait for our turn.
while wait_tracker[bar][0]:
condition.wait()
# Record that we're not waiting any more.
busy, waiters, condition = wait_tracker[bar]
waiters -= 1
# Record that we're entering the critical section.
busy = True
wait_tracker[bar] = busy, waiters, condition
try:
# Critical section runs here.
yield
finally:
with lock:
# Record that we're out of the critical section.
busy, waiters, condition = wait_tracker[bar]
busy = False
if waiters:
# Someone was waiting for us. Tell them it's their turn now.
wait_tracker[bar] = busy, waiters, condition
condition.notify()
else:
# No one was waiting for us. Clean up a bit so the wait_tracker
# doesn't grow forever.
del wait_tracker[bar]