我有兩個表之間存在一個一對多的關係:觸發器在Oracle中自動遞增的字段,並插入到關聯表
table1:
NUMBER users_id (primary key)
field2
field3
...
table2:
NUMBER users_id (foreign key)
VARCHAR2 name
...
...
,當我INSERT
到table1
,我想汽車增量(序列?)users_id
並插入一些記錄到table2
都具有相同的users_id
所以我最終
table1:
1,val1,val2
table2:
1,barry,...
1,bob,...
1,james,...
我想我需要一個觸發一個序列自動遞增users_id
在table1
中,並在table2
中創建行。
它可能不相關,但我是從PHP腳本做到這一點。
UPDATE
到目前爲止,我有一個序列,並建立了一個觸發,所以我可以INSERT
到table1
並有users_id
場自動遞增:
create sequence user_seq
start with 1
increment by 1
nomaxvalue;
create trigger user_trigger
before insert on table1
for each row
begin
select user_seq.nextval into :new.users_id from dual;
end;
所以現在我只需要自動插入到第二個表中。
非常感謝。
您想只將users_id插入到table2中嗎? – user75ponic
不,我想插入一些'name'值到'table2'中,但也需要'users_id',所以我有一個關聯。非常感謝。 – ale
我對PHP不熟悉,但是在插入語句中有一個'returns clause',可以返回id(剛剛插入) –