【oracle资料整理】--【8】约束

来源:互联网 发布:mp5播放器软件下载 编辑:程序博客网 时间:2024/05/17 22:20

约束
   主键约束 --  每个表要有主键,唯一的标识一行数据
   非空约束
   唯一性约束
   外键约束
   检查约束
     脚本(SCRIPT)
        create table cla( --班级表
          id number(2) primary key, --班级编号
          cname varchar2(20) not null --班级名字
       );

        create table stu( --学生表
          xh number(4) primary key, --学号是主键
          xm varchar2(20) not null, --姓名非空
          age number(2) check (age between 10 and 90),--年龄在10到90之间(10<= age  <=90 )
          birthday date,
          shenfenzheng number(18) unique, --身份证唯一 
          classid number(2) references cla(id) -- 班级编号外键
           --(引用的一定是主键或唯一性约束的字段)
         );
       
      <1>建立表的同时使用约束
  create table student( --学生表
           xh number(4) primary key, --学号主键
           xm varchar2(10) not null, --姓名不能为空
           sex char(2)  check (sex in ('男','女')), --性别
           birthday date unique, --日期
           sal number(7,2) check (sal between 500 and 1000),--奖学金 sal >=500 and sal <=1000
     classid number(2) references cla(id)
        );  --必须要先有cla表才对
            --一定先建立班级cla表
 
   主键约束 primary key
           not null
           check
           unique 唯一约束          

 create table student( --学生表
           xh number(4) constraint pk_stu primary key, --学号主键
           xm varchar2(10) constraint nn_stu not null, --姓名不能为空
           sex char(2) constraint ck_stu_sex check (sex in ('男','女')), --性别
           birthday date constraint uq_bir unique, --日期
           sal number(7,2) constraint ck_sal check (sal between 500 and 1000)--奖学金 sal >=500 and sal <=1000
        );
      <2>建立约束的同时给约束指定名字,便于删除
        create table cla( --班级表
          id number(2) constraint pk_cla primary key, --班级编号
          cname varchar2(20) constraint nn_cla not null --班级名字
       );
     
      create table stu( --学生表
          xh number(4) constraint pk_stu primary key, --学号是主键
          xm varchar2(20) constraint nn_stu not null, --姓名非空
          age number(2) constraint ck_stu check (age between 10 and 90),--年龄在10到90之间(10<= age  <=90 )
          birthday date,
          shenfenzheng number(18) constraint uq_stu unique, --身份证唯一 
          classid number(2) constraint fk_stu references cla(id) -- 班级编号外键
           --(引用的一定是另外表的主键或唯一性约束的字段)
         );
  
 
      <3>建完表后加约束
 
 学生表student
        create table student( --学生表
           xh number(4), --学号
           xm varchar2(10), --姓名
           sex char(2), --性别
           birthday date, --日期
           sal number(7,2) --奖学金
        );
 加约束
   加主键
    alter table student add constraint pk_stu
    primary key (xh);
   加非空
    alter table student modify (xm not null);
   检查约束
    alter table student add check(sex in ('男','女'));
    alter table student add constraint ck_sal check(sal between 500 and 1000));


 给student加班级字段
   alter table student add (classid number(2));


   班级表cla
    create table cla( --班级表
          id number(2), --班级编号
          cname varchar2(20) --班级名字
       );

添加 主键
 alter table cla add constraint pk_cla
       primary key (id);
加 not null
 alter table cla modify
       (cname not null);

               学生表student
      create table student( --学生表
          xh number(4) ,
          xm varchar2(20) , --姓名非空
          age number(2),--年龄在10到90之间(10<= age  <=90 )
          birthday date,
          shenfenzheng number(18), --身份证唯一 
          classid number(2) -- 班级编号外键
           --(引用的一定是另外表的主键或唯一性约束的字段)
         );

加外键约束
alter table student add constraint fk_stu
    foreign key (classid) references cla(id);

加主键
alter table student add constraint pk_stu
 primary key (xh);

加not null
alter table student modify(xm not null);

加检查约束
alter table student add constraint cc_age
 check (age >= 10 and age <=90);

加唯一约束
  alter table student add constraint
      uq_sfz unique(shenfenzheng);

加外键约束
 alter table student add constraint
    fk_stu foreign key (classid)
     references cla(id);

如何删除约束
  
  alter table student drop constraint
        fk_stu;
可以用一个统一的格式来删除
  alter table 表名 drop constraint 约束名

  <4>如何查看约束?? 约束一定加在表上

    一个表上到底有哪些约束???
  select constraint_name,constraint_type
      from user_constraints
        where table_name = 'STUDENT'
--查看表上有什么约束
  select * from user_constraints;
--查看约束作用在什么字段上
  select * from user_cons_columns
   where CONSTRAINT_NAME='PK_STU';

user_constraints数据字典表


  <5>约束是如何起作用的??

       create table cla( --班级表
          id number(2) constraint pk_cla primary key, --班级编号
          cname varchar2(20) constraint nn_cla not null --班级名字
       );
     
      create table stu( --学生表
          xh number(4) constraint pk_stu primary key, --学号是主键
          xm varchar2(20) constraint nn_stu not null, --姓名非空
          age number(2) constraint ck_stu check (age between 10 and 90),--年龄在10到90之间(10<= age  <=90 )
          birthday date,
          shenfenzheng number(18) constraint uq_stu unique, --身份证唯一 
          classid number(2) constraint fk_stu references cla(id) -- 班级编号外键
           --(引用的一定是另外表的主键或唯一性约束unique的字段)
         );
  
   主键 = 非空 + 唯一
   非空
   唯一 = 有值的话  值要不同
          null的话  都是可以的
   外键 = 有值 一定要在被引用的表的数据中
          null的话  是可以的 

原创粉丝点击