473,804 Members | 2,215 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Insert into tables - many to many relationship

2 New Member
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 13392
iburyak
1,017 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Top Contributor
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 Recognized Expert New Member
...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 Recognized Expert Top Contributor
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
3901
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 other browsers (I checked it under Konqueror). Thank you in advance for your help. Regards. /Mariusz <HTML>
14
4303
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 salvage the records from the many table and without going into detail, one of the reasons I can't do the opposite as there are records in the ONE table that I need to keep even if they don't have any child records in the MANY table. Below I created...
18
27723
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) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , NOT NULL , CONSTRAINT PRIMARY KEY CLUSTERED (
3
4507
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 tblPurchases, with PurchaseId, CustomerId and ProductId I created a form with a subform which shows all the customers, one at a time
5
10886
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 new database. For instance, her old database has a table with Band Info in it. Her new database also has a table with Band Info in it but slightly different. I was wondering if there was an easy way to compare the fields from similar tables in...
8
9247
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 is entered, either from a form or from the table view of the table, when the record gets saved it immediately displays #DELETED# in all of the fields. However, if I close the form or table view and reopen the record has in fact been inserted. The...
8
6302
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 one-to-one relationship but on this occasion I must keep username/password details within a seperate table. Here's the basic specs and database schema: -------------------------------------------
3
1273
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 show on requirement. thanks in advance KASHIF AHMED
0
3977
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 OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & db.Name) conn.Open() Dim oda As New OleDb.OleDbDataAdapter("select " & sqlCols & " from ;", conn) Dim cb As New OleDb.OleDbCommandBuilder(oda) cb.QuotePrefix = "" oda.UpdateCommand =...
2
1988
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 it says. "The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. " This error occurs on the daComputers.Update(dsHardware, "Computers") line Here is the...
0
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10350
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6866
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3834
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.