The "only" way to do this via SQL would be using stored procedures. You can pass your object tree using either XML or JSON string and process them (using object logic) inside your SP. Standard SQL as intended won't support what you want to achieve.
$LISTBUILD (IDs of referenced objects) should do the trick. The "parent" table here is only storing the corresponding IDs not the entire objects. You need to save each object individually and then link them.
While the object counterpart takes care of saving/inserting both objects in memory, the same is not true for SQL. You need to treat each object as individual rows on different tables.







Hi Omer, would your solution consider a trigger? Haven't tried this but you could have a trigger that executes after insert/update that would launch an update on the affected row that basically would "add" the new value in the counter to the old one:
Your queries should change to something like this
INSERT OR UPDATE myTable SET name='Omer', counter = 1
INSERT OR UPDATE myTable SET name='Omer', counter = -1
.....
The logic in the trigger would grab old value (handle NULL as 0) and add the new value only in cases where both are different or whatever your business logic might be.
To prevent infinite loops on updates your after update trigger can be defined to only be executed after any of the other columns in the table are updated with the exception of counter.