-- 一章节
1.日期函数
add_months()
extract()
round()
trunc()
last_day()
next_day()
2.转换函数
to_date()
to_char()
to_number()
3.锁
行级锁:
1.自动上锁
insert
update
delete
select...for update
表级锁:
1.手动锁
lock table emp in share mode;
1.同义词
grant create synonym to aniu;
create synonym emp for scott.emp;
create synonym e for emp;
grant create public synonym to aniu;
create public synonym pe for scott.emp;
--查看同义词
select * from user_synonyms;--数据字典
2.序列
create sequence seq
start with 1
increment by 2
maxvalue 30
minvalue -5
nocycle --不循环产生序列号
cache 5;
--修改
alter sequence seq cycle;
--序列位列
1.nextval:序列下一个值
select seq.nextval from dual;
2.currval:序列当前值
select seq.currval from dual;
create table test(i number(6));
insert into test values(seq.nextval);
--查看序列
desc user_sequences;
select SEQUENCE_NAME from user_sequences;
3.视图
--普通视图
grant create any view to scott;--授权
create or replace view v1
as
select empno,ename from emp;
--带检查约束的视图:不能通过视图修改条件数据
create or replace view v2
as
select empno,ename,sal from emp where sal>3000
with check option;
--只读视图
create or replace view v3
as
select empno,ename,sal from emp where sal<3000
with read only;
--错误视图
create or replace force view v4
as
select empno,ename,sal from emp2 where sal<3000
with read only;
--给列取别名的试图
create or replace view v5
as
select empno 编号,ename,sal from emp where sal<3000
with read only;
create or replace view v5
(编号,姓名,工资)
as
select empno,ename,sal from emp where sal<3000
with read only;
create or replace view v6
as
select empno,ename,dname from emp,dept
where emp.deptno(+)=dept.deptno;
-- 有+的是从表
4.索引
1.标准:
create index ind_bz on student(stuno);
2.唯一:不能有重复值
create unique index ind_un on student(name);
3.组合:基于多列查询时
create index ind_zh on student(name,address);
10001 10001
4.反向键:适合连续值的列
create index ind_fx on student(stuno) reverse;
5.位图:适合于低基数列
create bitmap index ind_bm on student(address);
6.函数索引:字符型数据列
create index ind_hs on student(lower(name));
--二章节
1.创建同义词的语法
create synonym sy_name for scott.dept;
2.创建序列的语法
create sequence seq_name
start with 1
increment by 2
maxvalue 1000000
minvalue -100
cycle
cache 10;
seq_name.nextval
seq_name.currval
3.创建视图的语法
create force view v8
as
select * from emp
with read only;
4.索引的种类及语法
1.标准
2.唯一
3.组合
4.函数
5.反向键
6.位图
1.PL/SQL内容
1.DML
2.TCL
3.游标控制
4.SQL函数和运算符
2.支持的数据类型
1.所有的SQL类型
2.支持NULL
3.Boolean
4.属性类型:%type %rowtype
3.组成部分
1.声明部分[可选的]
2.可执行部分
3.异常处理部分[可选的]
DECLARE --声明
...
BEGIN --可执行
....
EXCEPTION --异常
....
END;
4.变量和常量
1. v_empno number(4);
2. v_ename varchar2(10):='scott';
3. v_pi constant number default 3.14;
5.为变量赋值
1.v_empno:=7788;
2.select ename into v_ename from emp where empno=7788;
6.条件控制
1.IF ... THEN ... END IF;
2.IF ... THEN ... ELSE ... END IF;
3.IF ... THEN ... ELSIF ... THEN ... END IF;
7.循环语句
1.LOOP ... END LOOP; --无条件循环
2.WHILE 条件 LOOP ... END LOOP;
3.FOR i IN 1..10 LOOP ... END LOOP;
--pl/sql测试
set serveroutput on --打开输出
begin
dbms_output.put_line('世界上最简单的PL/SQL');
end;
--变量和常量
declare
v_empno number(4);
v_ename varchar2(10);
begin
v_empno:=7788;
select ename into v_ename from emp where empno=v_empno;
dbms_output.put_line('姓名:'||v_ename);
end;
/
--属性类型
declare
v_empno number(4);
v_ename emp.ename%type;
begin
v_empno:=7788;
select ename into v_ename from emp where empno=v_empno;
dbms_output.put_line('姓名:'||v_ename);
end;
/
declare
v_empno number(4);
v_emp emp%rowtype;
begin
v_empno:=7788;
select ename,sal into v_emp.ename,v_emp.sal from emp where empno=v_empno;
dbms_output.put_line('姓名:'||v_emp.ename||',工资'||v_emp.sal);
end;
/
--IF语句
declare
v_sex varchar2(2);
begin
v_sex := '&sex'; --从屏幕接受输入
if v_sex='男' then
dbms_output.put_line('先生...');
elsif v_sex='女' then
dbms_output.put_line('女士...');
else
dbms_output.put_line('人妖...');
end if;
end;
--CASE语句
begin
case '&性别'
when '男' then dbms_output.put_line('先生...');
when '女' then dbms_output.put_line('女士....');
else dbms_output.put_line('人妖...');
end case;
end;
declare
v_sex varchar2(2);
begin
v_sex := '&sex'; --从屏幕接受输入
case
when v_sex='男' then dbms_output.put_line('先生...');
when v_sex='女' then dbms_output.put_line('女士....');
else dbms_output.put_line('人妖...');
end case;
end;
--LOOP、
declare
v_num number:=1;
begin
loop
/*if (v_num>5) then
exit;
end if;
*/
exit when v_num>5; --退出
dbms_output.put_line('loop: '||v_num);
v_num:=v_num+1;
end loop;
end;
/
--while
declare
v_num number:=1;
begin
while v_num<=5
loop
dbms_output.put_line('loop: '||v_num);
v_num:=v_num+1;
end loop;
end;
--for
begin
for v_num in 1..5 loop
dbms_output.put_line('loop: '||v_num);
end loop;
end;
--动态sql
declare
v_sql varchar2(200);
v_a number:=1;
v_b varchar2(20):='hello';
begin
v_sql:='create table test(a number(2),b varchar2(20))';
--执行
execute immediate v_sql;
v_sql:='insert into test values(:a,:b)';
execute immediate v_sql using v_a,v_b;
end;
--异常
declare
v_name emp.ename%type;
v_i number;
begin
select ename into v_name from emp;
v_i:=5/0;
exception
when TOO_MANY_ROWS then
dbms_output.put_line('肉丝太多....');
when ZERO_DIVIDE then
dbms_output.put_line('除数不能为0');
when others then
dbms_output.put_line('未知异常');
end;
/
--自定义异常
declare
v_sex varchar2(2);
e_sex exception;--自定义异常
begin
v_sex:='&sex';
if v_sex!='男' and v_sex!='女' then
raise e_sex; --抛异常
end if;
exception
when e_sex then
dbms_output.put_line('性别搞错哒》。。。');
end;
--三章节
1.pl/sql支持的语句
dml
tcl
游标
函数和运算符
2.pl/sql的数据类型
所有sql类型
null
boolean
属性类型
%type:
%rowtype
3.为变量赋值
a:=10;
select ename,sal into a,s from emp where ...;
1.游标
1.概念:在pl/sql中,用来查询,并指向集合的指针
2.作用:逐行处理查询结果,以编程的方式访问数据
3.类型:隐式游标、显示游标、ref游标
2.隐式游标
1.在Pl/sql指向DML语句时自动创建
2.名称:SQL
3.属性
1.%FOUND:有影响的行数返回true
2.%NOTFOUNT:没有影响的行数返回true
3.%ROWCOUNT:返回影响的行数
4.%ISOPEN:是否打开
3.显示游标
1.作用:处理返回多行的结果集
2.步骤:
1.声明:CURSOR cur_name IS SELECT ....;
2.打开:OPEN cur_name;
3.提取记录:FETCH cur_name INTO var1,var2...;
4.关闭:CLOSE cur_name;
4.REF游标
1.执行动态指定SQL语句
2.强ref游标:有return,约束了查询的结果集结构
1.%rowtype
2.自定义Record类型
3.弱ref游标:没有return
--隐式游标
begin
delete from emp where deptno=10;
--判段执行的情况
if sql%FOUND then
dbms_output.put_line('删除了'||sql%ROWCOUNT||'行记录');
end if;
if sql%notfound then
dbms_output.put_line('没有删除人...');
end if;
end;
--显示游标
declare
--声明
cursor cur_name is
select empno,ename from emp;
v_no emp.empno%type;
v_name emp.ename%type;
begin
--打开
open cur_name;
loop
--提取数据
fetch cur_name into v_no,v_name;
--退出
exit when cur_name%notfound;
dbms_output.put_line(v_no||' '||v_name);
end loop;
close cur_name; --关闭
end;
--带参数的游标
declare
--声明
cursor cur_name(no number) is
select empno,ename from emp where deptno=no;
v_no emp.empno%type;
v_name emp.ename%type;
begin
--打开
open cur_name(&no);
loop
--提取数据
fetch cur_name into v_no,v_name;
--退出
exit when cur_name%notfound;
dbms_output.put_line(v_no||' '||v_name);
end loop;
close cur_name; --关闭
end;
--修改记录
declare
--声明
cursor cur_name is
select deptno,ename,sal from emp for update;
v_no emp.deptno%type;
v_name emp.ename%type;
v_sal emp.sal%type;
begin
--打开
open cur_name;
loop
--提取数据
fetch cur_name into v_no,v_name,v_sal;
--退出
exit when cur_name%notfound;
if(v_no=10) then
update emp set sal=sal+1000 where current of cur_name;
v_sal:=v_sal+1000;
end if;
dbms_output.put_line(v_no||' '||v_name||' '||v_sal);
end loop;
close cur_name; --关闭
end;
--循环游标
declare
cursor cur_name(no number)
is select ename,deptno from emp where deptno=no;
begin
for rec in cur_name(&no)
loop
dbms_output.put_line(rec.ename||' '||rec.deptno);
end loop;
end;
--弱类型ref游标
declare
TYPE ref_cur_name IS REF CURSOR;--游标类型
cur_name ref_cur_name;--游标变量
v_name emp.ename%type;
v_no emp.deptno%type;
begin
--打开
open cur_name for select ename,deptno from emp;
loop
fetch cur_name into v_name,v_no;
exit when cur_name%notfound;
dbms_output.put_line(v_name||' '||v_no);
end loop;
close cur_name;
end;
--强ref游标
declare
TYPE ref_cur_name IS REF CURSOR return emp%rowtype;--游标类型
cur_name ref_cur_name;--游标变量
v_emp emp%rowtype;
begin
--打开
open cur_name for select * from emp;--ename,deptno
loop
fetch cur_name into v_emp;
exit when cur_name%notfound;
dbms_output.put_line(v_emp.ename||' '||v_emp.deptno);
end loop;
close cur_name;
end;
--自定义Record类型
declare
TYPE rec_type IS RECORD(
name emp.ename%type,
deptno emp.deptno%type
);--类型
v_rev rec_type; --Record类型变量
TYPE ref_cur_type IS REF CURSOR return rec_type;
cur_name ref_cur_type;
begin
open cur_name for select ename,deptno from emp where deptno=&no;
loop
fetch cur_name into v_rev;
exit when cur_name%notfound;
dbms_output.put_line(v_rev.name||' '||v_rev.deptno);
end loop;
end;
/
--四章节
1.什么是游标
2.游标的分类
3.游标的四个属性
4.显示游标的操作步骤
5.如何定义和使用显示游标修改数据
1.过程
2.函数
3.程序包
--最简单的存储过程
create or replace procedure p1
is
begin
dbms_output.put_line('你好,.....');
end;
--调用
exec[ute] p1;
--IN 参数
create or replace procedure p2
(eno number)
as
v_name emp.ename%type;
begin
select ename into v_name from emp where empno=eno;
dbms_output.put_line(v_name);
end;
/
--执行
execute p2(7788); --按位置传参
execute p2(eno=>7788); --按名称传参
call p2(7788);
--pl/sql调用
begin
p2(&no);
end;
--OUT 参数
create or replace procedure p3
(eno IN number,name OUT varchar2)
as
begin
select ename into name from emp where empno=eno;
end;
/
--调用
declare
v_name varchar2(20);
begin
--p3(name=>v_name,eno=>7788);
p3(7788,name=>v_name);
dbms_output.put_line(v_name);
end;
--IN OUT 参数
create or replace procedure p4
(num1 IN OUT number,num2 IN OUT number)
is
num3 number;
begin
num3 := num2;
num2 := num1;
num1 := num3;
end;
/
--调用
declare
v_1 number:=10;
v_2 number:=20;
begin
p4(v_1,v_2);
dbms_output.put_line(v_1||' '||v_2);
end;
--授予执行权限
grant execute on p1 to aniu;
--简单的函数
create or replace function fun1
return varchar2
is
begin
return '你好,朋友....';
end;
--调用
select fun1 from dual;
--pl/sql调用
declare
v_n varchar(20);
begin
dbms_output.put_line(fun1);
v_n := fun1;
dbms_output.put_line(v_n);
end;
--调用
VAR v varchar2(20) --声明全局变量
exec :v:= fun1;
print :v;
--带参数的函数
create or replace function fun2
(eno number)
return varchar2
is
v_name varchar2(20);
begin
select ename into v_name from emp where empno = eno;
return v_name;
end;
--调用
declare
v_name varchar2(20);
begin
v_name:= fun2(&no);
dbms_output.put_Line(v_name);
end;
--授权
grant execute on fun1 to scott;
--程序包规范
create or replace package pack1
is
CURSOR cur_fenye(page number,line number) return emp%rowtype;--声明游标
function fun2(eno number) return varchar2; --声明函数
procedure p1(eno number); --声明过程
procedure proc_fenye(page number,line number);--分页的存储过程
end pack1;
--程序包主题
create or replace package body pack1
is
--实现游标
CURSOR cur_fenye(page number,line number)
return emp%rowtype IS
select EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO from(
select rownum r,e.* from emp e where rownum<=page*line
) a where a.r>(page-1)*line;
--实现函数
function fun2
(eno number)
return varchar2
is
v_name varchar2(20);
begin
select ename into v_name from emp where empno = eno;
return v_name;
end;
--实现过程
procedure p1(eno number)
is
v_name varchar2(20);
begin
select ename into v_name from emp where empno=eno;
dbms_output.put_Line(v_name);
end;
--实现分页的存储过程
procedure proc_fenye(page number,line number)
is
v_emp emp%rowtype;
begin
open cur_fenye(page,line);--打开
loop
fetch cur_fenye into v_emp;
exit when cur_fenye%notfound;
dbms_output.put_line(v_emp.empno||' '||v_emp.ename||' ' ||v_emp.sal);
end loop;
close cur_fenye;
end;
end pack1;
--调用
select pack1.fun2(7788) from dual;
exec pack1.p1(7788);
--调用分页
exec pack1.proc_fenye(4,4);
--格式化列的现实
COLUMN LINE FORMAT 999
COLUMN TEXT FORMAT A70
SELECT line,text FROM USER_SOURCE WHERE NAME=UPPER('P2');
分享到:
相关推荐
MySQL基础语法总结-新手教程 内容包括了: 1.SQL语言 的介绍 2.MySQL数据类型 3.约束类型 4.数据库语句 -表相关的语句-数据相关的语句 5.导入sql文件 6.DML语句(insert update delete)-truncate语句 7....
。
。
python基础语法总结(超详细) ⽬录 1、环境搭建 2、标识符 3、python保留字 4、注释和空⾏ 5、⾏与缩进 6、多⾏语句 7、声明变量 8、标准数据类型 8.1 Number(数字) 8.2 字符串(String) 8.3 List(列表) 8.4 ...
很多语法记不住,汇总一下,抄一下~~ 1. 标识符:用来标识Java类中的各个组成部分(包名,类名,方法名,变量名,参数名)的字符序列。 1)字母,数字,_,$组成。 2)不能以数字开头,长度不限。 3)不能是关键字...
该文档基于windows DOS命令行环境下进行DDL,DCL,DML基础语法练习,包括数据库,表的创建,删除,修改,子查询,表连接,用户创建,授权等。
Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式。 一、标题 1、使用 = 和 – 标记一级和二级标题 我是一级 我是二级 2、使用 # 号标记 一级标题 二...
看B站视频项目总结的文档
06 Python编程语言基础技术框架(2) 07 Python编程语言基础技术框架(3)之print输出 08 Python编程语言基础技术框架(4)之函数介绍 09 Python编程风格(1) 10 Python编程风格(2) 11 Python对象特性、比较及核心数据类型 ...
数据类型基础语法运算符 Java 执行控制流程条件语句 if 条件语句 if...else 条件语句if...else if 多分支语句switch 多分支语句 循环语句 while 循环语句do...while 循环for 循环语句 跳转语句 break 语句 continue ...
一、CSS语法规范 二、CSS选择器 (1)基础选择器 (i)标签选择器 (ii)类选择器 (iii)id选择器 (iv)通配符选择器 (2)复合选择器 (i)后代选择器 (ii)子代选择器 (iii)并集选择器 (iv)伪类选择器 三、...
python基础入门知识:1、简介 ; 2.基础知识:2.1语法基础,2.2条件循环 ,2.3.字符串、字典、元组,2.4函数,2.5.文件操作。。。2.8模块 ; 3.开发案例
主要介绍了Erlang程序设计(第2版)读书笔记:Erlang安装和基础语法,需要的朋友可以参考下
2.java开发环境配置 3.java基本语法 4.java执行控制流程 5.面向对象 6.访问控制权限 7.接口和抽象类 8.异常 9.内部类 10.集合 11.泛形 12.反射 13.枚举 14.I/O 15.注解 16.Null值处理 17.思维导图
下面,我将按以下步骤进行总结: 一、学习python基础概念和语法结构 在课程一开始,我们在老师的带领下对python的基础概念和语法结构进行了学习,深入了解了python的特点和基本操作。通过课堂上的一些代码练习,我对...
这是HTML学习总结的第三篇第二节,前两篇及上一节请戳下面: HTML 学习总结1入门 ...最后,需要介绍它的基础语法 什么是CSS 首先,什么是CSS?它和HTML有什么关系? 为了解释这个问题,我们的先了解CSS的名称。询问度
1、onclick是鼠标单击事件,... function add2(){ var numa,numb,sum; numa=6; numb=8; sum=numa+numb; [removed](两数和为:+sum); } [removed] </head> <body> <form> <input name=butt
完整图文版教程 优质的Python基础入门教程 03、Python基本语法2 (共17页).pptx 完整图文版教程 优质的Python基础入门教程 03、Python基本语法3 (共22页).pptx 完整图文版教程 优质的Python基础入门教程 04、...
在学习Python的过程中,需要掌握以下知识点: 一、基础语法 1. 变量和数据类型:Python中的变量不需要声明,直接赋值即可。常见的数据类型包括整数、浮点数、字符串、列表、元组和字典等。 2. 运算符:Python支持...
2、爬虫要遵纪守法,遵守行业规则与道德。 3、代码沟通世界。有时候感觉它好像是文学作品,但它也是一把双刃剑。 4、开源分享的与其说是代码,倒不如说是一种精神,但别去碰政治这条红线。 5、编程不仅需要懂理论...