inserting data into a dependent table

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dos360

    inserting data into a dependent table

    Hello,

    I have two tables, one is a list of activities, the other a list of
    participants. I want to insert one record in the activities table and
    then using its identity column as foreign key, I want to insert two or
    more records into the participants table.

    My problem is that I have no idea what foreign key to use when
    inserting names into the participants table. How can I get hold of the
    row's key or identity column, immediately after inserting a row into
    the activity table?

    Thanks in advance.

    Ahmet
  • Serge Rielau

    #2
    Re: inserting data into a dependent table

    dos360 wrote:
    Hello,
    >
    I have two tables, one is a list of activities, the other a list of
    participants. I want to insert one record in the activities table and
    then using its identity column as foreign key, I want to insert two or
    more records into the participants table.
    >
    My problem is that I have no idea what foreign key to use when
    inserting names into the participants table. How can I get hold of the
    row's key or identity column, immediately after inserting a row into
    the activity table?
    The best way is to do a "SELECT FROM INSERT"
    CREATE TABLE T(pk INT GENERATED ALWAYS AS IDENTITY, c1 INT);

    SELECT pk FROM NEW TABLE(INSERT INTO T(c1) VALUES 10);
    PK
    -----------
    1

    1 record(s) selected.

    You can also play with the IDENTITY_VAL_LO CAL() function, but that means
    an extra SQL statement.
    --
    Serge Rielau
    DB2 Solutions Development
    IBM Toronto Lab

    Comment

    • mirof007

      #3
      Re: inserting data into a dependent table

      On Jul 8, 2:39 am, dos360 <vtam...@yahoo. comwrote:
      Hello,
      >
      I have two tables, one is a list of activities, the other a list of
      participants. I want to insert one record in the activities table and
      then using its identity column as foreign key, I want to insert two or
      more records into the participants table.
      >
      My problem is that I have no idea what foreign key to use when
      inserting names into the participants table. How can I get hold of the
      row's key or identity column, immediately after inserting a row into
      the activity table?
      >
      Thanks in advance.
      >
      Ahmet
      Try SELECT FROM OLD TABLE(INSERT INTO activities ...), you can read up
      on it here: http://www.ibm.com/developerworks/db...dm-0411rielau/

      Regards,
      Miro

      Comment

      • Tekelioglu

        #4
        Re: inserting data into a dependent table

        Thank you Serge,

        It worked like a charm on my DB2 command editor, but not in my
        program. Instead of returning one line with incremented identity
        column contents, its returning zeros. At first one zero, then two
        zeros, then three...

        There were no errors in my sql statement so I thought, perhaps it
        needs to be committed. So I went back to the Control Center and
        discovered that my table T was still blank. Once I commit in the
        command editor, I get a blank table T!

        Sorry, I'm a little new to this.

        Ahmet

        On 8 Temmuz, 14:08, Serge Rielau <srie...@ca.ibm .comwrote:
        dos360 wrote:
        Hello,
        >
        I have two tables, one is a list of activities, the other a list of
        participants. I want to insert one record in the activities table and
        then using its identity column as foreign key, I want to insert two or
        more records into the participants table.
        >
        My problem is that I have no idea what foreign key to use when
        inserting names into the participants table. How can I get hold of the
        row's key or identity column, immediately after inserting a row into
        the activity table?
        >
        The best way is to do a "SELECT FROM INSERT"
        CREATE TABLE T(pk INT GENERATED ALWAYS AS IDENTITY, c1 INT);
        >
        SELECT pk FROM NEW TABLE(INSERT INTO T(c1) VALUES 10);
        PK
        -----------
        1
        >
        1 record(s) selected.
        >
        You can also play with the IDENTITY_VAL_LO CAL() function, but that means
        an extra SQL statement.
        --
        Serge Rielau
        DB2 Solutions Development
        IBM Toronto Lab

        Comment

        • --CELKO--

          #5
          Re: inserting data into a dependent table

          >I have two tables, one is a list of Activities, the other a list of Participants. I want to insert one record [sic: rows are not records] in the Activities table and then using its IDENTITY column [sic IDENTITY is not a column, but a property of the physical storage] as foreign key [sic: IDENTITY cannot be a key by definition], I want to insert two or more records [sic] into the Participants table.<<

          Please post DDL so we don't have to guess about the schema, if you
          want help. Here is a guess at a properly designed schema:

          CREATE TABLE Activities
          (activity_id INTEGER NOT NULL PRIMARY KEY
          activity_name CHAR(15) NOT NULL,
          etc.)

          CREATE TABLE Participants
          (participant_id INTEGER NOT NULL PRIMARY KEY,
          participant_nam e CHAR(15) NOT NULL,
          etc.);

          Participation is a relationship and will needs its own table

          CREATE TABLE Participation
          (participant_id INTEGER NOT NULL
          REFERENCES Participants (participant_id )
          ON DELETE CASCADE
          ON UPDATE CASCADE,
          activity_id INTEGER NOT NULL
          REFERENCES Activities (activity_id)
          ON DELETE CASCADE
          ON UPDATE CASCADE,
          PRIMARY KEY (participant_id , activity_id),
          etc.);
          >My problem is that I have no idea what foreign key to use when inserting names into the Participants table. How can I get hold of the row's key or IDENTITY column [sic], immediately after inserting a row into the activity table? <<
          That is the wrong question: you don't use IDENTITY and you don't make
          an entity into an attribute. You use a relationship table. If you had
          Books and Authors, you would not make a book an attribute of an author
          -- it does not grow out of his chest, does it? You would have an
          Authorship table.

          Comment

          • Tekelioglu

            #6
            Re: inserting data into a dependent table

            Thank you Celko. I see your point, if I had an association table
            called participation I would be able to generate more reports.
            Actually, I was trying to do something much simpler. First the DDL

            create table ahmet.activity
            (act_no bigint generated always as identity,
            act_date DATE NOT NULL, etc)

            create table ahmet.act_parti cipant
            (part_act_no bigint generated always as identity,
            part_act_no bigint, <<<< "primary key of activity table goes here"
            part_act_salesr ep char(40))

            My idea was first to insert a row (why can't a say a record then?)
            into the activity table, then get hold of its primary key (act_no) and
            then using it as a foreign key, to insert as many participants into
            the second table (act_participan t).

            Serge said "use a select from insert" but for some reason it only
            works (returns incremented results) in the command editor and even
            then the rows aren't actually inserted because the table remains
            blank. In my Lotus Notes clients basic code, it doesn't even return
            incremented keys.

            Any suggestions? With or without a third association table?

            Ahmet



            On Jul 9, 6:44 pm, --CELKO-- <jcelko...@eart hlink.netwrote:
            I have two tables, one is a list of Activities, the other a list of Participants. I want to insert one record [sic: rows are not records] in the Activities table and then using its IDENTITY column [sic IDENTITY is not a column, but a property of the physical storage] as foreign key [sic: IDENTITY cannot be a key by definition], I want to insert two or more records [sic] into the Participants table.<<
            >
            Please post DDL so we don't have to guess about the schema, if you
            want help.  Here is a guess at a properly designed schema:
            >
            CREATE TABLE Activities
            (activity_id INTEGER NOT NULL PRIMARY KEY
             activity_name CHAR(15) NOT NULL,
             etc.)
            >
            CREATE TABLE Participants
            (participant_id INTEGER NOT NULL PRIMARY KEY,
             participant_nam e CHAR(15) NOT NULL,
             etc.);
            >
            Participation is a relationship and will needs its own table
            >
            CREATE TABLE Participation
            (participant_id INTEGER NOT NULL
               REFERENCES Participants (participant_id )
               ON DELETE CASCADE
               ON UPDATE CASCADE,
             activity_id INTEGER NOT NULL
               REFERENCES Activities (activity_id)
               ON DELETE CASCADE
               ON UPDATE CASCADE,
             PRIMARY KEY (participant_id , activity_id),
              etc.);
            >
            My problem is that I have no idea what foreign key to use when inserting names into the Participants table. How can I get hold of the row's key or IDENTITY column [sic], immediately after inserting a row into the activity table? <<
            >
            That is the wrong question: you don't use IDENTITY and you don't make
            an entity into an attribute.  You use a relationship table. If you had
            Books and Authors, you would not make a book an attribute of an author
            -- it does not grow out of his chest, does it?  You would have an
            Authorship table.

            Comment

            Working...