Oracle学习系列7
************************************************************************************

关联表的约束:
    强制删除关联表中的父表:
        drop table tab_name cascade constraint ;

    约束本身是可以修改的,但是不建议修改约束

知识点:

        ,掌握视图的作用及定义
        ,掌握序列的使用:SEQUENCE
        ,掌握PowerDesigner设计工具的使用
        ,了解同义词,了解用户管理,了解嵌套表及可变数组
        ,理解数据库的设计范式

--------------------------------------------------------

        ,视图:

                封装了一条复杂的查询语句

            语法:

                create view view_name
                    as 子查询
                    {with check option | with read only } ; //不能更新视图的创建条件,不能更改视图数据

            建立一个视图,此视图包含了全部部门20的信息:
                create view view_emp20
                    as
                        select  empno,ename, job,hiredate
                            from emp
                                 ;

            查询视图:

                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
                        ) years
                            from emp e, dept d
                                where e.deptno=d.deptno
                                     group by d.dname ;

                更新视图:

                        修改视图中7369的部门编号:
                            update view_emp20

                                    ; //提示更新成功,但是视图表中无编号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 ;

            在序列中提供两种操作:
                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(),
                proname  varchar(),
                prodate date

        ) ;
        /                 //最后一个‘/' 不可少

        //使用porject_nt类型
        create type project_nt as table of project_ty;
         /                    //最后一个‘/' 不可少

        create table department(

                deptno  number() primary key not null,
                dname   varchar2() not null,
                projects   project_nt

            ) nested table projects store  as project_nt_tab_temp ;

        对于插入数据来讲,需要指定每个project_ty的数据类型
        insert into department(deptno,dname,projects)
            values(
                ,'技术部',
                project_nt(
                    project_ty(,'erp',sysdate),
                    project_ty(,'crm',sysdate),
                    project_ty(,'oa',sysdate),

                )

            );

        查询:
            select * from department ;

        若此时需要查看一个部门的全部项目的话,则需要查询嵌套表:
             ) ;

        更新:
        update table ( ) pro
            ','测试项目',to_date('2016-02-12','yyyy-mm-dd'))
                     ;

--------------------------------------------------------
可变数组(了解):

        属于嵌套表的升级版,在可变数组中手机上就是将内部的嵌套表的内容的长度进行了限制。

        //定义类型
        create type worker_info as object(

            id        number,
            name    varchar2(),
            sex     varchar2()

        );
        /

        //定义数组类型
        create type worker_info_list ) of worker_info ;
        /

        //创建可变数组表
        create table department(

            deptno        number()    primary key not null,
            dname        varchar2()    not null,
            workers        worker_info_list

        );

        //插入数据

        insert into department(deptno,dname,workers)
            values( ,'后勤部',
                worker_info_list(
                    worker_info(,'dustin','F'),
                    worker_info(,'kevin','F'),
                    worker_info(,'allen','M')

                )

            );

--------------------------------------------------------

数据库设计范式(了解):

        .第一范式(确保每列保持原子性)
        .第二范式(确保表中的每列都和主键相关)
        .第三范式(确保每列都和主键列直接相关,而不是间接相关)

--------------------------------------------------------

数据库设计工具(重点):
            powerDesigner工具的使用
05-11 15:39