错误,trigger instead of is invalid and failed re-validation

Error, trigger instead of is invalid and failed re-validation

我尝试创建一个 instead of 触发器。它的目的是插入 3 tables 而不是我拥有的视图,但我遇到了一系列问题。

Create or replace trigger trigg_view
Instead of Insert ON Carti_Beletristica
for each row

Begin

dbms_output.put_line('i dont know');

End;

这是我想要开始的最基本的代码。它让我创建它,但是当我尝试在我的视图中插入时 (Carti_Beletristica) 我得到下一个错误

ORA-04098: trigger 'RO_A372_SQL_S20.INSERT_VIEW_TRIGG' is invalid and failed re-validation

这非常令人沮丧,因为它是一个非常简单的触发器,我无法继续...之后,我将如何生成主键?因为在我看来,我没有那样的东西。我的想法是 select table 中主键的最大值,然后加一个,然后使用这个值,但我得到了很多错误。

CREATE VIEW Carti_Beletristica AS
SELECT titlu, nr_pagini, nr_exemplare, nume AS autor, telefon
FROM Carte NATURAL JOIN Autor JOIN Persoana ON (id_pers = id_aut)
WHERE upper(gen) = 'BELETRISTICA'

这是风景。

Create table Persoana(
    id_pers number(10) not null,
    nume varchar2(100) not null,
    telefon varchar2(15) not null,

    Constraint persoana_id_pers_pk primary key(id_pers)
 );

Create table Carte(
    id_carte number(10) not null,
    titlu varchar2(100) not null,
    nr_pagini number(10) not null,
    nr_exemplare number(10) not null,
    gen varchar2(20) not null,

    Constraint carte_id_carte_pk primary key(id_carte)  
 );

Create table Autor(
    id_carte number(10) not null,
    id_aut number(10) not null,

    Constraint autor_pk primary key(id_carte,id_aut),

    Constraint autor_id_carte_fk foreign key(id_carte) references Carte(id_carte),
    Constraint autor_id_aut_fk foreign key(id_aut) references Persoana(id_pers)

);

你能帮我一下吗?视图上的插入看起来像那样

Insert into Carti_Beletristica(titlu,nr_pagini,nr_exemplare,autor,telefon)
values('tiltu',69,96,'otor','07phonenumber')

编辑:

这是我试过的主键

Create or replace trigger trigg_view
Instead of Insert ON Carti_Beletristica
for each row
declare
    aux persoana.id_pers%type;
Begin

    select max(id_pers)+1 into aux from Persoana;

    dbms_output.put_line(aux);

End;

My idea was to select the max from the primary keys in a table, then add one, then use this value,

您可能会认为 "my idea worked" 但这是非常糟糕的做法:

select max(id_pers)+1 into aux from Persoana;

这是一种获取主键标识符的低效方法。更重要的是它 不安全 因为它不能在多用户环境中工作:两个用户同时插入 table 将得到相同的 "next value" (由于读取提交隔离)那么其中一个用户在提交事务时将遇到重复密钥冲突。

正确的解决方案是使用 Oracle 内置的唯一密钥生成器。在 12c 之前,这意味着一个序列。对于您的 persoana table 这意味着创建一个名为 persoana_seq 的序列,您将在触发器中将其引用为:

aux := persoana_seq.nextval;

序列是生成一系列保证唯一 数字的最高效机制。

在 Oracle 12c 中,我们可以将列定义为 IDENTITY 列。这为我们提供了一个自动递增的列:

create table persoana (
       id generated always as identity primary key,
       ....

persoana.id 将在插入时自动填充唯一值,无需我们采取任何进一步操作。 (标识列背后有关联序列,只是我们不需要担心它们。)