SQLPLUS Oracle's database

Three, SQLPLUS

SQLPlus is an interactive tool for Oracle database to provide specialized database management tasks using SQLPlus can manage all Oracle databases, SQLPlus manage the database by way of command, you can also execute SQL statements by SQLP lus.

3.1, SQLPlus set

If you want to know sqlplus command, we must first solve a new problem: data problems.

In Oracle 12C due to the presence of the CDB and PDB concept, so-called test data, the default is not found, so the following for the data must first make a recovery.

In D: \ app \ Oracleuser \ product \ 12.1.0 \ dbhome_1 \ RDBMS \ ADMIN directory being provided with a scott.sql create a script, but the script needs to be modified, and in order to modify certain to learn later finished advanced content, so in order to facilitate learning, there is provided a c ## scott.sql file, although the name is c ## scott, nature or scott.

c ## scott.sql script file code:

-- 使用超级管理员登录
CONN sys/change_on_install AS SYSDBA ;

-- 创建c##scott用户
CREATE USER c##scott IDENTIFIED BY tiger ;

-- 为用户授权
GRANT CONNECT,RESOURCE,UNLIMITED TABLESPACE TO c##scott CONTAINER=ALL ;

-- 设置用户使用的表空间
ALTER USER c##scott DEFAULT TABLESPACE USERS;
ALTER USER c##scott TEMPORARY TABLESPACE TEMP;

-- 使用c##scott用户登录
CONNECT c##scott/tiger

-- 删除数据表
DROP TABLE emp  PURGE ;
DROP TABLE dept PURGE ;
DROP TABLE bonus PURGE ;
DROP TABLE salgrade PURGE ;

-- 创建数据表
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 );
CREATE TABLE bonus (
enamE VARCHAR2(10) ,
job VARCHAR2(9)  ,
sal NUMBER,
comm NUMBER ) ;
CREATE TABLE salgrade ( 
grade NUMBER,
losal NUMBER,
hisal NUMBER );

-- 插入测试数据 —— 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');
-- 插入测试数据 —— emp
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('19-04-1987','dd-mm-yyyy')-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('23-05-1987','dd-mm-yyyy')-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);
-- 插入测试数据 —— salgrade
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;

If the configuration data in order, then the order of execution is as follows:

1, first open sqlplus.exe:

Run -> type: sqlplus / nolog

2, execute c ## scott.sql file (for a period of execution)

Open Window - Properties - Options page to quickly check the edit mode - can change the background color of the page and text color.

Then the code for a c ## scott.sql file copied to the period in sqlplus can be executed.

After the implementation of the above procedures, can be carried out behind the command explained.

3.2, SQLPlus commonly used commands

  • Setting a recording length per line: the SET LINESIZE 300;

  • Setting a recording length per page: the SET of PAGESIZE 30;

  • Users connect to the database: CONN username / password [AS SYSDBA];

  • Get current user data objects of all: the SELECT * the FROM the Tab;

  • View table structure: DESC table name;

  • Use the operating system command of the machine: HOST command ...;

    Given above is the most basic of several common operations command, described in detail below.

3.2.1 formatting commands

Now c ## scott user already exists among the corresponding data, begin to execute the query command:

SELECT * FROM emp;

This indicates that the query all data emp table.

Can be found at this time of the data shows that there is a problem fold lines, paging, data is also a problem, if you want to make the data look good thing, we must first solve the problem of screen width: Property - window settings. or:

Setting data for each line of length:

SET LINESIZE 300;

Setting page display data length:

SET PAGESIZE 30;

3.2.2 editing operations

Among sqlplus for the convenience of the user to write long SQL statement, but also specifically provides a command to call the native notepad, you can directly use the ed command to complete the operation in terminal.

ed mldn

At this point directly asks if you want to create a new mldn.sql file, open Notepad, prior written directly in the text query data, save and exit, then you can use the "@" mark, the implementation of the program, enter "@mldn"

3.2.3 connection operation

In Oracle has among many users, these users can be switched between each other to each other, and for switching the basic syntax is as follows:

CONN 用户名/密码 [AS SYSDBA];

If you are using sys user logged in, plus AS SYSDBA option

Examples : Use the sys user login

CONN sys change_on_install AS SYSDBA;

After the connection, if it is to know which is the current user, you can enter "SHOW USER;"

SHOW USER;

Then if the research c ## scott's emp table by the user in sys, then certainly not the query, then the user name must be added in front of the table, namely: c ## scott.emp;

SELECT * FROM c##scott.emp;

Among the principles of database, the user name can be sometimes simply referred to as model name, all the tables are having the mode name, namely: the name of the table schema name

If you now want to use a regular user login, you can not write SYSDBA when entering commands.

CONN c##scott tiger;
SELECT * FROM emp;

And certainly there is more than one table at a user data can be viewed with the following command:

SELECT * FROM tab;

Will find the format of the table is not standardized, the following commands locally formatted

COL tname FOR A20;
COL tabtype FOR A20;
COL clusterid FOR A20;
SELECT * FROM tab;

And the same token, if some now want to know the table structure data table, you can use the DESC command. This is the most used in development.

Examples : View emp table structure

DESC emp;
  • There are several major types in the type column:
    • NUMBER (4): represents a number, a length of 4
    • VARCHAR2 (10): represents the string length can only accommodate 10
    • DATE: Indicates the date
      • NUMBER (7,2): represents the number, which accounted for two decimal places, representing an integer of 5 bits, a total of 7
  • In sqlplus can enter a "/" indicates that the operation is repeatedly performed on a statement.

In addition to performing their own command sqlplus outside, you can also call the native operating system's command, then only need to add HOST command prior to the relevant command.

HOST dir;   
HOST copy d:\mldn.jpg d:\hello.jpg;

3.3, about the original data problems (understanding)

Always stressed: Now Oracle 12c, although the selected sample data to be created, but unfortunately, found that simply do not appear scott and sh users.

scott and real sh user, now also inside the database, but all operations of user data by default are stored in the CDB, and there's a user name must begin with c ##, such as: c ## scott.

  • The first step: the need to use the sys log in
CONN sys change_on_install AS SYSDBA;
  • Step two: Check the name of the current container
SHOW con_name;

Can clearly find that now is a return to "CDB $ ROOT", it represents a CDB container.

  • The third step: the container is changed PDB
ALTER SESSION SET CONTAINER=pdbmldn;
SHOW con_name;
  • Step Four: If you do not open the database, execute the command to open
ALTER DATABASE pdbmldn OPEN;

If you are not in the PDB container, you also need to add a PLUGGABLE on command.

ALTER PLUGGABLE DATABASE pdbmldn OPEN;

Query again, you will find the user scott and two real sh existed.

  • Fifth Step 3: Check the user
SELECT username FROM dba_users WHERE username='scott' OR username='sh';
  • Step Six: switch back to the CBD

    If you want to switch back to the CDB, only you need to log in again, or enter commands directly switch.

ALTER SESSION SET CONTAINER=cdb$root;

3.4, SQL Developer configuration

Among Oracle's learning, SQL Developer should be the focus now. And this tool is to start after the Oracle 11 users.

1, first open the SQL Developer

Start - All Programs - Oracle - Application Development Tools - SQL Developer

The tools rely on Java environment, so you need to configure Java-related commands.

2, the pop-up window, click Browse, locate the following path:

D;\app\oracleuser\produce\12.1.0\dbhome_1\jdk\bin

3. After you enter more than one path, click OK, wait

4, pop-up "configuration file type associations" window asking the user if the file association needs to be done, it is recommended not to do (that is, not checked), click OK, the SQL Developer interface.

5, if it is to use this tool, you must establish a new connection, and in order to facilitate follow-up study, recommended the establishment of two connections here:

Ordinary users (c ## scott / tiger)

Administrators (sys / change_on_install)

6 Click Connect - New connection, pop, enter the following:

Connection name: SCOTT_Connection

Username: c ## scott

Password: tiger

​ SID:mldn

The following password check the "Save Password", click the Test - Save - connection, then Tools page "connection" appears "SCOTT_Connection" next, click on the right input box enter:

SELECT * FROM emp;

Then click the Execute button (green triangle), the following results will be displayed in tabular form.

7, another New, enter the following:

Connection name: DBA_Connection

Username: sys

Password: change_on_install

​ SID:mldn

The following password check the "Save Password", select the connection type "basic" role select "SYSDBA", click on the Test - Save - connection, then Tools page "connection" appears "DBA_Connection" at this time, the administrator connection create success.

It can be found in the SQL Developer with a display format, so the significance of some of the formatting commands in sqlplus not much

3.5, c ## scott user table (back)

Among the database after the knowledge to explain, c ## scott user is the primary user operation, then the user data in the presence of a few tables to make it clear (best back down).

Employees table (EMP)

NO. Field Types of description
1 EMPNO NUMBER(4) <pk> It represents employee number, a unique number
2 ENAME VARCHAR2(10) It represents employee name
3 JOB VARCHAR2(9) Workplace representation
4 MGR NUMBER(4) It represents a leading number of employees
5 HIREDATE DATE Indicate the date of employment
6 WILL NUMBER(7,2) It represents a monthly salary, wages
7 COMM NUMBER(7,2) It represents bonus or commission called
8 DEPTNO NUMBER(2) <fk> Department Number
            EMP 表:
EMPNO        NUMBER(4)         <pk>
ENAME        VARCHAR2(10)
JOB              VARCHAR2(9)
MGR             NUMBER(4)
HIREDATE    DATE
SAL               NUMBER(7,2)
COMM          NUMBER(7,2)
DEPTNO       NUMBER(2)         <fk>

Department table (DEPT)

NO. Field Types of description
1 DEPTNO NUMBER(2) <fk> Department number is a unique number
2 DNAME VARCHAR2(14) Department name
3 PLACE VARCHAR2(13) Department Location
            DEPT 表:
DEPTNO    NUMBER(2)            <fk>
DNAME     VARCHAR2(14)
LOC           VARCHAR2(13)

Prize table (BONUS)

NO. Field Types of description
1 ENAME VARCHAR2(10)
2 JOB VARCHAR2(9)
3 WILL NUMBER
4 COMM NUMBER
            BONUS 表:
ENAME     VARCHAR2(10)
JOB           VARCHAR2(9)
SAL           NUMBER
COMM      NUMBER

Wage scales (SALGRADE)

NO. Field Types of description
1 GRADE NUMBER Level Name
2 LOSAL NUMBER This level of minimum wage
3 HISAL NUMBER This is the highest level of wages
            SALGRADE 表:
GRADE   NUMBER
LOSAL    NUMBER
HISAL     NUMBER

Description: The learning materials are based on Oracle development LiXingHua finishing the combat classic

Guess you like

Origin www.cnblogs.com/duncan1863/p/11414846.html