问题
I have a one-to-many relationship between two tables:
table1:
NUMBER users_id (primary key)
field2
field3
...
table2:
NUMBER users_id (foreign key)
VARCHAR2 name
...
...
and when I INSERT into table1, I want to auto increment (sequence?) users_id and insert a number of records into table2 all with the same users_id so I end up with
table1:
1,val1,val2
table2:
1,barry,...
1,bob,...
1,james,...
I think I need a trigger with a sequence to auto-increment users_id in table1 and create the rows in table2.
It might not be relevant but I'm doing this from a PHP script.
UPDATE
So far I have a sequence and a trigger set up so I can INSERT into table1 and have the users_id field auto-increment:
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;
so now I just need to automatically insert into the second table.
Many thanks.
回答1:
You can use returning into clause of the insert statement to return users_id value after a new record has been inserted into table1. Also you can use user_seq.currval to get the current value of the sequence. Here is an example (In this example a simple stored procedure has been implemented to demonstrate the usage of insert into clause. You can implement a similar stored procedure according to your requirements):
SQL> create table Tb_table_1(
2 user_id number primary key,
3 field_1 number
4 );
Table created
SQL>
SQL> create table Tb_table_2(
2 user_id number references tb_table_1(user_id),
3 name1 varchar2(17)
4 );
Table created
SQL> create sequence user_seq
2 start with 1
3 increment by 1
4 nomaxvalue;
Sequence created
SQL>
SQL> create trigger user_trigger
2 before insert on tb_table_1
3 for each row
4 begin
5 select user_seq.nextval into :new.user_id from dual;
6 end;
7 /
Trigger created
SQL> create or replace procedure Insert_Record
2 is
3 l_cur_id number;
4 begin
5 insert into Tb_table_1(Field_1)
6 values(123)
7 returning user_id into l_cur_id; -- store user_id of the new inserted record
8 for i in 1..5 -- in a local variable for later use
9 loop
10 insert into tb_table_2(user_id, name1) -- insert a bunch of sample data into table2 using previously stored user_id.
11 values(l_cur_id, dbms_random.string('l', 7));
12 end loop
13 commit;
14 end;
15 /
Procedure created
SQL> select * from tb_table_1;
USER_ID FIELD_1
---------- ----------
SQL> select * from tb_table_2;
USER_ID NAME1
---------- -----------------
SQL> exec insert_record;
PL/SQL procedure successfully completed
SQL> select * from tb_table_1
2 ;
USER_ID FIELD_1
---------- ----------
1 123
SQL> select * from tb_table_2;
USER_ID NAME1
---------- -----------------
1 jzsdbna
1 ozbibgs
1 btxrxcm
1 hxwwpzc
1 sdjbwzi
SQL>
In Oracle 11g onward you can directly assign sequence value to a variable:
:new.users_id := user_seq.nextval;
来源:https://stackoverflow.com/questions/13109740/trigger-in-oracle-to-auto-increment-a-field-and-insert-into-association-table