plsql - Oracle - Trigger BEFORE INSERT not checking constraints -


i have table describe loans of books in sort of library :

create table emprunt (     num_ab number(6,0) references abonne(numero),     num_ex number (4,0) references exemplaire(numero),     d_emprunt date,     d_retour date,     d_ret_reel date,     nb_relance number(1,0) check (nb_relance in (1,2,3)),     constraint pk_emprunt primary key (num_ab, num_ex, d_emprunt) ); 

and want make trigger that, when insert new loan, set date of loan today, , return date @ today + 21 days. there code, works :

create or replace trigger on_insert_emprunt before insert on emprunt each row begin     :new.d_emprunt := sysdate;     :new.d_retour := sysdate + 21; end; / 

but, problem primary constraint on table emprunt no longer checked... exemple, if several times :

insert emprunt values (921102,1010,to_date('29-04-2014','dd-mm-yyyy'),to_date('29-04-2014','dd-mm-yyyy'),null,null); 

it works perfectly... instead of saying me :

unique constraint violated

i have multiple same rows in table...

ps : sorry table in french.

sysdate, oracle dates, has time component. repeated inserts getting different times trigger, , won't see constraint violation unless happen 2 inserts within second of each other. dates different (down time level), , values used primary key different - hence no constraint violation.

you can see primary keys different querying:

select to_char(d_emprunt, 'yyyy-mm-dd hh24:mi:ss') d_emprunt,   to_char(d_retour, 'yyyy-mm-dd hh24:mi:ss') d_retour emprunt num_ab = 921102 , num_ex = 1010;  d_emprunt           d_retour           ------------------- ------------------- 2015-04-29 10:55:30 2015-05-20 10:55:30 2015-04-29 10:55:35 2015-05-20 10:55:35 

you'll see time varies records inserted.

if want set dates midnight you can truncate them, default resets time components zero:

create or replace trigger on_insert_emprunt before insert on emprunt each row begin     :new.d_emprunt := trunc(sysdate);     :new.d_retour := trunc(sysdate) + 21; end; / 

incidentally, primary key doesn't effective. if intention stop same book being lent out twice @ same time, it's not quite doing that. it's stopping book being lent out on same day, not overlapping periods. if took same book out tomorrow, primary key value still unique, ranges overlap.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -