Oracle数据库的约束,序列,索引

约束

  1:概念:也叫完整性约束条件。

      是数据表中的一些限制条件,当数据表中的数据发生变化时,必须遵循

      这些限制条件,不遵循,不能进行DML操作。

  2:分类

    (1)非空约束 not null 简称UN

    (2)唯一性约束 unique 简称UK

    (3)主键约束 primary key 简称PK

    (4)外键约束 foreign key 简称FK

    (5)检查约束 check 简称CK

  3:非空约束的建立

    建表时:

    reg:create table tname(

        name varchar2(20) not null,

        age number(3) constraint tname_age_NN not null--列级约束

      );

    约束:列级约束和表级约束两种写法

       列级约束:就是在建表时写在列类型后面。

       表级约束:就是在建表时写在最后一个字段后。

       非空约束没有表级约束的写法。

    建表后修改非空约束:

      解除非空约束:

        alter table tname modify age number(3) null;

      添加非空约束:

        alter table tname modify age number(3) not null;

  4:唯一性约束:unique

    对字段进行此约束时,字段的值是唯一的,不能重复,可以为null。

    reg:

      create table temp_101(

        name varchar2(20) not null,

          idcard varchar2(30),

        constraint t_idcard_uk unique(idcard)--表级约束写法
      );

    建表后设置唯一性约束:

      alter table temp_101 add constraint t_idcard_uk unique(idcard);

  5:主键约束:(非空且唯一)

    主键:是一张表中能给记录唯一标识的字段,通常此字段除了作为唯一标识符,没有其他意义。

    (1)一张表中只能有一个主键约束。其他约束没有个数限制。

    (2)主键约束相当于非空约束和唯一性约束

    (3)主键要选择表中尽可能没有数据逻辑的字段。

  --建表时:

    create table tname(

      id number(3) primary key,

      name varchar2(20)

    );

  6:外键约束

    两张表的两个字段存在关系或者一张表的两个字段的关系。

    其中表B的一个字段的值依赖表A一个字段中的某一个值或者可以为null。

    那么:

    表A叫主表、父表

    表B叫从表,子表

    建表时:

    create table tname(

      empno number(4) primary key,

      mgr number(4),

      constraint te_mgr_fk foreign key(mgr) references tname(empno)
    );

    create table temp(

      empno number(4) primary key,

      mgr number(4) reference temp(empno)

    );

    建表后:

    create table temp_1(

      in number(4),

      mgr number(4)

    );

    alter table temp_1 add constraint t_107_mgr_fk foreign key(mgr) reference temp(empno);

  7:检查约束

    对某一字段或某些字段做一些条件的限制,必须满足条件的可以DML。

    建表时:

    create table temp_108(
      name varchar2(20),
      gender char(1) check(gender in('f','m'))
    );
    insert into temp_108 values ('张三','f');

    create table temp_109(
      name varchar2(20),
      sal number(9,2) check(sal>1000 and sal<10000)
    );

    

    建表后:
    create table temp_110(
      name varchar2(20),
      gender char(3)
    );
    alter table temp_110 add constraint t_gender_ck check(gender ='女' or gender='男');

序列

  1:概念

    是数据库中为数据表提供的可以自动生成的唯一数值,通常用来给主键字段赋值,可以递增或者递减,作为主键的字段,通常不认为赋值,需要使用序列自动生成的值。

  2:关键字

    sequence,序列和表一样,都是数据库中的对象,通常序列为一张表提供主键值,也可以为多张表提供主键值。

  3:序列的创建  

    create sequence seqName

    [start with i] -->从i开始,不指定时默认从1开始

    [increment by j] -->每次递增或递减j/默认递增1,j为正表示递增,为负表示递减

    [maxvalue m | nomaxvalue]-->表示最大值m

    [minvalue n | nominvalue]-->表示最小值n

    [cycle | nocycle]-->表示循环|不循环

    [cache n| nocache]-->表示缓存n个数,默认存储20个数

   两个伪列:nextval:下一个值,currval:当前值,此伪列只有在使用了nextval之后才可以使用。

索引

  1:

    作用:为了方便快速查找数据表中的记录。

    索引也是数据库中的一个对象。

    索引内部信息包含记录的rowid,相当于地址信息。

  2:

    索引被创建后,由数据库自动维护。

  3:

    创建:create [unique] index indexName on tname(colName,[,......]);

  4:

   索引的优缺点:

      当数据表需要频繁进行DML操作时,此时数据库会自动维护索引

      会增加开销,索引索引的使用需要看表的操作情况

    

猜你喜欢

转载自www.cnblogs.com/lyr999736/p/9046498.html