我想在这两个表之间用 perl 创建一个关系表并在其中插入数据:
$create_query = qq{
create table article(
id_article int(10) auto_increment NOT NULL,
url MEDIUMTEXT COLLATE utf8_general_ci,
html_extr_text TEXT COLLATE utf8_general_ci,
concord_file TEXT COLLATE utf8_general_ci,
sys_time VARCHAR(50),
primary key (id_article)
)
};
$dbh->do($create_query);
$create_query = qq{
create table event(
id_event int(10) auto_increment NOT NULL,
event MEDIUMTEXT COLLATE utf8_general_ci,
primary key (id_event)
)
};
$dbh->do($create_query);
现在关系表如下所示:
$create_query = qq{
create table article_event_index(
id_article int(10) NOT NULL,
id_event int(10) NOT NULL,
primary key (id_article, id_event),
foreign key (id_article) references article (id_article),
foreign key (id_event) references event (id_event)
)
};
$dbh->do($create_query);
有人知道要填充“article_event_index”表的 perl 语句应该是什么吗?对于其他表,我为每个表使用数组,它看起来像这样:
my $i_event;
my $id_event = 0;
my @event_index;
for ($i_event = 0; $i_event < @event_prepare; $i_event++){
$dbh->do("
INSERT INTO `event`(`id_event`, `event`)
VALUES ('$id_event', '$event_prepare[$i_event]')
") || die $dbh->errstr;
push @event_index, $i_event;
}
$id_event++;
在这种情况下,'id_event' 是由 $id_event 的增量生成的。如果我想在索引表中重用这个 id,这是一个好习惯吗?