05|Oracle学习(UNIQUE约束)

1. UNIQUE约束介绍

  • 也叫:唯一键约束,用于限定数据表中字段值的唯一性。
    在这里插入图片描述
1.1 UNIQUE和primary key区别:
  • 主键/联合主键每张表中只有一个。
  • UNIQUE约束可以在一张表中,多个字段中存在。例如:学生的电话、身份证号都是唯一的。

2. 添加唯一约束

2.1 建表时添加

在这里插入图片描述

2.1.1 案例
  • 建立个学生信息表,将电话号码设置为唯一约束:
create table tb_students(
    stu_num char(5) not null,
    stu_name varchar(10) not null,
    stu_sex char(1) not null,
    stu_age number(2) not null,
    stu_tel char(11) not null,
    constraint uq_student_tel UNIQUE(stu_tel)
);

实际开发中,常用的是下面的,直接在stu_tel后面添加个unique就行:

create table tb_students(
    stu_num char(5) not null,
    stu_name varchar(10) not null,
    stu_sex char(1) not null,
    stu_age number(2) not null,
    stu_tel char(11) not null unique
);
2.2 建表后,再添加

在这里插入图片描述

2.2.1 案例
  • 建立一张学生信息表,无唯一键:
create table tb_students(
    stu_num char(5) not null,
    stu_name varchar(10) not null,
    stu_sex char(1) not null,
    stu_age number(2) not null,
    stu_tel char(11) not null
);
  • 接着为表添加唯一键:stu_tel:
alter table tb_students
add constraint uq_student_tel
unique(stu_tel);

3. 删除唯一约束

在这里插入图片描述

3.1 案例
  • 删除唯一约束uq_student_tel:
alter table tb_students
drop constraint uq_student_tel;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值