导图社区 Oracle数据库自学教程笔记思维导图
Oracle数据库自学教程笔记思维导图,整理了用户管理、sql语句、函数、视图/索引、存储过程-plsql编程、处发器、数据库3范式的内容,快来看看吧!
编辑于2023-02-23 22:28:53 广东Oracle数据库自学教程笔记思维导图
用户管理
用户创建
--创建用户 hello 密码 lihaoran create user hello identified by lihaoran; --为hello用户 grant 授予登陆权限 grant create session to hello; --连接hello用户 conn hello/lihaoran; --为hello用户分配resource权限 进行crud操作 grant resource to hello; --修改密码 alter user hello identified by lihaoran; --撤消crud权限 revoke resource from hello; --删除hello 用户 及相关的信息 drop user hello cascade;
--案例:lihaoran用户去查询 scott用户下的表 --第一步连接 scott用户 connect scott/tiger --第二步 将 emp表 查询权限赋给lihaoran用户 grant select[update,insert,delete,all] on emp to lihaoran; --第三步 登陆lihaoran 用户查询emp表 带scott用户名 select * from scott.emp; --创建用户hello create user hello identified by hello; grant connect to hello; grant resource to hello; --将liharoan用户下的scott表中的emp权限转让给hello用户 --连接scott用户 Connect scott/tiger --将lihaoran用户下的scott用户下的emp表受对象权限 可转让 grant all on emp to lihaoran with grant option;(必须在scott用户以及权限更新的用户操作) --//将系统权限往下分匹(with admin option) grant select on emp to hello;(lihaoran受权用户能及权限更大的用户操作) --撤消liharoan查询权限 --连接system/ scott用户 connect system/orcl --撤消查询权限 revoke select on scott.emp from lihaoran; --将scott用户上锁 alter user scott account lock;(此用户不可用,但上级用户可以操作该用户的数据(crud)) --将scott用户解锁 alter user scott account unlock;
sql语句
内连接
1、容易产生迪卡尔机,通过外键等唯一性约束 select * from emp e,dept d where e.deptno=d.deptno; 2、完整写法 inner join ...on... select * from emp e inner join dept d on e.deptno=d.deptno;
左右全连接
以联合查询雇员表和部门表为例 1、左连接:left join ... on .... select * from emp e left join dept d on e.deptno=d.deptno; 方法二右(+) select * from emp e,dept d where e.deptno=d.deptno(+); 2、右连接:right join..on... select * from emp e right join dept d on e.deptno=d.deptno; 方法二左(+) select * from emp e,dept d where e.deptno(+)=d.deptno; 3、全连接 select * from emp e full join dept d on e.deptno=d.deptno;
自连接
查询雇员的上级领导 select * from emp e1,emp e2 where e1.mgr=e2.empno;
Oracle 分页三层嵌套写法
查询雇员表2到10之间的数据 select * from (select e2.*,rownum rn from (select * from emp) e2 where rownum<=10) e1 where rn>=2;
group by(分组)/having(分组后进行过虑)/order by asc、desc(排序)
select avg(sal),max(sal),min(sal),count(*),sum(sal),e.deptno from emp e where to_char(e.hiredate,'yyyy-mm-dd')>'1980-12-20' group by e.deptno having avg(sal)>1000 order by e.deptno;
子查询
--单行子查询 =号来查询 --显示与SMITH同一部门的所有员工 select * from emp e where e.deptno=(select deptno from emp p where p.ename='SMITH'); --多行子查询 in 来查询 --显示与10号部门 job 职位相同的雇员信息 select * from emp e where e.job in(select distinct p.job from emp p where p.deptno=10); --比30部门员工工资都高的员工信息max,all select * from emp e where e.sal>(select max(sal) from emp p where p.deptno=30); select * from emp e where e.sal>all(select sal from emp p where p.deptno=30); --查询比30部门员工任意工资高的雇员信息min、any select * from emp e where e.sal>(select min(sal) from emp p where p.deptno=30); select * from emp e where e.sal>any(select sal from emp p where p.deptno=30); --多列子查询 --查询与SMITH同一部门同一岗位的员工 --方法一 select * from emp e where e.deptno=(select deptno from emp where ename='SMITH') and e.job=(select job from emp where ename='SMITH'); --方法二(deptno,job)=(select deptno,job from emp where ename='SMITH'); select * from emp where (deptno,job)=(select deptno,job from emp where ename='SMITH'); --在from 子句中使用子查询 --显示高于自己部门平均工资的员工信息 --方法一 --把(select avg(sal) deptno from emp group by deptno当作一个临时表) select e.deptno,e.ename,e.deptno,avgs from emp e,(select avg(sal) avgs,deptno from emp group by deptno) p where e.deptno=p.deptno and e.sal>avgs; --方法二(缺陷 不能查询平均工资) select * from emp e where e.sal>(select avg(sal) from emp p where p.deptno=e.deptno); --查找每个部门工资最高的人的详细信息 SQL> select * from emp e,(select max(sal) maxs,deptno from emp group by deptno) p where e.deptno=p.deptno and e.sal=maxs; --显示每个部门的信息(编号、名称、)和人员数量 select d.deptno,d.dname,counts from dept d,(select count(*) as counts,deptno from emp group by deptno) t where d.deptno=t.deptno(+);
序列sequence
--从1开始,每次自增1,最大值 99999999999,之后又循环从1开始 create sequence my_lihaoran--创建序列 start with 1 --从一开始自增 increment by 1 --每次自增1 maxvalue 99999999999 --NOMAXVALUE(不设置最大值) --最大值 minvalue 1 --最小值 cycle --cycle 表示当前序列增加到99999999999 从新从1开始,如果nocycle就停止; nocache --//nocache表示不缓存,【cache:10】 表示一次产生10个号供你使用,优点提高效率,缺点可以产生跳号 --创建表 create table lihaoran(id number primary key,name varchar2(100)); --添加序列 insert into lihaoran values(my_lihaoran.nextval,'lihaoran'); --显示当前序列值 select my_lihaoran.currval from dual; --删除序列 drop sequence my_lihaoran --sqlserver自增长identity create table sqlserver( id int primary key identity(1,1)); --mysql auto_increment create table mysql( id int primary key auto_increment);
大对象
Blog(4g)
存储二进制数据,例如电影,音乐,图片等
Clob(4g)
存储大文本对象,少量文字
函数
Upper(行,列)字符串转大写 Lower(行,列)字符串转小写返回 Initcap()开关首字母大写 Length()取字符串长度 Replace()替换 Substr(列,开始点,结束点)字符串截取
范例-转大写 select Upper('hello world') from dual;-- 1 HELLO WORLD select * from emp e where e.ename=upper('smith') EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO 1 7369 SMITH CLERK 7902 1980/12/17 800.00 20 范例—转小写 select Lower(ename) from emp; 范例-首字母大写 select initcap(ename) from emp; 范例-字段长度 select e.ename,length(e.ename) from emp e; 范例-字段姓名长度为5的字段 select e.ename,length(e.ename) from emp e where length(e.ename)=5 范例-字段中含有‘A’换成’_’ select replace(e.ename,'A','_') as ename from emp e; 范例-截取姓名后三位 select substr(e.ename,length(e.ename)-2,length(e.ename)) from emp e; select substr(e.ename,-3)from emp e; 请问substring截取是从0开始截取,还是从1开始的? Oracle数据库中从0或1都可以 Substr也可以设置负数;表示从后面设置起始点;
视图/索引
--创建视图 create view lihaoran_view as select e.ename,e.deptno,d.dname from emp e,dept d where e.deptno=d.deptno with readonly; --with read only 只可读不可修改 create view lihaoran_view as select e.ename,e.deptno,d.dname from emp e,dept d where e.deptno=d.deptno with read only; --or replace create or replace view lihaoran_view as select e.ename,e.deptno,d.dname from emp e,dept d where e.deptno=d.deptno with read only; --删除视图 drop view lihaoran_view; --索引 CREATE UNIQUE CLUSTERED INDEX index_viewFoo ON viewFoo(id)
存储过程-plsql编程
pl/sql是标准sql语句基础是扩展的一种对Oracle数据库进行编程的语句,可以定义变量,常量,而且可以进行条件语句和循环
可以编写客户端(sqlplus/developer),使用plsql编写储存过程
create or replace procedure fenyePro1 ( v_in_table in varchar2,v_in_pagesize in number, v_in_pagenow in number,v_out_result out pack1.my_cursor,v_out_rows out number, v_out_pagecount out number number ) is --定义变量 v_sql varchar2(2000); v_start number; v_end number; begin --执行代码 --回忆分页语句 --计算v_start 和v_end 是多少 v_start:=v_in_pagesize*(v_in_pagenow-1)+1; v_end:=v_in_pagesize*v_in_pagenow; v_sql:='select t2.* from(select t1.*,rownum rn from (select * from '||v _in_table||') t1 where rownum<='||v_end||') t2 where rn>='||v_start; --打开游标,让游标指向结果集 open v_out_result for v_sql; --查询共有多少条记录 select count(*) into v_out_rows from emp; if mod(v_out_rows,v_in_pagesize)=0 then v_out_pagecount:=v_out_rows/v_in_pagesize; else v_out_pagecount:=v_out_rows/v_in_pagesize+1; end if; end; procedure created;
处发器
create or replace trigger salary_trigger --当向table_a表中插入一行或修改一行后激活触发器。 before insert or update of num_col on table_a for each row begin --如果插入值或修改值大于10000时,报告应插入值 --并产生异常,退出该触发器。 if:new.num_col>10000 then raise_application_error(-200060,'插入值应小于10000!'); end if; end salary_trigger;
数据库3范式
1范式:必须有主键 每个字段都是不可分割的最单元
2范式:满足一范式的前提上,例如:机构表与人员表,分开设计,要不产生冗余
3范式:满足二范式的前提下,例如把表提练的精一些,不要连带,产生冗余