Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Primery key and foregion key

Author  Topic 

Reebal
Starting Member

1 Post

Posted - 2010-02-20 : 04:56:40
Have a good day

lets assume that i have 2 tables

First one is customer table and has primery key called cusid ( auto increment)

second one is order table and has foregion key custid

how i can coonect them , what i mean exactly when insert the record in the customer table i need the foregion key get the same value as primery key in customer table ,,

appreciate your help

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-02-20 : 05:05:21
Means automatically you want to insert it in another table or what ?

Vabhav T
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-20 : 06:04:38
quote:
Originally posted by Reebal

Have a good day

lets assume that i have 2 tables

First one is customer table and has primery key called cusid ( auto increment)

second one is order table and has foregion key custid

how i can coonect them , what i mean exactly when insert the record in the customer table i need the foregion key get the same value as primery key in customer table ,,

appreciate your help


something like

DECLARE @CustomerID int

INSERT INTO customer VALUES(....)

SET @CustomerID=SCOPE_IDENTITY()

INSERT INTO order (...,customer_fk,....)
VALUES(...,@CustomerID,...)


I hope you wont do batch inserts on both tables under any condition

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-02-20 : 06:35:58
Its correct if this is his requirement.


Vabhav T
Go to Top of Page
   

- Advertisement -