0

I have two tables (A and B) and I want each record in these tables to have an unique ID (id_C). How do I do that?

TABLE_A:

id_A | id_C
 1      1
 2      3

TABLE_B:

id_B | id_C
 1      2
 2      4

PS. I was thinking about something like this:

create table c(
    id_c int not null auto_increment,
    PRIMARY KEY(id_c)
);

create table a(
    id_a int not null auto_increment,
    a_c  int not null,
    PRIMARY KEY(id_a),
    FOREIGN KEY (a_c) REFERENCES c(id_c)
);

create table b(
    id_b int not null auto_increment,
    b_c  int not null,
    PRIMARY KEY(id_b),
    FOREIGN KEY (b_c) REFERENCES c(id_c)
);
4

1 に答える 1

0

ID を含むテーブルを作成し、それを自動インクリメント主キーにします。このテーブルを使用して一意の ID を生成し、他のテーブルからの外部キー制約でそれらを参照します。これがあなたの質問で提案したことだと思います。

もう 1 つの方法は、GUIDを使用することです。

于 2012-10-26T08:58:26.083 に答える