且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在SQLite数据库中创建嵌套表? (android)

更新时间:2022-11-29 22:27:55

你描述是不可能的;没有办法在另一个表的行中包含表。标准做法是通过将父表的主键包括在子表中的列来创建父/子表;例如:

What you're describing isn't possible; there is no way to include a table within a row in another table. Standard practice is to create "parent/child" tables by including the primary key of the parent table as a column in the child table; for instance:

PARENT TABLE

id | name
---------
1  | Fred
2  | Bob







CHILD TABLE
id | parent_id | name
---------------------
1  | 1         | John
2  | 1         | Jim
3  | 2         | Joe
4  | 2         | Jane

这对表将John和Jim ,Joe和Jane作为Bob的孩子。您可以使用以下查询来获取Bob(父级ID = 2)的所有子级的集合:

This pair of tables would have "John" and "Jim" as the children of "Fred", and "Joe" and "Jane" as children of "Bob". You could get the set of all children of "Bob" (parent id=2) with the query:

SELECT * FROM child_table WHERE parent_id = 2