I have this project in which user can create queries using web based UI in which there will be list of columns and applicable operators. Later users can edit these queries, too. So I need to store this in database.

I can store query in table as simple string but then editing will be not possible so I need to store it some other way. So I somehow managed to design it following way. 

[![Proposed DB Schema](https://i.stack.imgur.com/3ycVG.jpg)](https://i.stack.imgur.com/3ycVG.jpg)

So let's say I have to store this query:

```
C1 > 8 AND (C2 <= 7 OR (C4 LIKE '%all%' AND (C1 > 15 OR C2 <= 3)))

```

where: C denotes some column

If I have to store it in DB as shown in image, 

* I would group each condition and store it in sub\_operand table
* then there will be recursive mapping entry in op\_master table for each entry in sub\_operand table
* finally there will be master entry in op\_master

But it seems too much complicated to handle insert and update. Can someone help me with this? I am very much stuck here.

**UPDATE: I think I am missing something here in schema. It won't work as I have thought. I will update question as soon as I can correct it.**