473,395 Members | 1,676 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,395 software developers and data experts.

Insert into tables - many to many relationship

2
Pretty new to SQL:

I have the following scenario:
Table1
(TableID int ,
Value1 varchar(50))

Table 2
(Table2ID int ,
Value1 varchar (50))

TableLink
(TableLinkID int,
TableID
Table2ID)

How do I insert a record in tablelink when inserting records in table1. Table2 merely holds data for me?

Would it be some kind of trigger?
Jan 25 '07 #1
5 13366
iburyak
1,017 Expert 512MB
If you are doing insert into Table1 where do you get value for Table2ID in relation with TableID that you trying to insert?

I assume that table TableLink is a multi to multi relationship link and should be filled in separate insert statement when you know which relationships are valid.
Jan 25 '07 #2
Fibbo
2
Hi Iburyak ... thanks for the reply ...let me expand on my explanation.

I need to insert into the Tablelink table a record from table1 (table1ID) with many values from (table2) ... example : USA (value from table1) with (California - value from table2) = row1, next row USA (value from table1) with (Texas) = row2 etc.

This information I'm passing hrough my DAL in VB ...how do insert this information from table1 and table 2 into the tablelink? Hope this makes sense?
Jan 25 '07 #3
iburyak
1,017 Expert 512MB
So I have to assume that you are trying to insert one ID from talble1 and all IDs from table2?
What if you try to insert value Canada? No USA states will be valid entries then.

I don't see your logic, sorry.
Jan 25 '07 #4
almaz
168 Expert 100+
...This information I'm passing through my DAL in VB ...how do insert this information from table1 and table 2 into the tablelink? Hope this makes sense?
First of all, it looks like you don't need TableLinkID for TableLink table. If it's true (if there are no tables that reference to specific record in TableLink) - I would recommend you to remove it and set primary key to (TableID, Table2ID).

Still not clear what are you trying to ask. I assume that Table1 and Table2 have identity columns and you are using Dataset (or Typed Dataset) and DataAdapter (or TableAdapters for Typed Dataset) as DAL. If no - please describe your DAL.

If you want to add a new record to Table1 and immediately (without saving changes to DB) start adding related items from Table2 (thus inserting records into TableLink):
In this case you will encounter the problem when identity column is not set for Table1 yet, so you cannot insert records into TableLink DataTable (because you don't know TableID for newly-created Table1 record).
Solution - before adding DataRows to TableLink you have to save Table1 changes to DB, and it will automatically update the TableID column of Table1 DataTable to correct value.

If it doesn't answer your question, please describe how do you handle the relations between Table1 and Table2 on client - do you have a TableLink DataTable, or you have some custom collections, or smth. else. Try to submit parts of the code (or some pseudo-code) that will describe what are you trying to achieve.
Jan 27 '07 #5
iburyak
1,017 Expert 512MB
Almaz,
you understand questions better then me ..... :)
I am trying to help but not always quite understand what the question is.
This one is really hard to figure…

I will be on vacation Starting February 9 you are on your own here body…. :)
Thank you very much. I learn a lot from you.
Jan 27 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

44
by: Mariusz Jedrzejewski | last post by:
Hi, I'll be very grateful if somebody can explain me why my Opera 7.23 (runing under linux) doesn't show me inner tables. Using below code I can see only "inner table 1". There is no problem with...
14
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to...
18
by: Elroyskimms | last post by:
I have a table using an identity column as its Primary Key and two columns (table reduced for simplicity) EmployeeNumber and ArrivalTime. CREATE TABLE ( IDENTITY (1, 1) NOT NULL , (10)...
3
by: Nicolae Fieraru | last post by:
Hi All, I have a problem and I can't figure out how to solve it. My database has three tables: tblCustomers, with CustomerId and CustomerName tblProducts, with ProductId and ProductCode...
5
by: Megan | last post by:
Hi everybody- I'm helping a friend with a music database. She has an old one and is creating a new one. She wants to compare records and fields in the old database with records and fields in the...
8
by: Bri | last post by:
Greetings, I'm having a very strange problem in an AC97 MDB with ODBC Linked tables to SQL Server 7. The table has an Identity field and a Timestamp field. The problem is that when a new record...
8
by: Carl | last post by:
Hi, I hope someone can share some of their professional advice and help me out with my embarissing problem concerning an Access INSERT query. I have never attempted to create a table with...
3
by: kashifahmed.bse.mg | last post by:
Hello All, I would like to insert dynamically created related tables by using datatable collections in ado.net. So can anyone figure out which procedure is usefull to do this task. Example will...
0
by: mwenz | last post by:
I am trying to update an Access table using OLEDB in VB.Net 2005. I can add rows but I cannot update them. Code to instantiate the Access database and table... Dim conn As New...
2
by: cday119 | last post by:
Can someone help me out with this one. I am trying to insert a row of data into an access database. The primary key is set to auto increment. It will work once but the next time you try an insert...
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: 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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.