04oracle学习笔记

来源:互联网 发布:淘宝女舞蹈裤 编辑:程序博客网 时间:2024/05/22 15:36

一、查询:

--内连接:两边数据能对应起来的,对应不出来的查不到


select * from t_employee e inner join t_dept d on e.depton=d.depton;


select * from t_employee e,t_dept d where e.depton=d.depton;


--外连接:分为


--左外连接 左边的是主表(主表的信息全部展示),右边的从表(从表的信息如果跟主表对应不起来显示为null)


select * from t_employee e left join t_dept d on e.depton=d.depton;


select * from t_employee e , t_dept d where e.depton=d.depton(+);


--右外连接


select * from t_employee e right join t_dept d on e.depton=d.depton;


select * from t_employee e , t_dept d where e.depton(+)=d.depton;


--全连接


select * from t_employee e full join t_dept d on e.depton=d.depton;


二视图:

view,是一种有结构(有行有列),但没有结果(结构中不真实存放数据)的虚拟表,虚拟表的结构来源不是自己定义的,而是从对应的基表(视图的数据来源)中产生的。

1、创建视图的语法:

create or replace view my_view

as

select e.*,d.deptname from t_emploee e,t_dept d where  e.depton=d.deptno;

2、视图的作用:

如果需要经常执行某项复杂查询,可以基于这个复杂查询建立视图,此后查询此视图即可,简化复杂查询;

视图本质上就是一条SELECT语句,所以当访问视图时,只能访问到所对应的SELECT语句中涉及到的列,对基表中的其它列起到安全和保密的作用,可以限制数据访问。

INSERT INTO v_emp_10  VALUES(1234, 'DOCTOR', 4000, 10);


三、索引:

索引是一种允许直接访问数据表中某一数据行的树型结构,为了提高查询效率而引入,是独立于表的对象,可以存放在与表不同的表空间(TABLESPACE)中。索引记录中存有索引关键字和指向表中数据的指针(地址)。对索引进行的I/O操作比对表进行操作要少很多。

索引一旦被建立就将被Oracle系统自动维护,查询语句中不用指定使用哪个索引,是一种提高查询效率的机制。

创建索引的语法:

create unique index index_name on table (列名);

create index index_emp_name on emp(ename);

四、游标:

游标可以理解为是一个select的结果集合
作用主要是在存储过程里面,用来对取得的结果集进行遍历

declarecursor my_cur is select * from m_user;--定义一个游标,名字叫my_cur,存储t_staff;v_row m_user%rowtype;--定义为行类型begin--把游标遍历出来--打开游标open my_cur;--2循环游标获取值loop--3取出每一行数据fetch my_cur into v_row;--4添加退出条件,避免死循环exit when my_cur%notfound;--5取出信息dbms_output.put_line(v_row.psw||'=='||v_row.sex);end loop;--6关闭游标close my_cur;end;
declarecursor my_cur is select * from m_user;--定义一个游标,名字叫my_cur,存储t_staff;v_row m_user%rowtype;--定义为行类型begin--把游标遍历出for v_row in my_curloopdbms_output.put_line(v_row.psw||'=='||v_row.sex);end loop;end;

五、触发器:

触发器是一种特殊的存储过程,它在发生某种数据库事件时由Oracle系统自动触发。

create or replacetrigger my_triafter insert or update or delete on t_staff--for each rowbegin  if inserting then     insert into t_log values('插入语句',sysdate);  elsif updating then     insert into t_log values('修改语句',sysdate);  elsif deleting then     insert into t_log values('删除语句',sysdate);  end if;      --记录新值和旧值--  dbms_output.put_line('旧salary='||:old.stasalary);--  dbms_output.put_line('新salary='||:new.stasalary); end; 










原创粉丝点击