logical object oracle

view

The role of view:

Let a field returned by the query easier to understand.

b encapsulation complex select statement.

c security. View may be individually authorized. For the huge amount of data tables, views can be created only return to the previous 100 data, open the view select permissions. But do not open the table select permissions.

--grant create view to resource;

--a 让查询返回的字段更容易理解
create or replace view vw_emp
as
select empno 员工号,ename 员工名,job 工作岗位,sal 工资 from emp;

select *from vw_emp;

--b 对复杂的sql语句进行封装
--、薪水大于1200的雇员,按照部门编号进行分组,分组后平均薪水必须大于1500,查询各分组的平均水平,按照工资的倒序进行排列。*
create or replace view vw_avg_sal
as
select deptno,avg(sal) avg_sal from emp 
where sal>1200 group by deptno having avg(sal)>1500 order by avg(sal) desc;

select * from vw_avg_sal;

--c安全性 试图可以单独授权,对于数据量巨大的表,可以创建视图仅仅返回前100条数据,将该视图select权限开放.但是不开放表的select权限。
create or replace view vw_sub_emp
as
select empno,ename,job,mgr,deptno from emp;

grant select on scott.vw_sub_emp to zhangsan;

select *  from scott.vw_sub_emp;

--d将普遍使用的子查询转换成视图,将提高查询效率。

Exercise

--统计各部门员工数
create or replace view vw_countEmpByDept
as
select t.deptno 部门号,dname as 部门名,t.count_emp 员工数 from
(select deptno ,count(empno) count_emp  from emp group by deptno) t
inner join dept on dept.deptno=t.deptno;

select * from vw_countEmpByDept;

--显示员工部门数量最多的部门的详细信息
--第一种
create view vw_max_count
as
select * from dept where dept.deptno=(
select 部门号 from vw_countEmpByDept where 员工数=
(select max(员工数) from vw_countEmpByDept));

select * from vw_max_count;
--第二种
select * from dept inner join  (select deptno 部门号,count(empno) count_emp from emp group by deptno
having count(empno)=(
select max(mycount) from (select count(empno) mycount from emp group by deptno)
)) on dept.deptno=部门号;

--获得平均工资最高部门的详细信息、(把子查询转换成视图,在关联视图中完成)
create view vw_avg_dept
as
select deptno 部门编号,avg(sal) 平均薪资 from emp group by deptno;

select * from dept where deptno=(
select 部门编号 from vw_avg_dept where 平均薪资=
(select max(平均薪资) from vw_avg_dept));

note:

A, oracle view select statement can contain the sort order and the difference between sql server

B, the view is just another expression data, the data itself does not introduce new

C, allowing a view "CRUD", but it is strongly recommended not to view additions and deletions. (I'd rather you do not know at this point, but if the test examination to determine the question to be answer)


index

"Index" --index. Data records from the "Directory" effect, speed up queries. The cost of consumption of hard disk space.

Index is divided into clustered index and non-clustered indexes . The difference is: whether the arrangement order and the order index table records the same.

Example: gathering is in alphabetical dictionary, and the dictionary page as alphabetically. It is non-clustered by radical dictionary. Radicals and the order is inconsistent dictionary page order.

Specific differences:

1. clustered index a table can have only one clustered index instead of a table can be multiple .

2. clustered index record is stored on a physically continuous presence, rather than clustered index is continuous, a continuous physical storage is not logical.

3. The aggregate index query data faster, slower data insertion; non-clustered index on the contrary.

Just like the relationship between arrays and linked lists.

Unique index: (aggregated and non-aggregated unique unique: generally aggregate primary key is unique, the only other non-aggregated. )

Unique index and the unique relationship constraint : the index is a physical structure, as long as a unique index value can not store the same, the only constraint in the logical structure is built on the basis of a unique index. So long as the field of a table t meet the "unique constraint", it will automatically create "unique index." In general the same no difference both usages effect.

Note: A table can contain multiple unique indexes.

create unique index index name on table name (field name) - create an index

alter index index name rebuild; - rebuilding the index

--索引 index,对数据记录其目录作用,加快查询速度,代价是消耗硬盘空间。
create table myuser(
       id number primary key,--主键约束,number 默认为(12,2)
       uname varchar2(20) not null,
       password varchar(20) not null check(length(password)>=6) --检查约束
       )
       
       
       
create unique index uname_idx on myuser(uname);

insert into myuser values(1,'zhangsan','123456');

insert into myuser values(2,'zhangsan','123456');

alter index uname_idx rebuild;
drop index uname_idx;

select * from myuser;

sequence

sequence:

Achieve more than one table's primary key unified management;

Achieve incremental rules are based on the primary key of a string

CREATE SEQUENCE sequence  //创建序列名称

[INCREMENT BY n]  //递增的序列值是n 如果n是正数就递增,如果是负数就递减 默认是1

​       [START WITH n]    //开始的值,递增默认是minvalue 递减是maxvalue

​       [{MAXVALUE n | NOMAXVALUE}] //最大值

​       [{MINVALUE n | NOMINVALUE}] //最小值

​       [{CYCLE | NOCYCLE}] //循环/不循环  即达到最大后从头开始

​       [{CACHE n | NOCACHE}];//分配并存入到内存中
--序列
--实现多张表主键的统一管理
--实现基于字符串主键的递增规则

create sequence my_se --创建序列名称(最简单的序列,第一句就行了,下面的是可选)
increment by 1 --递增的序列值
start with 1 --开始的值
maxvalue 999999999
--minvalue n | nominvalue --最小值
nocycle --循环、不循环 就是到达最大值之后从头开始
cache 20 --分配并存入内存中

-- my_se.currval 当前值
-- my_se.nextval 下一个值

select my_se.nextval from dual;--要先启动序列,必须用nextval
select my_se.currval from dual;

--同一次查询中两次序列取值相同
select my_se.nextval,my_se.nextval from dual;

--序列作用:产生递增的不重复的字段,通常用于生成主键的值。用在给主键赋值
--案例一 实现多张表的主键的统一管理
--表:福建动车系统 、广东动车系统 、江西动车系统
--要求:国家要求所有动车系统的票根符合统一规则,可以采用公开的一个序列对象进行统一编号。
create table FJtrain(
       tid number(10) primary key,
       tname varchar2(20) 
);
create table GDtrain(
       tid number(10) primary key,
       tname varchar2(20) 
);
create table JXtrain(
       tid number(10) primary key,
       tname varchar2(20) 
);

insert into FJtrain values(my_se.nextval,'厦门-福州');
insert into GDtrain values(my_se.nextval,'深圳-广州');
insert into JXtrain values(my_se.nextval,'南昌-赣州');
select * from FJtrain;
select * from GDtrain;
select * from JXtrain;

--案例二 实现基于字符串主键的递增规则
--要求:主键是字符串,如何用序列去做改造

create table stu(
       sno varchar2(10) primary key,
       sname varchar2(20) not null
);


insert into stu values('s'||my_se.nextval,'张老大');

insert into stu values('s'||my_se.nextval,'李老二');

insert into stu values('s'||my_se.nextval,'赵老二');

select * from stu;

Guess you like

Origin www.cnblogs.com/Jotal/p/11359553.html