Solved

Doing an insert into two related tables at once in MYSQL

Posted on 2013-12-23
4
416 Views
Last Modified: 2013-12-26
I have two tables.   One is called dictionary, the other one is called tokendictionary.  
the identity column in the dictionary corresponds to a column in the tokendictionary called dindx.   Thus, when I make an entry in the dictionary, I need to make a corresponding entry in the tokendictionary such that the identity column in the dictionary equals the foreign key, dindx in the token dictionary.    I know how to do this using TSQL in a MS SQL database.

Here is an example of the script I use:
DECLARE id int;
BEGIN TRANSACTION;
INSERT INTO [dictionary] ([code], [acute], [gender], [codetype],  [codingsystem],[papplydate],[capplydate],[type]) VALUES
('0208T','a','n','p','9','9999-01-01','2014-01-01','0');
Select @id = SCOPE_IDENTITY();
INSERT INTO [tokendictionary] ([dindx], [token]) VALUES
( @id,'AUDIOMETRY AIR ONLY');
COMMIT TRANSACTION;

Open in new window


My question is, how do I do this in MYSQL?
0
Comment
Question by:efamilant
  • 2
4 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
LAST_INSERT_ID() is used in Mysql

Try to get identity like below
SELECT LAST_INSERT_ID() as fileId
0
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
INSERT INTO table1 (title,userid) VALUES ('test', 1);
SET @last_id_in_table1 = LAST_INSERT_ID();
INSERT INTO table2 (parentid,otherid,userid) VALUES (@last_id_in_table1, 4, 1);

this is from reference site
http://stackoverflow.com/questions/3837990/last-insert-id-mysql
0
 
LVL 33

Accepted Solution

by:
snoyes_jw earned 500 total points
Comment Utility
You don't even have to use the variable in the middle. You can call the function directly in the second INSERT statement:

INSERT INTO table2 (parentid, otherid, userid) VALUES (LAST_INSERT_ID(), 4, 1);

The function works only with AUTO_INCREMENT fields.
0
 

Author Closing Comment

by:efamilant
Comment Utility
This is a very nice solution to my problem.   It's so much easier than MS SQL.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now