学习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的版本里一直都不能少呢。

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

The relationship between SQL and MySQL is the relationship between standard languages and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor