0

我正处于需要在一个表中动态填充数据的阶段,该表与使用外键约束的其他两个表相关联。下面是我在 PostgreSQL 中创建的模式/表。

表格1:

application
--------------------------
id | name | size | rate   

表#1“应用程序”的“id”作为“序列”,这也是一个主键,“名称”作为 Varchar,“大小”和“速率”作为整数

表#2:

Create
-------------------------
createId | name | score

相似地。createId 再次是该表的序列号和主键,“name”为 varchar,“score”为整数。

现在,我正在创建第三个表,它应该是上述两个表的映射表,其中包含以下字段-

表3

app_create
---------------------
app_create_id | id | createId |

在上表中,我再次将 app_create_id 设置为序列和主键以及其他两个字段作为外键,并引用了上表 application(Id) 和 create(createId)。

接下来,当我在表#1 和#2 中插入值时,我的理解是它应该根据我在其他两个表中插入的数据自动将其填充到第三个表#3 中。但是,相反,我得到一个空白表,这意味着表#3(app_create)映射表中没有值。(插入表#1和2之后)

我希望得到如下所示的输出,例如-应用程序表和创建表填充了以下数据-

application
--------------------------
id | name | size | rate    
1  |    xyz  |   2096 | 12                                         
2  |   mno   |  1280  | 34


Create
-------------------------
createId | name | score                                                         
1        | a.b.c | 50       
2       |  m.cpm  |20    

现在,在我将数据插入上表这两个之后,我不确定我的下表第三个会发生什么。我认为一旦我填充上面的两个表,它也会以相同的方式填充,因为存在主键和外键关系。

表3

app_create
----------------------------------
app_create_id | id | createId |                         

我相信它与多对多关系有关,但我不确定我应该如何以一种或任何正确的方式进行相同的操作并获得同样的输出。如果我做错了什么,请帮助我理解和纠正我,或者指导我应该如何进行以便动态地将数据放入映射表# 3 中?

4

2 回答 2

0

这确实与多对多关系有关。任何人都无法为您填写表3的原因是因为没有人知道这种关系是什么。因此,您需要自己填写。应用程序如何与创建相关?(什么是创造?)

所以你需要问自己/老板/同事的问题是:哪些应用程序应该连接到哪些创建?

于 2013-06-29T03:15:01.067 回答
0

That is not how foreign keys are used. They tell the DBMS that a subrow of values for a column list must also appear as a subrow of values for another column list. They can be associated with effects on a table update cascading to referenced table updates.

Normally we would expect a base table with foreign key references, like any base table, to be updated by the user according to the rule/predicate/criterion for putting rows into or keeping them out of the table.

What you are describing is close to a view. Ie a virtual table whose value is the current value of an expression involving bases or other views. You have to say what expression you want. (It is not based on foreign keys.) But after that the DBMS calculates the appropriate value.

Here here you may be expecting that app_create (ignoring app_create_id) is the join of the other two:

create view app_create as
select id, createId
from application JOIN create

(But you don't explain what the output should look like or give example output.)

This doesn't account for app_create_id. However by virtue of the join, (id, createId) is unique. (In some SQL DBMSs that can be declared.) So you don't need app_create_id.

But app_create_id is problematic anyway. How should its value be determined? It would have to be a function of id and create and maybe something else. Can the same value identify different rows at different times? Can two different (id, createId) pairs use the same app_create_id at different times?

于 2015-07-14T23:36:04.810 回答