如果问题低于标准(基本上是最重要的),首先是appologies。
我有一个列,其值会根据其他列自动递增.Eg:如果列name
的值为"abc","abc" ,"xyz","xyz",xyz","hij"
,则自动递增列中的值必须为"1","2","1","2","3","1"
。
删除或更新记录时出现问题。
如果有人删除["xyz"]
值"2"
怎么办?
如何处理这种情况?
答案 0 :(得分:1)
作为其中一个选项(简单明了的选项),您可以创建视图并在每次查询视图时动态生成“自动增加的列”。
以下是一个例子:
-- source table, which does not contain that auto incremented
-- column you are interested in
create table t1(id, col1) as (
select 1, 'abc' from dual union all
select 2, 'abc' from dual union all
select 3, 'xyz' from dual union all
select 4, 'xyz' from dual union all
select 5, 'xyz' from dual union all
select 6, 'hij' from dual
);
-- and here is the view
create or replace view t1_v as
select id
, col1
, row_number() over(partition by col1
order by id) as auto_inc
from t1;
select *
from t1_v;
ID COL1 AUTO_INC
---------- ---- ----------
1 abc 1
2 abc 2
6 hij 1
3 xyz 1
4 xyz 2
5 xyz 3
更新值:
-- Update can be issued against base table or
-- a view, if it's a key-preserved one
update t1
set col1 = 'acb'
where id = 1;
select *
from t1_v;
ID COL1 AUTO_INC
---------- ---- ----------
2 abc 1
1 acb 1
6 hij 1
3 xyz 1
4 xyz 2
5 xyz 3
删除行:
-- You can delete from the base table or
-- a view, if it's a key-preserved one
delete from t1
where id = 4;
select *
from t1_v;
ID COL1 AUTO_INC
---------- ---- ----------
2 abc 1
1 acb 1
6 hij 1
3 xyz 1
5 xyz 2