数据库多对多关系举例说明
网站编辑2023-06-25 11:08:26286
假设有两个表:table1和table2。table1包含id列和name列,table2包含id列和name列。id列是唯一的,而name列是按字母顺序排序的。

现在,我们需要将table1中的行添加到table2中。可以使用以下语句实现:
INSERT INTO table1 (id, name)
VALUES (1, 'John'),
(2, 'Mary'),
(3, 'John'),
(4, 'Mary');
这将在table1中添加1行到table2中,同时将id列的值更新为2。
请注意,此示例仅包含两个表。如果需要更复杂的多对多关系,可以使用关系模型来实现,例如:
``` Database db1 = new SqlConnection(connectionString); db2 = new SqlConnection(connectionString);
// 创建两个连接 conn = db1.createStatement(); c = db2.createStatement();
// 将连接对象连接到数据库中 db1.executeUpdate(); db2.executeUpdate();
// 更新其他行 db1.commit(); db2.commit();
// 关闭连接 db1.close(); db2.close(); ```







