oracle对象【约束,序列,索引,视图,同义词】

一:约束 
 
        --直接添加不为空的约束 


       create table tb_userinfo( 

                    userid number primary key, --primary key表示主键 (唯一 并且不能为空) 
 

                   username varchar2(20) not null, --not null 表示当前的列 不允许插入 null 和 '' 


                ) 
 

        --直接添加不为空的约束 给约束添加别名 
 

       create table tb_userinfo( 
 

                    userid number primary key, --primary key表示主键 (唯一 并且不能为空) 
 

                   username varchar2(20) [constraint nullable_username] not null, --添加别名后 会添加一个别名对应的约束 
 

               ) 
 

        --通过check的方式 添加不为空的约束  check的语法要和where条件一致         
 

        create table tb_userinfo( 
 

                    userid number primary key, --primary key表示主键 (唯一 并且不能为空) 
 


                   username varchar2(20) [constraint check_username]  check(username is not null) --通过添加约束的方式 添加not null 
 


        )     
 

        --定义唯一约束  唯一约束相当于添加了一个唯一索引的列 
 

       create table tb_userinfo( 
 

                    userid number [constraint uni_userid] unique 
 

        )   
 

        --check约束必须满足where条件  不能使用子查询 
 

       create table tb_userinfo( 
 

               userid number primary key, 
 

               username varchar2(20) not null, 
 

               sex number constraint check_sex check(sex in(0,1)) 
 

        )    
 

        --primary key表示列上添加了 唯一约束+不为空约束  会为列上添加一个唯一索引  可以大大的提高效率 一张表的设计 一定要有一个主键 
 

       create table tb_userinfo( 
 

               userid number [constraint pri_userinfo_userid] primary key 
 

         ) 
 


       --定义外键  外键的定义 必须能唯一定位到外键对应的记录 比如 知道学生信息后 就唯一确定了他所在的班级 
 


       --班级表 
 


       create table tb_grade( 
 


           cid  number primary key, 
 


           cname varchar2(20) not null 
 


        ) 
 


        --cid number check(cid in(select cid from tb_grade)) 用于理解 
 


       create table tb_student( 
 


           sid number primary key, 
 


           sname varchar2(20) not null, 
 


           cid number [constraint FOR_STUDENT_CID] references TB_GRADE (CID)         
 


        ) 
 


        --外键的引用 必须通过alter table的方式来添加 
 


       alter table tb_student add constraint FOR_STUDENT_CID foreign key (CID) 
 


                      references TB_GRADE (CID)         
 


        --删除外键必须通过名称  未定义名称时 需要通过      dba_constraints表去查询         
 


        alter table tb_student drop constraint SYS_C008553    
 


        --查询表所有的外检 类型  C表示check约束 P代表主键  R代表外检  
 


        select * from dba_constraints  where table_name='TB_STUDENT'                    
 


二:序列 
 


        --创建序列  minvalue表示范围中最小的那个值 maxvalue表示范围中最大的那个值 
 


       --start with 表示序列从1开始递增  increment by表示步值(每次累加值) 
 


       create sequence TB_GRADE_SEC 
 


        minvalue 1 
 


        maxvalue 999999999999999999999999999 
 


        start with 1 
 


        increment by 1 
 


        cache 20; 
 


        --查询当前值得下一个值 
 


       select TB_GRADE_SEC.NEXTVAL from dual; 
 


        --查询当前值 
 


       select TB_GRADE_SEC.Currval from dual; 
 


        select * from tb_grade; 
 


        --在insert语句中使用序列 
 


       insert into tb_grade values(TB_GRADE_SEC.NEXTVAL,'test'); 
 


        commit;               
 


        --删除序列 
 


       drop sequence tb_grade_sec               
 


三:索引 
 


        索引候选列需要存储很大范围(重复的范围 每一个值都不一样 就是范围大)的值——“B-树”索引 
 


        索引候选列只包含很少范围(比如列上的值 都是某几个枚举的值  男,女)的值——“位图”索引 
 


        --在列上使用 unique,primary key可以自动添加btree索引 
 


       create table TB_STUDENT 
 


        ( 
 


          SID   NUMBER  primary key not null, 
 


          SNAME VARCHAR2(20) unique, 
 


          sex number check(sex in(0,1)), 
 


          CID   NUMBER 
 


        ) 
 


        --唯一索引(BTree索引) map.put(1,'test') 创建btree的语句 唯一索引 可以直接定位行 效率最高  
 


        create UNIQUE index index_student_sid on TB_STUDENT(sid) 
 


        --normal索引(BTREE索引) 表示列允许出现重复 但是不能出现大范围的重复 否则效率降低 
 


       create index index_student_sid on TB_STUDENT(sid)  
 


        --创建位图索引 大范围的重复时 使用索引 
 


       create bitmap index bitmap_student_sex on TB_STUDENT(sex) 
 


        --创建基于函数的索引 
 


       create index upper_sname on tb_student(upper(sname)) 
 


        --删除索引 
 


        drop index upper_sname                   
 


    四:视图 
 


    CREATE [OR REPLACE] VIEW 视图名  
 


          AS 查询语句 [WITH CHECK OPTION] [WITH READ ONLY]; 
 


        选项: 
 


       OR REPLACE:视图存在时就替换 
 


       WITH CHECK OPTION:视图的创建条件不能更改 
 


       WITH READ ONLY:视图中的内容不能更改 
 


       --创建视图 用于重复利用相同的sql 可以用于权限控制  可以隐藏机密的数据 
 


       create or replace view vi_student_grade as 
 


        select s.sname,g.cname from tb_student s inner join tb_grade g on s.cid=g.cid where cname='1501' WITH CHECK OPTION 
 


        --查询 
 


       select * from vi_student_grade where cname='1501' 
 
   

        create or replace view vi_student as select * from tb_student; 
 


        --直接通过表更新,删除 
 


       update tb_student set sname='test' where sid=10; 
 


        --单表视图 可以用于间接更新,删除 
 


       update vi_student set sname='test' where sid=10; 
 


        删除视图 
 


       DROP VIEW 视图名; 
 


    同义词 
 


        --创建同义词 
 


        CREATE SYNONYM syn_scott_emp FOR scott.emp; 
 


         select * from syn_scott_emp;     
 


         drop SYNONYM  syn_scott_emp 
 


                              
 


 


   




 
原文地址:https://www.cnblogs.com/t0404/p/10291067.html