Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - database problem
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

database problem

 Post Reply Post Reply Page  12>
Author
mike7510uk View Drop Down
Newbie
Newbie


Joined: 05 October 2006
Location: United Kingdom
Status: Offline
Points: 21
Post Options Post Options   Thanks (0) Thanks(0)   Quote mike7510uk Quote  Post ReplyReply Direct Link To This Post 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?
Back to Top
Freon22 View Drop Down
Groupie
Groupie


Joined: 04 December 2005
Status: Offline
Points: 42
Post Options Post Options   Thanks (0) Thanks(0)   Quote Freon22 Quote  Post ReplyReply Direct Link To This Post 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!
Back to Top
mike7510uk View Drop Down
Newbie
Newbie


Joined: 05 October 2006
Location: United Kingdom
Status: Offline
Points: 21
Post Options Post Options   Thanks (0) Thanks(0)   Quote mike7510uk Quote  Post ReplyReply Direct Link To This Post 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?
Back to Top
Freon22 View Drop Down
Groupie
Groupie


Joined: 04 December 2005
Status: Offline
Points: 42
Post Options Post Options   Thanks (0) Thanks(0)   Quote Freon22 Quote  Post ReplyReply Direct Link To This Post 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 
Back to Top
mike7510uk View Drop Down
Newbie
Newbie


Joined: 05 October 2006
Location: United Kingdom
Status: Offline
Points: 21
Post Options Post Options   Thanks (0) Thanks(0)   Quote mike7510uk Quote  Post ReplyReply Direct Link To This Post Posted: 18 October 2006 at 8:55am
Originally posted by Freon22 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
Back to Top
mike7510uk View Drop Down
Newbie
Newbie


Joined: 05 October 2006
Location: United Kingdom
Status: Offline
Points: 21
Post Options Post Options   Thanks (0) Thanks(0)   Quote mike7510uk Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
mike7510uk View Drop Down
Newbie
Newbie


Joined: 05 October 2006
Location: United Kingdom
Status: Offline
Points: 21
Post Options Post Options   Thanks (0) Thanks(0)   Quote mike7510uk Quote  Post ReplyReply Direct Link To This Post Posted: 31 October 2006 at 9:11am
Originally posted by Freon22 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?
Back to Top
michael View Drop Down
Senior Member
Senior Member
Avatar

Joined: 08 April 2002
Location: United States
Status: Offline
Points: 4670
Post Options Post Options   Thanks (0) Thanks(0)   Quote michael Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
 Post Reply Post Reply Page  12>

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.