与三个表的两个一对多关系
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')")
大家好,
我有三张桌子。一张将是祖父母,一张将是父母,最后一张将是子表。我的意思是,我希望我的孩子数据知道它属于哪个父母和祖父母。另外,我希望我的父母数据知道它属于哪个祖父母。我尝试自己做。但我不能。我应该如何设置表之间的关系?表结构应该如何?
提前致谢。
编辑
忽略代码。只需在原始 SQL 中设置三个表即可满足所需的关系。 这是我第一次做这样的事情,我需要指导。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解:
问题就在这里:
FOREIGN KEY(parent_id) REFERENCES Parent(name)
并且将是FOREIGN KEY(parent_id) REFERENCES Parent(id)
。参考 ID 而不是名称。引用父表,通过查询,您可以从子表返回祖父母记录。
如果不是您要找的,请评论。
If I understanded:
the problem is here:
FOREIGN KEY(parent_id) REFERENCES parent(name)
and would beFOREIGN 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.