Here is simplified version of my database:
+-----------+
| USER | +-------------------+
+-----------+ | EMAIL |
|user_id | +-------------------+
|email_id FK+-+--------<+email_id |
|email_name | |email_domain UNIQUE|
+-----------+ +-------------------+
As you can see, I have normalized email so that email domain and email name are separated
(e.g. if email = [email protected] then email_name = foo; email_domain = example.com)
But this introduced problem that I cannot solve on my own. When inserting into USER, I want to check if email_domain exists and if it does, then take that email_id and insert it as email_id in USER, else I want to create new EMAIL, get that newly created email's ID and insert it into user as email_id.
Is there some standard way when dealing with inserting into multiple tables at once ?