oracle 笔记

scott/tiger

system/manager

--创建一个临时表空间

create temporary tablespace bbb_temp

tempfile 'e:/bbb_temp.dbf'

size 10m

autoextend on;

--创建一个用户默认的表空间

create tablespace bbb_data

logging

datafile 'e:/bbb_data.dbf'

size 10m

autoextend on;

--创建一个用户并指定对应的表空间

create user bbb

identified by bbb

default tablespace bbb_data

temporary tablespace bbb_temp;

--授权

grant 

create sessioncreate any tablecreate any view ,create 

any indexcreate any procedure,

alter any tablealter any procedure,

drop any tabledrop any viewdrop any indexdrop any 

procedure,

select any tableinsert any tableupdate any table

delete any table

to bbb;

grant connect,resource to bbb;

--查询一个用户下面有哪些表

select * from user_tables;

--字符类型

char(10)固定长度的10个字符

varchar2(10)可变长度的10个字符

long长文本

--数字类型

number(4,2)

--日期类型

date,sysdate获取当前日期

--查询系统的当前时间,包含dual单行单列的表

select sysdate from dual;

17-11月-09

rowid

 select rowid,ename from emp;

 rownum

可以使用rownum来进行分页查询

select * from (select a.*, rownum rn from 

(select * from emp) a where rownum <= 10) 

where rn >= 5;

--建表建约束

create table student

(

stuid number(4) constraint pk_stu_id primary key not null,

stuname varchar2(10) not null,

stuage number(2) constraint ck_stu_age check(stuage between 16 and 

45),

stuemail varchar2(20),

stuaddress varchar2(50)

);

--查看表结构

desc student;

--给stuemail添加检查约束

alter table student

add constraint ck_stu_email check(stuemail like '%@%');

--给stuaddress添加默认值

alter table student modify(stuaddress default '荆州');

--创建课程表

create table course

(

courseid number(2) not null,

coursename varchar2(10) not null

);

--给course表加主键

alter table course add constraint pk_course_id primary key(courseid);

--创建成绩表

create table score

(

scoreid number(4) constraint pk_score_id primary key not null,

stuid number(4) not null,

courseid number(2) not null,

score number(4,2) not null constraint ck_score_score check(score between 0 and 100),

constraint fk_student_score foreign key(stuid) references student(stuid),

constraint fk_course_score foreign key(courseid) references course(courseid)

);

--创建三个序列

create sequence student_seq start with 1001 increment by 1;

create sequence course_seq start with 1 increment by 1;

create sequence score_seq start with 1 increment by 1;

--使用序列增加测试数据

insert into student values(student_seq.nextval,'刘德华',30,'liu@163.com','沙市');

insert into student values(student_seq.nextval,'张学友',28,'zhang@163.com',null);

insert into student values(student_seq.nextval,'范冰冰',18,'fan@163.com','公安');

insert into student values(student_seq.nextval,'张国荣',31,'guorong@163.com','荆州');

insert into course values(course_seq.nextval,'java');

insert into course values(course_seq.nextval,'sql');

insert into course values(course_seq.nextval,'c#');

commit;

insert into score values(score_seq.nextval,1001,1,80);

insert into score values(score_seq.nextval,1001,2,54);

insert into score values(score_seq.nextval,1001,3,90);

insert into score values(score_seq.nextval,1002,1,34);

insert into score values(score_seq.nextval,1002,2,56);

insert into score values(score_seq.nextval,1002,3,78);

insert into score values(score_seq.nextval,1003,1,32);

insert into score values(score_seq.nextval,1003,2,78);

insert into score values(score_seq.nextval,1003,3,99);

commit;

--查询java课程的总成绩

select avg(score) java平均成绩 from score where courseid=1;

--查询每个学员的三门课的平均成绩

select stuid 学员编号,avg(score) 平均成绩 from score group by stuid;

--按总分从高到低排列

select stuid 学号,sum(score) 总分 from score group by stuid order by 总分 desc;

--查询每个学员的总成绩

select stuid 学号,sum(score) 总成绩 from score group by stuid;

--查询java成绩的前三名

select score 成绩 from score where courseid=1 and rownum<=3;

--查询地址为空的学员

select * from student where stuaddress is null;

--查询没有参加c#(3)考试的学员id

select stuid from student where stuid not in(select stuid from score where courseid=3);

--查询学员的sql成绩并显示学号、sql成绩、以及学员姓名

select stu.stuid 学号,sco.score 成绩,stu.stuname 姓名

from student stu inner join score sco

on(stu.stuid=sco.stuid);

--查询哪些学生没有参加考试

select stu.stuid 学号,sco.score 成绩,stu.stuname 姓名

from student stu left join score sco

on(stu.stuid=sco.stuid) where sco.score is null;

--等价于

select stu.stuid 学号,sco.score 成绩,stu.stuname 姓名

from student stu,score sco where stu.stuid=sco.stuid(+) and sco.score is null;

(+)在左边是右连接

--查询显示学员的java成绩,如果成绩<60分      显示’不及格’

   60<=成绩<80    显示’三等’

   80<=成绩<90    显示’二等’

   90<=成绩       显示’一等’

set serveroutput on;

declare

num number(4);

begin

num:=&请输入一个数;

case(num)

when 0 then dbms_output.put_line('不及格');

when 1  then dbms_output.put_line('三等');

when 2 then dbms_output.put_line('二等');

when 3  then dbms_output.put_line('一等');

else  dbms_output.put_line('成绩不正确');

end case;

end;

/

set serveroutput on;

declare

javascore score.score%type;

begin

--假设返回单条记录就可以接收,多条记录就报异常

select score into javascore from score where courseid=100;

if javascore<60 then 

dbms_output.put_line('不及格');

elsif javascore between 60 and 79  then

dbms_output.put_line('三等');

elsif javascore between 80 and 89 then

dbms_output.put_line('二等');

elsif javascore between 90 and 100 then

dbms_output.put_line('一等');

else

dbms_output.put_line('错误');

end if;

exception

when too_many_rows then

dbms_output.put_line('返回行数不只一行');

when no_data_found then

dbms_output.put_line('找不到需要的数据');

end;

/

set serveroutput on;

declare

javascore score.score%type;

--1、定义游标

cursor score_cur is select score from score where courseid=1;

begin

--2、打开游标

open score_cur;

--3、从游标里面取数据

loop

fetch score_cur into javascore;

if javascore<60 then 

dbms_output.put_line('不及格');

elsif javascore between 60 and 79  then

dbms_output.put_line('三等');

elsif javascore between 80 and 89 then

dbms_output.put_line('二等');

elsif javascore between 90 and 100 then

dbms_output.put_line('一等');

else

dbms_output.put_line('错误');

end if;

--游标结束

exit when score_cur%notfound;

end loop;

--4、关闭游标

close score_cur;

exception

when too_many_rows then

dbms_output.put_line('返回行数不只一行');

when no_data_found then

dbms_output.put_line('找不到需要的数据');

end;

/

循环

set serveroutput on;

declare 

number(4):=0;

begin

loop

i:=i+1;

dbms_output.put_line('accp');

exit when i>10;

end loop;

end;

/

set serveroutput on;

declare 

number(4):=0;

begin

while i<10 loop

i:=i+1;

dbms_output.put_line('accp');

end loop;

end;

/

set serveroutput on;

declare 

number(4):=0;

begin

for i in 1..10 loop

dbms_output.put_line('accp');

end loop;

end;

/

--编写程序,显示从2到100之间的

set serveroutput on;

declare 

number(4):=1;

number(4):=1;

begin

loop

i:=i+1;

j:=1;

loop

j:=j+1;

exit when mod(i,j)=0;

end loop;

if i=j then

dbms_output.put_line(i);

end if;

exit when i>100;

end loop;

end;

/

--循环加分,java成绩普遍很低,现都给与加分,但是总分不能超过100分,直到所有人都及格,停止加分。

set serveroutput on;

declare 

num number(4):=1;

begin

while num>0 loop

update score set score=100 where score>=95 and courseid=1;

update score set score=score+5 where score<95 and courseid=1;

select count(*) into num from score where score<60 and courseid=1;

end loop;

commit;

end;

/

--使用游标查询并显示学员信息

set serveroutput on;

declare

stu_name student.stuname%type;

stu_age student.stuage%type;

--1、定义游标

cursor score_cur is select stuname,stuage from student;

begin

--2、打开游标

open score_cur;

--3、从游标里面取数据

loop

fetch score_cur into stu_name,stu_age;

dbms_output.put_line('姓名'||stu_name||',年龄'||stu_age);

--游标结束

exit when score_cur%notfound;

end loop;

--4、关闭游标

close score_cur;

exception

when too_many_rows then

dbms_output.put_line('返回行数不只一行');

when no_data_found then

dbms_output.put_line('找不到需要的数据');

end;

/

set serveroutput on;

declare

stu_row student%rowtype;

--1、定义游标

cursor score_cur is select * from student;

begin

--2、打开游标

open score_cur;

--3、从游标里面取数据

loop

fetch score_cur into stu_row;

dbms_output.put_line('姓名'||stu_row.stuname||',年龄'||stu_row.stuage);

--游标结束

exit when score_cur%notfound;

end loop;

--4、关闭游标

close score_cur;

end;

/

--使用参数游标传递学员学号,查询与之相关的信息

set serveroutput on;

declare

stu_row student%rowtype;

id student.stuid%type;

--1、定义游标

cursor score_cur(stu_id student.stuid%typeis select * from student where stuid>stu_id;

begin

--2、打开游标

id:=&输入学号;

open score_cur(id);

--3、从游标里面取数据

loop

fetch score_cur into stu_row;

dbms_output.put_line('姓名'||stu_row.stuname||',年龄'||stu_row.stuage);

--游标结束

exit when score_cur%notfound;

end loop;

--4、关闭游标

close score_cur;

end;

/

--编写过程,接收学员的学号,求出此学员的三门课的平均成绩打印输出。

create or replace procedure

findstudent 

(

stu_id number

)

as

stu_name student.stuname%type;

begin

select stuname into stu_name from student where stuid=stu_id;

dbms_output.put_line(stu_name);

end;

/

create or replace procedure findscore

(

stu_id number

)

as

cursor avg_score is select avg(score) from score where stuid=stu_id group by courseid;

score_avg score.score%type;

begin

open avg_score;

loop

fetch avg_score into score_avg;

dbms_output.put_line(score_avg);

exit when avg_score%notfound;

end loop;

close avg_score;

end;

/

--编写过程,带输入参数学号和课程编号,输出参数为这个学生这门课的成绩,如果缺考就为0

create or replace procedure findscore

(

stu_id in number,

course_id in number,

outscore out number

)

as

score1 number(4);

begin

select score into score1 from score where stuid=stu_id and courseid=course_id;

outscore:=score1;

exception

when no_data_found then

outscore:=0;

end;

/

调用

set serveroutput on;

declare

  stu_id score.stuid%type;

course_id score.courseid%type;

outscore score.score%type:=0;

begin

stu_id:=&输入学号;

course_id:=&输入课程编号;

findscore(stu_id,course_id,outscore);

dbms_output.put_line(outscore);

end;

/

--编写函数,接收课程的编号,求出这门课的总成绩并返回,调用此函数。

create or replace function 

findtotalscore(course_id number)

return number

as

stotal number(4);

begin

select sum(score) into stotal from score where courseid=course_id;

return stotal; 

end;

/

 select findtotalscore(1) from dual;

set serveroutput on;

declare

totalscore number(4);

begin

totalscore:=findtotalscore(&请输入课程号);

dbms_output.put_line(totalscore);

end;

/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
孟老师 oracle笔记中记录了他在学习和应用Oracle数据库管理系统过程中的重要知识点和经验总结。这些笔记对他个人来说是非常宝贵的学习工具,并且也可以分享给其他学习者。 首先,孟老师的笔记内容包括了Oracle数据库的概述和架构。他对Oracle数据库系统的组成部分、存储结构、进程和线程等进行了详细的介绍。这使得他自己能够更好地理解Oracle数据库的运行原理,从而能够更好地进行数据库管理和优化。 其次,他的笔记中也包含了Oracle数据库的安装和配置过程。他记录了安装过程中可能遇到的问题和解决方法,还有如何正确配置数据库参数以满足具体的需求。这部分内容非常实用,它可以帮助他在以后的实际应用中更加高效地进行数据库的部署和配置。 此外,孟老师还记录了Oracle数据库的备份和恢复策略。他介绍了不同的备份方法,如完全备份、增量备份和重写备份,并指出了每种备份方法的适用场景。在笔记中,他还总结了一些关键的恢复指令和技巧,以便在意外故障发生时能够及时应对。 最后,他还在笔记中分享了一些常见问题和实践经验。这些问题和经验既包括技术层面的,如如何提高数据库性能和调优查询语句,也包括管理层面的,如如何规划数据库用户和权限管理。这些实践经验对于其他Oracle学习者来说是非常宝贵的参考资料。 总体来说,孟老师的Oracle笔记是一个非常有用的学习资源。它不仅记录了他个人的学习过程和经验总结,也为其他学习者提供了重要的参考和指导,帮助他们更好地理解和应用Oracle数据库管理系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值