Solved

Doing an insert into two related tables at once in MYSQL

Posted on 2013-12-23
4
426 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
ID: 39737489
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
ID: 39737491
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
ID: 39738082
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
ID: 39741108
This is a very nice solution to my problem.   It's so much easier than MS SQL.
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

777 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