# Modify a database

Writing back to a database allows users of an app to directly interact with the data. For example, if an app is collecting user-inputted data, or editing existing data, these changes will immediately be reflected in your database.

Database direct nodes connect directly to your database, so you'll be able to write SQL statements in your nodes to modify the database. This can include:

[#creating-new-tables](#creating-new-tables "mention")

[#inserting-new-records](#inserting-new-records "mention")

[#updating-existing-records](#updating-existing-records "mention")

[#deleting-records](#deleting-records "mention")

The following examples are created in PostgreSQL data nodes.

## Creating new tables

You can create new tables in your database using a `CREATE TABLE` statement.&#x20;

Specify the name of the new table after the `CREATE TABLE` keywords.

Within the parentheses, specify a comma-separated list of table columns. Each column contains:

* Column name
* Data type that the column stores
* Length of the data
* Column constraint

Column constraints specify rules that the column must follow. For example, `NOT NULL` specifies that values in the column cannot be NULL. Other column constraints include `UNIQUE`, `PRIMARY KEY`, `CHECK`, and `FOREIGN KEY CONSTRAINTS.`

When you click <img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FMGszWHEF3z9NVfjbsblq%2Fimage.png?alt=media&#x26;token=a0ea2e03-e6a9-495d-bab1-97776c8cbb3b" alt="" data-size="line">, the node will run and a new table will be created in your database. The node only needs to be run once. Since this is a create table query, there will be nothing in the output preview table.

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FGHUcjeWdDj4s0fwle6My%2Fimage.png?alt=media&#x26;token=45afb57c-3374-449e-866e-021b64988468" alt=""><figcaption></figcaption></figure>

Query:

<pre class="language-sql"><code class="lang-sql">CREATE TABLE new_table (
	customer_id SERIAL PRIMARY KEY,
	first_name VARCHAR(255) NOT NULL,
<strong>	last_name VARCHAR(255)  NOT NULL,
</strong><strong> 	email VARCHAR(255)  NOT NULL,
</strong> 	uploaded_time TIMESTAMP
);
</code></pre>

This example creates a new table called `new_table` with 5 columns: `customer_id`, `first_name`, `last_name`, `email`, and `uploaded` time.&#x20;

* `customer_id`: This column is a SERIAL datatype. It will automatically be assigned a sequential integer when a new record is added. This column is designed to work as the primary key of the table.
* `first_name`, `last_name`, `email`: These columns are strings with a length of 255 characters. They also cannot contain NULL values.
* `uploaded_time`: This column stores a timestamp showing when a new record is added.

## Inserting new records

### Data node setup

New records can be inserted into an existing table with an `INSERT` statement.

Specify the name of the table you want to insert records into after the `INSERT INTO` keywords. Then list the columns you'd like to add values to.

After the `VALUES` keyword, list the values to add. Ensure that the columns and the column values are in the same order.

To insert new records based on user input, those values should be parameterized. See [query](https://docs.acho.io/app-builder/app-construction/query "mention") for detail on adding parameters.

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FiRTjGa5GNlk2CR4ZLy3Q%2Fimage.png?alt=media&#x26;token=f97c7bbd-02a1-4ab8-9153-73f58fe640c6" alt=""><figcaption></figcaption></figure>

Query:

```sql
INSERT INTO new_table (first_name, last_name, email, uploaded_time)
VALUES ('{{first_name}}', '{{last_name}}', '{{email}}', current_timestamp);
```

This example will insert new records based on user inputs for the columns `first_name`, `last_name`, and `email`. It will also insert the current time into the `uploaded_time` column to indicate when the record was created. Since this is a Insert query, nothing will come out in the output preview table.

### User input setup

User inputs are passed in through a Form element. See [use-custom-form-container-to-collect-user-inputs](https://docs.acho.io/app-builder/popular-use-cases/use-custom-form-container-to-collect-user-inputs "mention") for more on how to build a form to collect data and how to set up the necessary interactions.

Once the form and form elements have been created in your interface, first set up an interaction on the form to set SQL parameters in your node.

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FLUZ0sGYPlA0bGzcC1lcV%2Fimage.png?alt=media&#x26;token=3a64558d-6adf-4940-8bca-7f6710ceea36" alt=""><figcaption></figcaption></figure>

Remember to create another interaction to submit the form.

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FEtI5vbGJnUQM36Bx26lv%2Fimage.png?alt=media&#x26;token=b96f92ff-bc0c-4168-8e6f-d9d50ed51ffe" alt=""><figcaption></figcaption></figure>

When the button is clicked, the form will be submitted and run the statement in your node to insert a record with the values the user has inputted.

## Updating existing records

### Data node setup

An `UPDATE` statement changes the values of columns in all rows that satisfy a condition.&#x20;

Then, in a data node, set up a statement to change the email for the `customer_id` of the row being edited.

Paramaterize the email and customer\_id values so users can input an email for any customer.

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FfJy3MsWnR7AQn0Ku59Gq%2Fimage.png?alt=media&#x26;token=a4f014ef-0213-4f8e-a055-ecbdb876d272" alt=""><figcaption></figcaption></figure>

Query:

```sql
UPDATE new_table
SET email = '{{email}}'
WHERE customer_id = {{customer_id}};
```

This example changes the email address for the given `customer_id`.

After the `UPDATE` keyword, choose the table you'd like to edit.

Only the columns to be modified need be mentioned in the `SET` clause; columns not explicitly modified retain their previous values. To modify multiple columns, separate them with a comma.

In the `WHERE` clause, identify which rows will be modified with the new value. In this case, `customer_id` is a primary key, so only one row will be modified at a time. However, you can change the condition to match multiple rows to bulk edit data.

### User input setup

Let's say an app has a table to displaying data from `new_table`. Each row has a button that will open up a modal containing a form to change the customer's email.&#x20;

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FvhcbdNuAu5YUwpwM2RG2%2Fimage.png?alt=media&#x26;token=b756b7ef-a29f-43e8-9390-bb9eee870c48" alt=""><figcaption></figcaption></figure>

See [create-a-table](https://docs.acho.io/app-builder/popular-use-cases/create-a-table "mention") for table setup with a column of buttons.

See [modal](https://docs.acho.io/app-builder/app-construction/elements/web-elements/modal "mention") for how to create and trigger the modal popup. Make sure the modal is dragged **into** the table element, so the modal is able to access table values.

![](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FCm7eu8HiEF0NKtT51HZo%2Fimage.png?alt=media\&token=e2148d4a-28ae-49f0-8be7-427f1c87148c)

See [use-custom-form-container-to-collect-user-inputs](https://docs.acho.io/app-builder/popular-use-cases/use-custom-form-container-to-collect-user-inputs "mention") to learn how to create a form inside the modal to collect a new email address.

![](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FmUq2GexcqpHeKBtlh0Ho%2Fimage.png?alt=media\&token=6a5e1afb-bcef-4ea3-b07c-9c78086ef529)

* Customer ID: In a text element, use `${$row.customer_id}` to access the customer id for the clicked row. Since the modal is created inside of the table, it will be able to access table values.
* Email: Use an [input](https://docs.acho.io/app-builder/app-construction/elements/form-elements/input "mention") element and name the form item `email`. This will pass in the user input to the form.

Once the form and data node are ready, set up the interactions to submit the form and update the record based on the parameter values for `email` and `customer_id`.

For the interaction on the form to set SQL parameters for the data node, pass in `${$row.customer_id}` and `${event.form.customer_id}` as the action parameters. This will run the node with the parameter values and update the record.

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FqFUqOsh4Cmdv2qwPqP2V%2Fimage.png?alt=media&#x26;token=4d721efb-88d7-4195-a1fd-90cb3cb5b2d1" alt=""><figcaption></figcaption></figure>

Remember to add an action on the submit button to submit the form.

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FXks53I8otNtSD8MdiXLj%2Fimage.png?alt=media&#x26;token=27e8cfc7-685e-40b9-a3c0-9f6e7ef502d4" alt=""><figcaption></figcaption></figure>

## Deleting records

### Data node setup

Use a `DELETE` statement to delete rows that satisfy the `WHERE` condition.

Similarly, the value can be parameterized.

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2F8mVIApR1jNGy0yzLa1Ra%2Fimage.png?alt=media&#x26;token=8c546181-2e47-41b6-af98-577858ca9837" alt=""><figcaption></figcaption></figure>

Query:

```sql
DELETE FROM new_table
WHERE customer_id = {{customer_id}};
```

In this example, we delete a row from `new_table` for a given `customer_id`.

### User input setup

Let's say an app contains a table displaying data from `new_table` and each row in the table has a delete button for that row.

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2Fj4IM3NAkXqyDFQuZ2Ir9%2Fimage.png?alt=media&#x26;token=acd3dab0-0a06-40e4-b575-404794167361" alt=""><figcaption></figcaption></figure>

See [create-a-table](https://docs.acho.io/app-builder/popular-use-cases/create-a-table "mention") for table setup with a column of buttons.

When a user clicks on the delete button, the record will be deleted from the database.

On the button, add an interaction to set the SQL parameter of the data node to the `customer_id` of that row. Use `${$row.customer_id}` to access the value.&#x20;

When the button is clicked, the parameter value will be sent to the node and the node will run the `DELETE` statement to remove that record.

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FovCgcYBVeijWIIx003n6%2Fimage.png?alt=media&#x26;token=daee7fa1-57b1-42d6-9502-97307ae04a11" alt=""><figcaption></figcaption></figure>
