473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Inserting into two tables with one sql statement

Anyone who can help,

I have two tables T1 and T2. T1 has fields K1 and F2 and T2 has fields
K2 and F1. F1 is the foreign key relating to K1 and F2 is the foreign
key relating to K2.

My initial question has been how can I add a row to both tables with
accurate Fx values in one SQL INSERT statement? I have been told it
cannot be done.

If it can be done in one statement, what is that statement?

If it cannot be done in one statement, how can it be done in the least
number of statements?

Thanks,

Greg the SQL student
Jul 20 '05 #1
4 4395
Hallo Greg,

I'm not sure if I understood it right, but it sounds like that you
have 2 relationships between two tables. Did you really follow the
normalisation rules for relational databses ?
In SQL Server I always have created my IDs (Keys) automatically. Than,
after inserting a row into the first table, I gte the new ID by
functions provided in T-SQL e.g @@IDENTITY / SCOPE_IDENTITY ... The
new ID is used to insert a row in the second table.
Maybe you should put a smal example of what you are really doing here.
I also would be interested in an approch of inserting into multiple
related tables by one statement. As said, by today I do this
sequentially.
Regards
Rolf

gr********@yahoo.com (Greg Ofiesh) wrote in message news:<d9*************************@posting.google.c om>...
Anyone who can help,

I have two tables T1 and T2. T1 has fields K1 and F2 and T2 has fields
K2 and F1. F1 is the foreign key relating to K1 and F2 is the foreign
key relating to K2.

My initial question has been how can I add a row to both tables with
accurate Fx values in one SQL INSERT statement? I have been told it
cannot be done.

If it can be done in one statement, what is that statement?

If it cannot be done in one statement, how can it be done in the least
number of statements?

Thanks,

Greg the SQL student

Jul 20 '05 #2
gr********@yahoo.com (Greg Ofiesh) wrote in message news:<d9*************************@posting.google.c om>...
Anyone who can help,

I have two tables T1 and T2. T1 has fields K1 and F2 and T2 has fields
K2 and F1. F1 is the foreign key relating to K1 and F2 is the foreign
key relating to K2.

My initial question has been how can I add a row to both tables with
accurate Fx values in one SQL INSERT statement? I have been told it
cannot be done.

If it can be done in one statement, what is that statement?

If it cannot be done in one statement, how can it be done in the least
number of statements?

Thanks,

Greg the SQL student


hi there,

AFAIK ms-sql server supports neither deffered constraint checking nor
multiple assigment, it cannot be done in oe statement.

only workaround i can cuggest,
is make one of the foreign key columns nullable, say F1 in table T2,
insert into T2 (F1 is null), insert into T1 and then set appropriate
value
into column F1 (if your version supports "instead of" triggers i'd
look that way)

HTH
Strider
Jul 20 '05 #3
On 14 Sep 2004 17:03:57 -0700, Greg Ofiesh wrote:
Anyone who can help,

I have two tables T1 and T2. T1 has fields K1 and F2 and T2 has fields
K2 and F1. F1 is the foreign key relating to K1 and F2 is the foreign
key relating to K2.

My initial question has been how can I add a row to both tables with
accurate Fx values in one SQL INSERT statement? I have been told it
cannot be done.

If it can be done in one statement, what is that statement?

If it cannot be done in one statement, how can it be done in the least
number of statements?

Thanks,

Greg the SQL student


Hi Greg,

It's not possible to have one INSERT statement add data to two tables at
once. The minimum number of statements is, of course, two: one INSERT
statement for each table affected.

(Note - if you are really determined, you can pretend that it's done with
one statement by creating a view that joins both tables, creating an
INSTEAD OF INSERT trigger for that view that changes one INSERT statement
on the view to two INSERT statements for the underlying tables. But it's a
lot easier to just pop two INSERT statements into a stored procedure and
call that when you want to add data).

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #4
Following normalization rules, T1 is a table with account info (e.g.-
username and password) while T2 identifies an entity. Since an entity
may not have an account, T1 stands alone. T2 identifies the entity by
its primary key and other tables use that key as their foreign key to
refer names, addresses, etc. to the entity. The dual relationship was
a suggestion for performance improvements.

The reason I asked the question is that to populate T2 with F1 I need
to know K1, and to populate T1 with F2 I need to know K2. It seemed to
me that SQL may have grown to the point of dealing with this, but
apparently not.

The problem is I need to populate T2 first, since it is the entity. T1
is optional. But if T2 only has K2 and F1, then how will I identify
the record that I just added to get K2 to populate F2 if I don't know
K2 and have no F1?

I have been told by others I need some type of unique identifier to
query the record to get K2. So I am now thinking thread ID along with
'1000000000' to keep from colliding with valid F1 entries. Then I can
get K2 and change F1 after populating T1.

Any thoughts are welcomed...

Greg

Ke*****@ee.nec.de (Rolf Kemper) wrote in message news:<bb*************************@posting.google.c om>...
Hallo Greg,

I'm not sure if I understood it right, but it sounds like that you
have 2 relationships between two tables. Did you really follow the
normalisation rules for relational databses ?
In SQL Server I always have created my IDs (Keys) automatically. Than,
after inserting a row into the first table, I gte the new ID by
functions provided in T-SQL e.g @@IDENTITY / SCOPE_IDENTITY ... The
new ID is used to insert a row in the second table.
Maybe you should put a smal example of what you are really doing here.
I also would be interested in an approch of inserting into multiple
related tables by one statement. As said, by today I do this
sequentially.
Regards
Rolf

gr********@yahoo.com (Greg Ofiesh) wrote in message news:<d9*************************@posting.google.c om>...
Anyone who can help,

I have two tables T1 and T2. T1 has fields K1 and F2 and T2 has fields
K2 and F1. F1 is the foreign key relating to K1 and F2 is the foreign
key relating to K2.

My initial question has been how can I add a row to both tables with
accurate Fx values in one SQL INSERT statement? I have been told it
cannot be done.

If it can be done in one statement, what is that statement?

If it cannot be done in one statement, how can it be done in the least
number of statements?

Thanks,

Greg the SQL student

Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ryan Hubbard | last post by:
I'm inserting a record into MySQL 4.0 using Visual Basic ADO. When using the AddNew and Update method I am unable to retrieve the value of a Auto incrment field (Yes I know I can MoveLast but this...
0
by: Marko Poutiainen | last post by:
Situation: We had to make our SQLServer 2000 database multi-lingual. That is, certain things (such as product names) in the database should be shown in the language the user is using (Finnish,...
23
by: Eva | last post by:
Hi i am trying to insert a new row into one of my datatabels that i have in my dataset when a button is clicked. here is my code Dim ClientInsRow As DataRow = dtClient.NewRo ...
1
by: Srinadh | last post by:
Hi all, We have files with about 20 to 30 fields per row. We are trying to update such files with about 60 rows as contiguous data in a CLOB field. It passes through. But when we try...
1
by: Azel | last post by:
Hi, I am trying to learn ADO.net and I keep running into problems trying to insert data into my Access Database: data.mdb. here is my code: <code> // Database Variables
2
by: a | last post by:
NEW Post Here's my best guess at how to insert this dataset.... the code runs, but no new records are added to the sql table. I've read and split a delimited text file into a dataset. It...
14
by: Ben | last post by:
I don't know whether anyone can help, but I have an odd problem. I have a PSP (Spyce) script that makes many calls to populate a database. They all work without any problem except for one...
8
by: markjerz | last post by:
Hi, I basically have two tables with the same structure. One is an archive of the other (backup). I want to essentially insert the data in to the other. I use: INSERT INTO table ( column,...
5
by: dos360 | last post by:
Hello, I have two tables, one is a list of activities, the other a list of participants. I want to insert one record in the activities table and then using its identity column as foreign key, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.