Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8757725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:22:20+00:00 2026-06-13T14:22:20+00:00

I have a one-to-many relationship between two tables: table1: NUMBER users_id (primary key) field2

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:22:21+00:00Added an answer on June 13, 2026 at 2:22 pm

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

android noob... I have two tables, with a one to many relationship between country_tbl
I have a one to many relationship between two tables. The many table contains
I have a question about One-To-Many relationship between two tables when there is the
Is there a way to map one-to-many relationship between two tables that have the
I have a one to many relationship between two tables in mysql. One called
I have a one-to-many relationship between two classes for this situation. I have a
I have two tables connected with one to many relationship. Parent Table is a
I have a number of many to many relationships between two tables with conditionals
If I have a relationship between two tables (both tables have their own primary
I have two tables: invoices & invoice_items. I need a one to many relationship

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.