テーブルを外部キーと接続する
データ ソースの外部キーを編集するには、データ ソースの管理権限が必要です。
Foreign keys tell the database how to join two or more tables together in the same query. In the Analytics app, foreign keys allow us to join tables together in visual mode queries. For most data source types, Analytics can import any foreign keys you’ve set up already. If Analytics is unable to import your foreign keys—or none are set up—you may need to define the foreign key relationships manually in the schema or use the “Connect tables” feature to auto-detect possible foreign key relationships in your schema.
Setting foreign keys for the schema in Analytics does not affect or set up foreign keys in your original data source.
上記の例では、[ユーザー] テーブルの [user_id] 列が主キーです。[アクティビティ] テーブルの [user_id] 列で [ユーザー] テーブルの [user_id] 列が参照されています。したがって、[アクティビティ] テーブルの [user_id] 列は外部キーです。
テーブルを接続する
When possible foreign key relationships in your data source are detected, Analytics can assist with setting up the foreign keys between tables with the “Connect tables” feature.
一度に複数の外部キーを設定するには:
データ ソースの設定の [スキーマ] タブに移動します。
外部キーを設定するスキーマを選択します。
[Connect tables (テーブルを接続)] (有効な場合) を選択します。ポップアップが表示され、外部キーの候補が表示されます。
外部キーの候補を確認し、各候補の横にあるチェックボックスを選択して承認します。
Select Save selected to add the approved suggestions to your Analytics schema.
スキーマに対して [Connect tables (テーブルを接続)] を使用できない場合は、外部キーを手動で追加できます。外部キーを追加する方法については、下記のセクションを参照してください。
外部キーを追加する
列のプロパティを編集することで、手動でスキーマに外部キーを追加できます。列の編集方法を確認してください。
外部キーをテーブルに追加するには:
データ ソースの設定の [スキーマ] タブに移動します。
外部キーを追加する列を含むテーブルを展開します。
外部キーとして使用する列のプロパティを展開します。
[Foreign key (外部キー)] プロパティで、該当する外部キーで参照するスキーマ (複数存在する場合)、テーブル、および主キーの列を選択します。
外部キーと主キーを含むテーブルの接続に使用する結合のタイプを選択します。結合タイプの詳細を確認してください。
[保存] を選択します。
外部キーの設定例
前述の例に戻って、ここでは、外部キーを使用して [アクティビティ] テーブルと [ユーザー] テーブルを接続します。ユーザー名ごとのアクティビティ数をカウントするチャートを作成します。アクティビティ データは [アクティビティ] テーブルに保存されており、[ユーザーID] が各アクティビティに関連付けられています。ただし、[ユーザー名] は [ユーザー] テーブルに含まれています。つまり、[ユーザー] テーブルと [アクティビティ] テーブルをリンクする必要があり、その処理は [ユーザー ID] 列を使用して行います。これを行うと、ビジュアル モード クエリで [アクティビティ] テーブルを照会するときに、関連するユーザー データを [ユーザー] テーブルから取得できるようになります。
Without setting up foreign keys, we’d need to create separate queries—one for the “Activity” table and another for the “Users” table—then join those queries together. In Analytics, setting up foreign keys enables you to use both these tables in a single visual mode query, auto-generating the join in the backend so you don’t have to do it manually.
To join these tables in our Analytics schema, we’ll expand the “Activity” table to view a list of all its columns. Then, we’ll select User Id to open its column properties. In the Foreign key setting, we’ll select the Users table then select the User Id column.
外部キー結合
Once foreign key relationships are defined, Analytics automatically performs joins for you when using columns from related tables in visual mode queries. The tables don’t need to be directly related; Analytics automatically finds the closest path to join the tables you’re using.
カスタム結合タイプを設定する
既定では、ビジュアル モード クエリで生成される結合関係はすべて明示的な内部結合です。ただし、外部キーごとに結合タイプを変更できます。
2 つのテーブル間の結合タイプをカスタマイズするには:
スキーマ内で該当する列を探します。
列を選択して、その列のプロパティを開きます。
列の [Foreign key (外部キー)] 設定内で結合タイプを選択します。
[保存] を選択します。
スキーマを視覚化する
To see a visual representation of how your tables are connected in Analytics:
データ ソースの設定の [スキーマ] タブに移動します。
[Visualize (視覚化)] を選択します。
これにより、テーブルがどのように接続されているかを矢印で示すスキーマ図が生成されます。矢印は外部キーから始まり、参照先の主キーを指します。
この内容はお役に立ちましたか?