从自动增量列中删除值

Deleating value from auto incremented column

首先,如果问题低于标准(对大多数人来说都是基本的),请表示歉意。

我有一个列,其值根据其他 column.E 自动递增。例如:如果列 name 具有值 "abc","abc" ,"xyz","xyz",xyz","hij",则自动递增列中的值必须是 "1","2","1","2","3","1".

删除或更新记录时出现问题。 如果有人删除了具有 "2" 值的 ["xyz"] 值怎么办? 如何处理这种情况?

作为选项之一(简单直接的选项),您可以创建视图并即时生成该视图 "auto-incremented column" - 每次查询视图时。

这是一个例子:

 -- 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