# Insert/Upsert Method

## Overview

The **Insert** method in QueryDeck allows you to add new records to your database. The **Upsert** method is a combination of "update" and "insert," allowing you to either insert a new record or update an existing one if a conflict occurs. This ensures data consistency and prevents duplicate entries when using unique constraints.

With QueryDeck’s Insert/Upsert method, you can:

* Add new records to your database.
* Handle conflicts using the `on_conflict` clause.
* Update records if they already exist based on specified constraints.

## Using the Insert or Upsert query:

#### 1. Select the Base Table

* From the left pane in the demo app, choose a base table to use. For this example, we will use the `customers` table.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXf29C7GOxKIH2ydv47VSaSkGJc6otsU9K0-f1iCcyULSqk88AfLdTyl2u_Mqv2mTme66MsAAGOH98HSDra4GHcIxCtVrAUyL5G8XxLV3ir667hHWRy7VUaudh5pcnzSihArd6D4Aw?key=RlGttIorZEntZNt64lpV-H5a" alt=""><figcaption></figcaption></figure>

#### 2. Configure the Query Method

* In the left pane:
  * Change the **Method** to `Insert` or `Upsert`.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXeFtiVZ_1JXjoSREHSBz9fNr-MVYjuDLx9I3AlFytPu5rOhAK-YQAF3iAP5-U8ixn6w8gWDl8KJaWUreTCI3ToZ7GNPX4KxsjGjLBcJhHeTiURiHeEftn5Vk-b9JmIu__EFLj8QTA?key=RlGttIorZEntZNt64lpV-H5a" alt="" width="375"><figcaption></figcaption></figure>

* Update the **Route Name** if required.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXc_97YNxR3r1zw4EVrTxaTOiXuTsf7-6d5yWNcXQL3QT4xldkYQXi3hLyVqWQ7gCiIIBInO3-o0QqkxR_3DFfOdcrZruO5hIf4sOENxfi2bQK63aWeXf_2fQltr9_cwr5dhebWw?key=RlGttIorZEntZNt64lpV-H5a" alt="" width="375"><figcaption></figcaption></figure>

#### 3. Save the Endpoint

* Click on the **Save** button at the bottom of the left pane.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXcN1bKfYu-S8HrRLXMZvgEexobF1vVbOt_DNLzW-Z88S2jGTIi-2GwxQZuHwUxXxkZbL8rBSFtuwOpx-LHitrP1AoY9twpmEI8JrVcwnzXH333hkGz9HjbQXU4Ii2-XdBN6XI8o?key=RlGttIorZEntZNt64lpV-H5a" alt=""><figcaption></figcaption></figure>

* Your endpoint for `Insert/Upsert` is now ready.

## Testing

* Copy the endpoint URL by clicking it in the top right corner

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdQR21D7bNLsSJPu5kWdB2Ba4s2y_H7ysEi6pBOn1TtP-Si3SRnCtCZxdmYc7DADkRASOu3WDR35mUNFce6b0KU_MuX-zlaWdS1aO1y4B3qhUWF_ShmetBwymECoYp3Vj-ZxbfSdQ?key=RlGttIorZEntZNt64lpV-H5a" alt=""><figcaption></figcaption></figure>

* Open **Postman**:

  * Paste the endpoint URL into the input field.
  * Change the request method to `POST`.

  <figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdyM7OP6X43h6y_1tbv5LgNof5nK94QcEive-u9Ex413QCEPLHM3CPTZAtXEIk3BBbuXcO0x7iCWpq92vDcEDke8qBooyU-jLKdU-K4y2YACDVmR1zANq92dD_I2c0jpVba4D_bwQ?key=RlGttIorZEntZNt64lpV-H5a" alt=""><figcaption></figcaption></figure>

#### &#x20;Add a Sample Request Body

* Under the **Body** tab in Postman:
  * Select **raw** as the body type.
  * Paste a sample request body in JSON format. For example:

```
{
  "customer": [
    {
      "store_id": 1,
      "first_name": "Jared",
      "last_name": "Ely",
      "address_id": 530,
      "active": 1,
      "activebool": true,
      "create_date": "2006-02-14T00:00:00.000Z",
      "email": "jared.ely@sakilacustomer.org",
      "last_update": "2013-05-26T14:49:45.738Z"
    },
    {
      "store_id": 1,
      "first_name": "Mary",
      "last_name": "Smith",
      "address_id": 5,
      "active": 1,
      "activebool": true,
      "create_date": "2006-02-14T00:00:00.000Z",
      "email": "mary.smith@sakilacustomer.org",
      "last_update": "2013-05-26T14:49:45.738Z"
    }
  ]
}
```

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXf3DBNnFWCWXhva5YmMR1vOpoz3FGKnQaq-glDQm_vc3ajI5rSJfV-mSwSN4jAWK2i_H-H9DiUdjfMCT-B3t3uWwpL8XKbMW7FkHLBmKWoW8iwv7a9yAaN4fvUBDM_ZM6QS4m8alw?key=RlGttIorZEntZNt64lpV-H5a" alt=""><figcaption></figcaption></figure>

#### Send the Request

* Click on the **Send** button in Postman.
* You will receive a response indicating that the query was successful.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfAFmNHlXkyIZMpG3PjH-eacSN4vevmDqz6r0MN2uH8MtLubdnqH4i6TMw_GhQAgQveq_N8v2vMCKQoNLyzjwoIX_e3lQCrWslLruLzEbDk0fduhakYHzkO_jJeIcvRkeMiDMsj?key=RlGttIorZEntZNt64lpV-H5a" alt=""><figcaption></figcaption></figure>

You have now successfully created and tested your `Insert/Upsert` query.
