1

I have 3 tables:

Words { wordId, name } WordGroups { wordGroupId, type} WordSets { wordSetId, wordId, wordGroupId} 

I need to Insert new Word, and new WordGroup and after that insert WordSet with 2 new ids (wordId and WordGroupId).

I make this:

INSERT INTO `wordgroups`(`type`) VALUES ('some_type'); SET @gid:=LAST_INSERT_ID(); INSERT INTO `words`(`name`) VALUES ('test name'); SET @wid:=LAST_INSERT_ID(); INSERT INTO `wordsets`(`wordGroupId`,`wordId`) VALUES(@gid,@wid); 

It works, but I have doubts that this is the best way.

Does anyone have advice about better solution?

2
  • the solution you have is the best solution possible. Commented Feb 27, 2018 at 14:04
  • @RaymondNijland ok, thanks Commented Feb 27, 2018 at 14:05

2 Answers 2

1

In my opinion your code is good, I've tried that before and there is no conflict or bugs I encounter. That's the best practices.

Keep up the good work!

Sign up to request clarification or add additional context in comments.

Comments

1

Yes, this is the best possible solution. Just make sure this code in a transaction so that failure in anything will rollout others.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.