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

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

约束条件解释
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(约束列)
      );
      
    • 建表后添加约束

      --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;
      
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DreamHs-love-Wm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值