学校的Oracle数据库课程
知识点总结及实验练习
PL/SQL基本语法,掌握游标、异常、控制语句
PL/SQL(procedure language/SQL)语言是Oracle对SQL语言的过程化扩充,是一个完整的编程语言。
在PL/SQL中的SELECT语句
例
返回指定部门的所有员工的工资总额
DECLARE
v_sum_sal emp.sal%TYPE;
v_deptno NUMBER NOT NULL := 10;
BEGIN
SELECT SUM(sal) -- group function
INTO v_sum_sal
FROM emp
WHERE deptno = v_deptno;
dbms_output.put_line(v_sum_sal);
END;
•增加新的部门信息到dept表中
DECLARE
v_deptno dept.deptno%TYPE ;
v_dname dept.dname%TYPE;
BEGIN
v_deptno:=&no;
v_dname :='&name';
INSERT INTO dept (deptno,dname) VALUES (v_deptno,v_dname);
END;
更新数据
DECLARE
v_sal_increase emp.sal%TYPE := 2000;
BEGIN
UPDATE emp
SET sal = sal + v_sal_increase
WHERE job = 'ANALYST';
END;
删除数据
DECLARE
v_deptno emp.deptno%TYPE := 10;
BEGIN
DELETE FROM emp
WHERE deptno = v_deptno;
END;
IF逻辑结构
If var1>10 then
if var2<var1 then
var2:=var1+20;
end if;
End if;
注意:上面代码中的两个end if,分别对应两个不同的if。在PL/SQL****中实现if逻辑有两条规则:
–规则1:每个if语句都有自己的then,以if开始的语句行不跟语句结束符(;)。
–规则2:每个if语句块以相应的end if结束。
FOR Loop语句
FOR counter in [REVERSE]
lower_bound..upper_bound LOOP
statement1;
statement2;
. . .
END LOOP;
声明游标
CURSOR cursor_name IS
select_statement;
异常处理语法
EXCEPTION
WHEN exception1 [OR exception2 . . .] THEN
statement1;
statement2;
. . .
[WHEN exception3 [OR exception4 . . .] THEN
statement1;
statement2;
. . .]
[WHEN OTHERS THEN
statement1;
statement2;
. . .]
系统预定义异常
–CASE_NOT_FOUND (ORA-06592)
–NO_DATA_FOUND (ORA-1403)
–TOO_MANY_ROWS (ORA-1422)
–DUP_VAL_ON_INDEX (ORA-0001)
–ZERO_DIVIDE (ORA-1476)
–INVALID_CURSOR (ORA-1001)
–VALUE_ERROR (ORA-6502)
3.3 实验2——PLSQL程序设计基础
一、实验题目
PLSQL程序设计基础
二、实验目的
熟悉PL/SQL基本语法,掌握游标、异常、控制语句的使用。
三、实验内容
1、编写“显示指定教师信息”的匿名块。
2、编写“添加教师信息”的匿名块。
个人答案
--任务(1)显示指定教师信息”的匿名块
/*
set serveroutput on;
DECLARE
--定义获取教师编号的变量
v_teacherID teacher.teacher_id%TYPE:='&要查询的教师编号' ;
--定义输入教师编号为null时抛出的用户自定义异常变量
ex_null EXCEPTION; --系统非预定义异常的定义和关联
--定义根据输入教师编号查询教师详细信息列表的游标;
CURSOR teacher_AllEmp IS
SELECT * FROM teacher WHERE teacher_id= v_teacherID;
emp_rec teacher%ROWTYPE;
BEGIN
--如果教师编号为null,抛出用户自定义异常
IF v_teacherID is null THEN
RAISE ex_null; --用户自定义异常的触发
ELSE
OPEN teacher_AllEmp;
--循环遍历游标,输出教师详细信息
FETCH teacher_AllEmp INTO emp_rec;
dbms_output.put_line('id:'||emp_rec.Teacher_ID || ' 教师名:'||emp_rec.Tname || ' 性别:'||emp_rec.Tsex ||
' Tbirth :'||emp_rec.Tbirth || ' Educ :'||emp_rec.Educ || ' Profess :'||emp_rec.Profess || ' Dept_ID :'||emp_rec.Dept_ID
);
END IF;
EXCEPTION
--处理教师编号为null的用户自定义异常,输出提示信息
WHEN ex_null THEN --系统非预定义异常的捕获和处理
dbms_output.put_line('教师编号不能为空!');
END;
*/
--提供表格1
(-2.48) CzechREM DROP USER jxgl CASCADE;
REM GRANT CONNECT,RESOURCE,CREATE VIEW,UNLIMITED TABLESPACE TO jxgl IDENTIFIED BY jxgl ;
REM CONNECT jxgl/jxgl
CREATE TABLE Sys_User
(UserID Number Primary Key,
UserName Varchar2(20) NOT NULL,
UserType Number(1),
UserPwd Varchar2(40),
Remark Varchar2(200));
/
INSERT INTO Sys_User VALUES('1','Adimin', '1', 'Admin', null);
INSERT INTO Sys_User VALUES('2','Zhang', '2', 'Zhang', null);
CREATE TABLE Dept
(Dept_ID varchar2(3) primary key,
Dept_name Varchar2(30),
Director varchar(12),
Parent_ID varchar2(3) references Dept(Dept_ID)
);
/
INSERT INTO Dept VALUES('000','新世纪科技大学','刘忠明',NULL);
INSERT INTO Dept VALUES('070','经贸学院','李强','000');
INSERT INTO Dept VALUES('080','信息技术学院','胡明新','000');
INSERT INTO Dept VALUES('071','会计系','张志红','070');
INSERT INTO Dept VALUES('072','物流系','刘炎','070');
INSERT INTO Dept VALUES('073','电商系','王军','070');
INSERT INTO Dept VALUES('081','计算机科学系','李祥生','080');
INSERT INTO Dept VALUES('082','信息管理系','刘存勇','080');
INSERT INTO Dept VALUES('083','网络功能系','方刚','080');
CREATE TABLE Class
(Class_ID varchar2(5) primary key,
Class_name Varchar2(45),
Dept_ID varchar2(3) references Dept(Dept_ID)
);
/
INSERT INTO Class VALUES('08101','计算机科学与技术1班','081');
INSERT INTO Class VALUES('08102','计算机科学与技术2班','081');
INSERT INTO Class VALUES('08201','信息管理系1班','082');
INSERT INTO Class VALUES('08202','信息管理系2班','082');
CREATE TABLE Courses
(Course_ID Varchar2(4) primary key,
Cname Varchar2(45),
Credits NUMBER(1) );
/
INSERT INTO Courses VALUES('0001','大学语文',4);
INSERT INTO Courses VALUES('0002','高等数学',4);
INSERT INTO Courses VALUES('0003','计算机基础',4);
INSERT INTO Courses VALUES('0004','数据库概论',4);
INSERT INTO Courses VALUES('0005','数据库原理及应用',4);
INSERT INTO Courses VALUES('0006','计算机组成原理',4);
INSERT INTO Courses VALUES('0007','计算机网络',4);
INSERT INTO Courses VALUES('0008','数据结构',4);
INSERT INTO Courses VALUES('0009','人工智能',2);
INSERT INTO Courses VALUES('0010','Java程序设计',2);
INSERT INTO Courses VALUES('0011','数字逻辑',2);
INSERT INTO Courses VALUES('0012','电子线路',2);
INSERT INTO Courses VALUES('0013','操作系统',4);
INSERT INTO Courses VALUES('0014','编译原理',4);
INSERT INTO Courses VALUES('0015','艺术欣赏',2);
INSERT INTO Courses VALUES('0016','软件工程导论',2);
INSERT INTO Courses VALUES('0017','计算理论',2);
INSERT INTO Courses VALUES('0018','毕业实习',4);
INSERT INTO Courses VALUES('0019','毕业论文',6);
CREATE TABLE Teacher
(Teacher_ID Varchar2(8) primary key,
Tname Varchar2(12),
Tsex Varchar2(3),
Tbirth date,
Educ Varchar2(12),
Profess Varchar2(20),
Dept_ID Varchar2(3) references Dept (Dept_ID)
);
/
INSERT INTO Teacher VALUES('10459001','王鹏飞','男',to_date('1980-12-01','YYYY-MM-DD'),'研究生','讲师','081');
INSERT INTO Teacher VALUES('10459002','潘蕾','女',to_date('1964-09-27','YYYY-MM-DD'),'本科','副教授','081');
INSERT INTO Teacher VALUES('10459003','王安逸','男',to_date('1979-05-14','YYYY-MM-DD'),'研究生','副教授','081');
INSERT INTO Teacher VALUES('10459004','黄海冰','男',to_date('1984-08-29','YYYY-MM-DD'),'研究生','助教','081');
INSERT INTO Teacher VALUES('10459005','于晓龙','男',to_date('1981-03-24','YYYY-MM-DD'),'博士','副教授','082');
INSERT INTO Teacher VALUES('10459006','孙贝妮','女',to_date('1955-12-23','YYYY-MM-DD'),'本科','教授','082');
INSERT INTO Teacher VALUES('10459007','张健夫','男',to_date('1982-11-16','YYYY-MM-DD'),'研究生','讲师','082');
INSERT INTO Teacher VALUES('10459008','万志伟','男',to_date('1980-12-01','YYYY-MM-DD'),'本科','讲师','082');
INSERT INTO Teacher VALUES('10459090','刘宇新','男',to_date('1990-07-21','YYYY-MM-DD'),'研究生','讲师','071');
INSERT INTO Teacher VALUES('10459091','邓海航','男',to_date('1989-12-23','YYYY-MM-DD'),'研究生','讲师','071');
INSERT INTO Teacher VALUES('10459092','周进','男',to_date('1990-4-13','YYYY-MM-DD'),'研究生','讲师','071');
CREATE TABLE TC
(Teacher_ID Varchar2(8),
Course_ID Varchar2(4),
Semester NUMBER,
Dept_ID Varchar2(3),
CONSTRAINT pk_tc primary key(Teacher_ID,Course_ID,Dept_ID),
CONSTRAINT fk_tc_tid foreign key(Teacher_ID) references Teacher (Teacher_ID),
CONSTRAINT fk_tc_cid foreign key(Course_ID) references Courses (Course_ID),
CONSTRAINT fk_tc_did foreign key(Dept_ID) references Dept (Dept_ID)
);
/
INSERT INTO TC VALUES('10459001','0001',1,'081');
INSERT INTO TC VALUES('10459002','0003',1,'081');
INSERT INTO TC VALUES('10459003','0002',1,'081');
INSERT INTO TC VALUES('10459004','0011',1,'081');
INSERT INTO TC VALUES('10459005','0012',1,'082');
INSERT INTO TC VALUES('10459001','0006',2,'081');
INSERT INTO TC VALUES('10459007','0008',2,'082');
INSERT INTO TC VALUES('10459008','0004',2,'082');
INSERT INTO TC VALUES('10459003','0010',2,'081');
INSERT INTO TC VALUES('10459006','0005',3,'082');
INSERT INTO TC VALUES('10459005','0007',3,'082');
INSERT INTO TC VALUES('10459001','0009',3,'081');
INSERT INTO TC VALUES('10459003','0013',3,'081');
INSERT INTO TC VALUES('10459002','0014',3,'081');
INSERT INTO TC VALUES('10459005','0015',4,'082');
INSERT INTO TC VALUES('10459001','0016',4,'081');
INSERT INTO TC VALUES('10459006','0017',4,'082');
INSERT INTO TC VALUES('10459001','0018',8,'081');
INSERT INTO TC VALUES('10459001','0019',8,'081');
INSERT INTO TC VALUES('10459002','0018',8,'081');
INSERT INTO TC VALUES('10459002','0019',8,'081');
INSERT INTO TC VALUES('10459003','0018',8,'081');
INSERT INTO TC VALUES('10459003','0019',8,'081');
INSERT INTO TC VALUES('10459004','0018',8,'081');
INSERT INTO TC VALUES('10459004','0019',8,'081');
INSERT INTO TC VALUES('10459005','0018',8,'082');
INSERT INTO TC VALUES('10459005','0019',8,'082');
INSERT INTO TC VALUES('10459006','0018',8,'082');
INSERT INTO TC VALUES('10459006','0019',8,'082');
INSERT INTO TC VALUES('10459007','0018',8,'082');
INSERT INTO TC VALUES('10459007','0019',8,'082');
INSERT INTO TC VALUES('10459008','0018',8,'082');
INSERT INTO TC VALUES('10459008','0019',8,'082');
commit;
(-2.68) Dutch
--提供表格2
CREATE TABLE Students
(Student_ID Varchar2(11) primary key,
Sname Varchar2(12),
Ssex Varchar2(3),
Sbirth date,
Nationality Varchar2(40),
Dept_ID Varchar2(3) references Dept(Dept_ID ),
Class_ID Varchar2(5) references Class(Class_ID )
);
/
CREATE TABLE SC
(
Student_ID Varchar2(11) ,
Course_ID Varchar2(4),
Grade NUMBER,
CONSTRAINT pk_sc PRIMARY KEY(Student_ID,Course_ID),
CONSTRAINT fk_sc_sid FOREIGN KEY (Student_ID) REFERENCES Students(Student_ID),
CONSTRAINT fk_sc_cid FOREIGN KEY (Course_ID) REFERENCES Courses(Course_ID)
);
/
INSERT INTO Students VALUES('20110810101','王传璐','男',to_date('1990-04-24','YYYY-MM-DD'),'回族','081','08101');
INSERT INTO Students VALUES('20110810102','腾飞','男',to_date('1988-12-1','YYYY-MM-DD'),'汉族','081','08101');
INSERT INTO Students VALUES('20110810103','张继发','男',to_date('1989-08-09','YYYY-MM-DD'),'汉族','081','08101');
INSERT INTO Students VALUES('20110810104','赵济生','男',to_date('1990-04-05','YYYY-MM-DD'),'汉族','081','08101');
INSERT INTO Students VALUES('20110810201','程婷','女',to_date('1990-05-02','YYYY-MM-DD'),'汉族','081','08102');
INSERT INTO Students VALUES('20110810202','王康伟','男',to_date('1990-07-09','YYYY-MM-DD'),'汉族','081','08102');
INSERT INTO Students VALUES('20110810203','孙世国','男',to_date('1992-10-06','YYYY-MM-DD'),'汉族','081','08102');
INSERT INTO Students VALUES('20110810204','李俊男','女',to_date('1990-05-02','YYYY-MM-DD'),'汉族','081','08102');
INSERT INTO Students VALUES('20110810205','李明刚','男',to_date('1989-11-19','YYYY-MM-DD'),'汉族','081','08102');
INSERT INTO Students VALUES('20110810206','白哲峰','女',to_date('1992-10-17','YYYY-MM-DD'),'汉族','081','08102');
INSERT INTO Students VALUES('20110810207','张贤君','男',to_date('1987-11-10','YYYY-MM-DD'),'汉族','081','08102');
INSERT INTO Students VALUES('20110810208','刘玉鹏','男',to_date('1988-09-21','YYYY-MM-DD'),'汉族','081','08102');
INSERT INTO Students VALUES('20110810209','彭治华','男',to_date('1990-01-26','YYYY-MM-DD'),'土家族','081','08102');
INSERT INTO Students VALUES('20110810210','时铭','女',to_date('1989-04-24','YYYY-MM-DD'),'回族','081','08102');
INSERT INTO Students VALUES('20110820120','古雪松涛','男',to_date('1990-10-05','YYYY-MM-DD'),'汉','082','08201');
INSERT INTO Students VALUES('20110820121','赵代晓','女',to_date('1990-02-19','YYYY-MM-DD'),'蒙古族','082','08201');
INSERT INTO Students VALUES('20110820122','程玉鹏','男',to_date('1991-03-13','YYYY-MM-DD'),'汉族','082','08201');
INSERT INTO Students VALUES('20110820123','丁鹏举','男',to_date('1990-08-27','YYYY-MM-DD'),'汉族','082','08201');
INSERT INTO Students VALUES('20110820124','张浩','男',to_date('1992-10-09','YYYY-MM-DD'),'汉族','082','08201');
INSERT INTO Students VALUES('20110820125','李楠','女',to_date('1992-11-28','YYYY-MM-DD'),'汉族','082','08201');
INSERT INTO Students VALUES('20110820126','张松涛','男',to_date('1990-07-26','YYYY-MM-DD'),'汉族','082','08201');
INSERT INTO Students VALUES('20110820127','吴博','女',to_date('1991-05-26','YYYY-MM-DD'),'汉族','082','08201');
INSERT INTO Students VALUES('20110820128','郭丁','男',to_date('1990-02-20','YYYY-MM-DD'),'汉族','082','08201');
INSERT INTO Students VALUES('20110820201','殷晓明','女',to_date('1992-02-16','YYYY-MM-DD'),'达斡尔族','082','08202');
INSERT INTO Students VALUES('20110820202','康旭凯','男',to_date('1989-06-27','YYYY-MM-DD'),'汉族','082','08202');
INSERT INTO Students VALUES('20110820203','王文杰','男',to_date('1990-12-24','YYYY-MM-DD'),'汉族','082','08202');
INSERT INTO Students VALUES('20110820204','朴哲浩','男',to_date('1990-05-28','YYYY-MM-DD'),'朝鲜族','082','08202');
INSERT INTO Students VALUES('20110820205','罗运成','男',to_date('1991-03-09','YYYY-MM-DD'),'汉族','082','08202');
INSERT INTO Students VALUES('20110820206','陈兴涛','男',to_date('1990-11-16','YYYY-MM-DD'),'汉族','082','08202');
INSERT INTO Students VALUES('20110820207','耿春意','男',to_date('1991-08-15','YYYY-MM-DD'),'汉族','082','08202');
INSERT INTO Students VALUES('20110820208','卢剑','男',to_date('1990-09-19','YYYY-MM-DD'),'汉族','082','08202');
INSERT INTO Students VALUES('20110820209','冯胜翔','男',to_date('1992-10-16','YYYY-MM-DD'),'汉族','082','08202');
INSERT INTO Students VALUES('20110820210','王鹏飞','男',to_date('1990-07-15','YYYY-MM-DD'),'汉族','082','08202');
INSERT INTO Students VALUES('20110820129','赵红理','女',to_date('1990-05-02','YYYY-MM-DD'),'汉族','082','08201');
INSERT INTO Students VALUES('20110810105','李振辉','男',to_date('1990-07-09','YYYY-MM-DD'),'汉族','081','08101');
INSERT INTO Students VALUES('20110810106','李明','女',to_date('1987-10-28','YYYY-MM-DD'),'汉族','081','08101');
INSERT INTO Students VALUES('20110810107','陈鑫','男',to_date('1990-12-11','YYYY-MM-DD'),'汉族','081','08101');
INSERT INTO Students VALUES('20110810108','陈艳敏','女',to_date('1991-09-21','YYYY-MM-DD'),'汉族','081','08101');
INSERT INTO Students VALUES('20110810109','董辉','男',to_date('1990-09-26','YYYY-MM-DD'),'满族','081','08101');
INSERT INTO Students VALUES('20110810110','董兵','男',to_date('1989-04-09','YYYY-MM-DD'),'回族','081','08101');
INSERT INTO SC VALUES('20100810101','0001',86);
INSERT INTO SC VALUES('20100810101','0002',78);
INSERT INTO SC VALUES('20100810101','0003',90);
INSERT INTO SC VALUES('20100810101','0004',86);
INSERT INTO SC VALUES('20100810101','0005',69);
INSERT INTO SC VALUES('20100810205','0001',70);
INSERT INTO SC VALUES('20100810205','0002',54);
INSERT INTO SC VALUES('20100810205','0003',97);
INSERT INTO SC VALUES('20100810205','0004',64);
INSERT INTO SC VALUES('20100810205','0005',75);
INSERT INTO SC VALUES('20110810101','0001',76);
INSERT INTO SC VALUES('20110810101','0002',84);
INSERT INTO SC VALUES('20110810101','0003',59);
INSERT INTO SC VALUES('20110810101','0004',72);
INSERT INTO SC VALUES('20110810101','0005',79);
INSERT INTO SC VALUES('20110810129','0001',71);
INSERT INTO SC VALUES('20110810129','0002',80);
INSERT INTO SC VALUES('20110810129','0003',97);
INSERT INTO SC VALUES('20110810129','0004',66);
INSERT INTO SC VALUES('20110810129','0005',83);
INSERT INTO SC VALUES('20110820120','0011',71);
INSERT INTO SC VALUES('20110820120','0012',80);
INSERT INTO SC VALUES('20110820120','0003',97);
INSERT INTO SC VALUES('20110820120','0004',66);
INSERT INTO SC VALUES('20110820120','0005',83);
INSERT INTO SC VALUES('20110820121','0011',71);
INSERT INTO SC VALUES('20110820121','0012',66);
INSERT INTO SC VALUES('20110820121','0003',90);
INSERT INTO SC VALUES('20110820121','0004',89);
INSERT INTO SC VALUES('20110820121','0005',75);
INSERT INTO SC VALUES('20110810102','0001',71);
INSERT INTO SC VALUES('20110810102','0002',83);
INSERT INTO SC VALUES('20110810102','0003',90);
INSERT INTO SC VALUES('20110810102','0004',68);
INSERT INTO SC VALUES('20110810102','0005',83);
INSERT INTO SC VALUES('20110810103','0001',71);
INSERT INTO SC VALUES('20110810103','0002',80);
INSERT INTO SC VALUES('20110810103','0003',76);
INSERT INTO SC VALUES('20110810103','0004',80);
INSERT INTO SC VALUES('20110810103','0005',83);
INSERT INTO SC VALUES('20110810103','0015',78);
INSERT INTO SC VALUES('20110810201','0001',69);
INSERT INTO SC VALUES('20110810201','0002',78);
INSERT INTO SC VALUES('20110810201','0003',54);
INSERT INTO SC VALUES('20110810201','0004',89);
INSERT INTO SC VALUES('20110810201','0005',80);
INSERT INTO SC VALUES('20110810201','0015',70);
INSERT INTO SC VALUES('20110810202','0001',66);
INSERT INTO SC VALUES('20110810202','0002',84);
INSERT INTO SC VALUES('20110810202','0003',78);
INSERT INTO SC VALUES('20110810202','0004',83);
INSERT INTO SC VALUES('20110810202','0005',67);
INSERT INTO SC VALUES('20110810202','0015',70);
INSERT INTO SC VALUES('20110810203','0001',81);
INSERT INTO SC VALUES('20110810203','0002',90);
INSERT INTO SC VALUES('20110810203','0003',66);
INSERT INTO SC VALUES('20110810203','0004',60);
INSERT INTO SC VALUES('20110810203','0005',93);
INSERT INTO SC VALUES('20110810203','0015',72);
INSERT INTO SC VALUES('20110810204','0001',76);
INSERT INTO SC VALUES('20110810204','0002',68);
INSERT INTO SC VALUES('20110810204','0003',90);
INSERT INTO SC VALUES('20110810204','0004',88);
INSERT INTO SC VALUES('20110810204','0005',80);
INSERT INTO SC VALUES('20110810104','0015',90);
commit;