oracle12c创建用户,在PDB下创建scott用户,并创建相关表

    最近安装oracle12c后,创建用户遇到了一点小麻烦,因为12c引入了CDB与PDB的特性,即数据库容器与可插拔数据库。在CDB下的用户可以称公共用户,在PDB下的用户可以称之为本地用户。

在CDB下创建公共用户

    在CDB下创建公共用户的方法和之前创建方法一样,首先在sqlplus下用sys登陆CDB数据库(默认情况下直接登陆的就是CDB数据库),然后创建用户:

    语句:create user c##xiaoming identified by 123456;

    注意创建公共用户命名必须以c##或C##开头,否则无法创建。

在PDB下创建本地用户

    1.首先查看当前容器 

SQL> show con_name

CON_NAME
------------------------------
CDB$ROOT

 2.查看CDB下的可插拔数据库

select con_id, dbid, guid, name , open_mode from v$pdbs;

CON_ID       DBID             GUID               NAME      OPEN_MODE
1	2 3375419182 C7AD1B1D79134B89ADCDB8BE8BEB7E8D	    PDB$SEED	READ ONLY
2	3	2491116357 74BF1082DE55475BAD24842B355D3782	ORCLPDB	 MOUNTED

3.配置tns,在客户端服务端分别加上如下内容(service_name根据自己的插拔数据库命名)

ORCLPDB =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = ORCLPDB)
    )
  )

4.启动PDB数据库

SQL> alter pluggable database ORCLPDB open;

插接式数据库已变更。
5.切换至ORACLPDB
SQL> alter session set container=ORCLPDB;

会话已更改。

SQL> show con_name

CON_NAME
------------------------------
ORCLPDB

    此时可以看到容器已经改变。

6.再次查看CDB下的可插拔数据库

SQL>  select con_id, dbid, guid, name , open_mode from v$pdbs;

CON_ID       DBID             GUID               NAME      OPEN_MODE
1	2 3375419182 C7AD1B1D79134B89ADCDB8BE8BEB7E8D	    PDB$SEED	READ ONLY
2	3	2491116357 74BF1082DE55475BAD24842B355D3782	ORCLPDB	READ WRITE

创建soctt用户

    此时创建用户就可以像之前一样命名,创建scott用户:

create user scott identified by 123456;
    给用户授权
GRANT CONNECT,RESOURCE TO SCOTT;

    切换至ORCLPDB数据库

SQL> conn scott@orclpdb/123456
已连接。

创建scott下的自带表

CREATE TABLE DEPT
       (DEPTNO NUMBER(2) CONSTRAINT PK_DEPT PRIMARY KEY,
  DNAME VARCHAR2(14) ,
  LOC VARCHAR2(13) ) ;
CREATE TABLE EMP
       (EMPNO NUMBER(4) CONSTRAINT PK_EMP PRIMARY KEY,
  ENAME VARCHAR2(10),
  JOB VARCHAR2(9),
  MGR NUMBER(4),
  HIREDATE DATE,
  SAL NUMBER(7,2),
  COMM NUMBER(7,2),
  DEPTNO NUMBER(2) CONSTRAINT FK_DEPTNO REFERENCES DEPT);
INSERT INTO DEPT VALUES
  (10,'ACCOUNTING','NEW YORK');
INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS');
INSERT INTO DEPT VALUES
  (30,'SALES','CHICAGO');
INSERT INTO DEPT VALUES
  (40,'OPERATIONS','BOSTON');
INSERT INTO EMP VALUES
(7369,'SMITH','CLERK',7902,to_date('17-12-1980','dd-mm-yyyy'),800,NULL,20);
INSERT INTO EMP VALUES
(7499,'ALLEN','SALESMAN',7698,to_date('20-2-1981','dd-mm-yyyy'),1600,300,30);
INSERT INTO EMP VALUES
(7521,'WARD','SALESMAN',7698,to_date('22-2-1981','dd-mm-yyyy'),1250,500,30);
INSERT INTO EMP VALUES
(7566,'JONES','MANAGER',7839,to_date('2-4-1981','dd-mm-yyyy'),2975,NULL,20);
INSERT INTO EMP VALUES
(7654,'MARTIN','SALESMAN',7698,to_date('28-9-1981','dd-mm-yyyy'),1250,1400,30);
INSERT INTO EMP VALUES
(7698,'BLAKE','MANAGER',7839,to_date('1-5-1981','dd-mm-yyyy'),2850,NULL,30);
INSERT INTO EMP VALUES
(7782,'CLARK','MANAGER',7839,to_date('9-6-1981','dd-mm-yyyy'),2450,NULL,10);
INSERT INTO EMP VALUES
(7788,'SCOTT','ANALYST',7566,to_date('13-7-87','dd-mm-rr')-85,3000,NULL,20);
INSERT INTO EMP VALUES
(7839,'KING','PRESIDENT',NULL,to_date('17-11-1981','dd-mm-yyyy'),5000,NULL,10);
INSERT INTO EMP VALUES
(7844,'TURNER','SALESMAN',7698,to_date('8-9-1981','dd-mm-yyyy'),1500,0,30);
INSERT INTO EMP VALUES
(7876,'ADAMS','CLERK',7788,to_date('13-7-87', 'dd-mm-rr')-51,1100,NULL,20);
INSERT INTO EMP VALUES
(7900,'JAMES','CLERK',7698,to_date('3-12-1981','dd-mm-yyyy'),950,NULL,30);
INSERT INTO EMP VALUES
(7902,'FORD','ANALYST',7566,to_date('3-12-1981','dd-mm-yyyy'),3000,NULL,20);
INSERT INTO EMP VALUES
(7934,'MILLER','CLERK',7782,to_date('23-1-1982','dd-mm-yyyy'),1300,NULL,10);
CREATE TABLE BONUS
  (
  ENAME VARCHAR2(10)  ,
  JOB VARCHAR2(9)  ,
  SAL NUMBER,
  COMM NUMBER
  ) ;
CREATE TABLE SALGRADE
      ( GRADE NUMBER,
  LOSAL NUMBER,
  HISAL NUMBER );
INSERT INTO SALGRADE VALUES (1,700,1200);
INSERT INTO SALGRADE VALUES (2,1201,1400);
INSERT INTO SALGRADE VALUES (3,1401,2000);
INSERT INTO SALGRADE VALUES (4,2001,3000);
INSERT INTO SALGRADE VALUES (5,3001,9999);
COMMIT;

至此,结束

附:

--在容器间切换
alter session set container=ORCLPDB;
alter session set container=CDB$ROOT;
STARTUP
--关闭PDB 数据库
alter pluggable database ORCLPDB close;


你可能感兴趣的:(oracle12c创建用户,在PDB下创建scott用户,并创建相关表)