| Author |
Topic Search Topic Options
|
mike7510uk
Newbie
Joined: 05 October 2006
Location: United Kingdom
Status: Offline
Points: 21
|
Post Options
Thanks(0)
Quote Reply
Topic: database problem Posted: 16 October 2006 at 1:02pm |
i have 2 database tables and want the primary key from table 1 to appear in table 2. The tables are connected to two web forms, so in other words, the user enters personal details into form1 which is sent to table1..then the user enters company details into form 2 which is sent to table 2, the trouble is, is that at the moment there is no way of telling which personal details relate to which company details...
any ideas?
|
 |
Freon22
Groupie
Joined: 04 December 2005
Status: Offline
Points: 42
|
Post Options
Thanks(0)
Quote Reply
Posted: 17 October 2006 at 3:22pm |
What you need to do is setup a foreign key field in table 2. Then when you add a record into table 1 you get the primary key number and you insert it into the foreign key field in table 2.
So you would do something like this
INSERT INTO table 1 (field1, field2, field2) VALUES (value1, value2, value3); SELECT Scope_Identity()
What we just did was to add a record to table 1 and returned the primary key number from the record that we just added. Now you take that primary key number and add it to the foreign key field in table 2 when you add the record.
Do some google search and here is a link that will help.
Hope this help!
|
 |
mike7510uk
Newbie
Joined: 05 October 2006
Location: United Kingdom
Status: Offline
Points: 21
|
Post Options
Thanks(0)
Quote Reply
Posted: 17 October 2006 at 4:27pm |
ok, i think i get the first bit.
How do we now get the primary key number and add it to the foreign key field in table 2?
|
 |
Freon22
Groupie
Joined: 04 December 2005
Status: Offline
Points: 42
|
Post Options
Thanks(0)
Quote Reply
Posted: 17 October 2006 at 6:00pm |
Well there are a few ways, one is the example that I show you.
INSERT INTO table 1 (field1, field2, field2) VALUES (value1, value2, value3); SELECT Scope_Identity()
When you use a insert with a Select Scope_Identity at the end it will return the primary key number for that record. Atleast with sql2005 database if you are using access or someother database you will have to look into what method is used for returning the record number.
Anyway you can then put the id number in a variable and use it when you insert the data into table 2.
Or you can just insert all of the data into both tables at the same time.
I setup a few tables so table_1 has these fields user_id, user_name. table_2 has company_id, user_id, company_name
INSERT INTO table_1 (user_name) VALUES ('Jim');
INSERT INTO table_2 (user_id, company_name) VALUES (Scope_Identity(), 'Help Company')
Now if I run both of these inserts in the same connection it will put the primary key user_id from the record in table_1 into the foreign key field user_id in table_2
|
 |
mike7510uk
Newbie
Joined: 05 October 2006
Location: United Kingdom
Status: Offline
Points: 21
|
Post Options
Thanks(0)
Quote Reply
Posted: 18 October 2006 at 8:55am |
Freon22 wrote:
Well there are a few ways, one is the example that I show you.
INSERT INTO table 1 (field1, field2, field2) VALUES (value1, value2, value3); SELECT Scope_Identity()
When you use a insert with a Select Scope_Identity at the end it will return the primary key number for that record. Atleast with sql2005 database if you are using access or someother database you will have to look into what method is used for returning the record number.
Anyway you can then put the id number in a variable and use it when you insert the data into table 2.
Or you can just insert all of the data into both tables at the same time.
I setup a few tables so table_1 has these fields user_id, user_name. table_2 has company_id, user_id, company_name
INSERT INTO table_1 (user_name) VALUES ('Jim');
INSERT INTO table_2 (user_id, company_name) VALUES (Scope_Identity(), 'Help Company')
Now if I run both of these inserts in the same connection it will put the primary key user_id from the record in table_1 into the foreign key field user_id in table_2 |
i have tried this way but am unsure of the correct syntax i should be using. Sorry, im a bit new to this
|
 |
mike7510uk
Newbie
Joined: 05 October 2006
Location: United Kingdom
Status: Offline
Points: 21
|
Post Options
Thanks(0)
Quote Reply
Posted: 18 October 2006 at 9:01am |
|
also, the forms are on 2 different web pages, when the first form is submitted the info goes to table1 of the database,then the response.redirect brings up form2, where the user again enters info, presses submit and sends that data to table 2 in the database.
|
 |
mike7510uk
Newbie
Joined: 05 October 2006
Location: United Kingdom
Status: Offline
Points: 21
|
Post Options
Thanks(0)
Quote Reply
Posted: 31 October 2006 at 9:11am |
Freon22 wrote:
Well there are a few ways, one is the example that I show you.
INSERT INTO table 1 (field1, field2, field2) VALUES (value1, value2, value3); SELECT Scope_Identity()
When you use a insert with a Select Scope_Identity at the end it will return the primary key number for that record. Atleast with sql2005 database if you are using access or someother database you will have to look into what method is used for returning the record number.
Anyway you can then put the id number in a variable and use it when you insert the data into table 2.
Or you can just insert all of the data into both tables at the same time.
I setup a few tables so table_1 has these fields user_id, user_name. table_2 has company_id, user_id, company_name
INSERT INTO table_1 (user_name) VALUES ('Jim');
INSERT INTO table_2 (user_id, company_name) VALUES (Scope_Identity(), 'Help Company')
Now if I run both of these inserts in the same connection it will put the primary key user_id from the record in table_1 into the foreign key field user_id in table_2 |
how do i do this bit?
|
 |
michael
Senior Member
Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
|
Post Options
Thanks(0)
Quote Reply
Posted: 31 October 2006 at 2:29pm |
For page one you write a Stored Procedure that inserts the data as required into table one. After the insert statement you do a simple select like
Select Scope_Identity() as myNewPK
In asp, run a query (sql) to get this value and write it to a session, cookie or whatever you prefer.
Then redirect to page 2 and read the cookie there.
|
|
|
 |