我有两个表之间的一对多关系:
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 脚本中执行此操作的。
更新
到目前为止,我已经设置了一个序列和一个触发器,因此我可以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;
所以现在我只需要自动插入到第二个表中。
非常感谢。