oracle的大致回顾

–1 创建一个序列。并演示两个属性

create sequence xulie
start with 1
increment by 2
cache 20;

select xulie nextval from dual;
select xulie currval from dual;

–2 伪列rownum的特点
----只能小于不能大于,rownum永远从0开始

–3 使用伪列rownum进行分页,每页显示3条,第四页
select * from emp;
select * from(select e.*,rownum tr from emp e ) where tr>9 and tr<13;

–4 创建表空间 并删除表空间(需system进行赋予权限)

–system 下的权限 grant create tablespace to scott;
– grant drop tablespace to scott;
create tablespace fuxi
datafile’E:\fuxi\a.dbf’
size 100M
autoextend on
next 32M;

drop tablespace fuxi;

–5 创建用户并删除用户

–system 下的权限 grant create user to scott;
– grant drop user to scott;

create user hsj
identified by 1234

drop user hsj;

–6 oracle预定义角色

–7.给用户赋予权限并撤销权限
– 赋予权限
grant create tablespace to scott;

grant drop tablespace to scott;

– 撤销权限
revoke create tablespace,drop tablespace from scott;

–8 plsql程序块组成部分
declare
–定义部分
begin
–执行部分
exception
–异常处理部分
end
–例
declare
v_ename varchar2(50);
begin
select ename into v_ename from emp where empno=&eno;
dbms_output.put_line(‘您要查找的员工姓名是’||v_ename);
exception
when no_data_found then
dbms_output.put_line(‘输入员工编号不存在!’);
end;

–9 条件控制:怎么判断相等 用一个=

–例
declare
v_age number:=24;
begin
if v_age = 24 then
dbms_output.put_line(‘年龄是’||v_age||‘是他!’);
end if;
end;

– 10.loop循环、while循环、for循环
declare
v_i number :=1;
begin
loop
if v_i>=10 then exit;
end if;
dbms_output.put_line(‘这是第’||v_i||‘次’);
v_i:=v_i+1;
end loop;
end;

declare
v_i number :=1;
begin
while v_i<=10 loop
dbms_output.put_line(‘这是第’||v_i||‘次’);
v_i:=v_i+1;
end loop;
end;

declare
v_i number :=1;
v_end number :=10;
begin
for v_i in 1…v_end loop
dbms_output.put_line(‘这是第’||v_i||‘次’);
end loop;
end;

–11.异常:预定义异常和自定义异常:raise
select * from emp;
declare
e_comm exception;
v_comm emp.comm%type;
begin
select comm into v_comm from emp where empno=&empno;
if v_comm<500 then
raise e_comm;
else
dbms_output.put_line(‘大神’);
end if;
exception
when e_comm then
dbms_output.put_line(‘不够级别回去修炼!’);
end;

–12.
常见函数:to_char to_date
聚合函数:sum 和、count 统计、avg 平均数、distinct 消重、max 最大、min 最小

–13.
索引: 本质:数据结构
用途:用来查询
按照一定的顺序排列,
缺点:增删改的效率很低

索引的分类
单列索引 和 复合索引(经常查询的字段放最前面)、唯一索引 和 非唯一索引、B树索引(create index)和 位图索引(基数)

    B书索引在基数很小的得列不适合用,效率低

14.索引的创建

create table fuxi(
id number(10),
name varchar2(20),
age number(3),
sex char(2)
)
–create [unique] index 索引名 on 表名(字段名); --创建普通/唯一索引
create unique index I_id on fuxi(id);

–create bitmap index 索引名 on 表名(字段名); --创建位图索引
create bitmap index n_name on fuxi(name);

–15.
索引的删除
–drop index 索引名。
drop index n_name;

–16.
约束
–主键约束、非空约束、唯一约束、外键约束
–主键约束的创建:

第一种:建表的时候创建,但是不能自定义名称
create table a(
id number(10) primary key,
name varchar2(20)
)

第二种:建表的时候创建,可以自定义名称
–create table a(
– id number(10),
– constraint 主键名 primary key(字段名)
–)

create table b(
id number(10),
name varchar2(20),
constraint no_id primary key(id)
)

alter table b drop constraint no_id;
–如果想要创建新的外键或者其他约束需先将之前的删除

第三种:建完表的时候创建,可以自定义名称
–alter table 表名 add constraint 约束名 primary key(字段名);
create table c(
id number(10),
name varchar2(20)
)
alter table c add constraint no_id primary key(id);

唯一约束的创建:
create table a(
id number(10) unique
)

create table a(
id number(10) constraint id_uq unique
)

外键约束的创建:关联的表的字段必须是主键或者是唯一
第一种:
create table a(
id number(10),
b_id number(10) references 表名(字段名)
)
第二种:
alter table 表a名 add constraint foreign key(a表的字段) references 表b(b表的字段)

–17.禁用约束、激活约束
禁用
alter table b disable constraint no_id;
激活
alter table b [novalidate | validate] enable constraint no_id;

–18.
删除约束(主键约束、唯一约束、外键约束)
alter table 表名 drop constraint 约束名

–19.
游标
静态游标:显式游标和隐式游标
显示游标的步骤:
1.定义游标:cursor 游标名 is 查询语句。
2.打开游标:open
3.提取游标:fetch
4.关闭游标:close

–20.游标的属性
%found、%notfound、%rowcount、%isopen

–21.显示游标案例
declare
v_dept dept%rowtype;
cursor cursor_dept is select * from dept;
begin
open cursor_dept;
loop
fetch cursor_dept into v_dept;
exit when cursor_dept%notfound;
dbms_output.put_line(‘编号:’||v_dept.deptno||‘名称’||v_dept.dname||‘地址:’||v_dept.loc);
end loop;
close cursor_dept;
end;
–22.
参数游标

declare
cursor cursor_emp(param_dno number) is select * from emp where deptno=param_dno;
v_emp empt%rowtype;
begin
open cursor_emp(10);
loop
fetch cursor_emp into v_emp;
exit when cursor_empt%notfound;
dbms_output.put_line(v_emp.empno||’’||v_emp.enanme);
end loop;
close cursor_emp;
end;
23.触发器案例:
一个表最多有12个触发器;
触发器代码最大是32kb

24.条件谓语触发器案例:

create table stu(
id number(10),
number varchar2(20)
)
-条件谓语触发器:
create or replace trigger my _tri2before
insert or update or deleteon stu
begin
case
when inserting then
dbms_output.put_line('正在添加! ');
when updating then
dbms_output.put_line('正在修改! ');
when deleting then
dbms_output.put_line('正在删除! ');
end case;
end;

insert into stu values (1,‘花花’);
commit;
insert into stu values (2,‘李四’);
commit;
update stu set name=’小芳’where id=2;
commit;

25.行级触发器:for each row — :old :new

–行级触发器
create or replace trigger my_tri3before update of sal, comm or deleteon emp
for each row
when (old.deptno=30)begin
case
when updating(‘sal’) thenif :new.sal<:old.sal then
raise_application_error (4000,'不允许降低30部门的薪资! ');
end if ;
when updating(‘comm’)
then if :new.comm<: old.comm then
raise_application_error (4001,'不允许降低30部门的奖金! ');end if;
when deleting then
raise_application_error (4002,'不允许辞退30部门的员工! ');
end case;
end;
select * from emp;
–修i改30部门的员工薪资
update gmp set sal=1200 where empno=7499;commit;

26.管理触发器
禁用触发器:alter trigger 名称 disable;
启用触发器:alter trigger 名称 enable;
启用表的所有触发器:alter table 表名 enable all triggers;
删除触发器:drop trigger 名称;

27.存储过程
sql语句集,第一次调用会进行预编译,再次调用不再需要编译。
重复调用。

28.带in参数
in可以省略,参数只需要声明类型即可
–带有in参数:in可以省略,且参数类型不需要写大小
create or replace procedure my_pcd2(vid number, vname varchar2,vid2 number, vname2 varchar2)as
begin
insert into stu values(vid, vname) ;insert into stu values(vid2, vname2) ;commit;
end;

–调用begin
my _pcd2(1,’张三’,2,’李四’);
end;

29.out参数和in out参数
–创建out参数的存储过程
create or replace procedure my _pro2(eno number, esalloutnumber)as
begin
select sal into esal from emp where empno=eno;end ;
–调用:输出一个值,声明一个变量去接受这个值
declare
v_sal emp. sal%type;
begin
my_pro2(7499,v_sal);
dbms_output.put_line(v_sal) ;
end;

– in out输入输出参数-第一次作为输入参数,执行后变成输出参数
create or replace procedure my_pro3(myparam in out number)
is
begin
select sal into myparam from emp where empno=myparam;end;
–调用
declare
v_param number (11):=7499;
begin
my_pro3(v_param);
dbms_output.put_line(v_param) ;
end ;

30.多参传递的方式
位置传参
名称传参
组合传参

31.事务
四大特征:原子性、一致性、隔离性、持久性

使用事务隔离等级就可以解决
脏读、不可重复读、幻读;
脏读:事务A读取了事务B没有提交的数据,事务B又进行了回滚。 使用隔离等级:read_commit;
不可重复读:事务A进行多次查询,第一次查询后,第二次查询时查询了事务B修改提交的数据。造成两次查询结果不一样。使用隔离等级:REPEATABLE_READ
幻读:事务A正在修改数据,事务B添加数据,结果事务A提交事务时发现了一条没有修改的数据。SERLALIZABLE

oracle默认的事务隔离等级:read_commit; 只能防止脏读
mysql默认的事务隔离等级:REPEATABLE_READ;能防止脏读和不可重复读

32.锁
排他锁和共享锁、表级锁和行锁、死锁

33.commit、rollback、savepoint

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值