Sub topic 1
What Delta Lake Is and Where It Fits in Databricks
Prerequisites
Assume you know basic SQL, PySpark DataFrames, and Databricks notebooks. You should also know that a catalog contains schemas, and schemas contain tables.
Delta Lake in Databricks
Delta Lake is a storage layer that adds reliability to files in cloud object storage. It stores table data as Parquet files and records table changes in a transaction log. Databricks uses this layer as the default format for many tables.
The transaction log provides ACID guarantees. These guarantees make a change atomic, consistent, isolated from conflicting changes, and durable after it commits. Delta tables also support table versions, time travel, updates, deletes, and merges. A plain collection of Parquet files does not provide these features by itself.
How the Transaction Log Makes Changes Atomic
A write creates new data files or marks old files as removed. Delta then records the complete change as a new committed version in _delta_log. Readers use one consistent version instead of seeing a partially completed write.
If a job fails before committing, its uncommitted files are not part of the table. If two writers conflict, Delta detects the conflict and rejects one commit rather than silently losing data.
Managed and External Tables
A managed table has both its metadata and storage location controlled by Unity Catalog. Dropping the table normally removes its data files as well.
An external table stores data at a location that you specify, such as an S3 path. Unity Catalog manages the table definition and access, but the storage lifecycle remains separate. Use external tables when another system owns the files or when you need to preserve files independently of the table definition.
Creating Tables with SQL and CTAS
Create a table with an explicit schema when you need control over names, types, and nullability:
CREATE TABLE IF NOT EXISTS main.sales.orders (
order_id BIGINT NOT NULL,
customer_id BIGINT,
order_ts TIMESTAMP,
amount DECIMAL(10,2),
status STRING
) USING DELTA;CREATE TABLE AS SELECT, or CTAS, derives the schema from a query:
CREATE OR REPLACE TABLE main.sales.orders_summary AS
SELECT customer_id, COUNT(*) AS order_count, SUM(amount) AS total_amount
FROM main.sales.orders
GROUP BY customer_id;CTAS is convenient for transformed data. An explicit schema is safer for stable interfaces.
Exercise
Why can a failed Delta write leave temporary files without changing the table?
Show answer
The table changes only when a transaction-log commit records those files in a new table version.