Yes, sqlite does support that. You can easily set a foreign key that references the same table. The model that you describe is called the adjacency list model, and as is very popular.
However, the problem with the adjacency list model is that recursive operations are very difficult to write unless the DBMS can do recursive queries.
Some DBMSes, such as SQL Server 2005, Postgres 8.4 and Oracle 11g1, support recursive queries with common table expressions using the WITH
keyword). This feature allows recursive queries to be written easily, but sqlite and MySQL do not support recursive queries yet.
You may be interested in checking out the following article which describes an alternative model (the nested set model), which makes recursive operations much easier, even though the nested set model is definitely more complex than the adjacency list:
In addition, I also suggest checking out the following presentation and posts by @Bill Karwin, a regular contributor on Stack Overflow2:
The closure table model described in those links is a very valid alternative to the nested set model. Bill further describes this model in his SQL Antipatterns book (excerpt from the chapter on this topic [PDF]).
Otherwise, you could also stick to the simple adjacency list model and then do the recursive part in your application, downloading all the data, building a tree, and walking through it.
1 Oracle can also do recursive queries using CONNECT BY
, which is supported since version 2.
2 You may also want to check out the following Stack Overflow post: Recursive data processing performance using Java and SQLite, discussing this topic.