Oracle PL/SQL 上课学习笔记(一)

一、 热备份  //知识回顾

思考:热备份需要备份哪些文件?
只有数据文件是必须备份的。其他的可以靠文件冗余来保护。
一般备份的数据文件对应的表空间:
1、 system //系统表空间
    2、 user //用户表空间

            3、 自己建的数据文件对应的表空间 。//最重要


二、创建表空间:

<strong><span style="font-size:18px;">create tablespace backup_data
datafile 'f:\disk1\backup_data.dbf'
size 100M
extent management local
uniform size 1M</span></strong>
<strong><span style="font-size:18px;">create tablespace backup_index
datafile 'f:\disk2\backup_index.dbf'
size 100M
extent management local
uniform size 1M</span></strong>


查询自己的表空间:

<strong>  SQL><span style="font-size:18px;">select file_name,tablespace_name 
     fromdba_data_files ;</span></strong>



2015年3月13日星期五



:执行区必须有:


范例:第一个pl/sql编程:屏幕输出helloworld
<strong><span style="font-size:24px;">Begin 
Dbms_ouput.put_line(‘HelloWorld’) ;
End ;</span></strong>


但是当我们运行SQL语句后,我们发现helloworld并没有出现,为什么呢?那是因为没有‘开启屏幕输出显示‘,应使用set serveroutput on 命令打开屏幕输出显示。接下来加上命令set serveroutput on ;
<span style="font-size:24px;">Set serveroutput on ;
Begin
Dbms_output.put_line(‘HelloWorld’) ;
End ;</span><pre name="code" class="sql">create or replace procedure ShowHelloWorld
as 
begin

 加上set serveroutput on 命令后,屏幕边打印输出了Hello World . 

范例:编写第一个简单的存储过程

<strong><span style="font-size:24px;">SQL>create or replace procedure ShowHelloWorld
   as 
   begin
     dbms_output.put_line('hello world') ;
   end ;
   /</span></strong>


接下来调用它,输出helloworld

<strong><span style="font-size:24px;">SQL>exec Show HelloWorld ;</span></strong>


练习:编写程序输出名字,年龄,性别三个数,并才用两种方式实现:
<strong><span style="font-size:24px;">declare
name varchar2(8);
sex char(2);
age number(3);
begin 
name:='Jack' ;
sex:='男' ;
age:=22 ;
dbms_output.put_line(name||sex||age) ; Jack男22
end ;
/</span></strong>


改进:输出更人性化
<strong><span style="font-size:24px;">declare
name varchar2(8) :='Jack' ;
sex char(2):='男';
age number(3):=22;
begin
/* name:='Jack' ;
sex:='男' ;
age:=22 ;*/
dbms_output.put_line(name||' sex is '||sex||' ,his age is '||age) ;
end ;</span></strong>


执行输出结果为:Jack sex is 男 ,his age is 22


可以采用另外一种方式:

d<strong><span style="font-size:24px;">eclare
	name varchar2(8) :='Jack' ;
	sex char(2) :='男';
	age number(3) :=22;
begin
		dbms_output.put_line(name||' sex is '||sex||' ,his age is '||age) ;
end ;
/ </span></strong>


输出结果为:Jack sex is 男 ,his age is 22


联系:采用四个部分输出“helloworld”


<strong><span style="font-size:24px;">create or replace procedure ShowHelloWorld
as
begin
declare
mystr varchar2(15) ;
begin
mystr:= 'helloworld' ;
dbms_output.put_line(mystr) ;
exception
when others
then
dbms_output.put_line('this is a error') ;
end;
end;</span></strong>
/


实验:编写程序实现输出“Jack的出生日期年月为1994-10-8日,手机号码为13512345678.”要求:
1. jack的名字,出生日期,手机号用变量存放
2. 只能用一次输出。
3. 加上异常处理部分。


5.
       <strong><span style="font-size:24px;"> create or replace procedure ShowHelloWorld
	as
	begin
	 declare
	 name varchar2(8):='jack';
	 birthday date := date '1994-10-8';
	 tel char(11) :='13512345678' ;
	 begin
	  dbms_output.put_line(name||' his birthday is '||birthday||'tel is '||tel ) ;
	exception
	 when others
	 then
	  dbms_output.put_line('this is a error') ;
	  end ;
	end ;</span></strong>
<strong><span style="font-size:24px;">	/</span></strong>


执行存储过程:
<strong><span style="font-size:24px;">exec ShowHelloWorld ;</span></strong>

执行输出结果为: jack his birthday is 08-10月-94 ,his tel is 13512345678

<strong><span style="font-size:24px;">set serveroutput on ;
declare
scottsal number(7,2) ;
begin
select sal Into scottsal from emp where ename='SCOTT' ;
dbms_output.put_line('scottde sal is:'||scottsal) ;
end ;
/  scottde sal is:3000</span></strong>


实验:改写上面的程序,实现:首先屏幕提示:请输入员工名:
然后用户从键盘输入员工名后,显示“改员工的工资为XXX”,XXx以具体的值代替。

<strong><span style="font-size:24px;">declare
scottsal number(7,2) ;
begin
select sal Into scottsal from emp where ename='&ename' ;
dbms_output.put_line('scottde sal is:'||scottsal) ;
end ;
/</span></strong>


输入 ename 的值: SMITH
scottde sal is:800

实验:
编写程序实现:将部门表(dept)部门号为30的部门名和部门地址输出。和之前的实验相比较,得出什么结论。

<strong><span style="font-size:24px;">declare
ddname varchar2(14) ;
dloc varchar2(13) ;
begin
select dname ,loc Into ddname ,dloc from dept where deptno=30 ;
dbms_output.put_line('部门名是 :'||ddname||',地址为 '||dloc) ;
end ;
/</span></strong>


输出结果为:部门名是 :SALES,地址为 CHICAGO





课后作业
1、 编写程序实现输出“今天是XXXX年XX月XX日(即系统日期),离我们实习可能不到60天”.要求:日期和天数用变量存放, 只能用一次输出。
1.1 知识补充
分别获取系统日期的年月日 使用Extract函数

SELECT '年' AS "标题", EXTRACT(YEAR FROM SYSDATE) AS "数值" FROM dual UNIONALL
SELECT '月' AS "标题", EXTRACT(MONTH FROM SYSDATE) AS "数值" FROM dual UNION ALL
SELECT '日' AS "标题", EXTRACT(DAY FROM SYSDATE) AS "数值" FROM dual




标 数值
-- -------------------
年 2015
月 3
日 15
方法一:使用to_char函数
set serveroutput on ;
declare
year char(4) := to_char(sysdate,'YYYY') ;
month char(2):= to_char(sysdate,'MM') ;
day char(2):= to_char(sysdate,'dd') ;
begin 
dbms_output.put_line('今天是'||year||'年'||month||'月'||day||'日,离我们实习可能不到60天') ;
end ;


方法二:使用Extract函数
declare
year char(4) :=EXTRACT(YEAR FROM SYSDATE) ;
month char(2):=EXTRACT(MONTH FROM SYSDATE) ;
day char(2):= EXTRACT(DAY FROM SYSDATE) ;
begin 
dbms_output.put_line('今天是'||year||'年'||month||'月'||day||'日,离我们实习可能不到60天') ;
end ;





改进:加入存储过程
create or replace procedure ShowDate
as 
begin<pre name="code" class="html">Declare
Ggrade number(1) ;
Llosal number(4) ;
Hhisal number(4) ;
Begin
Select grade,losal,hisal into ggrade,llosal,hhisal from salgrade where grade = &工资等级 ;
dbms_output.put_line('工资等级为'||ggrade||'的最低工资是:'|| llosal ||'最高工资是:'||hhisal) ;
end ;

declare year char(4) :=EXTRACT(YEAR FROM SYSDATE) ; month char(2):=EXTRACT(MONTH FROM SYSDATE) ; day char(2):= EXTRACT(DAY FROM SYSDATE) ; begin dbms_output.put_line('今天是'||year||'年'||month||'月'||day||'日,离我们实习可能不到60天') ; exception when others then dbms_output.put_line('This is a error') ; end ;end ;
 2、编写程序实现将工资等级表salgrade的1等工资输出。输出结果应该为:x等工资的最高工资是xxxx,最低工资是xxxx 
Declare
Ggrade number(1) ;
Llosal number(4) ;
Hhisal number(4) ;
Begin
Select grade,losal,hisal into ggrade,llosal,hhisal from salgrade where grade = &工资等级 ;
dbms_output.put_line('工资等级为'||ggrade||'的最低工资是:'|| llosal ||'最高工资是:'||hhisal) ;
end ;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值