Oracle syntax (three: view, index, PL/SQL)

1. View

concept:

  1. The view itself saves a query SQL, this SQL can be reused after the view is created;
  2. The view can be regarded as a virtual table, the view itself does not store data, the data is stored in the table;
  3. The same table can have multiple views (in fact, the fields or where conditions of the select statement are different, and the final results displayed are different);
  4. You can block some sensitive data (for example, a field in a table does not want people who use the view to see).

Syntax to create a view:

create or replace view view name
as query statement Check for phrases
with read only Read-only view, can only query but not modify data

Note: permission is required to create a view

-- 创建视图需要权限
grant dba to scott;

-- 复杂的查询操作,以视图的方式保存下来
select e.ename 员工名, d.dname 部门名, s.grade 工资等级, m.ename 上级名字
 from emp e, dept d, salgrade s, emp m 
 where e.deptno = d.deptno and e.sal between s.losal and s.hisal and e.mgr = m.empno
 order by m.ename;
 
 -- 创建一个视图:新建或替换
 create or replace view view_iandboss as 
 select e.ename 员工名, d.dname 部门名, s.grade 工资等级, m.ename 上级名字
 from emp e, dept d, salgrade s, emp m  where e.deptno = d.deptno and e.sal between s.losal and s.hisal and e.mgr = m.empno
 order by m.ename;
 
 -- 使用视图,就可以当成表来直接使用
 select * from view_iandboss;
 
  select * from view_iandboss where 工资等级=3;

Case effect:
Insert picture description here

Note: After the user creates a view, other users cannot use the view (when the user does not authorize other users)

1)	创建用户test,并且给用户授权connect和resource权限
create user test identified by orcl;
grant connect,resource to test;
2)	使用test/orcl登录,能否查询view_emp这个视图
select * from view_emp;  -- 不能查询

3)	使用scott登录,给test分配视图view_emp的查询权限
grant select on view_emp to test;

4)	使用test/orcl登录,查询scott用户下的view_emp
select * from scott.view_emp;  -- 要指定用户名
5)	使用test更新其中一条数据能否更新成功
update scott.view_emp set ename='Rose' where empno=8080;  -- 没有权限

2. Index

As for the concept of index, I will not repeat it here. I will only write about the syntax of index creation.

Syntax for creating a single-column index:

create index index name on table name (column name)

Syntax for creating a composite index:

create index index name on table name (column 1, column 2)

3. PL/SQL

PL/SQL: The Process Language SQL structured query language;
this is a programming language developed by Oracle for Oracle on the basis of SQL . It is a process-oriented language, not object-oriented. Enhance the SQL language.

[declare
    声明变量或游标
]
begin 
    编程代码;
end;

3.1 Four kinds of variable type syntax:
Insert picture description here
3.2 Cursor:
The concept of cursor:

  1. Reference variable: used to save single row and single column data
  2. Record variable: used to save a row of data
  3. Cursor: used to save multiple rows of data, similar to a virtual table

3.2.1 Definition of cursor:

cursor 游标名 is SQL语句

3.2.2 Open and use the cursor

open 游标名;
   loop
      fetch 游标名 into 记录型变量
      exit when 游标名%notfound -- 满足条件退出
      游标处理的代码;
   end loop;
close 游标名;

Case: define cursor: find all employees in department 20, and print out their names, salaries, and positions

declare
	--    声明游标查询部门是20的员工
	cursor cur_emp is
		select * from emp where deptno = 20;
	--    声明记录类型的变量,用来保存游标中每条记录
	v_emp emp%rowtype;
begin
	--  打开游标,创建循环
	open cur_emp;
		loop
            --  抓取游标中的每条记录
            fetch cur_emp into v_emp;
            --  如果游标中没有记录则退出
            exit when cur_emp%notfound;
            --  打印输出姓名,薪水,职务
            dbms_output.put_line('姓名:' || v_emp.ename || ', 薪水:' || v_emp.sal || ',职务:' || v_emp.job); 
    	end loop;
	--  关闭游标
	close cur_emp;
end;

Case effect:
Insert picture description here

Guess you like

Origin blog.csdn.net/RookiexiaoMu_a/article/details/105148520