

# Write to Iceberg tables

The following procedure creates an Iceberg table in an S3 table bucket, inserts and updates rows, and reads the table. Start a new local session and complete [Install the extension](connect-with-duckdb-extension-download-and-install.md) to load the extension and create `athena_creds`.

## Step 1: Attach your table bucket
<a name="connect-with-duckdb-extension-write-attach"></a>

Run the following command. Replace {{us-west-2}}, {{my-duckdb-workgroup}}, {{amzn-s3-demo-table-bucket}}, and {{retail}} with your Region, workgroup, table bucket, and namespace. The command creates the local `sales` catalog alias.

```sql
ATTACH 'athena://{{us-west-2}}/{{my-duckdb-workgroup}}' AS sales
    (TYPE athena, Catalog 's3tablescatalog/{{amzn-s3-demo-table-bucket}}',
     Database '{{retail}}', Secret 'athena_creds');
```

## Step 2: Create an Iceberg table
<a name="connect-with-duckdb-extension-write-create"></a>

Run the following command to create the example `orders` table in your namespace.

```sql
CREATE TABLE sales.{{retail}}.orders
    (id INTEGER, product VARCHAR, qty INTEGER) WITH (type = 'ICEBERG');
```

## Step 3: Insert and update rows
<a name="connect-with-duckdb-extension-write-update"></a>

Run the following commands to insert two example rows and update the quantity for the first row.

```sql
INSERT INTO sales.{{retail}}.orders VALUES (1, 'Widget A', 3), (2, 'Widget B', 5);
UPDATE sales.{{retail}}.orders SET qty = 4 WHERE id = 1;
```

## Step 4: Read the table
<a name="connect-with-duckdb-extension-write-read"></a>

Run the following query to read the example table.

```sql
SELECT * FROM sales.{{retail}}.orders ORDER BY id;
```

## Delete rows
<a name="connect-with-duckdb-extension-write-delete"></a>

After reading the example table, run the following command to delete its second row.

```sql
DELETE FROM sales.{{retail}}.orders WHERE id = 2;
```

## Create a table from a query and add a column
<a name="connect-with-duckdb-extension-write-ddl"></a>

The following commands create an Iceberg table from the `orders` table, read it, and add a column to `orders`. Both the source and destination belong to the same attached `sales` catalog.

```sql
CREATE TABLE sales.{{retail}}.widget_a_orders WITH (type = 'ICEBERG') AS
    SELECT * FROM sales.{{retail}}.orders WHERE product = 'Widget A';
SELECT * FROM sales.{{retail}}.widget_a_orders ORDER BY id;
ALTER TABLE sales.{{retail}}.orders ADD COLUMN note VARCHAR;
DESCRIBE sales.{{retail}}.orders;
```

## Insert rows from another Athena table
<a name="connect-with-duckdb-extension-write-insert-select"></a>

After creating `widget_a_orders` in the preceding example, copy rows from `orders` with `INSERT SELECT`. Both tables belong to the same attached `sales` catalog. Run this example before cleanup. This command appends another copy of the rows already selected by CTAS; omit the INSERT if you want to keep a single copy.

```sql
-- Copy matching rows with a single remote statement.
INSERT INTO sales.{{retail}}.widget_a_orders (id, product, qty)
    SELECT id, product, qty
    FROM sales.{{retail}}.orders
    WHERE product = 'Widget A';
-- Inspect the destination after the write.
SELECT * FROM sales.{{retail}}.widget_a_orders ORDER BY id;
```

## Create a schema and a view
<a name="connect-with-duckdb-extension-write-schema-view"></a>

The following examples use the Glue catalog attached as `ath`. The first creates and drops an empty example database. The second creates, reads, and drops a view of a table with `c1` and `c2` columns.

```sql
CREATE SCHEMA ath.duckdb_demo;
DROP SCHEMA ath.duckdb_demo;
```

```sql
CREATE VIEW ath.{{my_database}}.demo_view AS
    SELECT c1, c2 FROM ath.{{my_database}}.{{my_table}}
    WHERE c1 IS NOT NULL;
SELECT count(*) FROM ath.{{my_database}}.demo_view;
DROP VIEW ath.{{my_database}}.demo_view;
```

## Write requirements and limitations
<a name="connect-with-duckdb-extension-write-restrictions"></a>

Direct writes and DDL targeting attached Athena catalogs require autocommit. The extension refuses these direct statements inside an explicit local transaction. Finish the local transaction first. A local `ROLLBACK` cannot undo a remote Athena write.

A direct write is sent to Athena as one statement. Every table it reads must be in the catalog being written, and the query must be eligible for pushdown. An `INSERT`, `CREATE TABLE AS`, or `COPY FROM` that reads local tables, local files, or a different attached catalog is refused. To load local data, first stage it in Amazon S3 and make it available as a table in the target catalog.

Qualify the database in write and DDL targets, or select a resolvable database with the attachment's `Database` option, `USE`, or the search path. An unresolved default `main` target is refused. An `INSERT` with a `RETURNING` clause is refused; run the write and then read with `SELECT`. A catalog attached with `READ_ONLY` refuses writes.

## Submit a statement with athena\_execute
<a name="connect-with-duckdb-extension-write-execute"></a>

The following command uses `athena_execute` to submit a statement exactly as written and wait for completion. It returns one `Count` row with the affected-row count when Athena reports one, or `0` otherwise.

`athena_execute` and `athena_query` can submit writes while an explicit local transaction is open, including a local read-only transaction. They do not use the direct-write autocommit guard. A local `ROLLBACK` cannot undo those writes. Use autocommit for remote writes and use a `READ_ONLY` attachment to refuse writes through these functions.

```sql
CALL athena_execute('sales', 'INSERT INTO {{retail}}.orders (id, product, qty) VALUES (3, ''Widget C'', 2)');
```

## Clean up the example tables
<a name="connect-with-duckdb-extension-write-cleanup"></a>

After completing the table-bucket examples, drop their two tables and detach the local catalog.

```sql
DROP TABLE sales.{{retail}}.widget_a_orders;
DROP TABLE sales.{{retail}}.orders;
DETACH sales;
```