Skip to content
Advertisement

How to add or insert foreign key value in child table from parent table using insert statement

I want to use the primary key of parent table in my child tables. I have created a foreign key to connect QUESTION and ANSWER table with each other. below is code of my database:

user table

So, In USER table I have added a new value:

Now in QUESTION table I want to add a new value and I want to use the user_id value from USER table. Since user_id is foreign key in QUESTION table. So, far i have tried this query but its not working.

I have referred this question and solution but its not working. inserting data from parent table to child table in postgres

Advertisement

Answer

The query you are trying is something like:

But as far as I can see you cant insert only this values because on

you have user_id INT NOT NULL so it expects a value.

Something like :

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement