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 5 5712
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
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
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
>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.
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_na me 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.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: simon |
last post by:
Hi,
I have two tables
Table A = {ID, Item}
Table B = {ID, IDA, subItem}
Where ID auto increment.
INSERT INTO TABLE_A ('items') values ('a')
|
by: Joachim Klassen |
last post by:
Hi all,
first apologies if this question looks the same as another one I recently
posted - its a different thing but for the same szenario:-).
We are having performance problems when inserting/deleting rows from a large
table.
My scenario:
Table (lets call it FACT1) with 1000 million rows distributed on 12
|
by: Timm |
last post by:
I have an ASP.NET 2.0 page with two DropDownLists. I am using declarative
data binding wherever possible and trying to minimize the use of code. The
list of values in DropDownList DDL2 should be (filtered) dependent upon the
selection in DDL1. I think this inevitably needs some code, but I'd be happy
to be told otherwise!
I have some code to handle OnSelectedIndexChanged for DDL1 that sets the
FilterExpression associated with the...
|
by: suslikovich |
last post by:
Hi all,
I am getting this error when insert values from one table to another in
the first table the values are varchar (10). In the second they are
datetime. The format of the data is mm/dd/yyyy to be easily converted
to dates. The conversion in this case is implicit as indicated in SQL
Server documentation. Here is my query:
INSERT INTO Campaign (CampaignID, Name, DateStart, DateEnd, ParentID,
ListID)
SELECT ...
|
by: rcoco |
last post by:
Hi,
I want to share this problem. I have a datagrid that will help me
Insert data into sql database. So I made a button On my form so that
when I press the button a new row on datagrid should be created and I
could be able to insert data. But with this code below I've failed
could someone help me and tell me where I'm going wrong:
private void Page_Load(object sender, System.EventArgs e)
{
if (! IsPostBack)
| |
by: madhuxml82 |
last post by:
Dear Forum Members,
I have generated an XML Schema and a Table of XMLType referencing the XML Schema.
Now When I am Inserting the Data into the Table.
I am getting the Error 0RA-30937: Error is Comming.
I am pasting the Code which I have written.
Appreciate any Help...
begin dbms_xmlschema.registerSchema(
'http://www.robertboschindia.com/example.xsd',
|
by: AlexanderDeLarge |
last post by:
Hi!
I got a problem that's driving me crazy and I'm desperately in need of help.
I'll explain my scenario:
I'm doing a database driven site for a band, I got these tables for their discography section:
Discography
---------------------
DiscID
|
by: cluce |
last post by:
I am trying to read a csv file with user info (username, password, email, address, city, zip, state, etc.) I am inserting the username, password, email into the aspnet_memberhsip table using the membership class and trying to insert the rest of the related info with a stored procedure into the custom table I created called aspnet_UserInfo using a one - one relationship by UserID but its not working. can someone look at my stored procedure for...
|
by: rando1000 |
last post by:
Okay, here's my situation. I need to loop through a file, inserting records based on a number field (in order) and if the character in a certain field = "##", I need to insert a blank record.
So here's my method. I created two tables with the same structure as the table I'm inserting from. One table, Split_Temp, is the one I'll be inserting to. The other table, Split_Insert, contains the "Blank" record, which actually just has the word...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |