oracle中schema指的是什麼?

一、Oracle schema 概念

Users not only access data in a database, but they own the objects thatsql

contain the data. The set of objects owned by a user is its schema. 擁有一個Oracle數據庫對象的用戶,就有schema。數據庫

Not all users own objects so schemas may be empty.session

Other users can access or execute objects within a user’s schema once theapp

schema owner grants privileges. ide

It is common practice to have one user own all of an application’s objects (tables, indexes, views, and so on) and then provide access to those objects to all the application users within the database.this

This is done via database grants, roles, and synonyms.spa

For example, assume you have the ACME application. You’d create a user called.net

ACME_OWN and create all objects as ACME_OWN. Then you’d create a databasecode

role called ACME_USER and grant SELECT, UPDATE, EXECUTE for the objects對象

in ACME_OWN’s schema to that role. Application users would be granted the

ACME_USER role so they could access the ACME_OWN’s objects. This way one

user owns the objects, but the actual database or application users access the

data. This separation improves both security and manageability.

《Oracle 11g for dummies》 p136

下面內容摘自 csdn 博文 http://blog.csdn.net/KimSoft/article/details/4627520

看來有的人仍是對schema的真正含義不太理解,如今我再次整理了一下,但願對你們有所幫助。

咱們先來看一下他們的定義:
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.
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爲數據庫對象的集合,爲了區分各個集合,咱們須要給這個集合起個名字,這些名字就是咱們在企業管理器的方案下看到的許多相似用戶名的節點,這些相似用戶名的節點其實就是一個schema,schema裏面包含了各類對象如tables, views, sequences, stored procedures, synonyms, indexes, clusters, and database links。

一個用戶通常對應一個schema,該用戶的schema名等於用戶名,並做爲該用戶缺省schema。這也就是咱們在企業管理器的方案下看到schema名都爲數據庫用戶名的緣由。Oracle數據庫中不能新建立一個schema,要想建立一個schema,只能經過建立一個用戶的方法解決(Oracle中雖然有create schema語句,可是它並非用來建立一個schema的),在建立一個用戶的同時爲這個用戶建立一個與用戶名同名的schem並做爲該用戶的缺省shcema。即schema的個數同user的個數相同,並且schema名字同user名字一一 對應而且相同,全部咱們能夠稱schema爲user的別名,雖然這樣說並不許確,可是更容易理解一些。

一個用戶有一個缺省的schema,其schema名就等於用戶名,固然一個用戶還可使用其餘的schema。若是咱們訪問一個表時,沒有指明該表屬於哪個schema中的,系統就會自動給咱們在表上加上缺省的sheman名。好比咱們在訪問數據庫時,訪問scott用戶下的emp表,經過select * from emp; 其實,這sql語句的完整寫法爲select * from scott.emp。在數據庫中一個對象的完整名稱爲schema.object,而不屬user.object。相似若是咱們在建立對象時不指定該對象的schema,在該對象的schema爲用戶的缺省schema。這就像一個用戶有一個缺省的表空間,可是該用戶還可使用其餘的表空間,若是咱們在建立對象時不指定表空間,則對象存儲在缺省表空間中,要想讓對象存儲在其餘表空間中,咱們須要在建立對象時指定該對象的表空間。

咳,說了這麼多,給你們舉個例子,不然,一切枯燥無味!
SQL> Gruant dba to scott

SQL> create table test(name char(10));
Table created.

SQL> create table system.test(name char(10));
Table created.

SQL> insert into test values('scott'); 
1 row created.

SQL> insert into system.test values('system');
1 row created.

SQL> commit;
Commit complete.

SQL> conn system/manager
Connected.

SQL> select * from test;
NAME
----------
system

SQL> ALTER SESSION SET CURRENT_SCHEMA = scott; --改變用戶缺省schema名
Session altered.

SQL> select * from test;
NAME
----------
scott

SQL> select owner ,table_name from dba_tables where table_name=upper('test');
OWNER TABLE_NAME
------------------------------ ------------------------------
SCOTT TEST
SYSTEM TEST
--上面這個查詢就是我說將schema做爲user的別名的依據。實際上在使用上,shcema與user徹底同樣,沒有什麼區別,在出現schema名的地方也能夠出現user名。


1.1 Oracle 建立用戶

CREATE USER <USERNAME>
IDENTIFIED BY 「<PASSWORD>」 ###密碼用「」
TEMPORARY TABLESPACE <TEMPORARY TABLESPACE>
DEFAULT TABLESPACE <DEFAULT TABLSPAC>;


In the following steps you create a user with SQL*Plus and grant the necessary

roles and privileges to connect to the database:

1. In SQL*Plus, type the following to create a user:

SYS@dev11g> create user acme_own
2 identified by 「acme_own2008!!」
3 temporary tablespace temp
4 default tablespace users;

User created.

In this example, the user is schema owner ACME_OWN. The default

tablespace is defined as USERS, although the TABLESPACE storage

clause is expected to specify ACME_DATA when objects are created


2. Grant the user CONNECT and RESOURCE roles so that the user can log

in to the database and create objects:

SYS@dev11g> grant connect to acme_own;
Grant succeeded.
SYS@dev11g> grant resource to acme_own;

Grant succeeded.

3. Create a new role:

SYS@dev11g> create role acme_user;
Role created.
SYS@dev11g> grant create session to acme_user;

Grant succeeded.

相關文章
相關標籤/搜索