我在创建oracle sql脚本时遇到问题。如何创建这两个约束?
这是我到目前为止的内容:
create table Employee(
Id int primary key,
Name varchar(15) not null,
VID int,
FID int
);
您无需触发器即可非常接近您想要的东西。
您可以对第一个使用检查约束:
alter table Employee add constraint chk_vid_fid
check (vid is not null or fid is null);
您可以使用唯一约束执行第二个操作:
create unique index unq_Employee_vid on
Employee(case when vid is null then -1 else id end);
该公式假定id
大多数id通常都是非负数。如果您确实使用了整数值的全部范围,那么我将使用字符串来明确表示:
create unique index unq_Employee_vid on
Employee(case when vid is null then 'null vid' else cast(id as varchar2(255)) end);
这样可以保证最多只有一行为vid
null,而不是只有一行。由于空表不符合条件,而且创建表时,表也为空,因此您不能轻易拥有一个保证恰好一行具有值的约束。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句