在oracle中对许多表使用相同的自动增量触发器
我在MySQL中创建了一个数据库,共有10个表,每个表都以列开头在oracle中对许多表使用相同的自动增量触发器,oracle,triggers,auto-increment,Oracle,Triggers,Auto Increment,我在MySQL中创建了一个数据库,共有10个表,每个表都以列开头 SN INT NOT NULL AUTO_INCREMENT SN没有任何意义,只是区分可能重复/相似的名称/标题等的主要名称 我现在将其移动到Oracle,并发现可以触发自动递增SN字段。基本上 CREATE SEQUENCE user_seq; CREATE OR REPLACE TRIGGER user_inc BEFORE INSERT ON users FOR EACH ROW BEGIN SELECT
SN INT NOT NULL AUTO_INCREMENT
SN没有任何意义,只是区分可能重复/相似的名称/标题等的主要名称
我现在将其移动到Oracle,并发现可以触发自动递增SN字段。基本上
CREATE SEQUENCE user_seq;
CREATE OR REPLACE TRIGGER user_inc
BEFORE INSERT ON users
FOR EACH ROW
BEGIN
SELECT user_seq.NEXTVAL
INTO :new.SN
FROM dual;
END;
/
现在,如何重写该触发器一次以应用于所有其他表?因为否则我必须为许多表重写它,只需更改触发器名称和序列名称。。。我在想象这样的事情:
BEFORE INSERT ON users OR other_table OR another_one
我也发现了,但是其中的一个答案没有帮助,因为我认为许多表有相同的SN字段是合理的,或者我误解了这一点
而且,不是Oracle 12c,因此没有标识列
提前谢谢
我只想对我提到的第一篇文章发表评论,但如果没有更多的信誉点,我就无法发表评论://在Oracle中创建引用多个表的触发器是不可能的, 您可以做的是使用PL/SQL语句生成触发器 下面是一个如何实现这一目标的示例
drop table tab_a;
drop table tab_b;
drop table tab_c;
drop sequence seq_tab_a_id;
drop sequence seq_tab_b_id;
drop sequence seq_tab_c_id;
--create test tables
create table tab_a (SN number, otherfield varchar2(30), date_field date);
create table tab_b (SN number, otherfield varchar2(30), date_field date);
create table tab_c (SN number, otherfield varchar2(30), date_field date);
-- this pl/sql block creates the sequences and the triggers
declare
my_seq_create_stmt varchar2(2000);
my_trigger_create_stmt varchar2(2000);
begin
for i in (select table_name
from user_tables
-- remember to change this where condition to filter
-- the tables that are relevant for you
where table_name in ('TAB_A', 'TAB_B', 'TAB_C') )loop <<TableLoop>>
my_seq_create_stmt := 'CREATE SEQUENCE '||'SEQ_'||i.table_name||'_ID '
||CHR(13)||' START WITH 1 INCREMENT BY 1 NOCYCLE ';
execute immediate my_seq_create_stmt;
my_trigger_create_stmt := 'CREATE OR REPLACE TRIGGER '||'TRG_'||i.Table_name||'_ID_BI '||' BEFORE INSERT ON '||i.table_name||' FOR EACH ROW '
||CHR(13)||'BEGIN '
||CHR(13)||' SELECT '||'SEQ_'||i.table_name||'_ID'||'.NEXTVAL '
||CHR(13)||' INTO :new.SN '
||CHR(13)||' FROM dual; '
||CHR(13)||'END; ';
execute immediate my_trigger_create_stmt;
end loop TableLoop;
end;
/
-- test the triggers and the sequences
insert into tab_a (otherfield, date_field) values ('test 1',sysdate);
insert into tab_a (otherfield, date_field) values ('test 2',sysdate);
commit;
Select * from tab_a;
无法在Oracle中创建引用多个表的触发器, 您可以做的是使用PL/SQL语句生成触发器 下面是一个如何实现这一目标的示例
drop table tab_a;
drop table tab_b;
drop table tab_c;
drop sequence seq_tab_a_id;
drop sequence seq_tab_b_id;
drop sequence seq_tab_c_id;
--create test tables
create table tab_a (SN number, otherfield varchar2(30), date_field date);
create table tab_b (SN number, otherfield varchar2(30), date_field date);
create table tab_c (SN number, otherfield varchar2(30), date_field date);
-- this pl/sql block creates the sequences and the triggers
declare
my_seq_create_stmt varchar2(2000);
my_trigger_create_stmt varchar2(2000);
begin
for i in (select table_name
from user_tables
-- remember to change this where condition to filter
-- the tables that are relevant for you
where table_name in ('TAB_A', 'TAB_B', 'TAB_C') )loop <<TableLoop>>
my_seq_create_stmt := 'CREATE SEQUENCE '||'SEQ_'||i.table_name||'_ID '
||CHR(13)||' START WITH 1 INCREMENT BY 1 NOCYCLE ';
execute immediate my_seq_create_stmt;
my_trigger_create_stmt := 'CREATE OR REPLACE TRIGGER '||'TRG_'||i.Table_name||'_ID_BI '||' BEFORE INSERT ON '||i.table_name||' FOR EACH ROW '
||CHR(13)||'BEGIN '
||CHR(13)||' SELECT '||'SEQ_'||i.table_name||'_ID'||'.NEXTVAL '
||CHR(13)||' INTO :new.SN '
||CHR(13)||' FROM dual; '
||CHR(13)||'END; ';
execute immediate my_trigger_create_stmt;
end loop TableLoop;
end;
/
-- test the triggers and the sequences
insert into tab_a (otherfield, date_field) values ('test 1',sysdate);
insert into tab_a (otherfield, date_field) values ('test 2',sysdate);
commit;
Select * from tab_a;
这很有趣,以前从未使用过PL/SQL。我需要安装一些驱动程序或其他东西来使用它吗?另外,我不明白这部分中的表名和用户表是什么:因为我在从用户表中选择表名时,PL/SQL是ORACLE数据库的标准部分。用户_表是字典视图的一部分,其中包含有趣的数据库元数据,以前从未使用过PL/SQL。我需要安装一些驱动程序或其他东西来使用它吗?另外,我不明白这部分中的表名和用户表是什么:因为我在从用户表中选择表名时,PL/SQL是ORACLE数据库的标准部分。用户_表是字典视图的一部分,其中包含数据库的元数据