5

I'm doing a project in the field of multilevel marketing on .Net and SQL server. In the database it should save like a binary tree. How should i design a database?

0

3 Answers 3

4
id | parentid | name
---------------------
 1 | null     | node1
 2 | 1        | node2
 3 | 1        | node3
Sign up to request clarification or add additional context in comments.

5 Comments

Exercise: Write a query that returns all decedents from node1.
you mean this? select * From tab where parentid = (select id from table where name = 'node1')
That's hilarious :-) However, you probably should have pointed out a better alternative, like using nested sets model.
To Arsenmkrt - no, that would be direct descendants. You were asked for all descendants :)
@ChssPly76 well, that's easy no? select * from tab where name != 'node1' ;) but seriously how does this answer have 3 upvotes?
4

This has been asked and answered before.

Here's a pretty decent tutorial which explains why adjacency model proposed by arsenmkrt is less than ideal.

1 Comment

+1 I didn't think about that before ;)
3

SQL Server 2008 has a built-in data-type called hierarchyid to store hierarchical information. Here are some pointers.

And of course you can do this as mentioned by arsenmkrt in databases other than sqlserver2008.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.