oracle - 在表中添加和删除时使用序列

标签 oracle plsql triggers sequence

我正在创建一个表,我将在其中添加文件名和许多其他字段。我使用 fileid 列按顺序表示文件;即要上传的第一个文件应具有 fieldid 1,然后下一个文件将具有 fileid 2,依此类推。我使用了一个序列和触发器:

create sequence create_file_id start with 1 increment by 1 nocache;

触发器是:
before insert on add_files_details
for each row
begin
select create_file_id.nextval into :new.file_id from dual;
end;

但是,如果从表中删除了任何记录/记录,则序列会变得困惑。因此,我正在考虑使用另一个带有触发器的序列来将前一个序列的值减少删除的行数。但我坚持执行这个序列的触发器。

序列:
create sequence del_file_id increment by -1 nocache;

有什么方法可以实现这一目标吗?

最佳答案

您可以让序列执行主键工作并创建基表的 View ,选择
rownum作为您希望按顺序查看从 1 到 N 的数字的列:

SQL> create table your_table(
  2    tab_id number primary key,
  3    col    number
  4  )
  5  ;

Table created

SQL> create sequence gen_id;

Sequence created

SQL> create trigger TR_PK_your_table
  2  before insert on your_table
  3  for each row
  4  begin
  5    :new.tab_id := gen_id.nextval; -- This kind of assignment is allowed in 11g  
  6  end;                             -- and higher, in version prior to 11g 
  7  /                                -- conventional select statement is used

Trigger created

SQL> insert into your_table(col)
  2  select level 
  3    from dual
  4  connect by level <=7;

7 rows inserted

SQL> commit;

Commit complete

SQL> select *
  2    from your_table;

    TAB_ID        COL
---------- ----------
         1          1
         2          2
         3          3
         4          4
         5          5
         6          6
         7          7

7 rows selected

SQL> create or replace view V_your_table
  2  as
  3  select tab_id
  4       , col
  5       , rownum as num
  6    from your_table
  7  ;

View created

SQL> select *
  2    from v_your_table;

    TAB_ID        COL        NUM
---------- ---------- ----------
         1          1          1
         2          2          2
         3          3          3
         4          4          4
         5          5          5
         6          6          6
         7          7          7

7 rows selected

SQL> delete from your_table where tab_id in (3,5,6);

3 rows deleted

SQL> commit;

Commit complete

SQL> select *
  2    from your_table;

    TAB_ID        COL
---------- ----------
         1          1
         2          2
         4          4
         7          7

SQL> select *
  2    from v_your_table;

    TAB_ID        COL        NUM
---------- ---------- ----------
         1          1          1
         2          2          2
         4          4          3
         7          7          4

SQL> 

关于oracle - 在表中添加和删除时使用序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18260941/

相关文章:

在 IN 子句中使用嵌套表的性能 - Oracle

forms - 如何在 Oracle Forms Builder 中显示对话框 Canvas ?

mysql - 更新后 SQL 触发器不起作用

sql - 触发器、存储过程与 DAL 中的内联 SQL 用于更新具有历史记录的数据库中的表

mysql - 我的表创建中的 CONSTRAINT 语句

java - Oracle Database 11g 如何通过 HTTPS TLS1.1、TLS1.2 连接到 Web 服务

sql - 在 Oracle 中使用 NOT LIKE

sql - 显示循环表的结果(oracle、pl/sql)

oracle - 为什么使用 varchar2 而不是 char 作为包常量?

where 子句中的 mysql 未知列。错误?