Oracle 新创建的 DB 充满了数据表

Fre*_*kyB 1 oracle oracle-19c

我刚刚使用 oracle 数据库配置助手创建了我的第一个数据库。

在此处输入图片说明

基本上没什么特别的,数据库创建成功。我不明白的是为什么数据库中已经有这么多表?如何在oracle中创建空数据库?

在此处输入图片说明

Bja*_*ndt 5

只是给你一个比喻。当您安装 Windows 操作系统或 Linux 操作系统时,您是否希望文件系统中的文件为零?这同样适用于 Oracle 数据库。创建 Oracle 数据库后,将有大量对象(表、函数、过程、索引、包等)构成您的 Oracle 数据库。所有这些对象都归 SYS,SYSTEM,.. (还有更多) 所有。

当您使用数据库的不同功能时,这些所有者和对象就会发挥作用。请记住,Oracle 数据库已经开发了 40 多年,并且具有大量特性!

现在您要创建一个空数据库。让我们再次应用操作系统类比。您将创建一个新用户... /home/myuser。这也正是您在 Oracle 数据库中所做的。

Oracle 数据库可以处理数千个用户和模式。(模式 = 对象的所有者,用户 = 操作已创建的对象(DML - 选择、插入、更新、删除))。

-- *nix (example: export TWO_TASK=localhost:1521/SERVICE_NAME)
export TWO_TASK=MY_DB_SERVICE
-- Windows
set LOCAL=MY_DB_SERVICE

sqlplus system/password

-- this user is the schema. The owner of your objects.
create user myschema identified by &password
default tablespace USERS
temporary tablespace TEMP
quota unlimited on USERS;     

-- grant privs to the schema
grant create session, resource to myschema;

-- yes, we need to take security into account.
create role myschema_ro;
create role myschema_rw;

-- lets login as myschema
sqlplus myschema/password

-- your database or schema is empty, now create all objects...
create table t (x int);

-- grant access to object
grant select on t to myschema_rw; 

-- we are done with the creation of the schema  

sqlplus system/password

-- no one is going to login as schema owner. 
-- you only unlock this account when you do schema changes
alter user myschema account lock;

-- time to create your app-user-account who is going to access 
-- the database/schema
create user myapp identified by &password;

-- grant privileges for your app-user
grant create session, myschema_rw to myapp;

-- login
sqlplus myapp/password

-- you will see your schema objects are listed
select object_name from all_objects;

-- now, if you want to avoid schema-prefixing (select * from myschema.t) there are some options
-- the first thing you do in your app
alter session set current_schema = MYSCHEMA;

-- now you can do: t is owned by myschema
select * from t;

-- you can create a trigger (log in as myschema)
create or replace trigger after_logon_trg
after logon on myschema.schema
begin
  execute immediate 'alter session set current_schema = MYSCHEMA';
end;
/
Run Code Online (Sandbox Code Playgroud)

您现在已准备好在您的对象受到保护的 Oracle 数据库服务器(实例)上创建一个空数据库(= Oracle 术语中的架构)。

澄清一下:数据库是磁盘上的一组文件,具有一个或多个模式(除了 sys/system),实例是一个正在运行的系统,它由磁盘的文件加上定义工作的内存结构组成一个软件。

祝你好运!