How to create nested tables in SQLite database?? (android)

前端 未结 2 1702
情书的邮戳
情书的邮戳 2021-01-01 00:19

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.

相关标签:
2条回答
  • 2021-01-01 00:38

    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
    

    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
    
    0 讨论(0)
  • 2021-01-01 00:45

    That is what i was going to do but You won't be able to create another sub-table of the current table.

    I will suggest you to create two different table from it, e.g.. if you have table employee and need to create another two sub-table marketing employee and engineering employee tables, just create the two table that i was describing. e.g.

    CREATE TABLE emp_engineer 
    

    and

    CREATE TABLE emp_marketing 
    
    0 讨论(0)
提交回复
热议问题