Describe 首頁/2020-11-22 here.

* OCP Day2

Operator

SQL> select distinct job from emp where deptno=20;

JOB
------------------
CLERK
ANALYST
MANAGER

  1  select distinct job from emp where deptno=20
  2  UNION
  3* select distinct job from emp where deptno=30
SQL> /

JOB
------------------
ANALYST
CLERK
MANAGER
SALESMAN

  1  select distinct job from emp where deptno=20
  2  UNION all
  3* select distinct job from emp where deptno=30
SQL> /

JOB
------------------
CLERK
ANALYST
MANAGER
SALESMAN
CLERK
MANAGER

6 rows selected.

  1  select distinct job from emp where deptno=20
  2  minus
  3* select distinct job from emp where deptno=30
SQL> /

JOB
------------------
ANALYST

Managing Tables Using DMS Statements

DML: insert update delete

SQL> select * from dept;

    DEPTNO DNAME                        LOC
---------- ---------------------------- --------------------------
        10 ACCOUNTING                   NEW YORK
        20 RESEARCH                     DALLAS
        30 SALES                        CHICAGO
        40 OPERATIONS                   BOSTON


SQL> desc dept;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 DEPTNO                                    NOT NULL NUMBER(2)
 DNAME                                              VARCHAR2(14)
 LOC                                                VARCHAR2(13)

SQL> insert into dept values(50,'NETWORK','BEJING');

1 row created.

SQL> insert into dept values(70,null,null);

1 row created.

SQL> insert into dept values(null,null,null);
insert into dept values(null,null,null)
                        *
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SCOTT"."DEPT"."DEPTNO")


SQL> select * from dept;

    DEPTNO DNAME                        LOC
---------- ---------------------------- --------------------------
        10 ACCOUNTING                   NEW YORK
        20 RESEARCH                     DALLAS
        30 SALES                        CHICAGO
        40 OPERATIONS                   BOSTON
        50 NETWORK                      BEJING
        70

6 rows selected.

SQL> create table dept1 
  2  as
  3  select * from dept
  4  where 1=2;

Table created.


SQL> insert into dept1
  2  select * from dept;

6 rows created.

SQL> select * from dept1;

    DEPTNO DNAME                        LOC
---------- ---------------------------- --------------------------
        10 ACCOUNTING                   NEW YORK
        20 RESEARCH                     DALLAS
        30 SALES                        CHICAGO
        40 OPERATIONS                   BOSTON
        50 NETWORK                      BEJING
        70

6 rows selected.


SQL> truncate table dept1;

Table truncated.

SQL> select * from dept1;

no rows selected

update

SQL> update emp set sal=sal+200 where ename='SMITH';

1 row updated.


SQL> update emp set sal=sal+300 where sal>2000;

6 rows updated.

  1  select ename,sal from emp
  2* fetch next 3 rows only
SQL> /

ENAME                       SAL
-------------------- ----------
SMITH                      1100
ALLEN                      1700
WARD                       1350


SQL> select sal from emp where ename='KING';

       SAL
----------
      5000

  1* select * from emp where ename='SMITH'
SQL> /

     EMPNO ENAME                JOB                       MGR HIREDATE            SAL       COMM     DEPTNO
---------- -------------------- ------------------ ---------- ------------ ---------- ---------- ----------
      7369 SMITH                CLERK                    7902 17-DEC-80          5000                    20

delete

DML delete by condition or line. is able to rollback before commit

SQL> delete from emp;


14 rows deleted.

SQL> SQL> select * from emp;

no rows selected

SQL> rollback;
Rollback complete.

truncate

cannot easy to undone

LOCK

SQL> update dept set loc='BEIJIN' where deptno=70;

1 row updated.


  1* select * from dept
SQL> /

    DEPTNO DNAME                        LOC
---------- ---------------------------- --------------------------
        10 ACCOUNTING                   NEW YORK
        20 RESEARCH                     DALLAS
        30 SALES                        CHICAGO
        40 OPERATIONS                   BOSTON
        50 NETWORK                      BEJING
        70                              BEIJIN

6 rows selected.

  1* select * from dept
SQL> /

    DEPTNO DNAME                        LOC
---------- ---------------------------- --------------------------
        10 ACCOUNTING                   NEW YORK
        20 RESEARCH                     DALLAS
        30 SALES                        CHICAGO
        40 OPERATIONS                   BOSTON
        50 NETWORK                      BEJING
        70

6 rows selected.

SQL> update dept set loc='SHANGHAI' where deptno=70;

will waiting...

SQL> update dept set loc='GUANGZHOU' where deptno=60; 

1 row updated.

SQL> select * from dept;

    DEPTNO DNAME                        LOC
---------- ---------------------------- --------------------------
        10 ACCOUNTING                   NEW YORK
        20 RESEARCH                     DALLAS
        30 SALES                        CHICAGO
        40 OPERATIONS                   BOSTON
        50 NETWORK                      BEJING
        70
        60                              GUANGZHOU

7 rows selected.

DEPTNO 70(Related) not commit

savepoint

SQL> rollback;
Rollback complete.

SQL> select * from dept;

    DEPTNO DNAME                        LOC
---------- ---------------------------- --------------------------
        10 ACCOUNTING                   NEW YORK
        20 RESEARCH                     DALLAS
        30 SALES                        CHICAGO
        40 OPERATIONS                   BOSTON
        50 NETWORK                      BEJING
        70

6 rows selected.

SQL> savepoint a;

Savepoint created.

SQL> insert into dept(deptno) values(73);

1 row created.

SQL> select * from dept;

    DEPTNO DNAME                        LOC
---------- ---------------------------- --------------------------
        10 ACCOUNTING                   NEW YORK
        20 RESEARCH                     DALLAS
        30 SALES                        CHICAGO
        40 OPERATIONS                   BOSTON
        50 NETWORK                      BEJING
        70
        73

7 rows selected.

SQL> insert into dept(deptno) values(71);

1 row created.

SQL> savepoint b;

Savepoint created.

SQL> insert into dept(deptno) values(72);

1 row created.

SQL> savepoint c;

Savepoint created.

SQL> select * from dept;

    DEPTNO DNAME                        LOC
---------- ---------------------------- --------------------------
        10 ACCOUNTING                   NEW YORK
        20 RESEARCH                     DALLAS
        30 SALES                        CHICAGO
        40 OPERATIONS                   BOSTON
        50 NETWORK                      BEJING
        70
        73
        71
        72

9 rows selected.

SQL> rollback to a;

Rollback complete.

SQL> select * from dept;

    DEPTNO DNAME                        LOC
---------- ---------------------------- --------------------------
        10 ACCOUNTING                   NEW YORK
        20 RESEARCH                     DALLAS
        30 SALES                        CHICAGO
        40 OPERATIONS                   BOSTON
        50 NETWORK                      BEJING
        70

6 rows selected.

table

create table department (
dept_id number(2),
name char(20)
master char(10) constraint dept_mas_nn not null,
location varchar(100) constraint dept_loc_nn not null
)

create table department (
dept_id number(2),
name char(20),
master char(10) constraint dept_mas_nn not null,
location varchar(100) constraint dept_loc_nn not null
)
SQL> /

Table created.

SQL> select * from department;

no rows selected

SQL> desc department;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 DEPT_ID                                            NUMBER(2)
 NAME                                               CHAR(20)
 MASTER                                    NOT NULL CHAR(10)
 LOCATION                                  NOT NULL VARCHAR2(100)

create table student (
id number(4)
name char(10) constraint stu_nu

read only table

ALTER TABLE emp READ ONLY;

-- perform table maintenance and then
-- return table back to read/write mode

ALTER TABLE emp READ WRITE;

rename table

rename student to stu

  1  select table_name,OWNER from dba_tables
  2* fetch next 5 rows only
SQL> /

TABLE_NAME                     OWNER
------------------------------ ------------------------------
ICOL$                          SYS
COL$                           SYS
IND$                           SYS
TAB$                           SYS
CLU$                           SYS

View

SQL> /

View created.

SQL> select * from v1;









14 rows selected.

SQL> desc user_views;




SQL> SELECT VIEW_NAME,TEXT from USER_VIEWS;

VIEW_NAME


TEXT


V1 select empno,ename,sal,comm from emp

SQL> create view v2

SQL> create view v3

SQL> select * from v3;







SQL> /

View created.

SQL> select * from v4;

ENAME SAL



SMITH 5000 JONES 3375 BLAKE 3250 CLARK 2850 SCOTT 3400 KING 5400 FORD 3400

7 rows selected.

SQL> update v4 set sal=sal-1000;

7 rows updated.

SQL> select * from v4;

ENAME SAL



SMITH 4000 JONES 2375 BLAKE 2250 SCOTT 2400 KING 4400 FORD 2400

6 rows selected.

SQL> create or replace view v4

View created.

SQL> select * from v4;

ENAME SAL COMM




SMITH 4000 JONES 2375 BLAKE 2250 SCOTT 2400 KING 4400 FORD 2400

6 rows selected.

SQL> update v4 set sal=sal-1000; update v4 set sal=sal-1000

ERROR at line 1: ORA-01402: view WITH CHECK OPTION where-clause violation

}}}

sequence

SQL> create sequence s1;

Sequence created.

SQL> select user from dual;

USER
-------------
SCOTT


SQL> select s1.currval from dual;

   CURRVAL
----------
         1

SQL> select s1.nextval from dual;

   NEXTVAL
----------
         2

SQL> /

   NEXTVAL
----------
         3

SQL> /

   NEXTVAL
----------
         4


  1* insert into dept values(s1.nextval,'xxxx','xxxx')
SQL> /

1 row created.

SQL> select * from dept;

    DEPTNO DNAME                        LOC
---------- ---------------------------- --------------------------
        10 ACCOUNTING                   NEW YORK
        20 RESEARCH                     DALLAS
        30 SALES                        CHICAGO
        40 OPERATIONS                   BOSTON
        50 NETWORK                      BEJING
        70                              BEIJIN
         1 xxxx                         xxxx


SQL> create sequence s2
  2  increment by 5
  3  start with 1000
  4  maxvalue 10000
  5  cycle
  6  cache 20
  7  /

Sequence created.

SQL> select s2.nextval from dual;

   NEXTVAL
----------
      1000

SQL> /

   NEXTVAL
----------
      1005

SQL> /

   NEXTVAL
----------
      1010

SQL> alter sequence s2 increment by 1;

Sequence altered.

SQL> select s2.nextval from dual;

   NEXTVAL
----------
      1011

SQL> /

   NEXTVAL
----------
      1012

SQL> /

   NEXTVAL
----------
      1013

index

SQL> create index idx1 on emp(ename);

Index created.

SQL> column index_name format a10
SQL> column table_name format a20
SQL> select index_name,table_name,UNIQUENESS FROM user_indexes;

INDEX_NAME TABLE_NAME           UNIQUENESS
---------- -------------------- ------------------
PK_DEPT    DEPT                 UNIQUE
PK_EMP     EMP                  UNIQUE
IDX1       EMP                  NONUNIQUE

SQL> select index_name,table_name,UNIQUENESS FROM user_indexes where INDEX_NAME='IDX1';

INDEX_NAME TABLE_NAME           UNIQUENESS
---------- -------------------- ------------------
IDX1       EMP                  NONUNIQUE

SQL> create index idx4 on emp(ename,job);

Index created.

SQL> create index idx2 on emp(sal);

Index created.

SQL> select index_name,table_name,UNIQUENESS FROM user_indexes;

INDEX_NAME TABLE_NAME           UNIQUENESS
---------- -------------------- ------------------
PK_DEPT    DEPT                 UNIQUE
PK_EMP     EMP                  UNIQUE
IDX1       EMP                  NONUNIQUE
IDX4       EMP                  NONUNIQUE
IDX2       EMP                  NONUNIQUE

首頁/2020-11-22 (last edited 2020-11-22 07:58:44 by localhost)