设置关系表

发布于 2024-11-03 03:10:34 字数 1318 浏览 1 评论 0原文

我想添加关系数据而不处理 id。让我解释一下。例如,Nokta1Nokta2Cihaz1 相关。 Nokta3Cihaz2 相关。所以这是一个一对多关系。

Cihaz1Cihaz2 行添加到 cihaz 表后,如何立即插入 Nokta1Nokta2< /code> 和 Nokta3 行到 nokta 表有这些关系吗?

我的代码有什么错误吗?

import sqlite3

# data
NOKTALAR = (
    ('Nokta1', 'AO', 'CVXY', '1'),
    ('Nokta2', 'AO', 'CVXY', '1'),
    ('Nokta3', 'BO', 'MESR', '1'),
    ('Nokta4', 'BO', 'MESR', '1'),
    ('Nokta5', 'BI', 'APTU', '2'),
    ('Nokta6', 'AI', 'FTRE', '1'),
    ('Nokta7', 'AI', 'FTRE', '1'),
)
CIHAZLAR = (
    ('Cihaz1'),
    ('Cihaz2'),
    ('Cihaz3'),
    ('Cihaz4'),
    ('Cihaz5'),
    ('Cihaz6'),
)

# get connection and cursor objects
conn = sqlite3.connect('iodatabase')
c = conn.cursor()

# create tables
c.execute('''create table cihaz (
    id integer primary key autoincrement,
    name text
)''')

c.execute('''create table nokta (
    id integer primary key autoincrement,
    name text,
    module text,
    type text,
    desc text,
    cihaz_id integer
    FOREIGN KEY(cihaz_id) REFERENCES cihaz(id)
)''')
c.execute('''create table link (
    nokta_id integer,
    cihaz_id integer,
)''')

I want to add a relational data without dealing id's. Let me explain. For example, Nokta1 and Nokta2 is related to Cihaz1. Nokta3 is related to Cihaz2. So this is a one-to-many relation.

Immediately after adding Cihaz1 and Cihaz2 rows to cihaz table, how can I insert Nokta1, Nokta2 and Nokta3 rows to nokta table with these relations?

Is there any error in my code?

import sqlite3

# data
NOKTALAR = (
    ('Nokta1', 'AO', 'CVXY', '1'),
    ('Nokta2', 'AO', 'CVXY', '1'),
    ('Nokta3', 'BO', 'MESR', '1'),
    ('Nokta4', 'BO', 'MESR', '1'),
    ('Nokta5', 'BI', 'APTU', '2'),
    ('Nokta6', 'AI', 'FTRE', '1'),
    ('Nokta7', 'AI', 'FTRE', '1'),
)
CIHAZLAR = (
    ('Cihaz1'),
    ('Cihaz2'),
    ('Cihaz3'),
    ('Cihaz4'),
    ('Cihaz5'),
    ('Cihaz6'),
)

# get connection and cursor objects
conn = sqlite3.connect('iodatabase')
c = conn.cursor()

# create tables
c.execute('''create table cihaz (
    id integer primary key autoincrement,
    name text
)''')

c.execute('''create table nokta (
    id integer primary key autoincrement,
    name text,
    module text,
    type text,
    desc text,
    cihaz_id integer
    FOREIGN KEY(cihaz_id) REFERENCES cihaz(id)
)''')
c.execute('''create table link (
    nokta_id integer,
    cihaz_id integer,
)''')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

感性 2024-11-10 03:10:34

如果您想通过名称而不是 ID 来关联它们,那么只需使用名称的外键即可。

c.execute('''create table nokta
             id integer primary key autoincrement,
             name text,
             parent text
             FOREIGNKEY(parent) REFERENCES cihaz(name)''')

现在,当您插入 nokta 时,只需使用适当的父级即可。

 c.execute('insert into nokta (text, parent) values ?, ?', ('Nokta1', 'Cihaz1'))

现在要查询与特定 cihaz 相关的所有 nokta,只需使用名称即可。

 c.execute('select * from nokta where parent = ?', ('Cihaz1',))

If you want to relate them by name and not by ids, then just use a foreign key to the name.

c.execute('''create table nokta
             id integer primary key autoincrement,
             name text,
             parent text
             FOREIGNKEY(parent) REFERENCES cihaz(name)''')

Now when you insert a nokta, just use the appropriate parent.

 c.execute('insert into nokta (text, parent) values ?, ?', ('Nokta1', 'Cihaz1'))

Now to query for all nokta related to a particular cihaz, just use the name.

 c.execute('select * from nokta where parent = ?', ('Cihaz1',))
猫弦 2024-11-10 03:10:34

链接表应该定义为:

   create table link (     
       nokta_id integer,      
       cihaz_id integer,
       FOREIGN KEY(nokta_id) REFERENCES nokta(id),
       FOREIGN KEY(cihaz_id) REFERENCES cihaz(id)
   )   

但你实际上根本不需要这个表。它只是 Nokta 表在属性 id 和 cihaz_id 上的投影(即相当于从 Nokta 中选择 id、chaz_id)。

将“Cihaz1”和“Cihaz2”行添加到“cihaz”表后,如何立即将“Nokta1”、“Nokta2”和“Nokta3”行插入具有这些关系的“nokta”表?

添加 Nokta1、Nokta2 和 Nokta3 应该没有问题,只要它们仅引用 cihaz 表中的现有值即可。

例如,

INSERT INTO CIHAZ(name) VALUES('Cihaz1');
...
INSERT INTO CIHAZ(name) VALUES('Cihaz7');

INSERT INTO NOKTA(name, module, type, desc, cihaz_id) VALUES ('Nokta1', 'AO', 'CVXY', '1', 1),

对于最后一个字段(外键字段),值 1、2、3、4、5、6 或 7 就可以了,因为我们已经将包含这些键的行插入到 CIHAZ(自动创建)中。

The link table should be defined as:

   create table link (     
       nokta_id integer,      
       cihaz_id integer,
       FOREIGN KEY(nokta_id) REFERENCES nokta(id),
       FOREIGN KEY(cihaz_id) REFERENCES cihaz(id)
   )   

but you really don't need this table at all. It is just a projection of the Nokta table over the attributes id and cihaz_id (i.e. equivalent to select id, chaz_id from Nokta).

Immediately after adding "Cihaz1" and "Cihaz2" rows to "cihaz" table, how can I >insert "Nokta1", "Nokta2" and "Nokta3" rows to "nokta" table with these relations?

There should be no problem adding Nokta1, Nokta2, and Nokta3 as long as they only reference existing values in the cihaz table.

e.g.

INSERT INTO CIHAZ(name) VALUES('Cihaz1');
...
INSERT INTO CIHAZ(name) VALUES('Cihaz7');

INSERT INTO NOKTA(name, module, type, desc, cihaz_id) VALUES ('Nokta1', 'AO', 'CVXY', '1', 1),

For the last field (the foreign key field) values 1, 2, 3, 4, 5, 6, or 7 would be ok, because we have already inserted rows with those keys into CIHAZ (automatically created).

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