我想打一个关系表用Perl这两个表之间并在其中插入数据:用Perl DBI模块关系表
$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,这是一个很好的做法吗?
是的,whit'last_insert_id'方法我得到了id的列表。但是,如何在“文章”和“事件”之间建立一对多的关系?因为一篇文章可能有多个事件。所以它会是:1 - 1,1 - 2,2 - 3,2 - 4,2 - 5 ...等等。第一个数字是文章,第二个是事件。再次感谢 – user2007958 2013-03-13 09:30:57
一对多关系在这里由你如何填充表格来定义。如果在你的映射表上,你插入相同的偶数id,几个文章ID(这是你的映射表定义允许的),你有一个一对多的关系。我觉得你正在寻找一种'神奇'的方式来创建一对多的关系,而无需处理idx和交叉表。你应该看看'DBIx :: Class',它可以让你这样做(但是比API更复杂的API)。 – Mattan 2013-03-13 09:37:30