Last post Oct 11, 2013 06:54 PM by Carthalion
Oct 11, 2013 12:29 PM|Carthalion|LINK
I have two tables using MySQL. One has a primary & foreign key and one has a primary key. The primary key in the child table is the foreign key in the master table. I think this is basic stuff.
What I want to do is create a new record in both tables. The primary keys are auto increment of course. How do I make the new primary key in the child table populate the column in the master table that holds the foreign key.
I hope this question makes sense.
Oct 11, 2013 12:46 PM|AZMatt|LINK
Here is an example SQL script using SCOPE_IDENTITY...
declare @ForeignKey int
insert into childtable (col1, col2)
values ('something', 'something else')
select @ForeignKey = SCOPE_IDENTITY()
insert into parenttable (FKey, col1, col2)
values (@ForeignKey, 'something', 'something else')
Oct 11, 2013 06:54 PM|Carthalion|LINK
Thank you for your help. I guess what I really need is an example for how to code it in the .vb page. Right now I use this to start a new record:
Dim SqlString As String = "Insert Into Clients (IdFolder,CustName,CustPho) Values (?,?,?)"
Using conn As New OdbcConnection(Connstr)
Using cmd As New OdbcCommand(SqlString, conn)
cmd.CommandType = CommandType.Text
Can you give me an example of code to use to send @ForeignKey to the child table?