2009-04-17 35 views
2

我正在为mysql设计一个bd-scheme。我的db存储3种点:a,b或c,路径由n对点组成:引用同一列中的外键

Route = [(a1或b1或c1; a2或b2或c2),(a2或b2或C2; A3或B3或C3),...]

create table a_points (
    point_id serial  not null, 
    owner_id bigint unsigned not null, 
    name  varchar(20) not null, 

    primary key (point_id), 
    foreign key (owner_id) references othertable (other_id) 
    ) engine = InnoDB; 


create table b_points (
    point_id serial  not null, 
    owner_id bigint unsigned not null, 
    name  varchar(20) not null, 
    fields  varchar(20) not null, 


    primary key (point_id), 
    foreign key (owner_id) references othertable (owner_id) 

    ) engine = InnoDB; 

create table c_points (
    point_id serial  not null, 
    name  varchar(20) not null, 
    cfields  varchar(20) not null, 

    primary key (point_id) 
    ) engine = InnoDB; 

create table paths (
    path_id serial   not null, 
    name  varchar(20) not null, 

    primary key (path_id) 
    ) engine = InnoDB; 

create table point_pairs (
    pair_id  serial  not null, 
    path_id bigint unsigned not null, 
    point_from bigint unsigned not null, 
    point_to bigint unsigned not null, 
    table_from varchar(9) not null, 
    table_to varchar(9) not null, 

    primary key (pair_id), 
    foreign key (path_id) references paths (path_id) 
    ) engine = InnoDB; 

(*)的一对点的是(M,N)或从m至n

所以我存储对点连同他们的路径ID。我的问题是,我不得不创建两列来标识m和n的表名。 table_from for m和table_to for n。因此,我必须在我的代码中使用这两列来知道路径中保存了哪些点(路径和point_pairs表)。我的问题:MySql是否提供了在同一列中引用n个外键的方法?我已经考虑过加入a,b和c点表,但是我必须在这个新表中添加一个类型列,我的php类将变得毫无用处。

在此先感谢!

回答

3

您正在使用称为Polymorphic Associations的模式,并且没有办法做到这一点,并使用外键来强制引用完整性。

我建议你做一个公共表a_points,b_pointsc_points参考。然后你的点对可以引用该公共表。

a_points --> 
b_points --> common_points <-- point_pairs 
c_points --> 

换句话说,使多形性关联起作用的方法是颠倒参照的方向。

+0

这是我在其他讨论中被称为“泛化专业化模式”的一个很好的总结。我将不得不了解多态协会。 – 2009-04-18 11:43:44

相关问题