简体   繁体   English

如何在 Oracle Database 11g 中创建新模式/新用户?

[英]How to create a new schema/new user in Oracle Database 11g?

I have applied for an internship in a company and as a question they have asked me to create a schema for their company with certain requirements and mail them the DDL file.我申请了一家公司的实习机会,作为一个问题,他们要求我为他们的公司创建一个具有特定要求的架构,并将DDL文件邮寄给他们。 I have installed Oracle database 11g Express edition, but how do I create a new schema in Oracle database 11g?我已经安装了 Oracle 数据库 11g 快捷版,但如何在 Oracle 数据库 11g 中创建新模式? I have searched in the net for a solution but I could not understand what to do.我在网上搜索了解决方案,但我不明白该怎么做。 And after creating a schema, which file should I mail them?创建模式后,我应该邮寄哪个文件?

Generally speaking a schema in oracle is the same as an user.一般而言,oracle 中的模式与用户相同。 Oracle Database automatically creates a schema when you create a user.创建用户时,Oracle 数据库会自动创建模式。 A file with the DDL file extension is an SQL Data Definition Language file.具有 DDL 文件扩展名的文件是 SQL 数据定义语言文件。

Creating new user (using SQL Plus)创建新用户(使用 SQL Plus)

Basic SQL Plus commands:基本 SQL Plus 命令:

  - connect: connects to a database
  - disconnect: logs off but does not exit
  - exit: exists

Open SQL Plus and log:打开 SQL Plus 并记录:

/ as sysdba

The sysdba is a role and is like "root" on unix or "Administrator" on Windows. sysdba 是一个角色,就像 unix 上的“root”或 Windows 上的“管理员”。 It sees all, can do all.它什么都看,什么都能做。 Internally, if you connect as sysdba, your schema name will appear to be SYS.在内部,如果您以 sysdba 身份连接,您的架构名称将显示为 SYS。

Create an user:创建用户:

SQL> create user johny identified by 1234;

View all users and check if the user johny is there:查看所有用户并检查用户 johny 是否存在:

SQL> select username from dba_users;

If you try to login as johny now you would get an error:如果您现在尝试以 johny 身份登录,则会出现错误:

ERROR:
ORA-01045: user JOHNY lacks CREATE SESSION privilege; logon denied

The user to login needs at least create session priviledge so we have to grant this privileges to the user:要登录的用户至少需要创建会话权限,因此我们必须将此权限授予用户:

SQL> grant create session to johny;

Now you are able to connect as the user johny:现在您可以以用户 johny 的身份进行连接:

username: johny
password: 1234

To get rid of the user you can drop it:要摆脱用户,您可以删除它:

SQL> drop user johny;

That was basic example to show how to create an user.这是展示如何创建用户的基本示例。 It might be more complex.它可能更复杂。 Above we created an user whose objects are stored in the database default tablespace.上面我们创建了一个用户,其对象存储在数据库默认表空间中。 To have database tidy we should place users objects to his own space (tablespace is an allocation of space in the database that can contain schema objects).为了使数据库整洁,我们应该将用户对象放在他自己的空间(表空间是数据库中可以包含模式对象的空间分配)。

Show already created tablespaces:显示已经创建的表空间:

SQL> select tablespace_name from dba_tablespaces;

Create tablespace:创建表空间:

SQL> create tablespace johny_tabspace
  2  datafile 'johny_tabspace.dat'
  3  size 10M autoextend on;

Create temporary tablespace (Temporaty tablespace is an allocation of space in the database that can contain transient data that persists only for the duration of a session. This transient data cannot be recovered after process or instance failure.):创建临时表空间(临时表空间是数据库中的空间分配,可以包含仅在会话期间持续存在的瞬态数据。在进程或实例失败后无法恢复此瞬态数据。):

SQL> create temporary tablespace johny_tabspace_temp
  2  tempfile 'johny_tabspace_temp.dat'
  3  size 5M autoextend on;

Create the user:创建用户:

SQL> create user johny
  2  identified by 1234
  3  default tablespace johny_tabspace
  4  temporary tablespace johny_tabspace_temp;

Grant some privileges:授予一些特权:

SQL> grant create session to johny;
SQL> grant create table to johny;
SQL> grant unlimited tablespace to johny;

Login as johny and check what privileges he has:以 johny 身份登录并检查他拥有哪些权限:

SQL> select * from session_privs;

PRIVILEGE
----------------------------------------
CREATE SESSION
UNLIMITED TABLESPACE
CREATE TABLE

With create table privilege the user can create tables:使用创建表权限,用户可以创建表:

SQL> create table johny_table
  2  (
  3     id int not null,
  4     text varchar2(1000),
  5     primary key (id)
  6  );

Insert data:插入数据:

SQL> insert into johny_table (id, text)
  2  values (1, 'This is some text.');

Select:选择:

SQL> select * from johny_table;

ID  TEXT
--------------------------
1   This is some text.

To get DDL data you can use DBMS_METADATA package that "provides a way for you to retrieve metadata from the database dictionary as XML or creation DDL and to submit the XML to re-create the object.".要获取 DDL 数据,您可以使用 DBMS_METADATA 包,该包“为您提供了一种从数据库字典中检索元数据作为 XML 或创建 DDL 并提交 XML 以重新创建对象的方法。”。 (with help from http://www.dba-oracle.com/oracle_tips_dbms_metadata.htm ) (在http://www.dba-oracle.com/oracle_tips_dbms_metadata.htm 的帮助下)

For table:对于表:

SQL> set pagesize 0
SQL> set long 90000
SQL> set feedback off
SQL> set echo off
SQL> SELECT DBMS_METADATA.GET_DDL('TABLE',u.table_name) FROM USER_TABLES u;

Result:结果:

  CREATE TABLE "JOHNY"."JOHNY_TABLE"
   (    "ID" NUMBER(*,0) NOT NULL ENABLE,
        "TEXT" VARCHAR2(1000),
         PRIMARY KEY ("ID")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DE
FAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "JOHNY_TABSPACE"  ENABLE
   ) SEGMENT CREATION IMMEDIATE
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DE
FAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "JOHNY_TABSPACE"

For index:对于索引:

SQL> set pagesize 0
SQL> set long 90000
SQL> set feedback off
SQL> set echo off
SQL> SELECT DBMS_METADATA.GET_DDL('INDEX',u.index_name) FROM USER_INDEXES u;

Result:结果:

  CREATE UNIQUE INDEX "JOHNY"."SYS_C0013353" ON "JOHNY"."JOHNY_TABLE" ("ID")
  PCTFREE 10 INITRANS 2 MAXTRANS 255
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DE
FAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "JOHNY_TABSPACE"

More information:更多信息:

DDL数据线

DBMS_METADATA数据库管理系统_元数据

Schema objects模式对象

Differences between schema and user架构和用户之间的差异

Privileges特权

Creating user/schema创建用户/模式

Creating tablespace创建表空间

SQL Plus commands SQL Plus 命令

It's a working example:这是一个工作示例:

CREATE USER auto_exchange IDENTIFIED BY 123456;
GRANT RESOURCE TO auto_exchange;
GRANT CONNECT TO auto_exchange;
GRANT CREATE VIEW TO auto_exchange;
GRANT CREATE SESSION TO auto_exchange;
GRANT UNLIMITED TABLESPACE TO auto_exchange;

Let's get you started.让我们开始吧。 Do you have any knowledge in Oracle?你对Oracle有什么了解吗?

First you need to understand what a SCHEMA is.首先,您需要了解什么是 SCHEMA。 A schema is a collection of logical structures of data, or schema objects.模式是数据或模式对象的逻辑结构的集合。 A schema is owned by a database user and has the same name as that user.模式归数据库用户所有,并与该用户同名。 Each user owns a single schema.每个用户都拥有一个架构。 Schema objects can be created and manipulated with SQL.可以使用 SQL 创建和操作模式对象。

  1. CREATE USER acoder;创建用户编码器; -- whenever you create a new user in Oracle, a schema with the same name as the username is created where all his objects are stored. -- 每当您在 Oracle 中创建新用户时,都会创建一个与用户名同名的模式,其中存储他的所有对象。
  2. GRANT CREATE SESSION TO acoder;授予 acoder 创建会话; -- Failure to do this you cannot do anything. -- 不这样做你就什么也做不了。

To access another user's schema, you need to be granted privileges on specific object on that schema or optionally have SYSDBA role assigned.要访问另一个用户的模式,您需要被授予对该模式上特定对象的权限,或者可以选择分配 SYSDBA 角色。

That should get you started.这应该让你开始。

SQL> select Username from dba_users
  2  ;

USERNAME
------------------------------
SYS
SYSTEM
ANONYMOUS
APEX_PUBLIC_USER
FLOWS_FILES
APEX_040000
OUTLN
DIP
ORACLE_OCM
XS$NULL
MDSYS

USERNAME
------------------------------
CTXSYS
DBSNMP
XDB
APPQOSSYS
HR

16 rows selected.

SQL> create user testdb identified by password;

User created.

SQL> select username from dba_users;

USERNAME
------------------------------
TESTDB
SYS
SYSTEM
ANONYMOUS
APEX_PUBLIC_USER
FLOWS_FILES
APEX_040000
OUTLN
DIP
ORACLE_OCM
XS$NULL

USERNAME
------------------------------
MDSYS
CTXSYS
DBSNMP
XDB
APPQOSSYS
HR

17 rows selected.

SQL> grant create session to testdb;

Grant succeeded.

SQL> create tablespace testdb_tablespace
  2  datafile 'testdb_tabspace.dat'
  3  size 10M autoextend on;

Tablespace created.

SQL> create temporary tablespace testdb_tablespace_temp
  2  tempfile 'testdb_tabspace_temp.dat'
  3  size 5M autoextend on;

Tablespace created.

SQL> drop user testdb;

User dropped.

SQL> create user testdb
  2  identified by password
  3  default tablespace testdb_tablespace
  4  temporary tablespace testdb_tablespace_temp;

User created.

SQL> grant create session to testdb;

Grant succeeded.

SQL> grant create table to testdb;

Grant succeeded.

SQL> grant unlimited tablespace to testdb;

Grant succeeded.

SQL>

From oracle Sql developer, execute the below in sql worksheet:从 oracle Sql developer,在 sql 工作表中执行以下内容:

create user lctest identified by lctest;
grant dba to lctest;

then right click on "Oracle connection" -> new connection, then make everything lctest from connection name to user name password.然后右键单击“Oracle 连接”-> 新建连接,然后将所有 lctest 从连接名称设置为用户名密码。 Test connection shall pass.测试连接应通过。 Then after connected you will see the schema.然后连接后,您将看到架构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM