1
import sqlite3
# get connection and cursor objects
conn = sqlite3.connect('iodatabase.sdb')
c = conn.cursor()
# create tables
c.execute('''create table grand_parent (
 id integer primary key autoincrement,
 name text
)''')
c.execute('''create table parent (
 id integer primary key autoincrement,
 name text
 grand_parent_id text,
 FOREIGN KEY(grand_parent_id) REFERENCES grand_parent(name)
)''')
c.execute('''create table child (
 id integer primary key autoincrement,
 name text,
 module text,
 type text,
 desc text,
 parent_id text,
 FOREIGN KEY(parent_id) REFERENCES parent(name)
)''')
c.execute("INSERT INTO grand_parent VALUES(null, 'AS1')")
c.execute("INSERT INTO parent VALUES(null, 'Parent1', 'AS1')")
c.execute("INSERT INTO child VALUES(null, 'Child1', 'AO', 'CVXY', '1', 'Parent1', 'AS1')")
c.execute("INSERT INTO child VALUES(null, 'Child2', 'AO', 'CVXY', '1', 'Parent1', 'AS1')"
c.execute("INSERT INTO child VALUES(null, 'Child3', 'AI', 'FTRE', '1', 'Parent1', 'AS1')"
c.execute("INSERT INTO child VALUES(null, 'Child4', 'AI', 'FTRE', '1', 'Parent1', 'AS1')")
c.execute("INSERT INTO parent VALUES(null, 'Parent2', 'AS1')")
c.execute("INSERT INTO child VALUES(null, 'Child1', 'AO', 'CVXY', '1', 'Parent2', 'AS1')")
c.execute("INSERT INTO child VALUES(null, 'Child6', 'AI', 'FTRE', '1', 'Parent2', 'AS1')")
c.execute("INSERT INTO child VALUES(null, 'Child4', 'BO', 'MESR', '1', 'Parent2', 'AS1')")

Hi All,

I have three tables. One will be grand parent, one will be parent and the last one will be child table. I mean, I want my child data to know which parent and grand parent it belongs to. Also, I want my parent data to know which grand parent it belongs to. I tried to do it by myself. But I couldnt. How should I set up the relations between the tables? How should be the table structures?

Thanks in advance.

EDIT

Ignore the code. Just set up three tables in raw sql so that the required relation is met. This is the first time I do such a thing and I need guidance.

asked Apr 22, 2011 at 6:33
3
  • 1
    please rename your tables to grandparent, parent and child in your example. Please tell us with code what you would like to get and how you fail. Commented Apr 22, 2011 at 7:37
  • I think I will go for tree structure in database by using triggers Commented Apr 22, 2011 at 12:26
  • Using foreign key on text field is asking for trouble I think. Normally you link on id. Commented Apr 22, 2011 at 12:42

1 Answer 1

2

If I understanded:

the problem is here: FOREIGN KEY(parent_id) REFERENCES parent(name) and would be FOREIGN KEY(parent_id) REFERENCES parent(id). Reference ID instead NAME.

Referencing the parent table, through a query you is able to return the grandparent record from the child table.

Please comment if it is not what you are looking for.

answered May 2, 2011 at 18:55
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.