2

私は mysql の bd スキームを設計しています。私のデータベースには、a、b、または c の 3 種類のポイントが格納されており、パスは n ペアのポイントで構成されています。

ルート = [ (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 を識別する 2 つの列を作成しなければならなかったことです。m の場合は table_from、n の場合は table_to です。したがって、コードでこれら 2 つの列を使用して、ルートに保存されているポイントの種類 (path と point_pairs テーブル) を知る必要があります。私の質問: MySql は、同じ列で n 個の外部キーを参照するためのものを提供していますか? a、b、c のポイント テーブルを結合することは既に考えていましたが、この新しいテーブルに type 列を追加する必要があり、私の php クラスは役に立たなくなります。

前もって感謝します!

4

1 に答える 1

3

ポリモーフィック アソシエーションと呼ばれるパターンを使用していますが、それを行う方法はなく、外部キーを使用して参照整合性を強制する方法はありません。

a_pointsb_points、およびc_points参照する 1 つの共通テーブルを作成することをお勧めします。その後、ポイント ペアはその共通テーブルを参照できます。

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

つまり、ポリモーフィック アソシエーションを機能させる方法は、参照の方向を逆にすることです。

于 2009-04-17T05:08:19.470 に答える