当前位置: 首页 > article >正文

唯一约束

Oracle从入门到总裁:​​​​​​https://blog.csdn.net/weixin_67859959/article/details/135209645

唯一约束

唯一约束的特点是在某一个列上的内容不允许出现重复。

例如,现在要收集用户的信息,假设包含编号(mid)、姓名(name)、E-mail(email)

很明显 email 的数据不可能重复,所以就可以使用 unique约束完成

-- 删除数据表
drop table member purge;
-- 创建数据表
create table member(
	mid    number,
    name    varchar2(20)    not null,
    email    varchar2(20)    unique
);
SQL> set linesize 250
SQL> -- 删除数据表
SQL> drop table member purge;

表已删除。

SQL> -- 创建数据表
SQL> create table member(
  2  mid    number,
  3      name    varchar2(20)    not null,
  4      email    varchar2(20)    unique
  5  );

表已创建。

上面数据表中添加了 email 字段为唯一约束

下面来看一下这个约束的作用。首先向该表添加一个数据

SQL> INSERT INTO member(mid,name,email) VALUES (3,'张三','hello@hello.hello') ;

已创建 1 行。

此时该行数据正确地添加到数据表中,然后再继续添加下面这条语句

可以看出 email 这个字段的值仍然是 hello@hello.hello,这就违反了唯一约束的限制

SQL> INSERT INTO member(mid,name,email) VALUES (1,'李四','hello@hello.hello') ;
INSERT INTO member(mid,name,email) VALUES (1,'李四','hello@hello.hello')
*
第 1 行出现错误:
ORA-00001: 违反唯一约束条件 (SCOTT.SYS_C008261)

在 Oracle 中约束本身也称为一个对象,也就是说只要你设置了约束,那么 Oracle 会自动为你创建与之相关的约束对象信息

而这些过程都是自动完成的

既然是对象,所有的对象都会在数据字典中进行保存,用户的约束数据字典应该使用 user_constraint

COL owner FOR A30 ;
COL constraint_name FOR A30 ;
COL table_name FOR A30 ;
SELECT owner,constraint_name,table_name FROM user_constraints ;
SQL> SELECT owner,constraint_name,table_name FROM user_constraints ;

OWNER
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CONSTRAINT_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TABLE_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SCOTT
FK_DEPTNO
EMP

SCOTT
PK_EMP
EMP

OWNER
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CONSTRAINT_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TABLE_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

SCOTT
SYS_C008233
PRODUCT

SCOTT
PK_DEPT

OWNER
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CONSTRAINT_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TABLE_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
DEPT

SCOTT
SYS_C008234
CUSTOMER

SCOTT

OWNER
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CONSTRAINT_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TABLE_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SYS_C008260
MEMBER

SCOTT
SYS_C008261
MEMBER


已选择 7 行。

我们可以发现“user_constraints”数据字典只是告诉用户约束属于那张表

但是并没有告诉用户约束具体是哪个列上的,所以此时可以利用另外一个数据字典 user_cons_column

COL owner FOR A30 ;
COL constraint_name FOR A20 ;
COL table_name FOR A20 ;
COL column_name FOR A30 ;
SELECT owner,constraint_name,table_name,column_name FROM user_cons_columns ;
SQL> SELECT owner,constraint_name,table_name,column_name FROM user_cons_columns ;

OWNER
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CONSTRAINT_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TABLE_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
COLUMN_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SCOTT
PK_EMP
EMP
EMPNO


OWNER
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CONSTRAINT_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TABLE_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
COLUMN_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SCOTT
FK_DEPTNO
EMP
DEPTNO


OWNER
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CONSTRAINT_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TABLE_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
COLUMN_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SCOTT
SYS_C008233
PRODUCT
PRODUCTNAME


OWNER
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CONSTRAINT_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TABLE_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
COLUMN_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SCOTT
PK_DEPT
DEPT
DEPTNO


OWNER
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CONSTRAINT_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TABLE_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
COLUMN_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SCOTT
SYS_C008234
CUSTOMER
NAME


OWNER
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CONSTRAINT_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TABLE_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
COLUMN_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SCOTT
SYS_C008260
MEMBER
NAME


OWNER
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CONSTRAINT_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TABLE_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
COLUMN_NAME
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SCOTT
SYS_C008261
MEMBER
EMAIL


已选择 7 行。

唯一约束并不像非空约束那样,可以很明确地告诉用户是哪个列上出现问题

所以为了解决这样的错误,可以采用“约束简写 _ 字段”来定义这个约束

例如唯一约束的简写是“UK”,现在是在 email 字段上设置了唯一约束

所以可以使用“uk_email”来作为此约束的名字。如果要指定名字,则必须在约束创建的时候完成,利用 CONSTRAINT 关键字定

-- 产出数据表
drop table member purge;
-- 创建数据表
create table member(
	mid		number,
    name	varchar2(20)	not null,
    email	varchar2(20),
    constraint    uk_email  unique(email)
);
SQL> create table member(
  2  mid number,
  3      name varchar2(20)not null,
  4      email varchar2(20),
  5      constraint    uk_email  unique(email)
  6  );

表已创建。

从现在开始,只要是进行数据表创建,约束一定要设置名字。约束的名字绝对不能够重复

如果说现在设置了唯一约束,但是保存的是 null 呢?null 并不在唯一约束的判断范畴之中

也就是说当保存的是 null 的时候,不认为违反了唯一约束条


http://www.kler.cn/a/271787.html

相关文章:

  • SQL注入漏洞之高阶手法 宽字节注入以及编码解释 以及堆叠注入原理说明
  • (2)SpringBoot自动装配原理简介
  • 为AI聊天工具添加一个知识系统 之78 详细设计之19 正则表达式 之6
  • 2024年记 | 凛冬将至
  • Rust语言进阶之zip用法实例(九十五)
  • 立创开发板入门ESP32C3第八课 修改AI大模型接口为deepseek3接口
  • PCM和I2S区别
  • 快速去除或提取视频中的任何声音,你学会了吗
  • linux解析域名指令 nslookup 或者 host
  • C++:菱形继承与虚继承
  • 数据分析 | NumPy
  • 爬虫的基本原理介绍,实现以及问题解决
  • Linux——线程池
  • C/C++整数和浮点数在内存中存储
  • Linux学习(4)——使用编辑器
  • 第十三届蓝桥杯省赛C++ C组《全题目+题解》
  • Xlua - 集成rapidjson(json序列化)
  • 力扣111---二叉树的最小深度(简单题,Java,递归+非递归)
  • 用户数据的FLASH存储与应用(FPGA架构)
  • matlab去除图片上的噪声
  • SpringBoot整合异步任务
  • unity3d Animal Controller的Animal组件中Stances,Advanced基础部分理解
  • 【Jenkins】data stream error|Error cloning remote repo ‘origin‘ 错误解决【亲测有效】
  • 2k_Day3:搞清楚最基本简单的crud
  • 大模型笔记:吴恩达 ChatGPT Prompt Engineering for Developers(1) prompt的基本原则和策略
  • 论文阅读——RemoteCLIP