Wrox Home  
Search P2P Archive for: Go

  Return to Index  

sql_language thread: INSERT INTO multiple tables


Message #1 by REdwards@c... on Thu, 11 Jul 2002 21:23:37
Sounds right.

Obviously, you cannot INSERT a new row with a primary key which already
exists, so you'll have to test to see if the PK is there and only attempt
the INSERT if it is not.

Make sure you worry about the converse: you should have a foreign key
constraint on the second table, which will *require* that every row in the
FK table have an entry in the PK table first.

So, you check to see if the PK entry is there, and add it if not.  Then
insert into the second table and all should be well.  You should wrap the
whole operation in a transaction to insure that you are not left with "half"
of the update done in one table and not the other if something horrible
happens...

--
Jeff Mason			Custom Apps, Inc.
Jeff@c...

-----Original Message-----
From: REdwards@c... [mailto:REdwards@c...]
Sent: Thursday, July 11, 2002 10:11 PM
To: sql language
Subject: [sql_language] RE: INSERT INTO multiple tables


You'll have to excuse me, I've been working on small Access DB using ASP
on our Intranet site up until now. Now I am working with ASP.NET - which
is fairly easy - trying to get accustomed to SQL Server. Up until now
normalization was never a concern (at least no one wanted to bother).

I understand the process for selecting using JOIN and have performed it
fairly easily. I am just now testing the INSERT for multiple tables.

For my two part question, the first part was the correct syntax. The
second I may have answered. I have a code number that is a PK in one table
and a FK in another. When a user inputs a new record with an existing code
number it will be added to the second table where it is an FK and can be
in multiple rows. For the first where it is a PK I am assuming that I must
have something to check to see if it already exists in that table. If it
does then it is not added. If it doesn't then it is added.

Sound right?


  Return to Index