How to create properly tree-hierarchy

database-design

I'm using web2py to create an application

I've got 2 different roles: client and referents. Every client can have multiple referents.
Both client and referent can insert row in a table, called object.

The user table, where client and referent are registered, has a column called "client_id".
Every referent in the column_id has the id of the client to whom he reference to.

The problem is: which value should a client have in client_id?
A possible answer could be NULL or the id of the client itself.

Which one is better? Or there's something better?

Best Answer

Main reason why I'd choose NULL is a bit simpler queries - you don't need to care about self-relationship when you are querying ,for instance, number of referred users by the given. You can also save some space in case you create an index on client_id (it usually makes sense) because some RDMS, for example Oracle, don't include NULLs into index, others, like SQL server, allow filtered indexes (WHERE client_id IS NOT NULL).