---
title: "oracle学习笔记"
author: "caoyong"
date: "2015-07-19T19:34:17Z"
tags: []
categories: ["编程语言"]
slug: "oracle"
draft: false
lastmod: "2015-07-19T19:34:58Z"
id: 2228566
url: "https://caoyong.iteye.com/blog/2228566"
---

1、ORACLE的安装
   a>、ORACLE的版本
   8i,9i :   i是internet
   10g,11g : grid (网格)
   12c : cloud (云计算)
   
   b>、10g不支持win7
   
   c>、安装路径中不能有中文，空格或其它特殊字符
   
   d>、几个账号
       sys   : change_on_install  : 数据库的拥有者
	   system : manager   : 数据库管理员	   
       scott : tiger
	   hr : hr
       其它: oracle
   e>、管理地址
       Enterprise Manager Database Control URL - (sxt) :
        http://USER-20140724TC:1158/em
        数据库配置文件已经安装到 D:\oracle\product\10.2.0,同时其他选定的安装组件也已经安装到 D:\oracle\product\10.2.0\db_1。
       iSQL*Plus URL 为:http://USER-20140724TC:5560/isqlplus
       iSQL*Plus DBA URL 为:http://USER-20140724TC:5560/isqlplus/dba

    f>、使用ORACLE必须启动的两个服务
	     aa>、OracleServiceSXT
		 bb>、OracleOraDb10g_home1TNSListener
   
   
    g>、使用ORACLE
	     aa>、sqlplus scott/tiger
		 bb>、sqlplusw scott/tiger
		 cc>、iSqlPlus
		 
	h>、在oracle中所有的数据库对象都属于一个特定的用户(schema)，一个用户要访问另一个用户的对象，必须指定用户名
         如:system用户要查询scott的dept表，应写为: select * from scott.dept;	

   
   2>、
       OCA : 3
	   OCP  : 10
	   OCM
	   
   3>、数据库对象
       a>、表
	   b>、视图
	   c>、索引
	   d>、存储过程
	   e>、函数
	   f>、触发器
	   g>、序列
	   h>、同义词
	   i>、包
	   
	   
    4>、scott账号下的表
	     dept (部门表)
		   deptno : 部门编号
		   dname : 部门名称
		   loc : 部门位置
		   
		 emp (员工表)
		    empno : 员工编号
			ename : 员工姓名
			job : 工作岗位
			mgr : 上级的员工中
			hiredate : 雇佣日期
			sal : 工资
			comm : 奖金
			deptno : 部门编号
			
		salgrade (工资等级表)
		   grade : 工资等级
		   losal : 该等级的最低多次
		   hisal : 该等级的最高工资
		
	5>、SQL
	    a>：DML(Data Manipulation Language,数据操作语言)  : select ,update,delete,insert
		b>、DDL(Data Definition Language,数据定义语言)    : crate ,alter , drop ,truncate table 
		c>、DCL(Data Control Lanaguage,数据控制语言)      : grant , revoke
		
	6>、创建表
	    crate table_name (col_name col_type )
		a>char,varchar将别
		b>、oracle的数据类型: char,varchar2,number,date
		
     		create table t_stud(s_id number(8),s_name varchar2(30),in_date date);
			create table t_stud2(s_id integer,s_name varchar(30),in_date date);
			create table d1 as select * from dept where 1 = 2;
			create table e1 as select empno,ename,job,sal from emp where sal > 1000;

         	
		c>、约束(5种)
		   aa>、主键约束    = unique + not null
		   bb>、外键约束
		   cc>、非空约束 not null
		   dd>、检查约束 check
		   ee>、唯一性约束  unique
		   
		   create table t_emp (
		     empno number(10) primary key,
			 ename varchar2(30) unique,
			 job varchar2(20) not null,
			 sal number(8,2) check(sal > 0), 
			 deptno number(5) references dept(deptno)
			)
		    
	7>、数据操作
        a>、转换函数
            to_char(); select empno,ename,job,to_char(hiredate,'yyyy-mm-dd hh24:mi:ss'),sal from t_emp
            to_date(); 字符串转日期			
        		
		b>、delete from emp 与 truncate table emp差别
		     aa>、delete : 逐行删除,truncate先摧毁表再重建
			 bb>、delete是dml语句，可以被回滚，truncate是ddl语句，不可以被回滚
			 cc>、delete语句不释放空间，truncate释放空间
			 dd>、delete会产生碎片，truncate不会
			 
		c>、
		    在ORACLE中，查询语句必须包含from tableName
		    获取当前时间: select  sysdate from dual;
			
	
	8>、查询:
         a>、基本查询
         b>、排序和过滤数据
         c>、单行函数
		    
         d>、多行函数
		      count,sum,avg,max,min			  
			  where : 在分组之间过滤数据,不能使用组函数
			  having : 在分组之后过滤数据
         e>、连接查询
		      等值连接 
			  from emp e ,dept d where e.deptno = d.deptno;
			  from emp e inner join dept d on e.deptno = d.deptno;
			  不等值连接
			  from emp ,salgrade where sal between losal and hisal
			  from emp join salgrade on sal between losal and hisal
			  
			  左外连接
			     from emp e left outer join dept d on e.deptno = d.deptno
			     from emp e left       join dept d on e.deptno = d.deptno
				 from emp e,dept d where e.deptno = d.deptno(+)				 
			  右外连接
			     from emp e right outer join dept d on e.deptno = d.deptno
			     from emp e right       join dept d on e.deptno = d.deptno
				 from emp e,dept d where e.deptno(+) = d.deptno
			  全外连接
			     from emp e full outer join dept d on e.deptno = d.deptno;
			     from emp e full       join dept d on e.deptno = d.deptno;
				 
			  自连接
			   from emp e,emp m where e.mgr = m.empno;
			   from emp e join emp m on e.mgr= m.empno;
		     
         f>、子查询
              select型子查询	
			  
		      from型子查询
			  
			  where型子查询
			  
			  exists/相关子查询
			  
			  
	9>、体系结构
	        a>、ORACLE服务器由实例和数据库组成
			    aa>、ORACLE是通过实例来操作数据文件的
			    bb>数据库由数据文件，日志文件和控制文件构成
				cc>、实例是由SGA(系统全局区)和后台进程构成
				dd>、创建表空间
				
    10>、使用ORACLE开发项目的流程
          a>、为该项目创建一个表空间
              create tablespace dogspace datafile 'c:/data01.dbf' size 100m uniform size 1m;
          b>、为该项目创建一个用户
              create user dog identified by wangwang default tablespace dogspace;
          c>、为该用户授权: 开发用户只要有两个角色:connect,resource	
              grant connt,resource to dog;
		  d>、使用dog登录数据库执行数据库操作
			  
			  
	11>、PL/SQL编程
	     a>、PL/SQL基于PASCAL语言
		 b>、一个程序的基本语法 : 变量,数据类型,运算符，流程控制(顺序结构，选择结构，循环结构)，函数
		 c>、一个PL/SQL程序的语法结构
		     declare
			    变量声明
			 begin
			    程序逻辑
		     exception 
			    异常处理
			 end
			 
		 d>、输出: (打开服务器端输出: set serveroutput on)
		      dbms_outout.put() : 不换行,最后必须调用put_line()，否则输出看不到
			  dbms_output.put_line(); 自动换行
         e>、 所有变量与关键字不区分大小写	

		 f>、语名以分号结尾
		 g>、注释分为单行注释--与块注释 /* */
		 h>、字符串用单引号引起来,字符串连接符为||
         i>、赋值运符为 := , 比较两个值是否相等用 = 
         j>、可以在PL/SQL中直接调用SQL函数		 
		 k>、逻辑运算为:not and or 
		 l>、求余符号为mod : 17 mod 5
		 m>、%type,%rowtype
		
	12>、游标(处理多行查询)
	     1>、处理多记录查询
		 2>、使用过程
		     a>、声明游标及存储每行记录的变量
			     cursor dept_cur is select * from dept; 
				 rec_dept dept.%rowtype;
			 b>、打开游标
                  open dept_cur;
             c>、提取游标每行数据(循环)
                  fetch dept_cur into rec_dept;	
             d>、关闭游标
                  close dept_cur;			 
	13>、查询部门的所有数据
			declare
			  cursor dept_cur is select * from dept;
			  dept_rec dept%rowtype; --存储每行数据的变量
			begin
			  open dept_cur;
			  /*
				游标的属性
				%notfound : 没有提取到数据
				%found : 提取到了数据
				%rowcount : 查询的总记录
				%isopen : 游标是否是打开的
			   */
			  loop
				fetch dept_cur into dept_rec;
				exit when dept_cur%notfound;
				dbms_output.put_line(dept_rec.deptno || '   ' || dept_rec.dname || '   ' || dept_rec.loc);
			  end loop;
			  close dept_cur;
			end;
	  
     14>、存储过程
          create  procedure 过程名称 (参数1  参数类型,..)
		  is/as
		     --变量声明
		  begin
		     --PL/SQL程序 
		  exception
		     --异常处理
		  end;
	 15>、函数
	      create or replace function 函数名称 (参数1 参数类型)  return 返回值类型
		  is/as
		    --变量声明
		  begin
		    --PL/SQL程序
		  exception
		     --异常处理
		  end;
	 
	 16>、包(package)
	       a>、将一组相关的存储过程，函数，数据类型等封装起来，以便于管理和使用
		   b>、声明包头(接口)
		      create or replace package emp_pkg
			  is
			    procedure  sayHello;
				procedure showEmpInfo(eno emp.empno%type);
				function  getEmpIncome(eno emp.empno%type) return number;
			  end;			  
		   
		   b>、实现包体
		      create or replace package body emp_pkg
			  is
			    procedure sayHello
                as
                begin
                   dbms_output.put_line('Hello World');
                end;
                
                procedure showEmpInfo(eno emp.empno%type)
                  is
                    emp_rec emp%rowtype;
                  begin
                     select * into emp_rec from emp where empno=eno;
                     dbms_output.put_line('姓名:' || emp_rec.ename || ', 工作岗位:' || emp_rec.job ||', 工资:' || emp_rec.sal );
                  exception
                     when no_data_found then
                         dbms_output.put_line('员工不存在!');
                  end;

                function getEmpIncome(eno emp.empno%type) return number
					is
					  total number(8,2);
					begin
					   select sal * 12 + nvl(comm,0) into total from emp where empno=eno;

					   return total;
					end;				  
		    end ;
	 
	 17>、触发器
	      a>、触发器是当特定的操作(insert,update,delete)发生时，自动执行的一段PL/SQL代码
		  b>、触发器不能被程序显式调用
		  c>、
		     create or replace trigger 触发器名
			 before/after   --触发时机
			 on 表名
			  -- 行级触发器，没有为语句级触发器
			 begin
			    --PL/SQL代码
			 end;

     18>、数据的导入导出 (exp,imp)
	      1>、导出指定用户的指定表
		       exp scott/tiger file=c:/tables.dmp log=c:/log.log tables=emp,dept
          2>、导出指定用户下的所有对象
		       exp scott/tiger file=c:/tables.dmp log=c:/log.log 
		  3>、导出整个数据库(需管理员权限)
		       exp system/manager file=c:/full.dmp log=c:/log.log full=y
			   
		  4>、导入指定表
              imp scott/tiger file=c:/tables.dmp log=c:/log.log tables=emp,dept commit=y ignore=y
		  5>、导入指定用户的所有对象
              imp scott/tiger file=c:/scott.dmp log=c:/log.log  commit=y ignore=y	  
				
		  6>、将scott的数据导入到另一账号(dog)下
		      imp system/manager file=c:/scott.dmp log=c:/log.log  commit=y ignore=y fromuser=scott touser=dog
		  
		  7>、导入整个数据库(需要管理员权限)
		    imp system/manager file=c:/full.dmp log=c:/log.log full=y commit=y ignore=y
	 19>、使用JDBC访问ORACLE