我的代码如下所示。一切都正常工作,但当我的insert语句违反触发器时,它会因为我的raise_application_error而引发错误。
--Drop tables in case they were created previously
DROP TABLE enrollment Purge;
DROP TABLE offering Purge;
DROP TABLE faculty Purge;
DROP TABLE course Purge;
DROP TABLE student Purge;
--Create student table
CREATE TABLE student(StdID number(3) constraint student_StdID_PK primary key,
StdFN varchar2(10),
StdLN varchar2(15),
StdCity varchar2(15),
StdState char(2),
StdZip number(5),
StdMajor varchar2(4),
StdClass char(2),
StdGPA number(2,1),
StdBalance NUMBER (12,2));
--create a table for courses and their descriptions
CREATE TABLE course(CourseNo varchar2(8) constraint course_courseNo_PK primary key,
CrsDesc varchar2(40),
CrsCredits number(1));
--create faculty table
CREATE TABLE Faculty(FacID number(4) constraint faculty_FacID_PK primary key,
FacFN varchar2(10),
FacLN varchar2(15),
FacDept varchar2(4),
FacRank varchar2(4),
FacHireDate date,
FacSalary number(6),
FacSupervisor number(4));
--create table for offered courses
CREATE TABLE Offering(OfferNo number(4) constraint offering_OfferNo_PK primary key,
CourseNo varchar(8)constraint offering_crs_no references course(courseno),
OffTerm varchar2(6),
OffYear number(4),
OffLoca varchar(6),
OffTime varchar(8),
OffDay varchar(5),
FacSSN number(4)constraint offering_fac_FK references faculty(facID));
--create table for student enrollment
CREATE TABLE enrollment(StdID number(3),
OfferNo number(4),
EnrGrade char(2),
constraint enrollment_PK primary key (StdID,OfferNo),
constraint enrollment_std_ID foreign key(StdID) references student(stdID),
constraint enrollment_class_ID foreign key(offerno) references offering(offerno));
CREATE OR REPLACE TRIGGER UNPAID_BALANCE
BEFORE INSERT ON STUDENT
FOR EACH ROW
DECLARE
BAL NUMBER;
BEGIN
BAL := :NEW.STDBALANCE;
IF BAL > 500
THEN
RAISE_APPLICATION_ERROR(-20001, 'Your balance is too high to register. Please pay to continue.');
END IF;
END;
/
CREATE OR REPLACE TRIGGER ENROLL_MAX
BEFORE INSERT ON ENROLLMENT
FOR EACH ROW
DECLARE
NOFSTUDENTS BINARY_INTEGER;
BEGIN
SELECT COUNT(*) INTO NOFSTUDENTS
FROM ENROLLMENT
WHERE OFFERNO = :NEW.OFFERNO AND ENRGRADE IS NULL;
DBMS_OUTPUT.PUT_LINE(NOFSTUDENTS);
IF (NOFSTUDENTS + 1) > 2
THEN
RAISE_APPLICATION_ERROR(-20003, 'EXCEED MAX NO OF STUDENTS ALLOWED');
END IF;
END;
/
--populate student table
INSERT INTO student values(101,'Joe','Smith','Eau Clare','WI',18121,'IS','FR',3.8,225.25);
INSERT INTO student values(102, 'Rob','King', 'Melrose', 'MN', 56352, 'IS','JR',3.2,120.98);
INSERT INTO student values(103, 'Dan','Robinson', 'Sartell', 'MN', 98042, 'IS','JR',3.6, 36);
INSERT INTO student values(104,'Sue','Williams','St.Cloud','MN',56301,'ACCT','SR',3.2,2386.55);
INSERT INTO student values(105,'Don','Robinson','St.Paul','MN',55103,'MKTG','SR',3.4, 306);
--populate course table
INSERT INTO Course values('CSCI 200','Elements of Computing',3);
INSERT INTO Course values('IS 250','Application of Program Dev. I',3);
INSERT INTO Course values('IS 251','Application of Program Dev. II',3);
INSERT INTO Course values('IS 454', 'Data Mining for Decision Support',3);
INSERT INTO Course values('IS 356','Systems Analysis and Design I',3);
INSERT INTO Course values('IS 460', 'Project Management',3);
INSERT INTO Course Values('ACCT 291','Accounting Principles II',3);
INSERT INTO Course values('IS 443','Database Design',3);
--populate faculty table
INSERT INTO faculty values(9001,'Leonard','Vince','IS','ASST','12-Apr-1997',67000,9003);
INSERT INTO faculty values(9002,'Victor','Strong','CSCI','ASSO','8-Aug-1999',70000,9003);
INSERT INTO faculty values(9003,'Nicki','Colan','IS','PROF','20-Aug-1981',75000,9010);
INSERT INTO faculty values(9004,'Fred','Wells','ACCT','ASST','28-Aug-1996',60000,9010);
INSERT INTO faculty values(9010,'Chris','Macon','ACCT','ASST','4-Aug-1980',75000,Null);
--populate offering table
INSERT INTO offering values(2201,'CSCI 200','Spring',2017,'ECC135','10:30AM','MWF',9002);
INSERT INTO offering values(2202,'CSCI 200','Spring',2017,'ECC135','8:00AM','MWF',9002);
INSERT INTO offering values(1102,'ACCT 291','Spring',2017,'CH 14A','2:00AM','MWF',9004);
INSERT INTO offering values(2203,'IS 356','Fall',2017,'CH494','3:30AM','TTH',9001);
INSERT INTO offering values(2204,'IS 251','Fall',2017,'CH494','12:30AM','TTH',9003);
INSERT INTO offering values(1101,'ACCT 291','Fall',2017,'CH350','12:30AM','TTH',9010);
INSERT INTO offering values(2205,'IS 443','Fall',2017,'CH494','9:30AM','MWF',9003);
--populate enrollment table
INSERT INTO enrollment values(101,2201,'A');
INSERT INTO enrollment values(102,2202,'B');
INSERT INTO enrollment values(102,2203,null);
INSERT INTO enrollment values(103,2203,null);
INSERT INTO enrollment values(103,2201,'C');
INSERT INTO enrollment values(103,1101,null);
INSERT INTO enrollment values(104,2202,'A');
INSERT INTO enrollment values(101,2203,null);
INSERT INTO enrollment values(101,1101,null);
INSERT INTO enrollment values(101,2205,null);
INSERT INTO enrollment values(102,2205,null);
INSERT INTO enrollment values(104,2205,null);
有没有一种方法可以让它在脚本输出部分看起来更干净/更好,因为一个特定的情况是由于引发应用程序错误而弹出的错误消息。
命令中第129行开始的错误-
INSERT INTO注册值(1042202,'A'(
错误报告-
ORA-0291:违反了完整性约束(ADMIN_BF.enrollment_STD_ID(-找不到父密钥
您所面临的错误已经足够清楚了。表中没有学生104
,因此无法在enrollment
中创建依赖记录。此错误与触发器无关,而是与数据有关。
现在让我们来看一下针对特定用户的insert
语句:
INSERT INTO student values(104,'Sue','Williams','St.Cloud','MN',56301,'ACCT','SR',3.2,2386.55);
您正试图输入余额2386.55
。但是您的触发器UNPAID_BALANCE
阻止了高于500
的余额,因此insert
会引发应用程序错误";您的余额太高,无法注册";,这是问题的根本原因。你需要修复那个插件。
旁注:在enrollments
的最后一系列插入中还隐藏着另一个问题。触发器ENROLL_MAX
只允许每个OFFERNO
有两个学生,但您试图分配三个学生提供2203
,三个学生也提供2205
。这将引起应用程序错误";超过允许的最大学生人数";。