【发布时间】:2023-04-09 18:58:01
【问题描述】:
考虑表 A 引用表 B 的情况:
create table tableA
(
id INT NOT NULL PRIMARY KEY,
information VARCHAR(32)
);
create table tableB
(
id INT NOT NULL PRIMARY KEY,
tableAid INT NOT NULL REFERENCES tableA(id),
other_information VARCHAR(32)
):
请注意,我是用 Perl 编写代码,数据库是 PostgreSQL。
所以,我有 2 个 tableB 条目与单个 tableA 条目相关联。我想说的是:
use DBI;
my $dbh = DBI->connect("DBI:Pg:dbname=mydb;host=localhost","userid","password",('RaiseError' -> 1));
my $otherinfo = "other info, Table B";
my $moreotherinfo = "more other info, table B";
my info = "table A info";
my $insertitA = $dbh->prepare("insert into tableA (information) values (?)");
my $insertitB = $dbh->prepare("insert into tableB (tableAid,other_information) values (?,?)");
my $nrowsA = $insertitA($info);
my $tableAidreference = ????;
my $nrowsB = $insertitB($tableAidreference, $otherinfo);
my $nrowsB2 = $insertitB($tableAidreference, $moreotherinfo);
我在哪里可以得到$tableAidreference?我必须搜索tableA 吗?
【问题讨论】:
标签: sql postgresql insert foreign-keys dbi