Oracle Schema

时间:2022-04-24
本文章向大家介绍Oracle Schema,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1、这是Schema的definition:

A schema is a collection of database objects (used by a user.)  Schema objects are the logical structures that directly refer to the database’s data. A user is a name defined in the database that can connect to and access objects. Schemas and users help database administrators manage database security.

Schema是数据库对象(Not 数据库)的一个集合,为了区分各个集合,我们需要给集合取个名字。方便管理数据库对象。

我们在登录PL/SQL Development开发工具后会看到

展开红框

左图这些类似用户名的节点就是Schema,一个Schema对象包括了

左图都是属于schema的对象

2、Schema和Oracle 用户的关系

Note:This statement does not actually create a schema. Oracle Database automatically creates a schema when you create a user (see CREATE USER). 

大致的意思是:Schema是当你创建一个Oracle用户时,Oracle会自动给你创建一个Schema。

一个用户有一个默认的Schema,该用户的Schema名就等于用户名,Oracle数据库不能新建一个Schema,要想新创建一个Schema,只能通过新建一个用户的方式,Oracle 中虽然有create schema的语句,但是他不是用来创建Schema的,下面是文档对Create Schema的作用的解释

use the create schema statement to create multiple tables and views and perform multiple grants in your own schema in a single transaction. To execute a CREATE SCHEMA statement, Oracle Database executes each included statement. If all statements execute successfully, then the database commits the transaction. If any statement results in an error, then the database rolls back all the statements.

大致意思是:在单个事务中创建多个表和视图,并在自己的schema中执行对这些表和视图的授权。后面的则是说明执行create schema语句,遵守事务的原则,如果其中的一条语句执行失败,那么整个操作会回滚。

3、使用Schema

(1)、当我们访问一个表时,如果没有在表前面加上Schema名(也就是用户名),那么Oracle就会给我们加上默认的Schema名(也就是当前登录的用户名),比如我们在访问数据库时,访问scott用户下的emp表,通过

select * from emp;

其实,这sql语句的完整写法为

select * from scott.emp

(2)、在Oracle中表示一个数据库对象应该是Schema.object  而不是user.object 虽然在代码上是一样的,但是意义上是不一样的

(3)、创建数据库对象(不是创建数据库,而是创建table之类的数据库对象)时,需要注意的是:如果我们在创建数据库对象时,不指定Schema,那么Oracle就会使用默认的Schema(即当前用户的用户名)