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

编程入门 行业动态 更新时间:2024-10-10 03:30:05

oracle数据库中约束条件(<a href=https://www.elefans.com/category/jswz/34/1767190.html style=五种(主键、外键、非空、唯一、检查))"/>

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 keyalter 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;
      

更多推荐

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

本文发布于:2024-02-27 19:31:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1765891.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:五种   数据库中   主键   条件   oracle

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!