oracle数据库中约束条件(五种(主键、外键、非空、唯一、检查))

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/HS2529077916/article/details/101165794

约束条件(五种(主键、外键、非空、唯一、检查)):

约束条件 解释
primary key 主键约束
foreign key 外键约束
not null/ null 非空约束/空约束
check 检查约束
unique default ‘默认值’ 唯一约束
  • primary key
    是表中的一个或多个字段,它的值用于唯一地标识表中的某一条记录
    特点:唯一、不为空、易于区分

    • 建表时添加约束

      --注释 constraint 约束名 primary key(约束列)
      create table student(
          id number(8,0),
          name varchar2(20),
          constraint pk_id primary key(id)
      );
      --或者
      create table student(
          id number(8,0) primary key,
          --如果此处这样定义主键,则主键名称系统自己定义设置 
          --id number(8,0) constraint pk_id primary key;
          name varchar2(20)
      );
      
      
    • 建表后添加约束

      --alter table 表名 add constraint 主键名 primary key(要设为主键的列名);
      create table student(
          id number(8,0),
          name varchar2(20)
      );
      alter table student add constraint pk_id primary key(id);
      
    • ​ 删除主键

      --1. alter table 表名 drop constraint 主键名
      	alter table student drop constrait pk_id;
      --2. alter table 表名 drop primary key
      	alter table student drop primary key;
      
  • foreign key
    该表是另一个表之间联接的字段,外键的用途是确保数据的完整性。
    注意:
    1.必须为另一个表中的主键;
    2.一般外键名称为”fK_”开头;

    • 建表时添加约束

      --constraint 外键名(一般外键名称为”fK_”开头) foreign key (要设为外键的列名) references 主表名(主表中该列列名)
      create table dept(
          deptid number(8,0) primary key,
          deptname varchar2(20),
          constraint fk_deptid foreign key(deptid) references dept(deptid)
      );
      
    • 建表后添加约束

      --alter table 从表名 add constraint 外键名称 foreign key(要设为外键的列名) references 主表名(主表中该列列名);
      alter table student add constraint fk_deptid foreign key(deptid) references dept(deptid);
      
    • 删除外键

      --alter table "表名" drop constraint "外键名"
      alter table dept drop constraint fk_deptid;
      
  • not null

    • 建表时添加约束

      create table student(
          id number(8,0),
          name varchar2(20) not null,
          constraint pk_id primary key(id)
          --注释 constraint 约束名 primary key(约束列)
      );
      
    • 建表后添加约束

      扫描二维码关注公众号,回复: 7613631 查看本文章
      --alter table 表名 modify 列名 not null/null;
      --如果表中已经存在null,就不能更改其为not null约束
      alter table student modify name not null;
      
    • 删除 非空约束

      --alter table 表名 modify 列名 null;
      --删除非空,就是将其设为空
      alter table student modify name null;
      
  • check

    • 建表时添加约束

      --constraint 列名 check(检查约束的条件);
      create table student(
       id number(8,0) primary key,
       name varchar2(20),
       age number(3,0),
       constraint name check (age>=15 and age<=25)
      );
      
    • 建表后添加约束

      --alter table 表名 add constraint 列名 check(检查条件);
      alter table student add constraint age check(age>=15 and age<=25);
      
    • 删除检查约束

      --alter table 表名 drop constraint 列名;
      alter table student drop constraint age;
      
  • unique

    • 建表时添加约束

      --constraint unique_列名 unique(列名)
      create table student(
       id number(8,0) primary key,
       name varchar2(20),
       age number(3,0),
       constraint unique_name unique(name)
      );
      
    • 建表后添加约束

      --alter table 表明 add constraint unique_列名 unique(列名);
      alter table student add constraint unique_name unique(name);
      
    • 删除唯一性约束

      --alter table 表明 drop constraint unique_列名;
      alter table student drop constraint unique_name;
      

猜你喜欢

转载自blog.csdn.net/HS2529077916/article/details/101165794