Question: I want to insert a row only if it's parent exists in another table. 

**thread1 table**

```
id | content      | 
1  |  Hello World |
2  |  Bye World   |

```

**thread2 table**

```
id | content     | 
1  |  Naruto     |
2  |  DragonBallz|

```

**comment table**

```
id | thread_id| thread_type | content    |
1  |    1     |     thread1 |    hellow  |
2  |    1     |     thread2 |    bye-bye |

```

Now if i do 

```
INSERT INTO comment(thread_id,thread_type,content)VALUES('3','thread2','Whatever');

```

it should fail because `3` *does not* exists in `thread2` table.

This is possible by checking from thread table. But is it possible without it ? Without doing an extra query ? 

**Update** 

Tables above has been updated. the `thread_type` refers to the table `thread1` & `thread2`

Answer: 
Creating a foreign key between the two tables, using thread(id) as the parent and comment(thread\_id) as the child, should do the trick.

This is the command you should run - 

```
ALTER TABLE comment
    ADD FOREIGN KEY
    (thread_id)
    REFERENCES thread (id)

```
This may do that:

```
INSERT INTO comment(thread_id,thread_type,content)
(select id as thread_id, thread_type, '[content]' as content
from ((select id,'thread1' as thread_type from thread1)
union 
(select id,'thread2' as thread_type from thread2 )) threads
where threads.id=[thread_id] and threads.thread_type ='[thread_type]')

```

As in your sample

```
[thread_id]=3
[thread_type]=thread2
[content]=Whatever

```

So if there is such a parent it will be insert a row, otherwise won't insert anything.