Oracle summary [views, indexes, transactions, user permissions, batch operations]

foreword

In the first article of Oracle's summary, we have summarized some commonly used SQL-related knowledge points... Then this article mainly summarizes some content about Oracle views, sequences, and transactions ...

In the database, we can divide various SQL statements into four categories...

  • (1) DML (Data Manipulation Language): select, insert, update, delete
  • (2) DDL (Data Definition Language): create table, alter table, drop table, truncate table
  • (3) DCL (Data Control Language): grant select any table to scott/revoke select any table from scott
  • (4) TCL (Transaction Control Language): commit, rollback, savepoint to rollback point

Bulk operations

What is a batch operation, that is, inserting multiple pieces of data at one time.... In SQL, the data we query can be regarded as a table, then when we insert data, we can insert it according to the queried data . ..this can be seen as a batch operation...

It is worth noting that if you do not specify which fields to insert, all the fields queried will be inserted into the table ..


将xxx_emp表中所有20号部门的员工,复制到emp表中,批量插入,insert into 表名 select ...语法
insert into emp
select * 
from xxx_emp
where deptno=20;

Delete operation comparison

We have three delete syntaxes:

  • delete from
  • truncate from
  • drop from

Let's compare their differences:

drop table

  • 1) Belongs to DDL
  • 2) No rollback
  • 3) can not bring where
  • 4) Table content and structure deletion
  • 5) Fast delete speed

truncate table

  • 1) Belongs to DDL
  • 2) No rollback
  • 3) can not bring where
  • 4) Delete table content
  • 5) Fast delete speed

delete from

  • 1) Belongs to DML
  • 2) can be rolled back
  • 3) can bring where
  • 4) The table structure is there, and the content of the table depends on the execution of where
  • 5) The deletion speed is slow and needs to be deleted line by line

affairs

In fact, we have already explained transactions in the JDBC chapter. For details, please check my JDBC blog post.

To be clear again: a transaction is to make an indivisible sub-operation form a whole, and the whole can either execute successfully or fail to execute

It is worth noting that the transaction in Oracle is somewhat different from the transaction operation in Mysql:

Oracle's transaction begins:

  • The first DML operation starts as a transaction [does not need to manually open the transaction]

Oracle's commit transaction

  • (1) Display submission: commit
  • (2) Hidden submission: DDL/DCL/exit (sqlplus tool) [Note]

Oracle's rollback transaction

  • (1) Display rollback: rollback
  • (2) Hidden rollback: close the window (sqlplus tool), crash, power down

Because Oracle has the concept of instance pool, Oracle supports rollback ...

 

write picture description here

 

The isolation level supported by Oracle by default is: read committed

The isolation level supported by Mysql by default is: reapatable read


Oracle中设置事务隔离级别为serializable
set transaction isolation level serializable;

Access objects of other users

As mentioned in the previous blog post, Oracle treats tables/users as objects... So how do we access the tables under the hr user under the scott user? ? ?

In fact, we only need to specify a specific user and database table when accessing the table, but we also need to see if the user has permission to query the data table of other users, so we need to give permission ...

声明:scott或hr叫用户名/方案名/空间名
      scott--tiger
      hr-----lion
      
查询当前用户是谁
show user;

查询scott自己表空间下的所有对象时,可加,或不加用户名select * from emp;
select * from emp;
select * from scott.emp;

以sysdba身份解锁hr普通帐户
alter user hr account unlock;

以sysdba身份设置hr普通帐户的密码
alter user hr identified by lion;

当scott查询hr表空间下的所有表时,必须得加用户名
select * from hr.jobs;

在默认情况下,每个用户只能查询自已空间下的对象的权限,不能查询其它用户空间下的对象

以sysdba身份角色,授予scott用户查询所有用户空间下的对象权限
grant select any table to scott;

以sysdba身份,撤销scott用户查询所有用户空间下的对象权限
revoke select any table from scott;

scott自已查看自己所拥有的权限
select * from user_sys_privs;

从scott用户空间导航到sysdba用户空间
conn / as sysdba;

从sysdba用户空间导航到scott用户空间
conn scott/tiger;

从scott用户空间导航到hr用户空间
conn hr/lion;

查询hr用户空间中的所有对象
select * from tab;

从hr用户空间导航到scott用户空间
conn scott/tiger;

在scott用户空间下,查询hr用户空间下的jobs表,必须加上hr用户空间名
select * from hr.jobs;


view

A view is a virtual table based on a data table

  • (1) A view is a virtual table
  • (2) Views are built on the basis of existing tables, and these tables on which views are built are called base tables
  • (3) The statement that provides data content to the view is a SELECT statement, and the view can be understood as a stored SELECT statement
  • (4) Views provide users with another representation of base table data
  • (5) The view does not store real data, the real data is still stored in the base table
  • (6) Although the programmer operates the view, the final view will also be converted into the operation base table
  • (7) A base table can have 0 or more views

Why use views?

Sometimes, we may only relate to some fields in a data table, while others only relate to some fields of the same data table...

Then it is unreasonable to show them all the fields. We should do it: we give them what kind of data they want to see ... On the one hand, we can let them only pay attention to their own data, on the other hand, we also guarantee some confidential data in the data table won't leak out...

 

write picture description here

 

There is another reason:

When we query data, we often need to write very long SQL statements, which are very long and long almost every time.... As mentioned above, a view is a virtual table based on a query, that is to say, a view The queried data can be encapsulated . . . Then it will be very convenient when we use it...

Small summary:

  • (1) If you don't want the user to see all the data (fields, records), but only want the user to see some data, you can use the view at this time
  • (2) When you need to reduce the writing of SQL query statements, you can use views, but it does not improve query efficiency


基于emp表所有列,创建视图emp_view_1,create view 视图名 as select对一张或多张基表的查询
create view emp_view_1
as
select * from emp;

默认情况下,普通用户无权创建视图,得让sysdba为你分配creare view的权限 

以sysdba身份,授权scott用户create view权限
grant create view to scott;

以sysdba身份,撤销scott用户create view权限
revoke create view from scott;

基于emp表指定列,创建视图emp_view_2,该视图包含编号/姓名/工资/年薪/年收入(查询中使用列别名)
create view emp_view_2
as
select empno "编号",ename "姓名",sal "工资",sal*12 "年薪",sal*12+NVL(comm,0) "年收入"
from emp;

基于emp表指定列,创建视图emp_view_3(a,b,c,d,e),包含编号/姓名/工资/年薪/年收入(视图中使用列名)
create view emp_view_3(a,b,c,d,e)
as
select empno "编号",ename "姓名",sal "工资",sal*12 "年薪",sal*12+NVL(comm,0) "年收入"
from emp;

查询emp_view_3创建视图的结构
desc emp_view_3;

修改emp_view_3(id,name,salary,annual,income)视图,create or replace view 视图名 as 子查询
create or replace view emp_view_3(id,name,salary,annual,income)
as
select empno "编号",ename "姓名",sal "工资",sal*12 "年薪",sal*12+NVL(comm,0) "年收入"
from emp;

查询emp表,求出各部门的最低工资,最高工资,平均工资
select min(sal),max(sal),round(avg(sal),0),deptno
from emp
group by deptno;

创建视图emp_view_4,视图中包含各部门的最低工资,最高工资,平均工资
create or replace view emp_view_4
as
select deptno "部门号",min(sal) "最低工资",max(sal) "最高工资",round(avg(sal),0) "平均工资"
from emp
group by deptno;

创建视图emp_view_5,视图中包含员工编号,姓名,工资,部门名,工资等级
create or replace view emp_view_5
as
select e.empno "编号",e.ename "姓名",e.sal "工资",d.dname "部门名",s.grade "工资等级"
from emp e,dept d,salgrade s
where (e.deptno=d.deptno) and (e.sal between s.losal and s.hisal);

删除视图emp_view_1中的7788号员工的记录,使用delete操作,会影响基表吗
delete from emp_view_1 where empno=7788;写法正确,会影响基表

修改emp_view_1为只读视图【with read only】,再执行上述delete操作,还行吗?
create or replace view emp_view_1
as
select * from emp
with read only;
不能进行delete操作了

删除视图中的【某条】记录会影响基表吗?
会影响基表

将【整个】视图删除,会影响表吗?
不会影响基表

删除视图,会进入回收站吗?
不会进入回收站

删除基表会影响视图吗?
会影响视图

闪回基表后,视图有影响吗?
视图又可以正常工作了

synonym

When we use multi-table query, or the meaning of the query table fields is unclear, we use aliases instead.... Of course, aliases are only for column or table names

Now, the objects we already know are users/views/tables and other objects, and Oracle also provides synonyms [similar to aliases] for us to use

The role of synonyms

  • (1) Shorten the length of the object name
  • (2) Objects that facilitate access to other users
创建与salgrade表对应的同义词,create synonym 同义词 for 表名/视图/其它对象
create synonym e for salgrade;
create synonym ev5 for emp_view_5;

以sys身份授予scott普通用户create synonym权限
grant create synonym to scott;

以sys身份从scott普通用户撤销create synonym权限
revoke create synonym from scott;

使用同义词操作salgrade表
select * from s;

删除同义词
drop synonym ev5;

删除同义词,会影响基表吗?
不会影响基表

删除基表,会影响同义词吗?
会影响同义词


sequence

The automatic growth of Mysql can be directly followed by the auto increase keyword after the field when the table is created. Does Oracle have an automatic growth strategy? ? ?

Oracle uses an object like a sequence ....

  • (1) Similar to the auto_increment automatic growth mechanism in MySQL, but there is no auto_increment mechanism in Oracle
  • (2) is a mechanism provided by oracle to generate unique numeric values
  • (3) Usually used for the main key value of the table
  • (4) The sequence can only be guaranteed to be unique, not continuous
    •  声明:**oracle中,只有rownum永远保持从1开始,且继续**
      
  • (5) Sequence value, which can be stored in memory and taken faster

What is the difference between the sequence in oralce and the automatic growth in Mysql? ? ?

  • Mysql maintains an automatic growth program for each table...
  • Oralce will store the sequence in memory, which can be used by several tables...

 

write picture description here

 

Some students may question, we use a pseudo column such as rownum when paging, why not use it for automatic growth? ? ?

Although the value of rownum is unique and continuous, it cannot always uniquely identify the record ... That is, once the record is deleted, the value of rownum will change .

Why use sequences

  • (1) In the past, we set the value for the main key, which requires manual setting, which is prone to errors
  • (2) The main key value of each table in the past was independent and could not be shared

为emp表的empno字段,创建序列emp_empno_seq,create sequence 序列名
create sequence emp_empno_seq;

删除序列emp_empno_seq,drop sequence 序列名
drop sequence emp_empno_seq;

查询emp_empno_seq序列的当前值currval和下一个值nextval,第一次使用序列时,必须选用:序列名.nextval
select emp_empno_seq.nextval from dual;
select emp_empno_seq.currval from dual;

使用序列,向emp表插入记录,empno字段使用序列值
insert into emp(empno) values(emp_empno_seq.nextval);
insert into emp(empno) values(emp_empno_seq.nextval);
insert into emp(empno) values(emp_empno_seq.nextval);

修改emp_empno_seq序列的increment by属性为20,默认start with是1,alter sequence 序列名
alter sequence emp_empno_seq
increment by 20;

修改修改emp_empno_seq序列的的increment by属性为5
alter sequence emp_empno_seq
increment by 5;

修改emp_empno_seq序列的start with属性,行吗
alter sequence emp_empno_seq
start with 100;

有了序列后,还能为主健手工设置值吗?
insert into emp(empno) values(9999);
insert into emp(empno) values(7900);

删除表,会影响序列吗?
你无法做insert操作,表真正亡,序列亡

删除序列,会影响表吗?

不会


在hibernate中,如果是访问oracle数据库服务器,那么User.hbm.xml映射文件中关于<id>标签如何配置呢?
<id name="id" column="id">
   <generator class="increment/identity/uuid/【sequence】/【native】"/>
</id>

index

what is an index

What is an index [Index]

(1) It is a mechanism for quickly querying the content in the table , similar to the directory of the Xinhua dictionary (2) It is applied to some/some fields in the table, but when stored, it is independent of the table

Why use an index

Why use an index

  • (1) Accelerate the query speed of the Oracle server through the pointer
  • (2) The method of quickly locating data by rowid reduces disk I/O
    •  rowid是oracle中唯一确定每张表不同记录的唯一身份证
      

The index table turns the data into order....

write picture description here

 

Quickly locate data files in the hard disk...

 

write picture description here

 

rowid features

Features of rowid

  • (1) Located in each table, but not visible on the surface, for example: desc emp is invisible
  • (2) Only in the select, the rowid is displayed and written, so that it can be seen
  • (3) It is bound to each table, the table is dead, the rowid of the table is dead, the rownum of the two tables can be the same, but the rowid must be unique
  • (4) rowid is a mixture of 18 uppercase and lowercase numbers, which uniquely represents the position of the record in the DBF file
  • (5) When rowid can participate in =/like comparison, use '' single quotes to wrap the value of rowid, and it is case-sensitive
  • (6) rowid is the bridge between the contact table and the DBF file

Index Features

Index Features

  • (1) Once the index is established, the Oracle management system will automatically maintain it, and the Oracle management system decides when to use the index
  • (2) The user does not need to specify which index to use in the query statement
  • (3) After defining the primary key or unique constraint, the system automatically creates an index on the corresponding column
  • (4) Users can also add indexes to the specified single field or multiple fields according to their own needs

It should be noted that Oracle automatically manages indexes for us, and if we specify a primary key or unique constraint, the system will automatically create an index on the corresponding column ..

When [to] create an index

  • (1) The table often performs SELECT operations
  • (2) The table is very large (there are too many records), and the content of the records is widely distributed
  • (3) Column names often appear in WHERE clauses or join conditions

When to [DO NOT] create an index

  • (1) The table often performs INSERT/UPDATE/DELETE operations
  • (2) The table is very small (very few records)
  • (3) Column names do not often appear as join conditions or in WHERE clauses



为emp表的empno单个字段,创建索引emp_empno_idx,叫单列索引,create index 索引名 on 表名(字段,...)
create index emp_empno_idx
on emp(empno);

为emp表的ename,job多个字段,创建索引emp_ename_job_idx,多列索引/联合索引
create index emp_ename_job 
on emp(ename,job);
如果在where中只出现job不使用索引
如果在where中只出现ename使用索引
我们提倡同时出现ename和job

注意:索引创建后,只有查询表有关,和其它(insert/update/delete)无关,解决速度问题

删除emp_empno_idx和emp_ename_job_idx索引,drop index 索引名
drop index emp_empno_idx;
drop index emp_ename_job_idx;


Permissions and Users

 

write picture description here

 



一)用户
Oracle中的用户分为二大类
1)Oracle数据库服务器创建时,由系统自动创建的用户,叫系统用户,如sys。
2)利用系统用户创建的用户,叫普通用户,如scott,hr,c##tiger,zhaojun,...

》用sys登录,查询当前Oracle数据库服务器中已有用户的名字和状态
  username表示登录名
  expired&locked表示帐号过期和锁定
  open表示帐号现在可用
  sqlplus / as sysdba;
  col username for a30;
  col account_status for a30;
  set pagesize 100;
  select username,account_status from dba_users;
  
  查询Oracle中有哪些用户
  select * from all_users;



二)创建与删除普通用户
可以在Oracle中创建新的普通用户,创建普通用户命令是:create user,在创建普通用户的同时,应该为其分配一个具体的表空间,通常叫users。

》用sys登录,查询Oracle中有哪些可用存储空间,所有普通用户默认为users存储空间
  select * from v$tablespace;

》用sys登录,创建普通用户c##tiger,密码为abc,默认使用users存储空间,即对应硬盘上的一个DBF二进制文件
  sqlplus / as sysdba;
  create user c##tiger identified by abc default tablespace users;

》用sys登录,为c##tiger分配users空间无限制使用,即数据库中DBF文件可以无限增加,一个DBF文件不够,会创建第二个DBF文件
  sqlplus / as sysdba;
  alter user c##tiger quota unlimited on users;

》用c##tiger登录,能进orcl数据库吗?
  sqlplus c##tiger/abc
  进不去orcl数据库

》用sys登录,删除普通用户c##tiger
  sqlplus / as sysdba;
  drop user c##tiger cascade;



三)了解系统用户
sys是Oracle中一个重要的系统用户,sys是Oracle中最高权限用户,其角色为SYSDBA(系统管理员)
sqlplus / as sysdba



四)权限
权限的最终作用于用户。即所有用户在数据库内的操作对象和可执行的动作都是受到限制的。
Oracle中权限分为二大类:
1)系统权限
2)对象权限



五)系统权限
针对数据库中特定操作的许可,例如:让c##tiger能登录到orcl数据库,能在orcl数据库中创建表

》用sys登录,获取系统权限的相关信息,例如:select any table表示针对所有表的select权限
  sqlplus / as sysdba;
  select distinct privilege from dba_sys_privs;

》用sys登录,为c##tiger分配create session与数据库建立会话的权限,即允许该用户登录
  sqlplus / as sysdba;
  grant create session to c##tiger;

》用c##tiger登录,能进orcl数据库吗?
  sqlplus c##tiger/abc
  能进去orcl数据库

》用c##tiger登录,创建一张tiger的表,能创建吗?
  sqlplus c##tiger/abc
  create table tiger(
    name varchar2(20)
  );
  这时c##tiger没有权限创建表

》用sys登录,为c##tiger分配create table权限,即允许创建表
  sqlplus / as sysdba;
  grant create table to c##tiger;

》用c##tiger登录,创建一张tiger的表,能创建吗?
  sqlplus c##tiger/abc
  create table tiger(
    name varchar2(20)
  );
  可以创建c##tiger表

》用sys登录,查询c##tiger所拥有的系统权限
  sqlplus / as sysdba;
  select grantee,privilege from dba_sys_privs where lower(grantee) = 'c##tiger';
  grantee表示普通用户名
  privilege权限名  

》用sys登录,撤销c##tiger的create table权限
  sqlplus / as sysdba;
  revoke create table from c##tiger;



六)对象权限
用户对已有对象的操作权限,包括:
1)select可用于表,视图和序列
2)insert向表或视图中插入新的记录
3)update更新表中数据
4)delete删除表中数据
5)execute函数,过程的执行
6)index为表创建索引
7)references为表创建外健
8)alter修改表或者序列的属性

》用sys登录,查询c##tiger所拥有的对象权限
  sqlplus / as sysdba;
  col grantee for a10;
  col table_name for a10;
  col privilege for a20;
  select grantee,table_name,privilege from dba_tab_privs where lower(grantee) = 'c##tiger';

》用sys登录,为c##tiger分配对tiger表的所有权限,即增删改查操作
  sqlplus / as sysdba;
  grant all on c##tiger.tiger to c##tiger;
  注意:c##tiger表示空间名
        tiger表示该空间下的表名
  C##TIGER   TIGER      FLASHBACK
  C##TIGER   TIGER      DEBUG
  C##TIGER   TIGER      QUERY REWRITE
  C##TIGER   TIGER      ON COMMIT REFRESH
  C##TIGER   TIGER      REFERENCES
  C##TIGER   TIGER      UPDATE
  C##TIGER   TIGER      SELECT
  C##TIGER   TIGER      INSERT
  C##TIGER   TIGER      INDEX
  C##TIGER   TIGER      DELETE
  C##TIGER   TIGER      ALTER

》用c##tiger登录,对tiger表进行增删改查操作
  sqlplus c##tiger/abc;
  insert into tiger(name) values('AA');
  update tiger set name = 'BB';
  delete from tiger where rownum = 1;
  select * from tiger;


If there are any mistakes in the article, please correct me, and we can communicate with each other. Students who are accustomed to reading technical articles on WeChat and want to get more Java resources can follow WeChat public account: Java3y

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325312571&siteId=291194637