pl/sql
create index  ename_emp_sal on emp(sal);
drop index  ename_emp_sal ;
/*索引*//*范式:不存在冗余数据,就是信息重复*/
第一范式:a.要有主键, b.列不可分
第二范式:a.当一张表存在多个主键时,b.不存在部分函数依赖。c.存在于多对多的关系
第三范式:a.不存在传递依赖。begindbms_output.put_line('HelloWorld!');end;
/
set serveroutput on ;/*系统环境默认变量关闭*/
begindbms_output.put_line('HelloWorld!');end;
/
/*输出 HelloWorld!*/declare  /*声明变量*/v_name varchar(20);
begin v_name:= 'myname';dbms_output.put_line(v_name);end;
/declare v_num number := 0;
begin v_num :=2/v_num;dbms_output.put_line(v_num);
exceptionwhen others then dbms_output.put_line('error');
end;
/--声明变量类型变量名不能够使用保留字,如from、select等第一个字符必须是字母变量名最多包含30个字符不要与数据库的表或者列同名每一行只能声明一个变量--常见数据类型binary_integer:整型number:数字类型char:定长字符串varchar2:边长字符串date:日期long:长字符串boolean:布尔类型,可以取值为true、false和null值--变量声明declarev_temp number(1);v_count binary_integer :=0;v_sal number(7,2) := 4000.00;v_date date := sysdate;v_pi constant number(3,2) := 3.14;v_valid boolean := false;v_name varchar2(20) not null :='myname';
begindbms_output.put_line('v_pi value :' || v_pi);
end ;
/
/* || 变量输出*/--变量声明,使用%type属性
declarev_empno number(4);v_empno2 emp.empno%type;v_empno3 v_empno2%type;
begindbms_output.put_line('Test');
end;--table组合变量declaretype type_table_emp_empno is table of emp.empno%type index by binary_integer;v_empnos type_table_emp_empno;
beginv_empnos(0) :=7369;v_empnos(2) :=7839;v_empnos(-1) :=9999;dbms_output.put_line(v_empnos(-1));
end;
/
--Record 变量类型declare
--声明变量,定义变量type type_record_dept is record(deptno dept.deptno%type,dname dept.dname%type,loc dept.loc%type);v_temp type_record_dept;beginv_temp.deptno :=50;v_temp.dname := 'aaaa';v_temp.loc := 'bj';dbms_output.put_line(v_temp.deptno|| '' || v_temp_name);
end;
/--sql 语句的运用
返回记录,且只能返回一条数据
declare v_ename emp.ename%type;v_sal emp.sal%type;
beginselect ename,sal into v_ename,v_sal from emp where empno =7369;dbms_output.put_line(v_ename||''||v_sal);
end;
/
/*只能返回一条记录*/DDL语句
beginexecute immediate  'create table  T( nnn varchar2(20) default ''aaa'')';
end;
/
/*两个单引号代表一个单引号*/declare v_sal emp.sal%type; /*声明v_sal为emp.sal列*/
beginselect sal into v_sal from empwhere empno = 7369;if(v_sal <1200)thendbms_output.put_line('low');elsif(v_sal<2000)thendbms_output.put_line('middle');elsedbms_output.put_line('high');end if ;
end;
/declare i binary_integer :=1;
beginloopdbms_output.put_line(i);i := i+1;exit when(i >= 11);end loop;
end;
/
/*打印1.2.3...10相当于do while循环*/beginfor k  in 1..10 loopdbms_output.put_line(k);end loop;for k in reverse 1..10 loopdbms_output.put_line(k);end loop;
end;
/
/*for循环*/declarev_temp number(4);
beginend;create table errorlog
(
id number primary key,
error number,
errmsg varchar2(1024),
errdate date
);create sequence seq_errlog_id start with 1 increment by 1;declare v_deptno dept.deptno%type :=10;v_errcode number;v_errmsg varchar2(1024);
begindelete from dept where deptno = v_deptno;commit;
exceptionwhen others thenrollback;v_errcode := SQLCODE;v_errmsg := SQLERRM;insert into errorlog values(seq_errlog_id.nextval,v_errcode,v_errmsg,sysdate);commit;
end;
/
select * from errorlog;--重中之重游标declarecursor c is  /*指定游标c */select * from emp;v_emp c%rowtype; /**/
beginopen c;fetch c into v_emp;  /*游标自动移到下一格*/dbms_output.put_line(v_emp.ename);close c ;
end;
/declarecursor c is  /*指定游标c */select * from emp;v_emp c%rowtype; /**/
beginopen c;loopfetch c into v_emp;  /*游标自动移到下一格*/dbms_output.put_line(v_emp.ename);exit when (c%notfound);end loop;close c ;
end;
/
/*游标遍历*/declarecursor c is  /*指定游标c */select * from emp;v_emp c%rowtype; /**/
beginfor v_emp in c loopdbms_output.put_line(v_emp.ename);end loop;
end;
/
/*赋值的时候用 :=,判断时用=*/create or replace procedure p
iscursor c is select * from emp2 for update;
beginfor v_emp in c loopif (v_emp.deptno = 10) then update emp2 set sal  = sal + 10 where current of c ;elsif(v_emp.deptno =  20) thenupdate emp2 set sal  = sal + 20 where current of c ;elseupdate emp2 set sal  = sal+50 where current of c ;end if ;end loop;commit;
end ;
/--function
create or replace function sal_tax(v_sal number)return number
is
beginif(v_sal < 2000)then return 0.10;elsif(v_sal < 2750)thenreturn 0.15;elsereturn 0.20;end if;
end;
/
select lower(ename),sal_tax(sal)from emp;--触发器,当你做一件事的时候当你一做某件事产生的动作叫触发。触发器必须依附在某张表上。
create table emp2_log
(
uname varchar2(20),
action varchar2(10),
atime date
)create or replace trigger trigafter insert or delete or update on emp
begin if inserting theninsert into emp2_log values(USER,'insert',sysdate);elsif updating then insert into emp2_log values(USER,'update',sysdate);else deleting theninsert into emp2_log values(USER,'delete',sysdate);end if ;
end;--递归 recursion,树状结构create table article
(
id number primary key,
cont varchar2(4000),
pid number,
isleaf number(1),
alevel number
) ;
--isleaf0代表非叶子结点,1代表叶子节点
insert  into article values(1,'蚂蚁战胜大象',0,0,0);
insert  into article values(2,'大象被打趴下了',1,0,1);
insert  into article values(3,'蚂蚁也不好过',2,1,2);
insert  into article values(4,'瞎说',2,0,2);
insert  into article values(5,'没有瞎说',4,1,3);
insert  into article values(6,'怎么可能',1,0,1);
insert  into article values(7,'蚂蚁战胜大象',6,1,2);
insert  into article values(8,'蚂蚁战胜大象',6,1,2);
insert  into article values(9,'蚂蚁战胜大象',2,0,2);
insert  into article values(10,'蚂蚁战胜大象',9,1,3);create or replace   procedure p (v_pid article.pid%type, v_level binary_integer)
iscursor c is select * from article where pid = v_pid;v_preStr varchar2(1024) := '';
beginfor i in 0..v_level loopv_preStr := v_preStr || '****';end loop;for v_article in c loopdbms_output.put_line(v_preStr ||v_article.cont);if(v_article.isleaf = 0 )then p(v_article.id,v_level+1);end if;end loop;
end;
/exec p(0,0)

个人的尚学堂数据库oracle笔记(3)相关推荐

  1. 尚学堂Java学习笔记

    尚学堂Java学习笔记 ============================ J2SDK&JRE J2SDK:JAVA2 SOFTWARE DEVELOPMENT KIT JRE:JAVA ...

  2. 第十三章 J20飞机游戏项目完整代码(尚学堂java300集笔记)

    第十三章 J20飞机游戏项目 DAY10-DAY11 通过键盘控制飞机前后移动,躲避炮弹,看谁坚持的时间长.如果碰到炮弹,则发生爆炸,游戏结束,并显示本次生存的时间. 图片资源 images包下存放: ...

  3. 尚学堂JAVA基础学习笔记_2/2

    尚学堂JAVA基础学习笔记_2/2 文章目录 尚学堂JAVA基础学习笔记_2/2 写在前面 第10章 IO技术 1. IO入门 2. IO的API 3. 装饰流 4. IO实战 5. CommonsI ...

  4. 尚学堂JAVA高级学习笔记_1/2

    尚学堂JAVA高级学习笔记 文章目录 尚学堂JAVA高级学习笔记 写在前面 第1章 手写webserver 1. 灵魂反射 2. 高效解析xml 3. 解析webxml 4. 反射webxml 5. ...

  5. 类oracle数据库pss,Oracle笔记

    Oracle笔记 Oralce简介和安装 ·常见数据库: Access .SQL Server .My SQL .DB2 .Oracle .Sybase .Infomix 数据库网络中全局数据库名不能 ...

  6. 学习笔记(01):Oracle数据库-Oracle安装与配置

    立即学习:https://edu.csdn.net/course/play/3574/62057?utm_source=blogtoedu oracle笔记

  7. 尚学堂学习笔记。。。

    1.02_尚学堂马士兵_Struts2_Struts2_HelloWorld_2.avi 指定Tomcat的目录,指定JDK搭建开发环境(拷贝jar包,复制struts.xml文件 此文件不要放在WE ...

  8. [原创 - 尚学堂科技 - 马士兵老师]

    JAVA自学之路 一:学会选择 [转载请注明出处:http://www.bjsxt.com/zixue/zixuezhilu_1.html] 为了就业,不少同学参加各种各样的培训. 决心做软件的,大多 ...

  9. [转]尚学堂科技 - 马士兵老师-JAVA自学之路

    [原创 - 尚学堂科技 - 马士兵老师] JAVA自学之路 一:学会选择 [转载请注明出处:http://www.bjsxt.com/zixue/zixuezhilu_1.html] 为了就业,不少同 ...

  10. JAVA自学之路 [原创 - 尚学堂科技 - 马士兵老师]

    (我觉得看了之后挺不错的所以分享一下) JAVA自学之路 一:学会选择 为了就业,不少同学参加各种各样的培训. 决心做软件的,大多数人选的是java,或是.net,也有一些选择了手机.嵌入式.游戏.3 ...

最新文章

  1. 信息安全系统设计基础第3周学习总结
  2. 互联网协议 — 使用 Wireshark 调试 HTTPS 及 HTTP/2 流量
  3. python编程有哪些-python编程工具有哪些
  4. SAP SOAMANAGER报错原因与故障排除方法
  5. ACCESS中的Update语句不支持Select的解决办法
  6. “云计算+DevOps”的正确打开方式
  7. plt python 画直线_Matplotlib:先搞明白plt. /ax./ fig再画
  8. Rust性能分析-迭代器的enumerate方法是否会影响程序性能
  9. 2011MBP在Win7下打开ACHI
  10. 第七课 Linux裸机开发+SourceInsight3.5使用+notepad++使用
  11. 轻松搭建docker应用的mesos集群
  12. 二分、冒泡、快速、插入排序
  13. 怎样用计算机算出54188,计算机应用技术练习题.doc
  14. H5+实现保存图片到本地相册
  15. oracle季初,Oracle获取月初/月末/季初/季末/半年初/半年末/年初/年末
  16. [树状数组] 洛谷P3374
  17. OpenMesh-网格光顺的算法
  18. 6 个超酷的学习算法网站,Leetcode 不是第一 ?
  19. 解决标题党的计算机算法用户app,今日头条算法工程师:做好推荐必须打击标题党...
  20. ROCm平台简介及使用汇总

热门文章

  1. 服务器端给客户端发送消息,linux 服务器端给客户端发送消息
  2. iphone/ipad 连接smb服务器,实现局域网内文件共享
  3. 申请苹果公司版开发者账号实录【99美元,非299美元企业版账号】
  4. 2022年玻璃包装容器行业市场发展环境分析预测及下游需求规模增长率研究预测
  5. 利用组策略部署软件——将软件发布给用户
  6. 华人的旗帜——首位亚裔图灵奖获得者姚期智
  7. pip install 报错:ERROR: Exception: Traceback (most recent call last):..raise ValueError(“check_hostnam
  8. 福建广电网络显示服务器异常,无法浏览网页故障
  9. python数据处理可以做什么菜_python 文件处理
  10. Aloha (世界上最早的无线电计算机通信网)