欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

重建SCOTT用户及SCOTT轶事

程序员文章站 2022-05-19 17:39:21
...

学习Oracle最早就是从scott用户下的dept表和emp表开始的,直到现在也习惯于在scott用户下做测试。有时为了方便测试,需要重建sco

学习Oracle最早就是从scott用户下的dept表和emp表开始的,直到现在也习惯于在scott用户下做测试。有时为了方便测试,需要重建scott用户以便提供一个全新的测试环境,在这儿记录一下重建scott用户的过程。

1.scott用户简介
SCOTT是在Oracle数据库中,一个示例用户的名称。其作用是为初学者提供一些简单的应用示例,不过其默认是锁定状态。SCOTT的缺省口令为tiger,下面有表emp, dept等,这些表和表间的关系演示了关系型数据库的一些基本原理,Oracle举例说明时一般都用这个用户,一些关于Oracle的书、教材上一般也都用这个用户来讲解。它对于Oracle本身不是必须的,如果不想用可以删除(如果你没在它下面建其它对象的话)。

2.环境准备
我们在Oracle 10g中进行试验,把scott用户删除。

点击(此处)折叠或打开

C:\\Users\\Administrator>sqlplus sys/hoegh as sysdba

SQL*Plus: Release 10.2.0.4.0 - Production on 星期三 5月 13 10:24:10 2015

Copyright (c) 1982, 2007, Oracle. All Rights Reserved.

连接到:

Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL>

SQL>

SQL> select * from v$version;

BANNER

----------------------------------------------------------------

Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi

PL/SQL Release 10.2.0.4.0 - Production

CORE 10.2.0.4.0 Production

TNS for 64-bit Windows: Version 10.2.0.4.0 - Production

NLSRTL Version 10.2.0.4.0 - Production

SQL>

SQL> drop user scott cascade;

用户已删除。

SQL>

3.重建scott用户
Oracle提供了scott用户的重建脚本,脚本位于ORACLE_HOME\RDBMS\ADMIN目录下,脚本名称为utlsampl.sql。我们在sys用户下执行该脚本,如下:

SQL>

SQL> show user

USER 为 \"SYS\"

SQL>

SQL> @C:\\oracle\\product\\10.2.0\\db_1\\RDBMS\\ADMIN\\utlsampl.sql

从 Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options 断

C:\\Users\\Administrator>
执行完脚本后,系统会自动推出sql*plus。接下来我们连接scott用户,确认脚本是否执行成功。

C:\\Users\\Administrator>sqlplus scott/tiger

SQL*Plus: Release 10.2.0.4.0 - Production on 星期三 5月 13 10:34:29 2015

Copyright (c) 1982, 2007, Oracle. All Rights Reserved.

连接到:

Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> select * from cat;

TABLE_NAME TABLE_TYPE

------------------------------ -----------

DEPT TABLE

EMP TABLE

BONUS TABLE

SALGRADE TABLE

SQL> select * from dept;

DEPTNO DNAME LOC

---------- -------------- -------------

10 ACCOUNTING NEW YORK

20 RESEARCH DALLAS

30 SALES CHICAGO

40 OPERATIONS BOSTON

SQL>

SQL>

4.utlsampl.sql脚本内容
在ORACLE_HOME\RDBMS\ADMIN目录下有很多脚本,utlsampl.sql只是其中一个。感兴趣的话,看看这些脚本也会有收获的。在这儿我们看一下utlsampl.sql脚本的内容。

Rem Copyright (c) 1990, 1996, 1997, 1999, 2001 by Oracle Corporation
Rem NAME
REM UTLSAMPL.SQL
Rem FUNCTION
Rem NOTES
Rem MODIFIED
Rem menash 02/21/01 - remove unnecessary users for security reasons
Rem gwood 03/23/99 - make all dates Y2K compliant
Rem jbellemo 02/27/97 - dont connect as system
Rem akolk 08/06/96 - bug 368261: Adding date formats
Rem glumpkin 10/21/92 - Renamed from SQLBLD.SQL
Rem blinden 07/27/92 - Added primary and foreign keys to EMP and DEPT
Rem rlim 04/29/91 - change char to varchar2
Rem mmoore 04/08/91 - use unlimited tablespace priv
Rem pritto 04/04/91 - change SYSDATE to 13-JUL-87
Rem Mendels 12/07/90 - bug 30123;add to_date calls so language independent
Rem
rem
rem $Header: utlsampl.sql 21-feb-01.18:15:30 menash Exp $ sqlbld.sql
rem
SET TERMOUT OFF
SET ECHO OFF

rem CONGDON Invoked in RDBMS at build time. 29-DEC-1988
rem OATES: Created: 16-Feb-83

DROP USER SCOTT CASCADE;
DROP USER ADAMS CASCADE;
DROP USER JONES CASCADE;
DROP USER CLARK CASCADE;
DROP USER BLAKE CASCADE;
GRANT CONNECT,RESOURCE,UNLIMITED TABLESPACE TO SCOTT IDENTIFIED BY TIGER;
DROP PUBLIC SYNONYM PARTS;

CONNECT SCOTT/TIGER
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-JUL-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-JUL-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;
EXIT
脚本主要分为几个部分,
第一部分是备注部分,行首有Rem关键字,描述了utlsampl.sql脚本的演进过程;
第二部分是drop对象,大家关注一下我标黄的语句;也就是说,在环境准备部分的drop user语句,纯粹是画蛇添足,Oracle已经替我们想到了;
第三部分是创建表和插入数据。

5.scott是谁

从Oracle第一个商业化版本至今,oracle的默认数据库里都少不了这个名字为scott,密码为tiger的用户。这个scott究竟有何来历,怎么在我们的oracle的版本里一直都不能少呢。