MySQL Forums
Forum List  »  General

How to insert rows with foreign key?
Posted by: Laura Berger
Date: December 12, 2017 12:28PM

Hi! I'm newbie in SQL and for porpouse of leanging I want to create a system to register users.
I made these tables:
account(id, email, password)
address(id, city, street, number, phone)
user(id, name, lastName, c_id, d_id)
c_id = foreign key for account
d_id = foreign key for address

So, if I want to register a new user I run three commands of insert.
INSERT INTO account(email, password) VALUES('user01@gmail.com', '12345')
INSERT INTO address(city, street) VALUES('city01', 'street01')

And here is the problem. For c_id I know how to get the id.
INSERT INTO user(name, lastName, c_id, d_id) VALUES('user01', 'lastname01', SELECT id FROM account WHERE(email='user01@gmail.com' AND password='12345'), ?)

But how can I get the id from address considering the user doesn't type all fields only the mandatory(city, and street)?

Maybe my table scheme is not right. Could you make it better?

Options: ReplyQuote


Subject
Written By
Posted
How to insert rows with foreign key?
December 12, 2017 12:28PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.