课程程序应该是:
COURSE表中已存在先决条件课程(如果有)。为简单起见 我们会假装一门课程最多只能有一门课程作为先决条件。
课程的先决条件必须是较低的水平。例如,前提条件 ISYS326必须是100或200级课程,不能是300级课程。
如果上述任一要求失败,则该过程引发异常WRONG_PREREQ 打印适当的警报消息,不修改COURSE表。
所以我完成了上述程序:
create or replace PROCEDURE NEW_COURSE (
CID IN COURSE.COURSEID%TYPE,
CNAME IN COURSE.COURSENAME%TYPE,
PID IN HAS_PREREQUISITE.PREREQUISITEID%TYPE,
DID IN DEPARTMENT.DEPTID%TYPE
)
IS
CONDITION1 NUMBER;
CONDITION2 NUMBER;
WRONG_PREQ EXCEPTION;
BEGIN
/* this can be checked before any SQL */
IF (SUBSTR(CID,5,3) <= SUBSTR(PID,5,3)) THEN /*PREREQUISITE HAS A HIGHER ID THAN COURSEID */
RAISE WRONG_PREQ;
END IF;
SELECT COUNT(COURSEID)
INTO CONDITION1
FROM COURSE
WHERE COURSEID = PID;
/* this can be checked here, without running the second select */
IF (CONDITION1 <>1) THEN /*VIOLATION OF CONDITION 1, NO EXISTING
PREREQUISITE COURSE*/
RAISE WRONG_PREQ;
END IF;
SELECT COUNT(*)
INTO CONDITION2
FROM HAS_PREREQUISITE
WHERE COURSEID = CID
AND PREREQUISITEID = PID
AND SUBSTR(CID,5,3) > SUBSTR(PID,5,3);
IF (CONDITION2 = 1) THEN /*THE RECORD ALREADYS EXISTS IN HAS_PREREQUISITE
TABLE*/
RAISE WRONG_PREQ;
END IF;
INSERT INTO COURSE VALUES(CID,CNAME,DID,NULL,NULL,NULL,NULL,NULL,NULL); /* edit the statement to fit the table structure also 'NULL' or null ?*/
INSERT INTO HAS_PREREQUISITE VALUES(CID,PID); /* the table only has 2 colums */
INSERT INTO DEPARTMENT VALUES (DID,NULL,NULL,NULL); /* edit the statement to fit the table structure. also 'NULL' or null ? */
EXCEPTION
WHEN WRONG_PREQ THEN
DBMS_OUTPUT.PUT_LINE('COURSEID OR PREREQUISITE ID IS INVALID');
END NEW_COURSE;
当我试图通过像这样的匿名阻止来调用该程序时
SET SERVEROUTPUT ON
DECLARE
CID char(8):='isys228';
CNAME varchar2(20):='Greendata';
PID char(8):='isys114';
DID char(8):='comp3647';
BEGIN
DBMS_OUTPUT.PUT_LINE('The course has been enrolled');
NEW_COURSE(CNAME,CID,PID,DID);
END;
/
更新:匿名块有效,但我无法看到表中输入的数据
我试图以ISYS114为先决条件,通过此程序开设一门新课程ISYS228,“绿色数据”。
请帮助!!我对这个sql相当新,所以我很难看到我出错的地方
只是抱怨ddl
/*==============================================================*/
/* DBMS name: ORACLE Version 11g */
/* Created on: 11/10/2016 3:47:29 PM */
/*==============================================================*/
alter table ACADEMIC_REC
drop constraint FK_ACADEMIC_RELATIONS_STUDENT;
alter table ACADEMIC_REC
drop constraint FK_ACADEMIC_RELATIONS_COURSE;
alter table COURSE
drop constraint FK_COURSE_OFFERS_DEPARTME;
alter table ENROLS
drop constraint FK_ENROLS_ENROLS_STUDENT;
alter table ENROLS
drop constraint FK_ENROLS_ENROLS2_TUT_PRAC;
alter table HAS_PREREQUISITE
drop constraint FK_HAS_PRER_HAS_PRERE_COURSE;
alter table HAS_PREREQUISITE
drop constraint FK_HAS_PRER_HAS_PRERE_COURSE;
alter table STAFF
drop constraint FK_STAFF_RELATIONS_DEPARTME;
alter table TEACHING_INFORMATION
drop constraint FK_TEACHING_RELATIONS_STAFF;
alter table TEACHING_INFORMATION
drop constraint FK_TEACHING_RELATIONS_COURSE;
alter table TUT_PRAC
drop constraint FK_TUT_PRAC_HAS_COURSE;
alter table TUT_PRAC
drop constraint FK_TUT_PRAC_RELATIONS_STAFF;
drop table ACADEMIC_REC cascade constraints;
drop table COURSE cascade constraints;
drop table ENROLS cascade constraints;
drop table HAS_PREREQUISITE cascade constraints;
drop table STAFF cascade constraints;
drop table STUDENT cascade constraints;
drop table TEACHING_INFORMATION cascade constraints;
drop table TUT_PRAC cascade constraints;
/*==============================================================*/
/* Table: ACADEMIC_REC */
/*==============================================================*/
create table ACADEMIC_REC
(
STUID CHAR(8) not null,
COURSEID CHAR(8) not null,
STATUS VARCHAR2(5),
YEAR NUMBER(4),
SEMESTER CHAR(2),
GRADE VARCHAR2(2)
constraint CKC_GRADE_ACADEMIC check (GRADE is null or (GRADE in ('HD','D','CR','P','F'))),
constraint PK_ACADEMIC_REC primary key (STUID, COURSEID)
);
/*==============================================================*/
/* Table: COURSE */
/*==============================================================*/
create table COURSE
(
COURSEID CHAR(8) not null,
DEPTID CHAR(8) not null,
COURSENAME VARCHAR2(20),
TEXTBOOK VARCHAR2(20),
CREDITHOUR NUMBER(2),
MAX_ENROL NUMBER(4),
ACTUAL_ENROL NUMBER(4),
AVAILABILITY NUMBER(4),
COURSE_TIME CHAR(7),
constraint PK_COURSE primary key (COURSEID)
);
/*==============================================================*/
/* Table: DEPARTMENT */
/*==============================================================*/
create table DEPARTMENT
(
DEPTID CHAR(8) not null,
DEPTNAME VARCHAR2(20),
DEPTCONTACTNO NUMBER(10),
BUILDING VARCHAR2(5),
constraint PK_DEPARTMENT primary key (DEPTID)
);
/*==============================================================*/
/* Table: ENROLS */
/*==============================================================*/
create table ENROLS
(
STUID CHAR(8) not null,
CLASSID CHAR(8) not null,
constraint PK_ENROLS primary key (STUID, CLASSID)
);
/*==============================================================*/
/* Table: HAS_PREREQUISITE */
/*==============================================================*/
create table HAS_PREREQUISITE
(
COURSEID CHAR(8) not null,
PREREQUISITEID CHAR(8) not null,
constraint PK_HAS_PREREQUISITE primary key (COURSEID, PREREQUISITEID)
);
/*==============================================================*/
/* Table: STAFF */
/*==============================================================*/
create table STAFF
(
STAFFID CHAR(8) not null,
DEPARTMENTID CHAR(8) not null,
STAFFNAME VARCHAR2(50),
STAFFADDRESS VARCHAR2(70),
STAFFCONTACTNO NUMBER(10),
STAFFEMAIL VARCHAR2(50),
OFFICENO NUMBER(5),
ROLE VARCHAR2(10),
constraint PK_STAFF primary key (STAFFID)
);
/*==============================================================*/
/* Table: STUDENT */
/*==============================================================*/
create table STUDENT
(
STUID CHAR(8) not null,
DEGREE VARCHAR2(10),
MAJOR VARCHAR2(10),
STU_NAME VARCHAR2(50),
STU_ADDRESS VARCHAR2(70),
CONTACTNO NUMBER(10),
EMAIL VARCHAR2(50),
constraint PK_STUDENT primary key (STUID)
);
/*==============================================================*/
/* Table: TEACHING_INFORMATION */
/*==============================================================*/
create table TEACHING_INFORMATION
(
STAFFID CHAR(8) not null,
COURSEID CHAR(8) not null,
SEMESTER CHAR(2) not null,
YEAR NUMBER(4) not null,
constraint PK_TEACHING_INFORMATION primary key (STAFFID, COURSEID, SEMESTER, YEAR)
);
/*==============================================================*/
/* Table: TUT_PRAC */
/*==============================================================*/
create table TUT_PRAC
(
CLASSID CHAR(8) not null,
COURSEID CHAR(8) not null,
STAFFID CHAR(8) not null,
TYPE VARCHAR2(5),
DAY VARCHAR2(10),
TIME DATE,
ROOMID CHAR(10),
NO_OF_SEATS NUMBER(2),
constraint PK_TUT_PRAC primary key (CLASSID)
);
alter table ACADEMIC_REC
add constraint FK_ACADEMIC_RELATIONS_STUDENT foreign key (STUID)
references STUDENT (STUID);
alter table ACADEMIC_REC
add constraint FK_ACADEMIC_RELATIONS_COURSE foreign key (COURSEID)
references COURSE (COURSEID);
alter table COURSE
add constraint FK_COURSE_OFFERS_DEPARTME foreign key (DEPTID)
references DEPARTMENT (DEPTID);
alter table ENROLS
add constraint FK_ENROLS_ENROLS_STUDENT foreign key (STUID)
references STUDENT (STUID);
alter table ENROLS
add constraint FK_ENROLS_ENROLS2_TUT_PRAC foreign key (CLASSID)
references TUT_PRAC (CLASSID);
alter table HAS_PREREQUISITE
add constraint FK_HAS_PRER_HAS_PRERE_COURSE foreign key (COURSEID)
references COURSE (COURSEID);
alter table HAS_PREREQUISITE
add constraint FK_HAS_PRER_HAS_PRERE_COURSE foreign key (PREREQUISITEID)
references COURSE (COURSEID);
alter table STAFF
add constraint FK_STAFF_RELATIONS_DEPARTME foreign key (DEPARTMENTID)
references DEPARTMENT (DEPTID);
alter table TEACHING_INFORMATION
add constraint FK_TEACHING_RELATIONS_STAFF foreign key (STAFFID)
references STAFF (STAFFID);
alter table TEACHING_INFORMATION
add constraint FK_TEACHING_RELATIONS_COURSE foreign key (COURSEID)
references COURSE (COURSEID);
alter table TUT_PRAC
add constraint FK_TUT_PRAC_HAS_COURSE foreign key (COURSEID)
references COURSE (COURSEID);
alter table TUT_PRAC
add constraint FK_TUT_PRAC_RELATIONS_STAFF foreign key (STAFFID)
references STAFF (STAFFID);
答案 0 :(得分:0)
SET SERVEROUTPUT ON
DECLARE
CID char(8):='isys228';
CNAME varchar2(20):='Greendata';
PID char(8):='isys114';
DID char(8):='comp3647';
BEGIN
DBMS_OUTPUT.PUT_LINE('The course has been enrolled');
END;
尝试改为
答案 1 :(得分:0)
尝试使用此代码。
SET SERVEROUTPUT ON
DECLARE
CID COURSE.COURSEID%TYPE:='isys228';
CNAME COURSE.COURSENAME%TYPE:='Greendata';
PID HAS_PREREQUISITE.PREREQUISITEID%TYPE:='isys114';
DID DEPARTMENT.DEPTID%TYPE:='comp3647';
BEGIN
DBMS_OUTPUT.PUT_LINE('The course has been enrolled');
NEW_COURSE(CNAME,CID,PID,DID);
END;
/
答案 2 :(得分:0)
您需要提交插入内容。 试试
commit;
之后
NEW_COURSE(CNAME,CID,PID,DID);
或者如果您想要提交过程
INSERT INTO DEPARTMENT VALUES (DID,NULL,NULL,NULL); /* edit the statement to fit the table structure. also 'NULL' or null ? */
COMMIT;
EDIT 重新排序您的插入订单,因为COURSE有一个FK到DEPARTMENT
-- FIRST THE INSERT IN DEPARTMENT
INSERT INTO DEPARTMENT VALUES (DID,NULL,NULL,NULL); /* edit the statement to fit the table structure. also 'NULL' or null ? */
INSERT INTO COURSE VALUES(CID,CNAME,DID,NULL,NULL,NULL,NULL,NULL,NULL); /* edit the statement to fit the table structure also 'NULL' or null ?*/
INSERT INTO HAS_PREREQUISITE VALUES(CID,PID); /* the table only has 2 colums */