0

たとえば、いくつかのテーブルがあります。

news:
id | title | description
1 | This is first title of news | This is description
2 | This is second title of news | This is second description

blog:
id | title | description
1 | This is first title of blog | This is description
2 | This is second title of blog | This is second description

さらに10を超えるテーブル。

0 または 1 の 2 つの値を持つことができる「some_state」フィールドを 1 つ追加する必要があります。

だから私は2つの方法で行くことができます:

1) このフィールドを各テーブルに追加します。

2) 新しいテーブルを作成します。例:

tables_some_state:
id | table_name | table_id | some_state
1 | news | 1 | 0
2 | news | 2 |1
3 | blog | 1 | 1
4 | blog | 2 | 1
...

このテーブルをクエリの各テーブルに左結合します。

では、私の場合のベストプラクティスは何ですか?

4

3 に答える 3

3

1対1の関係である限り、テーブルに追加します(ここにあります)。一般的に、このように別のテーブルに何かを配置する目的は、多対 1 の関係を持つことだと思います。

于 2013-06-12T18:18:11.617 に答える
2

それらをすべて 1 つのテーブルにして、「タイプ」列を指定し、その 1 つの列をすべてのテーブルに追加します。

posts:
id | type | title | description | status
1 | news | This is the first title of news | This is the description | 0
2 | blog | This is the first title of blog | This is the description | 1
...
于 2013-06-12T18:17:05.750 に答える
1

私の個人的な意見は、新しいテーブルを作成し、それらの他のテーブルの列を ID でリンクすることです。これにより、データベースの正規化が可能になり、データベースの冗長性が制限されます。最終的には、後でさらに「状態」を追加することもできます。InnoDB を使用している場合は、いくつかの外部キーも追加できます。

于 2013-06-12T18:17:27.097 に答える