# 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="/files/foObD1XkssbfwNtU3kfr" alt=""><figcaption><p>Master table</p></figcaption></figure>

<figure><img src="/files/FMp4KZxYU2fcgu9x5LFG" alt=""><figcaption><p>Details table</p></figcaption></figure>

We can also use a [lookup column](https://docs.powertable.com/explore-powertable/master-detail-tables-parent-child-linked-records/pages/PCbjP6zrTFcrIPrKHx1g#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="/files/WcR45qQ4Co8MwhUzTEfp" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/bV2aVpibaTi3U7dDkHqR" 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="/files/evKD1sbgKw1oiNrIcM6d" 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="/files/dnyZQ8lvkLcRDJ0NEn5p" 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="/files/YLwDrzZj69JAM9BEj7jq" 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="/files/TeWoX1TiMfprONOmU6jn" 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="/files/smCWOfpQAZF7N7nDQaGk" 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="/files/Iyig29am4fhlKMAj1nV4" alt=""><figcaption><p>Configure Label Column</p></figcaption></figure>

<figure><img src="/files/mewAXnvcJlhmUSp334e4" alt=""><figcaption><p>Display name</p></figcaption></figure>

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

<figure><img src="/files/PxVEuzDndHbPbXPmtJuS" 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="/files/KJkaslXMhSck1jMv2tly" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.powertable.com/explore-powertable/master-detail-tables-parent-child-linked-records/creating-master-detail-tables.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
