Sub topic 8
Appending and Overwriting Data
This chapter assumes you can run SQL and PySpark in a Databricks notebook, and that you have already created a Delta table such as main.sales.orders. Here you will learn the two basic ways to put new data into an existing table: append and overwrite.
Append adds rows
An append writes new rows and leaves existing rows alone. Each append commits as one new table version in the transaction log. The DataFrame example below creates main.sales.orders_raw if it does not exist and appends to it if it does.
from pyspark.sql import Row
df = spark.createDataFrame([
Row(order_id=1001, customer_id=1, amount=25.50, status="new"),
Row(order_id=1002, customer_id=2, amount=40.00, status="new"),
])
df.write.format("delta").mode("append").saveAsTable("main.sales.orders_raw")In SQL, INSERT INTO does the same job:
INSERT INTO main.sales.orders
SELECT order_id, customer_id, order_ts, amount, status
FROM main.sales.orders_staging;INSERT INTO matches columns by position. If your source columns are in a different order, the values land in the wrong columns, or the write fails on a type mismatch. List the columns explicitly, or use INSERT INTO ... BY NAME on runtimes that support it.
A plain append does not check for duplicates. Running the same cell twice inserts the same rows twice. If a job may be retried, use MERGE (a later chapter) instead.
Overwrite replaces data
An overwrite removes existing data and writes the new data in one atomic commit. Readers see either the old version or the new one, never a half-empty table.
(df.write
.format("delta")
.mode("overwrite")
.saveAsTable("main.sales.orders_raw"))The SQL equivalents are INSERT OVERWRITE and CREATE OR REPLACE TABLE ... AS SELECT.
INSERT OVERWRITE main.sales.orders_raw
SELECT * FROM main.sales.orders_staging;A full overwrite keeps the table's schema. If the new data has a different schema, the write fails unless you opt in to schema changes (see the schema evolution chapter). The old data files stay on storage until VACUUM removes them, so you can still read the earlier version.
Selective overwrite
Often you want to replace only part of a table, such as one day of data. Delta supports this with replaceWhere.
(df.write
.format("delta")
.mode("overwrite")
.option("replaceWhere", "order_ts >= '2026-12-01' AND order_ts < '2026-12-02'")
.saveAsTable("main.sales.orders"))Delta deletes the rows matching the predicate and inserts the new rows. By default, every row in the DataFrame must satisfy the predicate. If a row falls outside it, the write fails with a constraint error. This check protects you from silently writing data into the wrong range.
The other option is dynamic partition overwrite, which replaces whichever partitions appear in the incoming data. It only applies to partitioned tables and depends on a configuration setting, so replaceWhere is usually the clearer choice.
Choosing between them
| Goal | Approach |
|---|---|
| Add new records | `append` or `INSERT INTO` |
| Rebuild the whole table | `overwrite` or `INSERT OVERWRITE` |
| Reload one date range | `overwrite` with `replaceWhere` |
| Change rows by key | `MERGE` |
Common errors
- Duplicate rows after a retry. Appends are not idempotent. Use
MERGEor a replace-by-range pattern. - Wrong column order.
INSERT INTOis positional. - Schema mismatch. Overwrite does not change the schema unless you allow it.
- Wiping a table by accident.
mode("overwrite")withoutreplaceWherereplaces everything. UseDESCRIBE HISTORYandRESTOREto recover.
Exercise
A nightly job reloads yesterday's orders with mode("append"). The job fails halfway and is re-run. What goes wrong, and which write would make the reload safe to repeat?
Show answer
The re-run appends the rows again, so some orders are duplicated, because append does not check for existing rows. Use an overwrite with replaceWhere on yesterday's date range, or a MERGE on order_id. Either one gives the same table state however many times the job runs.