如何在 SQLite 数据库中创建嵌套表?(安卓) [英] How to create nested tables in SQLite database?? (android)

查看:56
本文介绍了如何在 SQLite 数据库中创建嵌套表?(安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在android中创建一个嵌套的sqlite数据库.IE.我希望表中的特定字段以不同表的形式拥有其全新的一组值.

i want to create a nested sqlite database in android. ie. I want a particular field in a table to have its whole new set of values in the form of a distinct table.

推荐答案

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

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"作为Fred"的孩子,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

这篇关于如何在 SQLite 数据库中创建嵌套表?(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆