SQLTeam.com | Weblogs | Forums

How to insert in a table given a foreign key


#1

So, I have these three tables and I want to insert a new event in a certain address but i don't know how to do that.

account(id, email, password)
address(id, city, street, number, c_id(foreign key for account))
event(id, title, description, e_id(foreign key for address))

The idea is an user can registry an event in his own address. Bellow what I'm trying to do.

/I believe this can return multiple lines./
SELECT c_id FROM public.address WHERE city='new york';

/This will return only one line./
SELECT id FROM public.account WHERE email='user01@gmail.com' AND password='user01';

The insertion in event would be something like:

/I don't know how to combine those lines above./
INSERT INTO public.event(id, title, description, c_id, e_id) VALUES ('1', 'event title', 'event description', '????')


#2

For event you would need to have the e_id before doing the insert.