考虑表 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。
因此,我有 2tableB
个条目与单个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
它吗?