3 Replies - 7223 Views - Last Post: 03 November 2012 - 10:12 AM

#1 AkoSiRonald   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-November 12

How to Make an Entry in a Table and Do it in Another

Posted 03 November 2012 - 05:35 AM

Well i am currently making a system using ASP.net C# and SQL Server

What i want to achieve is when i create an entry in a Table. Lets say "Customers" with primary key "customerID".

When i create entries in the table and i have a Column with Identity and i successfully entered every data needed.

I want to create an entry in another table with the same value as my primary key.

So in the first table

Customers
customerID: 1
Name: Mr. Random
Pass: RandomPassword

it will create an entry in another table like this and the other data are empty.

CustomersOrder
customerID: 1
order1: <empty>
order2: <empty>

i want to do this because i separated some data on tables and i want them to have the same key or ID to search or edit them easily.

Is This A Good Question/Topic? 0
  • +

Replies To: How to Make an Entry in a Table and Do it in Another

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15433
  • View blog
  • Posts: 61,842
  • Joined: 12-June 08

Re: How to Make an Entry in a Table and Do it in Another

Posted 03 November 2012 - 07:59 AM

Then why not, after you insert, grab the last id inserted, select all the data, and insert it into another table?

http://blog.sqlautho...tity-of-record/
Was This Post Helpful? 0
  • +
  • -

#3 AkoSiRonald   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-November 12

Re: How to Make an Entry in a Table and Do it in Another

Posted 03 November 2012 - 08:37 AM

what i mean is, its like a linked table

when i input data on table1, the primary key is linked to table2,

so they have like the same data from each table. I think its relationship, foreign key.

but it is not creating a data in table2 where the foreign key is created.

Something like this

Posted Image

This is the table with the primary key and has identity set to 1 seed. When i fill every data in there
i want its primary key value (in this case 1) to be the same value of the foreign key in table2 even without me filling up the values there.

Posted Image

i haven't set this foreign key to have identity
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15433
  • View blog
  • Posts: 61,842
  • Joined: 12-June 08

Re: How to Make an Entry in a Table and Do it in Another

Posted 03 November 2012 - 10:12 AM

Your logic is all off. Why would you do it this way? You insert your patient record into your patient table.. then get that identity and use it as a foreign key when you insert into on all details table. .. those detail tables should have their own primary key.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1