zoukankan      html  css  js  c++  java
  • Oracle学习系列7

        
    Oracle学习系列7
    ************************************************************************************
                
    关联表的约束:
        强制删除关联表中的父表:
            drop table tab_name cascade constraint ; 
            
        约束本身是可以修改的,但是不建议修改约束
        
    
    知识点:
            
            1,掌握视图的作用及定义
            2,掌握序列的使用:SEQUENCE
            3,掌握PowerDesigner设计工具的使用
            4,了解同义词,了解用户管理,了解嵌套表及可变数组
            5,理解数据库的设计范式
    
    
    --------------------------------------------------------
    
            1,视图:
                
                    封装了一条复杂的查询语句
                    
                语法:
                
                    create view view_name 
                        as 子查询  
                        {with check option | with read only } ; //不能更新视图的创建条件,不能更改视图数据
                        
                建立一个视图,此视图包含了全部部门20的信息:
                    create view view_emp20 
                        as  
                            select  empno,ename, job,hiredate 
                                from emp 
                                    where deptno=20 ;
                                    
                查询视图:
                    
                    sele * from view_emp20 ;
                    
                删除视图:
                
                    drop view view_name ;
                    
                    ex: 
                        drop view view_emp20 ;
                    
                    
                完整语法格式:
                
                    create or replace view  view_name
                        as 子查询   //系统会为用户自动进行删除及重建的功能
                        
                        
                ex:
                    create or replace view view_emp20
                        as 
                            select d.dname, count(e.empno), avg(e.sal), avg(months_between(sysdate,e.hiredate)/12) years
                                from emp e, dept d
                                    where e.deptno=d.deptno
                                         group by d.dname ;
                    
                    更新视图:
                        
                            修改视图中7369的部门编号:
                                update view_emp20 
                                    set deptno=30
                                        where empno=7369; //提示更新成功,但是视图表中无编号7369的雇员
    
    
    --------------------------------------------------------
    序列(重点)
    
        在oracle完成自动序列增长的功能,则只能依靠序列完成
        格式:
                
            create sequence  seq_name 
                [increment by n] [start with n]
                [{maxvalue n |nomaxvalue}]
                [{minvalue n | nominvalue }]
                [{cycle | nocycle}]
                [{cache n | nocache }];
                
            删除:
                drop sequence seq_name
            
            
            
            
            
            ex:创建一个myseq的序列,验证自动增长的操作:
            
                create sequence myseq  increment by 2;
                
                在序列中提供两种操作:
                    nextVal:取得序列的下一个内容
                    currVal:取得序列的当前内容
                    
                    
                创建表test_seq:
                    create table test_seq(
                     
                            curr number,
                            next number
                    
                    );
                                                                                    
                使用序列:
                    insert into test_seq(curr,next)
                        values(myseq.currval,myseq.nextVal)  ;
    
    --------------------------------------------------------
    同义词(了解):
            
            功能:可以让其他用户通过一个名称方便的访问‘user.table_name’.
            
            语法:
            
                create  synonym  syn_name  for  user.tab_name  ; //建立
                
                drop  synonym syn_name ;   //删除
                
            ex: create synonym   dual  for sys.dual ;
                
                ------------------------------------------
            
                select  sysdate from dual ;//dual是张虚拟表
                
                conn sys/change_on_install as sysdba ;
                select * from tab 
                    where TNAME='DUAL';//在sys用户下存在此表
                    
    
    --------------------------------------------------------
    用户管理(了解):
    
            语法:    
                create user user_name  identified by passwd //创建用户(sys用户权限)
                    [default tablespace  default_tablespace]
                    [temporary tablespace  temporary_tablespace ]
                    
                grant 权限1,权限2,权限3.. to user ;//给用户授权   create session
                
                alter user user_name identified by passwd_new ; //修改用户密码
                
             eg:
                 create user kevin  identified by root ;
                grant  conncet, resource to kevin ; 
                
            角色:connect 、resource   //每个角色有好多不同的权限
    /锁住用户:
                alter user user_name  account  lock /unlock;
                
                eg: 
                    alter user kevin account lock /unlock ;
                    
                    
                    
                    授权emp表:
                        grant 权限1,权限2  on   user_name.tabl_name  to user_name ;
                将scott用户下的emp表的查询权限及删除权限给kevin:
                        grant select ,delete on scott.emp to kevin;
                        
                        
                    回收权限:
                        
                        revoke 权限1,权限2,.. on  user.tab_name  from user_name ;
                        
                        eg:
                            revoke select ,delete on scott.emp from kevin ;
                                                         
                                            
            grant/(revoke)  权限1,权限2,权限3   on  user.tab_name  to / (from)  user_name ;                    
                                                        
    --------------------------------------------------------
    
    数据库的备份与恢复(了解):
    
            数据库备份: exp
            
                    D:data>exp         
                    
            数据库恢复:    imp        
                            
                    D:data>impf    
    
            查看错误:show error 
    --------------------------------------------------------
    嵌套表(了解):
    
                在一个表中包含另外一个子表
            //创建project_ty类型
            create type project_ty as object(
            
                    proid number(4),
                    proname  varchar(50),
                    prodate date 
            
            ) ;
            /                 //最后一个‘/' 不可少
            
            
            //使用porject_nt类型
            create type project_nt as table of project_ty;
             /                    //最后一个‘/' 不可少
    
            create table department(
            
                    deptno  number(2) primary key not null,
                    dname   varchar2(50) not null,
                    projects   project_nt
            
                ) nested table projects store  as project_nt_tab_temp ;
    
    
            对于插入数据来讲,需要指定每个project_ty的数据类型
            insert into department(deptno,dname,projects)
                values(
                    1,'技术部',
                    project_nt(
                        project_ty(1001,'erp',sysdate),
                        project_ty(1002,'crm',sysdate),
                        project_ty(1003,'oa',sysdate),
                    
                    )
                
                );
    
    
            查询:
                select * from department ;
    
            若此时需要查看一个部门的全部项目的话,则需要查询嵌套表:
                select * from table( select projects from department where deptno=1 ) ;
    
            更新:
            update table (select projects from department where deptno=1 ) pro
                set values(pro )=project_ty('1001','测试项目',to_date('2016-02-12','yyyy-mm-dd'))
                        where pro.proid=1001 ; 
    
    
    --------------------------------------------------------
    可变数组(了解):
            
            属于嵌套表的升级版,在可变数组中手机上就是将内部的嵌套表的内容的长度进行了限制。
            
            //定义类型
            create type worker_info as object(
            
                id        number,
                name    varchar2(50),
                sex     varchar2(6)
            
            );
            /
            
            //定义数组类型
            create type worker_info_list as varray(10) of worker_info ;
            /
    
    
            //创建可变数组表
            create table department(
            
                deptno        number(2)    primary key not null,
                dname        varchar2(50)    not null,
                workers        worker_info_list
            
            );
    
            
            //插入数据
            
            insert into department(deptno,dname,workers)
                values( 20,'后勤部',
                    worker_info_list(
                        worker_info(1,'dustin','F'),
                        worker_info(2,'kevin','F'),
                        worker_info(3,'allen','M')
                    
                    ) 
                
                );
    
    
    --------------------------------------------------------
    
    数据库设计范式(了解):
    
            1.第一范式(确保每列保持原子性)
            2.第二范式(确保表中的每列都和主键相关)
            3.第三范式(确保每列都和主键列直接相关,而不是间接相关)
                
    
    --------------------------------------------------------
    
    数据库设计工具(重点):
                powerDesigner工具的使用

     

  • 相关阅读:
    设计模式之单例模式
    常用正则表达式
    前台页面json格式数据中文显示问号
    关于SpringMVC中静态资源配置问题
    Tomcat 9启动后控制台中文输出乱码问题
    SpringAOP中 order属性问题
    避坑!Mybatis Generator 生成文件的时候错误使用了其它数据库的同名数据表(关于nullCatalogMeansCurrent参数)
    关于JDBC中 Statement 和 PreparedStatement的区别
    关于Class.newInstance()方法被弃用
    Celery
  • 原文地址:https://www.cnblogs.com/askDing/p/5468830.html
Copyright © 2011-2022 走看看