sql - Oracle中同一表中2列的唯一索引/约束
问题描述
我需要在表中的 2 列上创建唯一约束/索引。这 2 列之一可以是null
. 我的唯一约束/索引需要检查这 2 列是否不为空,那么它们不能重复。
不知道如何解决这个问题,任何初学者?
解决方案
您可以为此使用主键和基于函数的索引。这是一个例子:
create table t (
id int primary key,
x int,
y int
);
create unique index t_x_y on t(x, y, (case when x is null or y is null then id end));
这是一个 db<>fiddle。
推荐阅读
- python-3.x - 在numpy数组中查找第N个位置的位置?
- java - mAdapter.notifyDataSetChanged() 在某些情况下不起作用
- javascript - 如何将 SVG 图像添加到 javascript html5 画布动画?
- python - 检查已使用 Selenium 检索的元素是否具有特定类
- java - 我不明白输出这段代码。(地图)
- xamarin.android - Xamarin.Android webview 不打开文件,但文件上传工作正常(需要 C# 代码)
- python - PyQT 删除 QVBoxLayout 以适应数据库中的更新值
- c++ - 为什么代码在大于 10 的输入上给出分段错误?
- snmp - 如何在没有 RowStatus 的 dot1dStaticEntry 中添加条目
- dialogflow-es - 尝试更新您的生产数据失败 - Google 上的操作