与三个表的两个一对多关系

发布于 2024-11-02 16:52:06 字数 1723 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潦草背影 2024-11-09 16:52:06

如果我理解:

问题就在这里: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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文