分几个维度校验吧。。
四个维度
存在start is null and end id null任何数据都不能插入
取出所有start is null and end最大的值,插入的值不能比最大的end小。。
取出所有end is null and start 最小的值,插入的值不能比最小的start 大。
最后插入的值不能在所有start与end的之间。。可以用子查询判断。。
综上,只有都满足才可以插入,否则不允许插入。
四个维度
存在start is null and end id null任何数据都不能插入
取出所有start is null and end最大的值,插入的值不能比最大的end小。。
取出所有end is null and start 最小的值,插入的值不能比最小的start 大。
最后插入的值不能在所有start与end的之间。。可以用子查询判断。。
综上,只有都满足才可以插入,否则不允许插入。
其实如果你可以保证不需要插空的话,直接:start_time>decode(end_time,
null,
to_date('4000-12-31', 'yyyy-mm-dd'),
end_time)即可,但是如果需要插空就得比较麻烦了:
select *
from (select decode(start_time,
null,
to_date('1000-12-31', 'yyyy-mm-dd'),
start_time) start_time,
decode(end_time,
null,
to_date('4000-12-31', 'yyyy-mm-dd'),
end_time) end_time
from tab) a,
(select decode(start_time,
null,
to_date('1100-12-31', 'yyyy-mm-dd'),
start_time) new_start_time,
decode(end_time,
null,
to_date('3000-12-31', 'yyyy-mm-dd'),
end_time) new_end_time
from new_tab) b
where a.start_time between b.new_start_time and b.new_end_time
or a.end_time between b.new_start_time and b.new_end_time
or (a.start_time < b.new_start_time and a.end_time > b.new_end_time)这里应该不存在start null end not null 的情况吧