• Oracle_数据库表的约束


    Oracle_数据库表的约束

    完整性约束分类
    域完整性约束 (非空not null,检查check)
    实体完整性约束 (唯一unique,主键primary key)
    参照完整性约束 (外键foreign key)
    三种完整性约束的区别
    域完整性约束:字段约束
    实体完整性约束:行和行之间的约束
    引用完整性约束:表和表之间的约束
     

    实例:

    --设计要求:
    --建立一张用来存储学生信息的表
    --字段包含学号、姓名、性别,年龄、入学日期、班级,email等信息
     
     
    --学号是主键
    --姓名不能为空
    --性别默认值是男
    --年龄范围18至30岁
    --Email唯一  
    完整性约束
     
     
    --完整性约束
    --创建表时添加约束,也可以在修改表时添加约束
     
    --主键约束 primary key
    --联合主键(不能在列级别定义,因为一个表只允许有一个主键),只有多列同时相同的时候才算重复,多列都不允许为null
    drop table stu;
    create table stu(
           id number(6),
           sname varchar2(12),
           gender char(3),
           age number(2),
           enterdate date,
           clazz varchar2(30),
           email varchar2(100),
           --constraints pk_id primary key (id)    
           constraints pk_id primary key (id, sname)  
    );  


     
     
    --非空约束 not null, 只能在列级定义
    drop table stu;
    create table stu(
           id number(6) primary key,
           sname varchar2(12)  not null,
           gender char(3),
           age number(2),
           enterdate date,
           clazz varchar2(30),
           email varchar2(100), 
           --constraints pk_id primary key (id)
    );  


     
     
    --唯一约束 unique
    drop table stu;
    create table stu(
           id number(6) primary key,
           sname varchar2(12)  not null,
           gender char(3),
           age number(2),
           enterdate date,
           clazz varchar2(30),
           email varchar2(100), 
           --constraints pk_id primary key (id),
           constraints uk_email unique (email)
    );  


     
     
    --检查约束  check
    drop table stu;
    create table stu(
           id number(6) primary key,
           sname varchar2(12)  not null,
           gender char(3) default '男' check(gender in('男','女')),
           age number(2) check(age between 18 and 30),
           enterdate date,
           clazz varchar2(30),
           email varchar2(100) unique
           --constraints pk_id primary key (id),
           --constraints uk_email unique (email),
           --constraints ck_age check (age>=18 and age <=30)
    );
     
    insert into stu(id,sname) values(100001,'阿三');  


     
     
    --外键约束  foreign key
    --先有主表dept,后有依赖表emp
    drop table clazz;
    create table clazz(
           id number(3),
           cname varchar2(30) not null,
           constraints pk_clazz_id primary key(id)
    );
    insert into clazz values(405,'JavaEE班');
    insert into clazz values(406,'Android班');
    insert into clazz values(407,'iOS班');
    insert into clazz values(408,'UI班');
     
     
    drop table stu;
    create table stu(
           id number(6),
           sname varchar2(12)  not null,
           gender char(3) default '男',
           age number(2),
           enterdate date,
           email varchar2(100),
           cid number(3),
           constraints pk_stu_id primary key (id),
           constraints ck_stu_gender check(gender in ('男','女')),
           constraints ck_stu_age check (age between 18 and 30),
           constraints un_stu_email unique (email),
           --constraints fk_stu_cid foreign key(cid) references clazz(id) on delete cascade--级联删除,将子表中相关项一起删除
           constraints fk_stu_cid foreign key(cid) references clazz(id) on delete set null--级联删除,将字表对应的列设置为null
    );
     
    insert into stu values(100002, '小明1', '男', 18, sysdate, 'ming@162.com',405);
    insert into stu values(100004, '小明1', '男', 18, sysdate, 'ming@163.com',406);  
     
    delete from clazz where id=405;
     
     
    select * from stu;
    select * from clazz;  
     
    drop table clazz;--表中的主键/唯一键被引用
    --强制将主键删除
    drop table clazz cascade constraints;  

     
     
     
    --修改表的时候添加约束信息
    drop table stu;
    create table stu(
           id number(6),
           sname varchar2(12) not null,
           gender char(3) default '男', 
           age number(2),
           enterdate date,
           email varchar2(100),
           clazz varchar2(30)
             
    );
     
    alter table stu add constraints pk_stu_id primary key (id);
    alter table stu add constraints ck_stu_gender check(gender in ('男','女','妖'));
    alter table stu add constraints ck_stu_age check (age between 18 and 30);
    alter table stu add constraints un_stu_email unique (email);
    alter table stu rename column clazz to cid;
    alter table stu modify(cid number(3));
    alter table stu add constraints fk_stu_cid foreign key(cid) references clazz(id) on delete cascade;
     
     
    --删除约束  不能修改,只能删除约束
    alter table stu drop constraints ck_stu_gender;  


    sequence序列
     
     
    --序列: sequence
    --创建序列
    create sequence seq_stu_id start with 10000 increment by 1;
     
    --删除序列
    drop sequence seq_stu_id;
     
     
    --查看序列
     
     
    insert into stu values (seq_stu_id.nextval, '小明1', '妖', 18, 
    sysdate,'ming@163.com', 406);
     
    select * from stu;  


    index索引
     
    --索引: index
    --创建索引
    create index index_stu_sname on stu(sname);
    --删除索引
    drop index index_stu_sname;
     
    select sname from student;

    create index index_cname on clazz(cname desc);
    select cname from clazz;  
      

     
     
     
     
     

     
  • 相关阅读:
    新安装的Apache和php,测试可以解析phpinfo,但是无法打开drupal网站
    Drupal7安装注意事项
    drupal7 为视图添加 过滤标准 内容类型
    Drupal网站报错:PDOException: in lock_may_be_available()
    窗口聚合函数与分组聚合函数的异同
    Linux环境下段错误的产生原因及调试方法小结(转)
    gzip 所使用压缩算法的基本原理(选摘)
    InfluxDB使用纪录
    子网掩码解释(转)
    列存的压缩原理学习
  • 原文地址:https://www.cnblogs.com/haozhengfei/p/6538327.html
Copyright © 2020-2023  润新知