在SQL Server中向具有关系的表写入数据。

huangapple go评论76阅读模式
英文:

Writing data to tables with relationship in SQL Server

问题

以下是翻译的内容:

我有两个不同的表格,每个表格有多个列。如下所示:

Table1

ID  CUSTOMER  PRODUCT DETAILS
21    joe         phone
22    doe         shoe
23    john        cup       

Table2

PRODUCT ID   COST    ID
   1         9000    21
   2          600    22  
   3          30     23

Table1中的ID是主索引,Table2中的PRODUCT ID是主索引。这两个表格之间的共同链接是ID。我使用以下查询读取这些值。

String query = "SELECT * FROM Table1 c1 INNER JOIN Table2 c2 ON c1.ID = c2.ID";

但是我的问题是,当我添加新行时,我使用以下查询。

String query1 = "INSERT INTO Table1(CUSTOMER) VALUES ('" + alex + "') ";
String query2 = "INSERT INTO Table2(COST) VALUES ('" + 500 + "') ";

由于Table2的ID不是主索引,我在ID中得到NULL值。当我再次尝试读取时,由于ID不匹配,我无法重新加载保存的数据。

编辑

根据此处提供的解释,我尝试了以下内容:

String foreignKey = "ALTER TABLE Table2 ADD FOREIGN KEY (ID) REFERENCES Table1(ID)";

仍然在Table2中,ID列为空。我是否需要将查询以某种方式组合起来以实现结果?因为我只是按以下方式逐个执行查询。

Statement stmt = connect.createStatement();
stmt.executeUpdate(query1);
stmt.executeUpdate(query2);
stmt.executeUpdate(foreignKey);
英文:

I have two different tables with multiple columns. Like the following

Table1

ID  CUSTOMER  PRODUCT DETAILS
21    joe         phone
22    doe         shoe
23    john        cup       

Table2

PRODUCT ID   COST    ID
   1         9000    21
   2          600    22  
   3          30     23

ID in table1 is primary index and PRODUCT ID in table2 is the primary index. The common link between these two tables is ID. I used the following query to read the values.

String query = "SELECT * FROM Table1 c1 INNER JOIN Table2 c2 ON c1.ID = c2.ID";

But my problem is when I am adding new row, I use the following queries for it.

String query1 = "INSERT INTO Table1(CUSTOMER) VALUES ('" + alex + "') ";
String query2 = "INSERT INTO Table2(COST) VALUES ('" + 500 + "') ";

Since ID is not the primary index for Table2, I get a NULL value in ID. When I try to read again, since ID are not matched the I cannot reload the saved data.

Edit

This is what I have tried based on the explanation given here,

String foreignKey = "ALTER TABLE Table2 ADD FOREIGN KEY (ID) REFERENCES Table1(ID)";

Still the column ID is null in table2. Do I need combine the queries in some sort to acheive the result ? Because I just execute queries one by one as follows.

Statement stmt = connect.createStatement();
stmt.executeUpdate(query1);
stmt.executeUpdate(query2);
stmt.executeUpdate(foreignKey);

答案1

得分: 1

总体思路是,您需要从第一次“插入”到Table1中获取自动生成的ID,并在第二次“插入”时显式地使用此ID在Table2中设置“ID”。您可以在此处查看如何实现。

第二个表中的外键只是一个约束,有助于防止数据不一致性。其主要目的是在您尝试插入具有在主表中不存在的外键的某些数据时向您发出一些消息。此外,如果在创建外键时设置了这一点,它还可以帮助在删除Table1的行时删除与Table2中的外键关联的行。

您不应该每次向Table1、Table2插入数据时都调用stmt.executeUpdate(foreignKey);。在描述数据库结构时应该只执行一次。

英文:

General idea here - you need to get autogenerated id from first "insert" to Table1 and set "ID" at Table2 explicitly with this id in the second "insert". You can see how to do that here for example.

Foreign key in your second table - is just a constraint, that helps to prevent data's inconsistency. Its main purpose - is to give some message to you when you're trying to put some data with foreign key that doesn't exist in primary table. Also it can help (if you set this while creating foreign key) to remove from Table2 rows linked on foreign key with Table1 when you delete row at Table1.

And you shouldn't call stmt.executeUpdate(foreignKey); each time you insert data to Table1, Table2. It should be done once when you describe the structure of your database.

huangapple
  • 本文由 发表于 2020年6月5日 21:46:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/62216840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定