# Creating Master-Detail Tables

If two or more tables in a dataset share a foreign key column (relation column), a master-detail relationship can be established between them. Establishing a master-detail view allows you to easily organize, scale, and maintain your data.

In our example, we have two tables: the *'categories'* table and the *'products'* table. They share a foreign key column, the '*Category ID'* column.&#x20;

<figure><img src="https://1781349671-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKyXRCFh0c08ryiUFvyMV%2Fuploads%2FRT6SpJ8HMV0oQwiB0wnf%2Fimage.png?alt=media&#x26;token=c5dfdc47-0a71-40e9-ad05-db83c5f4739b" alt=""><figcaption><p>Master table</p></figcaption></figure>

<figure><img src="https://1781349671-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKyXRCFh0c08ryiUFvyMV%2Fuploads%2FmjdzPTCtuPfvvvLY0tOj%2Fimage.png?alt=media&#x26;token=ae22a6f7-ae99-48e9-b107-9ad37f40bc43" alt=""><figcaption><p>Details table</p></figcaption></figure>

We can also use a [lookup column](https://docs.powertable.com/working-with-powertable/column-setup/configure-column-properties/lookup-and-relation#id-3.-lookup-table) in the *products* table for the **CategoryID** field; this lookup column retrieves the corresponding category names from the *categories* table (key: CategoryID, value: CategoryName).

<figure><img src="https://1781349671-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKyXRCFh0c08ryiUFvyMV%2Fuploads%2FvwAaT7jo6xKQpSrdHWmg%2Flookup%20md%20view.gif?alt=media&#x26;token=2d491c77-6e40-4a32-a83d-c13a116a85a9" alt=""><figcaption></figcaption></figure>

Both tables look as shown below. Let's now create a master-detail relationship between these tables.

<figure><img src="https://1781349671-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKyXRCFh0c08ryiUFvyMV%2Fuploads%2FnHrBVHpiioFlgabb7eUv%2Fimage.png?alt=media&#x26;token=a793fa7f-40f3-44ce-84f6-af4bd871b2d9" alt=""><figcaption></figcaption></figure>

In the master table, you need to add a **relation column.** This column connects both tables using the foreign key.&#x20;

**STEP 1:** To add this column, go to **Setup > Columns.**

<figure><img src="https://1781349671-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKyXRCFh0c08ryiUFvyMV%2Fuploads%2FQNrnNO6xAngtRYF36Iz8%2Fimage.png?alt=media&#x26;token=cdc72687-99ca-4092-bc25-c8a36307f76c" alt=""><figcaption><p>Setup column</p></figcaption></figure>

**STEP 2:** In the 'Columns' window that opens, click on **Add Visual Column > Add Relation Column.**

<figure><img src="https://1781349671-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKyXRCFh0c08ryiUFvyMV%2Fuploads%2F6lR6AXw1pKU2mYjjATX0%2Fimage.png?alt=media&#x26;token=8ce689d4-84ea-43fc-97e3-37f76e8e5cd3" alt=""><figcaption><p>Add Relation Column</p></figcaption></figure>

**STEP 3:** Configure the required details as below:

* **Type:** Select the column type. We chose the 'Text' type because we wanted to display the category name.
* **Column Name:** Enter the name you wish to use for the new column.

<figure><img src="https://1781349671-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKyXRCFh0c08ryiUFvyMV%2Fuploads%2FcHb3wjAZKYUJ8YNii8VM%2Fimage.png?alt=media&#x26;token=53e429a6-211d-454e-a4c9-b303ddcf63a7" alt=""><figcaption><p>Type and Column Name</p></figcaption></figure>

#### Configure Relation Table

Enter the details of the child table that you want to connect to. To establish a connection to the *products* table, let's input the following details:

**Connection**: The name of the connection used to fetch the child table.

**Schema:** The name of the database schema where the table is available.

**Relation Table:** Select the necessary child table to connect to.

<figure><img src="https://1781349671-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKyXRCFh0c08ryiUFvyMV%2Fuploads%2FoeqGDKuJbbH9BDdAw2vf%2Fimage.png?alt=media&#x26;token=6b13fe1e-b9ae-4425-87d2-6ce407141086" alt=""><figcaption><p>Configure Relation Table</p></figcaption></figure>

#### Configure Relation Columns

**Column from Current Table:** Choose the foreign key column from the **current master table** as the relation column.

**Column from Relation Table:** Choose the foreign key column from the **relation table** as the relation column.

This is the column that connects both tables.

<figure><img src="https://1781349671-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKyXRCFh0c08ryiUFvyMV%2Fuploads%2FeJQN5PrCVavbB6KUVUMX%2Fimage.png?alt=media&#x26;token=d3eb7740-dbe1-4770-88b4-f9a722f4f202" alt=""><figcaption><p>Relation Column Configuration</p></figcaption></figure>

#### Configure Label Column

**Column To Display:** Choose the label/display name that will appear when you drill down a record to its details table.

<figure><img src="https://1781349671-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKyXRCFh0c08ryiUFvyMV%2Fuploads%2FnAbPbo16HEPUu4SS275w%2Fimage.png?alt=media&#x26;token=e56ecdca-7305-4dd5-bc9d-7dfc07e1264e" alt=""><figcaption><p>Configure Label Column</p></figcaption></figure>

<figure><img src="https://1781349671-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKyXRCFh0c08ryiUFvyMV%2Fuploads%2FMaHiyS11JmTT9fw0JDpx%2Fimage.png?alt=media&#x26;token=4f14a249-67ae-4f43-8ed9-38296682ae2d" alt=""><figcaption><p>Display name</p></figcaption></figure>

STEP 4: Click **Save** to save the configuration. The relation column is added.

<figure><img src="https://1781349671-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKyXRCFh0c08ryiUFvyMV%2Fuploads%2F8VUvq5w2ZUrn3Adaa55E%2Fimage.png?alt=media&#x26;token=7fbda72e-65c4-4e79-9e70-ffe472f828e1" alt=""><figcaption><p>Relation Column added</p></figcaption></figure>

The master-detail relationship is now established between the tables, as shown below:

<figure><img src="https://1781349671-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FKyXRCFh0c08ryiUFvyMV%2Fuploads%2Fj5Nz8R78PmdxryNjCV6B%2Fmd%20view.gif?alt=media&#x26;token=a8e66ba6-a3b1-4700-8de4-1e97ae220049" alt=""><figcaption></figcaption></figure>
