oracle笔记二(入门)
98. with as
可以把使用频繁的sql查询 抽取出来, 将查询结果作为一个临时的表
相当于虚拟视图
用法:
with tempName as (select .....)
针对多个别名:
with
t1 as (select ...),
t2 as (select ...),
....
99. lag, lead函数
lag 用法: 查询这个字段前面offset条记录对应的值,默认为1,如果查不到记录结果为空,则用默认值defaultVl,defaultVal缺省记为null(如果原来的值就是空,那结果就是空)
select col1, col2, ..., lag(coln,offset, defaultVal) over(order by columnName) from table tableName where .....;
在where过滤的查询结果中, 查询 col1,col2,...,coln的第前offset个值(按columnName排序)
lead用法: 查询这个字段后面 后面offset条记录对应字段的值,默认为1,如果查不到记录结果为空,则用默认defaultVal,defaultVal缺省记为null(如果原来的值就是空,那结果就是空)
select col1, col2, ..., lead(coln,offset, defaultVal) over(order by columnName) from table tableName where .....;
在where过滤的查询结果中, 查询 col1,col2,...,coln的第后offset个值(按columnName排序)
100.trunc 对日期,数值进行截取
案例:
select sysdate from dual; --返回当前日期 精确到时分秒
select trunc(sysdate) from dual; --返回当前日期 精确到天 同 select trunc(sysdate,'dd') from dual
select trunc(sysdate, 'yyyy') from dual; --返回当年的第一天 日期精确到天
select trunc(sysdate, 'mm') from dual; --返回这个月的第一天 精确到天
select trunc(sysdate, 'd') from dual;--返回这个星期的第一天 (周日) 精确到天
select trunc(sysdate, 'hh') from dual; --返回当前日期 精确到小时
select trunc(666.666) from dual; ---666
select trunc(666.666, 0) from dual; ---666
select trunc(666.666,1) from dual; --666.6
select trunc(666.666, -1) from dual; --660
101.
create table tableName as select * tableName1;
insert into tableName select * from tableName1;
建立索引:
create index indexName on tableName(columnName1,columnName2....)
drop index indexName
建立序列:
create sequence seq;
insert into tableName (seq.nextval,......);
102. start with connect by
语法:
select * from tablename
start with 条件一
connect by 条件二
从第条件一条开始,根据条件二(向上或向下取决于prior放在哪)递归查询
有node表 字段为 id, no(编号), name(目录名),pno(上级编号)
select * from node
start with no = 3
connect by prior no = pno 从no为3开始查起 查询所有上一条记录的no 等当前记录的pno的记录 从上往下查询
select * from node
start with no = 3
connect by no = prior pno 从no为3开始查起 查询所有上一条记录的pno 等当前记录的no的记录 从下往上查询
103. execute immediate 动态执行sql
语法 execute immediate sqlString
into var1,var2... --将动态sql的结果传入var1...中
using para1,para2... --给动态sql传参
returning into v1,v2... --如果sqlString中有returning into 语句
主要用于 begin ...end中
sqlString := 'create table t1 (col1 number, col2 varchar2(20))';
execute immediate sqlString;
动态的创建表,原本t1是不存在的
sqlString := 'insert into t1(:d1, :d2)'; --使用占位符
execute immediate sqlString
using 10, 'zhangsan';
动态传入参数值, 也可以是变量
sqlString := 'update t1 set col1=:d1 where col2=:d2 returning col1 into :r1';
execute immediate sqlString returning into r1;
r1是变量
104. merge into using语法
merge into tablename as A
using [sql结果] as B
on [condition expression]
where matched then
[update sql]
where not matched then
[insert sql]
根据sql结果 通过 on 条件与A表进行匹配 如果匹配成功 则将sql结果(可选择其中某些字段)更新到A表中
如果不匹配 则将sql结果插入到A表中
where matched then
[update sql] --可以省略
where not matched then
[insert sql] --可以省略
如果想全部插入 on条件可改为 1=0
105.oracle 使用别名时 如果存在空格时 必须使用双引号
select lastname as "姓 名" from emp;
106. null值在排序中,默认是最大的
107.单行函数:
upper('xxx') 大写 lower('xxx') 小写 initcap('xxx')首字母大写
length('xxx') 长度 stbstr(v1,s1,n1) 从v1的s1位置开始截取,截取n1个字符
concat(v1,v2) 将v1,v2进行连接 ,类似于||
replace(v1,c1,c2) 将v1中的c1用c2代替
nvl(v1,d1) 如果v1为空取默认值d1
round(v1,n1)四舍五入保留小数点后n1位
trunc(v1,n1)保留小数点后n1位,直接截取
to_char(c1,c2) 将数值型转为c2格式的字符串 to_char(c1) 将c1转为字符串
add_months(d,n) 给日期d加减n个月
108.日期转字符串
to_char(date,'pattern');
yyyy:年
mm:月
dd:日
hh:小时
mi:分钟
ss:秒
day: 星期几,如星期一
month: 几月,如12月
109.drop table t1 cascade constraint;
删表的同时删除与该表相关的约束,比如外键约束
110.增加约束,删除约束 该写法非空约束除外
alter table tname add constraint cname ctype(columnname); --primary key (column..),foreign key (column..) references..,unique(column..).....
alter table tname drop constraint cname;
111.序列
create sequence seqname ; --省略相关属性
案例:
create SEQUENCE seq_stu
INCREMENT BY 2 MAXVALUE 5000 CYCLE;
112.伪列 rownum
rownum 作为过滤条件只能是 <,<=,between and 等 不能单使用>
反例:select rownum,s.* from student s where rownum >2; --查询结果始终为空
正例:select * from (select rownum r, s.* from student s) s1 where r>2; --查询结果有数据
113.存储过程输出
如果是命令窗口 比如cmd形式的
set serveroutput on;
begin dbms_output.put_line('hello world');
end;
/
一定要先set serveroutput on; 开启数据库输出,否则看不到输出信息; / 程序结束符号
114.plsql
declare
...
begin
...
end;
=用于判断 :=用于赋值 || 用于连接
115.常量 VS 非空变量
declare
name varchar2(100) not null :='xx';
salary constant int := '9999';
begin
name := '欣淡定';
dbms_output.put_line(name||'今年加'||salary||'薪');
end;
都必须赋初始值; 常量赋值后不能在修改,非空变量可以修改但不能改为空
116.type,recode,rowtype的使用
type: 声明某个变量的类型
record: 定义一组数据,相当于一行
rowtype: 声明为某个表一行的类型
案例:
declare
type sinfo is record(
no student.id%type,
name student.
sname%type );
stu sinfo; s1 student%rowtype;
begin
stu.no := 1001;
stu.name := '欣淡定';
dbms_output.put_line('编号'||stu.no);
dbms_output.put_line('姓名'||stu.name);
dbms_output.put_line('---------------------------');
select * into s1 from student where id = 1;
dbms_output.put_line('编号'||s1.id);
dbms_output.put_line('姓名'||s1.sname);
end;
117.plsql嵌套
<<wai>>
declare
val int := 10;
begin
<<wai1>>
declare
val int := 20;
begin
dbms_output.put_line(val); --20
dbms_output.put_line(wai.val); --10
dbms_output.put_line(wai1.val); --20
end;
end;
118.if结构
declare
v1 int;
begin
v1 := 11;
if(v1>10) then
dbms_output.put_line('工资'||v1||'+');
elsif v1 < 10 then
dbms_output.put_line('房子数量'||v1||'-');
else
dbms_output.put_line('豪车数量'||v1);
end if;
end;
119.循环
- loop循环
declare
tcount int :=1;
begin
loop
dbms_output.put_line('知识点' || tcount);
exit when (tcount=10); --退出条件
tcount := tcount + 1;
end loop;
end;
- while循环
declare
friend varchar(100) := 'monkey';
num int;
begin
num := 1;
while num < 10 loop
dbms_output.put_line(friend || num);
num := num + 1;
end loop;
end;
- for循环
declare
total int := 100;
begin
for money in 1..total loop
dbms_output.put_line('某付宝到账'|| money || '元');
end loop;
end;
--for money in reverse 1..total 从后往前输
120.退出嵌套循环
exit: 默认退出当前的整个循环,可以使用标签 退出标签所在的整个 循环
continue: 默认退出 本次循环, 可以使用标签 退出标签所在循环的本次循环
案例:
declare
x int :=10;
y int := 1;
begin
<<label1>>
while x >=0 loop
while y<=10 loop
dbms_output.put_line('坐标('||x||','||y||')');
if y =2 then
exit label1; --退出指定的循环
end if;
y := y+1;
end loop;
y := 1;
x := x-1;
end loop;
end;
121.goto
可以跳转到指定标签的位置,但是标签不可以是最后一条语句。解决办法,在末尾加个null语句
案例:
begin
dbms_output.put_line('1');
dbms_output.put_line('2');
goto label1;
dbms_output.put_line('3');
dbms_output.put_line('4');
<<label1>>
null;
end;
122.游标的使用
游标属性:%rowtype,%found,%notfound;
使用%found,%notfound时 一定要先移动游标,也就是使用fetch一下
案例:
declare
cursor mycursor is select sname from student; --声明游标
sname student.sname%type;
begin
open mycursor; --打开游标
for i in 1..3 --取前三个 loop
fetch mycursor into sname; --游标取数
dbms_output.put_line('学生姓名'||sname);
end loop;
close mycursor; --关闭游标
end;
游标属性的使用:
declare
cursor mycursor is select sname from student;
sname student.sname%type;
begin
open mycursor;
loop fetch mycursor into sname; --移动游标
exit when mycursor%notfound;
dbms_output.put_line('学生姓名'||sname);
end loop;
close mycursor;
end;
for循环使用游标,自动打开/关闭游标
declare
cursor mycursor is select id,sname from student;
begin
for c in mycursor loop
dbms_output.put_line('学生编号'||c.id||'学生姓名'||c.sname);
end loop;
end;
- 参数游标的使用
declare
cursor mycursor(myid student.id%type) is select * from student where id=myid;
rowStu mycursor%rowtype;
myid student.id%type;
begin
myid := &请输入学生编号; --用户输入
open mycursor(myid);
fetch mycursor into rowStu;
if mycursor%found then
dbms_output.put_line('学生姓名:' || rowStu.sname);
else
dbms_output.put_line('查无此人');
end if;
end;
123.异常处理
格式:
exception
when 异常类型 then
。。。。
when 异常类型 then
。。。。
when others then
。。。。
案例:
declare
sname student.sname%type;
begin
select sname into sname from student where id = -1;
dbms_output.put_line(sname);
exception when no_data_found then
dbms_output.put_line('查无此人');
when others then
dbms_output.put_line('未知异常');
end;
124.存储过程
格式:
create or replace procedure pname(参数列表) --参数形式 in 默认,out, in out
as
变量声明区
begin
代码区
end;
调用:
1)declare
begin
pname(参数);
end;
2)exec pname(参数);
3)call pname(参数);
125.函数
格式
create or replace function fname(参数列表) return 参数类型
as
变量声明区
begin
代码区
end;
调用:
1.select fname(参数列表) from dual;
2.declare
begin
变量 := fname(参数列表);
end;
126.oracle定时任务
案例:
--建表
create table fruit( id int primary key, fname varchar2(100) )
--创建序列
create sequence seq_fruit;
--创建存储过程
create or replace procedure myp3
as
begin
insert into fruit(id, fname) values(seq_fruit.nextVal, 'x');
end;
declare job_id int;
begin
--提交任务
dbms_job.submit(job_id, 'myp3();', sysdate, 'sysdate + (5/(24*60*60))');
--运行任务
dbms_job.run(job_id);
end;
--查询数据
select * from fruit;
--查询任务表
select * from all_jobs;
--删除定时任务
begin
dbms_job.remove(23);
end;
submit(v1,v2,v3,v4)参数介绍:
v1: 任务id,属于输出参数
v2: 需要执行的存储过程, 属于输入参数
v3: 从指定时间开始执行定时任务,属于输入参数
v4: 第一次执行后,每隔多长时间执行任务,也就是输入的存储过程 属于输入参数
127.触发器,针对DML语句
格式:
create or replace trigger 名称 before|after insert|update|delete
on 表名 【for each row】
declare
begin
....; --触发时执行的代码
end;
如果有for each row ; dml语句涉及多行数据时,会多次执行 触发代码
案例1:行级触发器,触发时只执行一次
create or replace trigger myt1
after insert
on fruit
declare
begin
dbms_output.put_line('又有新水果了');
end;
insert into fruit(id, fname) values(seq_fruit.nextVal, 'apple'); --输出: 又有新水果了
案例2: 语句级触发器,涉及多行,触发代码就会执行多次
create or replace trigger myt2
after update
on fruit
for each row
declare
begin
dbms_output.put_line('水果更新了');
end;
update fruit set fname='banana' where id between 2 and 4; --输出: 水果更新了 水果更新了 水果更新了
oracle笔记二(入门)相关推荐
- 冯柯《我的ORACLE笔记二:关于回滚段》接力来袭!
感谢大家对此文的关注!我们的活动还在继续中~您对文章内容有任何疑问时,欢迎您点击原文链接,填写阅读反馈表.我们不仅会邀请作者来回答问题,还会从中抽取一位粉丝赠送精美礼品一份哦~ <我的ORACL ...
- oracle直查和call哪个更快,让oracle跑的更快1读书笔记二
当前位置:我的异常网» 数据库 » <>读书笔记二 <>读书笔记二 www.myexceptions.net 网友分享于:2013-08-23 浏览:9次 <> ...
- Oracle数据库基础入门《二》Oracle内存结构
Oracle数据库基础入门<二>Oracle内存结构 Oracle 的内存由系统全局区(System Global Area,简称 SGA)和程序全局区(Program Global Ar ...
- MySql入门笔记二~悲催的用户
这些是当年小弟的MySql学习笔记,木有多么复杂的结构操作,木有多炫丽的语句开发,木有...总之就是木有什么技术含量... 日复一日,彪悍的人生伴随着彪悍的健忘,运维操作为王,好记性不如烂笔头,山水有 ...
- 【台大郭彦甫】Matlab入门教程超详细学习笔记二:基本操作与矩阵运算(附PPT链接)
Matlab入门教程超详细学习笔记二:基本操作与矩阵运算 前言 一.基本操作 1.把matlab当作计算器使用 2.变量 3.控制格式输出 二.矩阵运算 1.矩阵 2.矩阵索引 3.使用:创建向量 4 ...
- 《Docker 技术入门与实践》-读书笔记二
<Docker 技术入门与实践>-读书笔记一 <Docker 技术入门与实践>-读书笔记二 一.数据管理 用户在使用 Docker 的过程中,往往需要能查看容器内应用产生的数据 ...
- Oracle数据库从入门到精通系列之二:SGA和后台进程
Oracle数据库从入门到精通系列之二:SGA和后台进程 一.SGA 二.后台进程 一.SGA Oracle有一个很大的内存块,称为系统全局区(SGA),用于(但不限于): 维护所有进程需要访问的内部 ...
- (Python入门)学习笔记二,Python学习路线图
(Python入门)学习笔记二,Python学习路线图 千里之行始于足下,谋定而后动,冰冻三尺非一日之寒.之所以说这三句话,就是对于下面整理的路线图,即不让自己感觉路途的遥远而感到达到巅峰神界的遥遥无 ...
- 《逻辑思维简易入门》(第2版) 阅读笔记二
<逻辑思维简易入门>(第2版) 阅读笔记二 本周阅读的是<逻辑思维简易入门>的第三章,也就是说,本书的第一部分就已经读完了. 第三章.信念的优点 信念和负信念是人们在接受一个事 ...
最新文章
- python 列表推导式 - 列表字典取value
- jzoj5223-B【矩阵乘法】
- This subject is anonymous - it does not have any identifying principals and authorization operations
- 第四章信息系统安全基础考试要点及真题分布
- java连接rabbitmq_RabbitMQ教程(工作模式篇)
- SpringBoot开源项目(企业信息化基础平台)
- java fields是_一个快速生成R2.java中fields的插件
- SQL Identity自增列清零方法
- DELL笔记本大小写切换时鼠标焦点转移的解决办法
- RTR/SLA 在多ISP环境下下的应用--已经更新,切换后线路恢复时,已能自动恢复
- TortoiseSVN - Download下载 - 1.5.4
- python下载电影链接_Python:输入电影名,爬获取到阳光电影网中对应名称的迅雷下载链接,获取的链接有点问题(具体问题在内容里)...
- 管道 pipe是什么?(进程通信的一种方式)
- UCT树用于四子棋对抗实验
- 订阅号做了77天,没有挣钱,但是收获很多。
- HTML——表格合并
- 8种CSS3按钮动画特效【附源码】
- OpenNURBS 3DM Viewer
- 微服务之spring Boot+MyBatis-Plus +mysql框架
- DBeaver Enterprise 7.1 企业版注册方法
热门文章
- Linux刻录光盘win10认不到,Windows10系统读不取DVD光驱和刻录光盘
- git Incorrect username or password (access token)问题解决
- 强烈推荐33个 GitHub 前端学习资源
- 羊皮卷之二:我要用全身心的爱来迎接今天
- js实现点击上一题和下一题出现对应的题目,
- CSS——设置元素边框的bug,当boder-radius比border本身大的时候
- 面部识别预处理全家桶:mtcnn人脸捕捉、人脸点对齐、仿射运算
- ADF单位根检验三种形式_第三章(下):t 检验和 F 检验
- 基于协同过滤的推荐系统
- STL常用——acwing——yxc