Oracle全部实验

1. 日志文件

show user
desc dba_data_files
select file_name, tablespace_name, bytes from dba_data_files;
select file#, name, checkpoint_change# from v$datafile;
desc V$log;
desc V$logfile;
select group#, archived, status from v$log; 
select group#, status,member from v$logfile;
alter database add logfile group 10 ('log1a.rdo', 'log2a.rdo') size 5000k;
alter database add logfile('log1c.rdo', 'log2c.rdo') size 5000k;
alter database add logfile member 'log3a.rdo' to group 10;
select group#, status,member from v$logfile;
alter database drop logfile member 'log1a.rdo';
select group#, status,member from v$logfile;
alter database drop logfile group 10;
select name, log_mode from v$database;
select member from v$logfile;
 select * from v$controlfile;

create tablespace hrtbs1 datafile 'C:\oracle\product\10.2.0\oradata\ORCL\HRTBS1.DBF' size 50M;
create tablespace hrtbs2 datafile 'C:\oracle\product\10.2.0\oradata\ORCL\HRTBS2_1.DBF' size 50M extent management local uniform size 512K;
create tablespace hrtbs3 datafile 'C:\oracle\product\10.2.0\oradata\ORCL\HRTBS3_1.DBF' size 50M segment space management manual;
create tablespace hrtbs4 datafile 'C:\oracle\product\10.2.0\oradata\ORCL\HRTBS4_1.DBF' size 50M extent management local uniform size 512K segment space management manual;
create tablespace indx datafile 'C:\oracle\product\10.2.0\oradata\ORCL\INDEX01.DBF' size 50M;
create temporary tablespace hrtemp1 tempfile 'C:\oracle\product\10.2.0\oradata\ORCL\HRTEMP1_1.DBF' size 20M extent management local uniform size 15M;
create temporary tablespace hrtemp2 tempfile 'C:\oracle\product\10.2.0\oradata\ORCL\HRTEMP2_1.DBF' size 20M extent management local uniform size 15M tablespace group temp_group;
alter tablespace hrtemp1 tablespace group temp_group;
create undo tablespace hrundo1 datafile 'C:\oracle\product\10.2.0\oradata\ORCL\HRUNDO1_1.DBF' size 20M;
alter  tablespace users add datafile 'C:\oracle\product\10.2.0\oradata\ORCL\USERS02.DBF' size 10M;
alter  tablespace temp add tempfile 'C:\oracle\product\10.2.0\oradata\ORCL\TEMP02.DBF' size 5M;
alter database datafile 'C:\oracle\product\10.2.0\oradata\ORCL\USERS02.DBF' autoextend on next 1M maxsize unlimited;
alter database datafile 'C:\oracle\product\10.2.0\oradata\ORCL\USERS02.DBF' autoextend off;
alter database datafile 'C:\oracle\product\10.2.0\oradata\ORCL\USERS02.DBF' RESIZE 8M;


Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 
Connected as system
 
SQL> show user
User is "system"
 
SQL> desc dba_data_files
Name            Type          Nullable Default Comments                                            
--------------- ------------- -------- ------- --------------------------------------------------- 
FILE_NAME       VARCHAR2(513) Y                Name of the database data file                      
FILE_ID         NUMBER        Y                ID of the database data file                        
TABLESPACE_NAME VARCHAR2(30)  Y                Name of the tablespace to which the file belongs    
BYTES           NUMBER        Y                Size of the file in bytes                           
BLOCKS          NUMBER        Y                Size of the file in ORACLE blocks                   
STATUS          VARCHAR2(9)   Y                File status:  "INVALID" or "AVAILABLE"              
RELATIVE_FNO    NUMBER        Y                Tablespace-relative file number                     
AUTOEXTENSIBLE  VARCHAR2(3)   Y                Autoextensible indicator:  "YES" or "NO"            
MAXBYTES        NUMBER        Y                Maximum size of the file in bytes                   
MAXBLOCKS       NUMBER        Y                Maximum size of the file in ORACLE blocks           
INCREMENT_BY    NUMBER        Y                Default increment for autoextension                 
USER_BYTES      NUMBER        Y                Size of the useful portion of file in bytes         
USER_BLOCKS     NUMBER        Y                Size of the useful portion of file in ORACLE blocks 
ONLINE_STATUS   VARCHAR2(7)   Y                Online status of the file                           
 
SQL> select file_name, tablespace_name, bytes from dba_data_files
  2  
SQL> select file_name, tablespace_name, bytes from dba_data_files
  2  
SQL> select file_name, tablespace_name, bytes from dba_data_files;
 
FILE_NAME                                                                        TABLESPACE_NAME                     BYTES
-------------------------------------------------------------------------------- ------------------------------ ----------
C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\USERS01.DBF                                USERS                             5242880
C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\SYSAUX01.DBF                               SYSAUX                          272629760
C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\UNDOTBS01.DBF                              UNDOTBS1                         73400320
C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\SYSTEM01.DBF                               SYSTEM                          503316480
C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\EXAMPLE01.DBF                              EXAMPLE                         104857600
 
SQL> select file#, name, checkpoint_chang# from v$datafile;
 
select file#, name, checkpoint_chang# from v$datafile
 
ORA-00904: "CHECKPOINT_CHANG#": 标识符无效
 
SQL> select file#, name, checkpoint_chang# from v$datafile;
 
select file#, name, checkpoint_chang# from v$datafile
 
ORA-00904: "CHECKPOINT_CHANG#": 标识符无效
 
SQL> select file#, name, checkpoint_change# from v$datafile;
 
     FILE# NAME                                                                             CHECKPOINT_CHANGE#
---------- -------------------------------------------------------------------------------- ------------------
         1 C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\SYSTEM01.DBF                                          1201919
         2 C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\UNDOTBS01.DBF                                         1201919
         3 C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\SYSAUX01.DBF                                          1201919
         4 C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\USERS01.DBF                                           1201919
         5 C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\EXAMPLE01.DBF                                         1201919
 
SQL> desc V$log;
Name          Type         Nullable Default Comments 
------------- ------------ -------- ------- -------- 
GROUP#        NUMBER       Y                         
THREAD#       NUMBER       Y                         
SEQUENCE#     NUMBER       Y                         
BYTES         NUMBER       Y                         
MEMBERS       NUMBER       Y                         
ARCHIVED      VARCHAR2(3)  Y                         
STATUS        VARCHAR2(16) Y                         
FIRST_CHANGE# NUMBER       Y                         
FIRST_TIME    DATE         Y                         
 
SQL> desc V$logfile;
Name                  Type          Nullable Default Comments 
--------------------- ------------- -------- ------- -------- 
GROUP#                NUMBER        Y                         
STATUS                VARCHAR2(7)   Y                         
TYPE                  VARCHAR2(7)   Y                         
MEMBER                VARCHAR2(513) Y                         
IS_RECOVERY_DEST_FILE VARCHAR2(3)   Y                         
 
SQL> select group#, archived, status from v$log; 
 
    GROUP# ARCHIVED STATUS
---------- -------- ----------------
         1 NO       INACTIVE
         2 NO       CURRENT
         3 NO       INACTIVE
 
SQL> select group#, status,member from v$logfile;
 
    GROUP# STATUS  MEMBER
---------- ------- --------------------------------------------------------------------------------
         3 STALE   C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\REDO03.LOG
         2         C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\REDO02.LOG
         1 STALE   C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\REDO01.LOG
 
SQL> alter database add logfile group 10 ('log1a.rdo', 'log2a.rdo') size 5000k;
 
Database altered
 
SQL> alter database add logfile('log1c.rdo', 'log2c.rdo');
 
alter database add logfile('log1c.rdo', 'log2c.rdo')
 
ORA-00301: 添加日志文件 'log1c.rdo' 时出错 - 无法创建文件
ORA-17610: 文件 'log1c.rdo' 不存在, 大小也未指定
ORA-27041: 无法打开文件
OSD-04002: 无法打开文件
O/S-Error: (OS 2) 系统找不到指定的文件。
 
SQL> alter database add logfile('log1c.rdo', 'log2c.rdo') size 5000k;
 
Database altered
 
SQL> alter database add logfile member 'log3a.rdo' to group 10;
 
Database altered
 
SQL> select group#, status,member from v$logfile;
 
    GROUP# STATUS  MEMBER
---------- ------- --------------------------------------------------------------------------------
         3 STALE   C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\REDO03.LOG
         2         C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\REDO02.LOG
         1 STALE   C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\REDO01.LOG
        10         C:\ORACLE\PRODUCT\10.2.0\DB_1\DATABASE\LOG1A.RDO
        10         C:\ORACLE\PRODUCT\10.2.0\DB_1\DATABASE\LOG2A.RDO
         4         C:\ORACLE\PRODUCT\10.2.0\DB_1\DATABASE\LOG1C.RDO
         4         C:\ORACLE\PRODUCT\10.2.0\DB_1\DATABASE\LOG2C.RDO
        10 INVALID C:\ORACLE\PRODUCT\10.2.0\DB_1\DATABASE\LOG3A.RDO
 
8 rows selected
 
SQL> alter database drop logfile member 'log1a.rdo';
 
Database altered
 
SQL> select group#, status,member from v$logfile;
 
    GROUP# STATUS  MEMBER
---------- ------- --------------------------------------------------------------------------------
         3 STALE   C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\REDO03.LOG
         2         C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\REDO02.LOG
         1 STALE   C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\REDO01.LOG
        10         C:\ORACLE\PRODUCT\10.2.0\DB_1\DATABASE\LOG2A.RDO
         4         C:\ORACLE\PRODUCT\10.2.0\DB_1\DATABASE\LOG1C.RDO
         4         C:\ORACLE\PRODUCT\10.2.0\DB_1\DATABASE\LOG2C.RDO
        10 INVALID C:\ORACLE\PRODUCT\10.2.0\DB_1\DATABASE\LOG3A.RDO
 
7 rows selected
 
SQL> alter database drop logfile group 10;
 
Database altered
 
SQL> archive log list;
 
archive log list
 
ORA-00900: 无效 SQL 语句
 
SQL> select name, log_mode from v$database;
 
NAME      LOG_MODE
--------- ------------
ORCL      NOARCHIVELOG
 
SQL> select member from v$logfile;
 
MEMBER
--------------------------------------------------------------------------------
C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\REDO03.LOG
C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\REDO02.LOG
C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\REDO01.LOG
C:\ORACLE\PRODUCT\10.2.0\DB_1\DATABASE\LOG1C.RDO
C:\ORACLE\PRODUCT\10.2.0\DB_1\DATABASE\LOG2C.RDO
 
SQL> select * from v$controlfile;
 
STATUS  NAME                                                                             IS_RECOVERY_DEST_FILE BLOCK_SIZE FILE_SIZE_BLKS
------- -------------------------------------------------------------------------------- --------------------- ---------- --------------
        C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\CONTROL01.CTL                              NO                         16384            430
        C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\CONTROL02.CTL                              NO                         16384            430
        C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\CONTROL03.CTL                              NO                         16384            430
 
SQL>



SQL> creat tablespace hrtbs1 datafile 'C:\dss\HRTBS1.DBF' size 50M;
 
creat tablespace hrtbs1 datafile 'C:\dss\HRTBS1.DBF' size 50M
 
ORA-00900: 无效 SQL 语句
 
SQL> create tablespace hrtbs1 datafile 'C:\dss\HRTBS1.DBF' size 50M;
 
create tablespace hrtbs1 datafile 'C:\dss\HRTBS1.DBF' size 50M
 
ORA-01119: 创建数据库文件 'C:\dss\HRTBS1.DBF' 时出错
ORA-27040: 文件创建错误, 无法创建文件
OSD-04002: 无法打开文件
O/S-Error: (OS 3) 系统找不到指定的路径。
 
SQL> create tablespace hrtbs1 datafile 'C:\oracle\product\10.2.0\oradata\ORCL\HRTBS1.DBF' size 50M;
 
Tablespace created
 
SQL> create tablespace hrtbs2 datafile 'C:\oracle\product\10.2.0\oradata\ORCL\HRTBS2_1.DBF' size 50M extent management local uniform size 512K;
SQL> 
 
Tablespace created
 
SQL> create tablespace hrtbs3 datafile 'C:\oracle\product\10.2.0\oradata\ORCL\HRTBS3_1.DBF' size 50M segment space management manual;
 
Tablespace created
 
SQL> create tablespace hrtbs4 datafile 'C:\oracle\product\10.2.0\oradata\ORCL\HRTBS4_1.DBF' size 50M extent management local uniform size 512K segment space management manual;
 
Tablespace created
 
SQL> create tablespace index datafile 'C:\oracle\product\10.2.0\oradata\ORCL\HRTBS3_1.DBF' size 50M;
 
create tablespace index datafile 'C:\oracle\product\10.2.0\oradata\ORCL\HRTBS3_1.DBF' size 50M
 
ORA-02216: 需要表空间名
 
SQL> create tablespace indx datafile 'C:\oracle\product\10.2.0\oradata\ORCL\HRTBS3_1.DBF' size 50M;
 
create tablespace indx datafile 'C:\oracle\product\10.2.0\oradata\ORCL\HRTBS3_1.DBF' size 50M
 
ORA-01537: 无法添加文件 'C:\oracle\product\10.2.0\oradata\ORCL\HRTBS3_1.DBF' - 该文件已是数据库的一部分
 
SQL> create tablespace indx datafile 'C:\oracle\product\10.2.0\oradata\ORCL\INDEX01.DBF' size 50M;
 
Tablespace created
 
SQL> create temporary tablespace hrtemp1 tempfile 'C:\oracle\product\10.2.0\oradata\ORCL\HRTEMP1_1.DBF' size 20M extent management local uniform size 15M;
 
Tablespace created
 
SQL> create temporary tablespace hrtemp2 tempfile 'C:\oracle\product\10.2.0\oradata\ORCL\HRTEMP2_1.DBF' size 20M extent management local uniform size 15M tablespace group temp_group;
 
Tablespace created
 
SQL> alter tablespace hrtemp1 tablespace group temp_group;
 
Tablespace altered
 
SQL> create undo tablespace hrundo1 tempfile 'C:\oracle\product\10.2.0\oradata\ORCL\HRUNDO1_1.DBF' size 20M;
 
create undo tablespace hrundo1 tempfile 'C:\oracle\product\10.2.0\oradata\ORCL\HRUNDO1_1.DBF' size 20M
 
ORA-02180: 无效的 CREATE TABLESPACE 选项
 
SQL> create undo tablespace hrundo1 datafile 'C:\oracle\product\10.2.0\oradata\ORCL\HRUNDO1_1.DBF' size 20M;
 
Tablespace created
 
SQL> alter  tablespace users add datafile 'C:\oracle\product\10.2.0\oradata\ORCL\USERS02.DBF' size 10M;
 
Tablespace altered
 
SQL> alter  tablespace temp add tempfile 'C:\oracle\product\10.2.0\oradata\ORCL\TEMP02.DBF' size 5M;
 
Tablespace altered
 
SQL> alter database datafile 'C:\oracle\product\10.2.0\oradata\ORCL\USERS02.DBF' autoextend on next 1M maxsize unlimited;
 
Database altered
 
SQL> alter database datafile 'C:\oracle\product\10.2.0\oradata\ORCL\USERS02.DBF' autoextend off;
 
Database altered
 
SQL> alter database datafile 'C:\oracle\product\10.2.0\oradata\ORCL\USERS02.DBF' RESIZE 8M;
 
Database altered
 


2. 表空间


1.	横向
SQL> create bigfile tablespace tbs datafile 'c:\3.dbf' size 1G uniform size 512k;
Tablespace created

SQL> create temporary tablespace ts1 tempfile 'c:\1.dbf' size 20m extent management local uniform size 15m;
 
Tablespace created
SQL> create undo tablespace ts2 datafile 'c:\2.dbf' size 20m;
 
Tablespace created
2.	纵向
create temporary tablespace ts2 tempfile 'C:\2.dbf' size 2m reuse autoextend off;


create bigfile tablespace ts5 datafile 'C:\5.dbf'size 2m reuse autoextend off;

3.	区管理(本地的,数据字典两种)
SQL> create tablespace ts9 datafile 'c:\11.dbf' size 2m reuse ,'c:\12.dbf' size 3m extent management local uniform size 1m;
 
Tablespace created

SQL> create tablespace ts5 datafile 'c:\5.dbf' size 2m reuse ,'c:\6.dbf' size 3m extent management local autoallocate;
 
Tablespace created
4.	段管理
SQL> create tablespace ts6 datafile 'c:\7.dbf' size 2m reuse ,'c:\8.dbf' size 3m segment space management manual;
 
Tablespace created
 
SQL> create tablespace ts7 datafile 'c:\9.dbf' size 2m reuse ,'c:\10.dbf' size 3m segment space management auto;
 
Tablespace created
5.	区,段管理
SQL> create tablespace ts10 datafile 'c:\13.dbf' size 2m reuse ,'c:\14.dbf' size 3m  extent management local autoallocate segment space management auto;
 
Tablespace created
SQL> create tablespace ts11 datafile 'c:\15.dbf' size 2m reuse ,'c:\16.dbf' size 3m  extent management local autoallocate segment space management manual;
 
Tablespace created
SQL> create tablespace ts12 datafile 'c:\17.dbf' size 2m reuse ,'c:\18.dbf' size 3m  extent management local uniform size 1m segment space management manual;
 
Tablespace created
SQL> create tablespace ts12 datafile 'c:\17.dbf' size 2m reuse ,'c:\18.dbf' size 3m  extent management local uniform size 1m segment space management auto;
 
Tablespace created


1.重命名表空间和数据文件
Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 
Connected as system
 
SQL> create tablespace ts1 datafile 'c:\1.dbf' size 2m,'c:\2.dbf' size 3m;
SQL> 
 
Tablespace created
 
SQL> Alter tablespace ts1 rename to ts2;
 
Tablespace altered
 
SQL> alter tablespace ts2 offline;
 
Tablespace altered

SQL> alter tablespace ts2 rename datafile 'c:\1.dbf' to 'e:\cp.dbf';
 
alter tablespace ts2 rename datafile 'c:\1.dbf' to 'e:\cp.dbf'
 
ORA-01525: 重命名数据文件时出错
ORA-01141: 重命名数据文件 6 时出错 - 未找到新文件 'e:\cp.dbf'
ORA-01110: 数据文件 6: 'C:\1.DBF'
ORA-27041: 无法打开文件
OSD-04002: 无法打开文件
O/S-Error: (OS 2) 系统找不到指定的文件。

SQL> alter tablespace ts2 rename datafile 'c:\1.dbf' to 'e:\cp1.dbf';
 
Tablespace altered

SQL> alter tablespace ts2 online;
 
Tablespace altered
 
2.扩充表空间容量
1.为表空间添加数据文件
(1).向USERS表空间中加入10M的数据文件
SQL> alter tablespace users add datafile 'C:\oracle\product\10.2.0\oradata\orcl\users02.dbf' size 10m;
 
Tablespace altered

(2).向TEMP表空间中加入5M的数据文件
SQL> alter tablespace temp add tempfile 'C:\oracle\product\10.2.0\oradata\orcl\temp02.dbf' size 5m;
 
Tablespace altered

改变数据文件的扩展性
(1).将USERS表空间中的数据文件USERS02.DBF改为自动增长方式
SQL> alter database datafile 'C:\oracle\product\10.2.0\oradata\orcl\users02.dbf' autoextend on next 1m maxsize unlimited;
 
Database altered

重新设置数据文件大小
SQL> alter database datafile 'C:\oracle\product\10.2.0\oradata\orcl\users02.dbf' resize 8m;
 
Database altered

3.DB的开启(黑屏下)
(1)SQL> startup nomount;
ORACLE 例程已经启动。

Total System Global Area  612368384 bytes
Fixed Size                  1292036 bytes
Variable Size             285214972 bytes
Database Buffers          318767104 bytes
Redo Buffers                7094272 bytes
(2)SQL> alter database mount;
数据库已更改。
(3)SQL> alter database archivelog;

数据库已更改。

(4)SQL> archive log list;
数据库日志模式            存档模式
自动存档             启用
存档终点            USE_DB_RECOVERY_FILE_DEST
最早的联机日志序列     25
下一个存档日志序列   27
当前日志序列           27

(5)SQL> alter database noarchivelog;

数据库已更改。

(6)SQL> alter database open;

数据库已更改。

DB的关闭(黑屏下)
SQL> shutdown immediate;
数据库已经关闭。
已经卸载数据库。
ORACLE 例程已经关闭。



3. Merge语句

概念&用处

用来合并update和insert语句

通过merge语句,根据一张表(原数据表,source table)对另外一张(目标表,target table)表进行查询,连接条件匹配上的进行update,无法匹配的执行INSERT。

登录

sqlplus / as sysdba
sqlplus scott/tiger

构建平台sql语句

create table KC
(
  kch  VARCHAR2(3) not null,
  kcm  VARCHAR2(16) not null,
  kkxq NUMBER(1) not null,
  xs   NUMBER(2) not null,
  xf   NUMBER(1)
)


create table XS
(
  xh   VARCHAR2(6),
  xm   VARCHAR2(6),
  zym  VARCHAR2(6),
  xb   VARCHAR2(2),
  cssj DATE,
  zxf  NUMBER(2),
  bz   VARCHAR2(20)
)



create table XS_KC
(
  xh  CHAR(6) not null,
  kch CHAR(6) not null,
  cj  INTEGER,
  xf  NUMBER(2)
)



insert into KC (kch, kcm, kkxq, xs, xf)
values ('001', '001', 1, 11, 1);
insert into KC (kch, kcm, kkxq, xs, xf)
values ('001', '001', 1, 11, 1);


insert into XS (xh, xm, zym, xb, cssj, zxf, bz)
values ('061101', '王林', '计算机', '男', to_date('10-02-1986', 'dd-mm-yyyy'), 50, null);
insert into XS (xh, xm, zym, xb, cssj, zxf, bz)
values ('101112', '李明', '计算机', '男', to_date('30-01-1986', 'dd-mm-yyyy'), 36, null);
insert into XS (xh, xm, zym, xb, cssj, zxf, bz)
values ('001', '张琼', '计算机', null, null, 45, '三好学生');
insert into XS (xh, xm, zym, xb, cssj, zxf, bz)
values ('121112', '王小二', '计算机', '男', to_date('30-01-1986', 'dd-mm-yyyy'), 36, null);



insert into XS_KC (xh, kch, cj, xf)
values ('061101', '101   ', 80, null);
insert into XS_KC (xh, kch, cj, xf)
values ('061101', '102   ', 78, null);
insert into XS_KC (xh, kch, cj, xf)
values ('061101', '206   ', 76, null);
insert into XS_KC (xh, kch, cj, xf)
values ('061103', '101   ', 82, null);
insert into XS_KC (xh, kch, cj, xf)
values ('061103', '102   ', 82, null);
insert into XS_KC (xh, kch, cj, xf)
values ('061103', '206   ', 83, null);
insert into XS_KC (xh, kch, cj, xf)
values ('061104', '101   ', 90, null);
insert into XS_KC (xh, kch, cj, xf)
values ('061107', '101   ', 98, null);
insert into XS_KC (xh, kch, cj, xf)
values ('061107', '102   ', 80, null);
commit;


使用MERGE语句XS1表中新增的数据插入表XS中:

create table xs1 as select * from xs;

insert into xs1(xh,xm,zym,xb,cssj,zxf) values('007','test','计算机','男',to_date('19900130','YYYYMMDD'),36)

--使用MERGE语句XS_JSJ表中新增的数据插入表XS中:
merge into xs a using xs1 b on(a.xh=b.xh) 
when not matched then 
insert(a.xh,a.xm,a.zym,a.xb,a.cssj,a.zxf)
values(b.xh,b.xm,b.zym,b.xb,b.cssj,b.zxf);

构建平台

create table PRODUCTS
    (
    PRODUCT_ID             INTEGER,
    PRODUCT_NAME      VARCHAR2(60),
    CATEGORY                VARCHAR2(60)
    );



create table NEWPRODUCTS
    (
    PRODUCT_ID                 INTEGER,
    PRODUCT_NAME          VARCHAR2(60),
    CATEGORY                    VARCHAR2(60)
    );

insert into PRODUCTS values (1501, 'VIVITAR 35MM', 'ELECTRNCS');
insert into PRODUCTS values (1502, 'OLYMPUS IS50', 'ELECTRNCS');
insert into PRODUCTS values (1600, 'PLAY GYM', 'TOYS');
insert into PRODUCTS values (1601, 'LAMAZE', 'TOYS');
insert into PRODUCTS values (1666, 'HARRY POTTER', 'DVD');
commit;


insert into NEWPRODUCTS values (1502, 'OLYMPUS CAMERA', 'ELECTRNCS');
insert into NEWPRODUCTS values (1601, 'LAMAZE', 'TOYS');
insert into NEWPRODUCTS values (1666, 'HARRY POTTER', 'TOYS');
insert into NEWPRODUCTS values (1700, 'WAIT INTERFACE', 'BOOKS');
commit;

products为目标表,newproducts为源表,则若产品号相匹配,根据源表信息修改目标表的产品名(product_name)和产品类别(category)

merge into products p 
using newproducts np 
 on (p.product_id = np.product_id) when matched then 
 update set p.product_name=np.product_name, p.category=np.category;

若产品号不匹配,则根据源表将新的纪录添加到目标表

merge into products  p using newproducts np on (p.product_id=np.product_id)
when not matched then insert values(np.product_id,np.product_name,np.category);

增删改

merge into products  p using newproducts np on (p.product_id=np.product_id)
when matched then update set p.product_name= np.product_name,
	p.category = np.category
	delete where(p.category='ELECTRNCS')
when not matched then insert values(np.product_id,np.product_name,np.category);

4. pl/sql1

基本结构

declare 

   example_text  varchar2(100);
begin
		 example_text:='本例为plsql示例程序块!!!';
		 dbms_output.put_line(example_text);

exception
   when others then
		dbms_output.put_line('出现异常了!!!');
		
end;

declare
        v_xm varchar2(8):='Jame';
		v_zym varchar2(10):='计算机';
		v_zxf number(2):=55;

begin
	  update xs set zxf=v_zxf
		   where xm=v_xm;
		if sql%notfound then 
			dbms_output.put_line('没有该人,需要插入该人!!!');
			insert into xs(xh,xm,zym,zxf) values('007',v_xm,v_zym,v_zxf);
     end if;
end;
--禁用约束
alter table dept disable constraint deptno CASCADE

alter table EMP disable constraint FK_DEPTNO;

--启用约束
alter table dept enable constraint deptno

alter table EMP enable constraint FK_DEPTNO;
--查询约束具体状况
select * from user_constraints   --用SCOTT用户执行,否则找不到

declare 
     row_id rowid;
	 info varchar2(100);

begin
	update scott.dept set deptno=90 where dname='RESEARCH' 
	  returning rowid,dname || '::::' || to_char(deptno)
		into row_id,info;
		dbms_output.put_line('rowid:'||row_id);
		dbms_output.put_line(info);
end;

select t.*, t.rowid from DEPT t
declare 
  row_id rowid;
  info varchar2(40);
	
begin
	insert into scott.dept values(12,'财务室','海口')
	returning rowid,dname||'--->'||to_char(deptno) 
	 into row_id,info;
    dbms_output.put_line('rowid:'||row_id);
		dbms_output.put_line(info);
end; 

5. pl/sql2

属性类型

%TYPE - 引用变量和数据库列的数据类型
%ROWTYPE - 提供表示表中一行的记录类型

显示输出scott.emp表中的部分数据

declare 

   emp_number constant number(4):=7876;
   emp_name  varchar2(10);
	 emp_job  varchar2(9);
	 emp_sal  number(7,2);
begin
	select ename,job,sal
	   into emp_name,emp_job,emp_sal
		 from scott.emp where empno = emp_number;
		 dbms_output.put_line('查询的员工号为'||'---->'||emp_number);
      dbms_output.put_line('该员工名为'||'---->'||emp_name);
			 dbms_output.put_line('该员工职位为'||'---->'||emp_job);
			  dbms_output.put_line('该员工工资为'||'---->'||emp_sal);

end;  

查询的员工号为---->7876
该员工名为---->ADAMS
该员工职位为---->CLERK
该员工工资为---->1100

%TYPE使用举例


declare
    emp_number number(4):=7876;
		emp_name scott.emp.ename%type;
		emp_job scott.emp.job%type;
		emp_sal scott.emp.sal%type;

begin
	  select ename,job,sal
		into emp_name,emp_job,emp_sal
		from scott.emp where empno = emp_number;
		 dbms_output.put_line('查询的员工号为'||'---->'||emp_number);
      dbms_output.put_line('该员工名为'||'---->'||emp_name);
			 dbms_output.put_line('该员工职位为'||'---->'||emp_job);
			  dbms_output.put_line('该员工工资为'||'---->'||emp_sal);

end;  

使用PL/SQL语言,统计xs表中同学的个数并显示出来

declare 

  v_1 number;
begin
	select count(*) into v_1 from xs;
	dbms_output.put_line(v_1);
	
	exception 
		when others then 
			dbms_output.put_line('出现异常了!!!');

end; 

%ROWTYPE复合数据类型

declare

    one_emp scott.emp%rowtype;
begin
	select * into one_emp
	  from scott.emp where empno =7900;
		 dbms_output.put_line('该员工职位为'||'---->'||one_emp.job);
			  dbms_output.put_line('该员工工资为'||'---->'||one_emp.sal);
		
end;


该员工职位为---->CLERK
该员工工资为---->950

变量名 数据表.列名%type
变量名 数据表%rowtype。

6. 游标

概念

提供了一种对具有多行数据查询结果集中的每一行数据分别进行单独处理的方法

显式游标

不带参数的显式游标

声明一个游标,读取学生的学号。

declare
   cursor my_cursor is
	 select xh from xs;
	 v_xh xs.xh%type;

begin
	
  open my_cursor;
	fetch my_cursor into v_xh;
	dbms_output.put_line('学号:'||v_xh);
	dbms_output.put_line(my_cursor%rowcount);
	close my_cursor;

   exception
		 when others then 
			 dbms_output.put_line(sqlcode||sqlerrm);
end;  


学号:061101
1

带参数的显式游标

declare 

   varid number;
	 varname varchar2(50);
	 cursor mycur(v_xb xs.xb%type) is 
	 select xh,xm from xs
	 where xb=v_xb;
begin
	
   open mycur('男');
	 fetch mycur into varid,varname;
	 dbms_output.put_line('学生编号:'||varid||'学生姓名:'||varname);
	 close mycur;

end;

学生编号:61101学生姓名:王林

游标的属性

declare 
   cursor c_1 is select * from xs;
	 v_1 c_1%rowtype;
	  
begin
   open c_1;
	 fetch c_1 into v_1;
	 dbms_output.put_line('学号:'||v_1.xh||'姓名:'||v_1.xm||'总学分:'||v_1.zxf);
	 fetch c_1 into v_1;
	 dbms_output.put_line('当前游标指向第'||c_1%rowcount||'行');		 
	 close c_1;
end;

学号:061101姓名:王林总学分:50
当前游标指向第2行

游标遍历

使用游标分别遍历xs表中的xh,zxf

declare 
   v_xh varchar2(6);
   v_zxf number(2);
	 cursor xs_cur3 is select  xh,zxf from xs;
begin
	open xs_cur3;
	fetch xs_cur3 into v_xh,v_zxf;
	while xs_cur3%found
		loop
			dbms_output.put_line('学号:'||v_xh||'总学分'||v_zxf);
			fetch xs_cur3 into v_xh,v_zxf;
		end loop;
   close xs_cur3;
end;


学号:061101总学分50
学号:101112总学分36
学号:001总学分45
学号:121112总学分36
学号:007总学分36
学号:007总学分55

利用游标WHILE循环统计并输出scott.emp表各个部门的平均工资;若平均工资大于2000,则输出“该部门平均工资较高"

declare 

  cursor c_dept_stat is select deptno,avg(sal) avgsal
	from scott.emp group by deptno;
	
	v_dept c_dept_stat%rowtype;
begin
	open c_dept_stat;
	fetch c_dept_stat into v_dept;
	while c_dept_stat%found
		loop
			dbms_output.put_line('部门号:'||v_dept.deptno||'--'||'平均工资:'||trunc(v_dept.avgsal,1));
			if(v_dept.avgsal >= 2000) then 
			  dbms_output.put_line(v_dept.deptno||'号部门工资较高');
				end if;
			fetch c_dept_stat into v_dept;
		end loop;
end;


部门号:30--平均工资:1566.6
部门号:20--平均工资:2175
20号部门工资较高
部门号:10--平均工资:2916.6
10号部门工资较高

利用FOR循环统计并输出各个部门的平均工资

declare 
   cursor c_1 is select deptno,avg(sal) avgsal from scott.emp group by deptno;
	 
	 v_dept c_1%rowtype;
begin
	for v_dept in c_1
	loop
		dbms_output.put_line('部门号:'||v_dept.deptno||'--'||'平均工资:'||v_dept.avgsal);
	end loop;

end;

部门号:30--平均工资:1566.666666666666666666666666666666666667
部门号:20--平均工资:2175
部门号:10--平均工资:2916.666666666666666666666666666666666667

带update 的游标

Scott.emp表,利用游标,给工资低于1200 的员工增加工资50。并输出“编码为’员工编码号’的工资已经更新”。

declare

  v_empno scott.emp.empno%type;
	v_sal scott.emp.sal%type;
	cursor c_cursor is select empno,sal from scott.emp where sal <1200 for update;
begin
	open c_cursor;
	loop
		fetch c_cursor into v_empno,v_sal;
		exit when c_cursor%notfound;
		 update scott.emp set sal=sal+50 where current of c_cursor;-- 前面sal<1200
		 dbms_output.put_line('编码为'||v_empno||'工资已更新!!');
		 DBMS_output.put_line('记录数:'||c_cursor%rowcount);
	end loop;
	close c_cursor;

end;

编码为7369工资已更新!!
记录数:1
编码为7876工资已更新!!
记录数:2
编码为7900工资已更新!!
记录数:3

修改scott.emp表员工的工资,如果员工的部门号为10,工资提高100;部门号为20,工资提高150;部门号为30,工资提高200;否则工资提高250。

declare 
    cursor c_emp is select * from scott.emp for update;
     v_zl number;
		 v_emp c_emp%rowtype;
begin
	for v_emp in c_emp loop
		case v_emp.deptno
			when 10 then v_zl:=100;
			when 20 then v_zl:=150;
			when 30 then v_zl:=200;
			else v_zl:=250;
			end case;
			update scott.emp set sal= sal+v_zl where current of c_emp;
	end loop;
end;

7. 触发器

-- 增加一张表xs_1,用来存放从xs表中删除的记录

create table xs_1 as select * from xs;

 

truncate table xs_1;

 

create or replace trigger t1

    before delete on xs

    for each now

     

begin

     insert into xs_1(xh,xm,zym,xb,cssj,zxf) values(:old.xh,:old.xm,:old.zym,:old.xb,:old.cssj,:old.zxf);

end t1;   

 

 

-- 监控用户对xs表的操作,要求:当xs表执行插入,更新和删除操作后在sql-info表中给出相应的提示和执行时间

Create table sql_info(info varchar2(10),time date);

 

create or replace trigger t2

 after delete or insert or update on xs for each row

declare

  v_info sql_info.info%type;

begin

  if deleting then

    v_info:='删除';

  elsif inserting then

    v_info:='插入';

  else

    v_info:='更新';

   

   end if;

   insert into sql_info values(v_info,sysdate);

end t2;

 

--当插入新员工时显示新员工的员工号、员工名;当更新员工工资时,显示修改前后员工工资;当删除员工时,显示被删除的员工号、员工名

 

create or replace trigger t3

before insert or update or delete on scott.emp

for each row

 

begin

     if inserting then

       dbms_output.put_line(:new.empno||'---->'||:new.ename);

      

     elsif updating then

        dbms_output.put_line(:old.sal||'----->'||:new.sal);

       

     else

       dbms_output.put_line(:old.empno||'----->'||:old.ename);

     end if;

end t3

 

 

--触发t3

Set serveroutput on

declare

begin

    update scott.emp set empno=7521 where empno=7522;

  commit;

end;

 

 

 

--针对Scott.emp表,记录其相应操作的信息,具体如下:

--当执行插入操作时,统计操作后员工人数;

--当执行更新工资操作时,统计更新后员工平均工资;

--当执行删除操作时,统计删除后各个部门剩余的人数(游标)。

 

create or replace trigger t4

 

after insert or update or delete on scott.emp

 

declare

   v_1 number;

   v_2 scott.emp.sal%type;

begin

  if inserting then

      select count(*) into v_1 from scott.emp;

      dbms_output.put_line('添加成员后的总人数:'||'---->'||v_1);

  elsif updating then

    select avg(sal) into v_2 from scott.emp;

    dbms_output.put_line('更新记录后平均工资:'||'----->'||v_2);

   else

     for v_s in (select deptno, count(*) num from scott.emp group by deptno)

       loop

       dbms_output.put_line('删除记录后各个部门的部门号和总人数:'||v_s.deptno||'<---->'||v_s.num);

       end loop;

    end if;

end t4;

 

 

 

delete from scott.emp where hiredate<=to_date('1980-12-17','yyyy-mm-dd');

 

 

update scott.emp set empno=1111 where empno=7934;

 

 

 

Select to_char(sysdate,'yyyy-MM-dd HH24:mi')  from dual;

 

 

Select to_char(sysdate, 'DAY')  from dual;

 

-- 建一触发器,作用为禁止在休息日(周六、周天)改变scott.emp雇员信息(包括添加删除和修改)。

create or replace trigger t6

  before insert or update or delete

  on scott.emp

 

begin

  if to_char(sysdate,'day') in ('星期五') then

    raise_application_error(-20222,'不能在星期三修改员工信息');

    end if;

end;



update scott.emp set ename='candy'  where empno=7876;
上一篇:7.oracle表空间管理-非标准块表空间


下一篇:周度学习总结-2021-12-31