Sub topic 3
Managed and External Tables
This chapter assumes you can run SQL and PySpark in a Databricks notebook, and that you know what a catalog, schema and table are in Unity Catalog. It also assumes you know the Delta transaction log keeps a table's state in a _delta_log folder next to the data files. Here you will learn who owns a table's files, what that means for DROP TABLE, and how to choose between a managed and an external table.
Two kinds of table, one format
In Unity Catalog, every table is a Delta table (or another supported format) described by a three-level name: catalog.schema.table. A table is either managed or external. The difference is who controls the storage location and the lifecycle of the files.
- A managed table keeps its data files in storage that Unity Catalog manages. You do not choose the path. Unity Catalog does, and it deletes the files when the table is dropped.
- An external table keeps its data files at a path you choose, in cloud storage you register with Unity Catalog. Unity Catalog stores only the metadata and permissions. Dropping the table leaves the files where they are.
Both kinds use the same Delta transaction log, so UPDATE, MERGE, time travel and OPTIMIZE work the same way on both. What changes is ownership of the files.
| Aspect | Managed table | External table |
|---|---|---|
| Storage path | Chosen by Unity Catalog | Chosen by you with `LOCATION` |
| `DROP TABLE` | Removes metadata, then deletes data after a retention window | Removes metadata only. Files stay |
| Formats | Delta (default) or Iceberg | Delta, plus Parquet, CSV, JSON and others |
| Setup needed | A managed storage location on the metastore, catalog or schema | A storage credential and an external location |
| Typical use | New tables built and owned inside Databricks | Data shared with other engines, or files that already exist |
Where managed table files live
Unity Catalog looks for a managed storage location in this order and uses the first one it finds:
- The schema's managed location, if set.
- The catalog's managed location, if set.
- The metastore's managed location.
This lets you put different business units in different storage accounts without putting a path in any CREATE TABLE statement. You can set a location when you create the catalog or schema:
CREATE CATALOG IF NOT EXISTS finance
MANAGED LOCATION 's3://my-company-lake/finance';The path must be covered by an external location that you are allowed to use. On Azure the path would start with abfss://, and on Google Cloud with gs://.
Files in a managed table's folder are laid out and named by Databricks. Do not read or write them directly with cloud tools. Always go through the table name, so that Unity Catalog can enforce permissions and keep the transaction log consistent.
Creating a managed table
Leave out LOCATION and you get a managed table:
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;USING DELTA is optional on current Databricks runtimes, because Delta is the default format. The main.sales.orders table above is the managed table used in the rest of the tutorial.
Creating an external table
Before you can create an external table, an administrator sets up two Unity Catalog objects:
- A storage credential is a reference to a cloud identity, such as an IAM role, that can reach the storage.
- An external location pairs a storage path with a storage credential. Access to it is controlled with grants.
CREATE EXTERNAL LOCATION IF NOT EXISTS sales_landing
URL 's3://my-company-lake/sales'
WITH (STORAGE CREDENTIAL sales_cred);
GRANT CREATE EXTERNAL TABLE ON EXTERNAL LOCATION sales_landing
TO `data-engineers`;A user with the CREATE EXTERNAL TABLE privilege can then name a path under that location:
CREATE TABLE IF NOT EXISTS main.sales.orders_ext (
order_id BIGINT NOT NULL,
customer_id BIGINT,
amount DECIMAL(10,2)
)
USING DELTA
LOCATION 's3://my-company-lake/sales/orders_ext';If a Delta table already exists at that path, you can leave out the column list. Unity Catalog reads the schema from the transaction log. This is the usual way to register data that another system wrote:
CREATE TABLE main.sales.orders_registered
LOCATION 's3://my-company-lake/sales/orders_ext';The same distinction applies in the DataFrame API. Setting a path option turns saveAsTable into an external table:
(df.write
.format("delta")
.mode("append")
.option("path", "s3://my-company-lake/sales/orders_ext")
.saveAsTable("main.sales.orders_ext"))Without the path option, the same call creates a managed table.
Telling the two apart
Do not guess from the table name. Ask the catalog:
DESCRIBE EXTENDED main.sales.orders_ext;The output includes a Type row with the value MANAGED or EXTERNAL, and a Location row with the storage path. DESCRIBE DETAIL also returns the location and the table's format, size and file count.
detail = spark.sql("DESCRIBE DETAIL main.sales.orders_ext")
detail.select("format", "location", "numFiles").show(truncate=False)What DROP TABLE does
This is the behavior that most affects your choice.
DROP TABLE main.sales.orders_ext; -- external: files remain at the path
DROP TABLE main.sales.orders; -- managed: table is marked deletedAfter dropping an external table, the data and the _delta_log folder are still in storage. You can register the path again with a new CREATE TABLE ... LOCATION, and the history comes back with it. Permissions granted on the old table do not come back.
After dropping a managed table, Unity Catalog keeps the data for a recovery period (7 days at the time of writing). Within that period the owner can bring it back:
SHOW TABLES DROPPED IN main.sales;
UNDROP TABLE main.sales.orders;After the recovery period, the underlying files are deleted for good. Check the current Databricks documentation for the exact windows, because they can change.
Choosing between them
Prefer managed tables by default. Unity Catalog controls the files, so no one can change them behind its back. Databricks can also run maintenance for you through predictive optimization, which schedules OPTIMIZE and VACUUM for managed tables when enabled. You also avoid managing paths, which removes a common source of mistakes.
Choose external tables when one of these applies:
- Another engine, such as a non-Databricks Spark cluster or a different query tool, must read or write the same files.
- The data already exists in storage and you must not move it.
- Organizational rules require data to stay at a fixed path or in a specific account.
- You need a format that managed tables do not support, such as plain Parquet or CSV.
Treat the trade-offs plainly. External tables give you control of the files and take on the responsibility for them. You schedule your own maintenance, you handle cleanup after a drop, and you must make sure no tool writes to the folder in a way that bypasses the Delta log.
Common errors
- "No parent external location found for path": the path is not under any external location you can use. Ask an administrator to create one or fix the path.
- Permission errors on
CREATE TABLE ... LOCATION: you needCREATE EXTERNAL TABLEon the external location, plus the usualUSE CATALOG,USE SCHEMAandCREATE TABLEprivileges on the parent objects. - "Path is not empty" or a schema mismatch: the folder already holds data. If a Delta log exists, omit the column list so the schema is read from it. If the folder holds non-Delta files, clear it or choose a new path.
- Table not found after a drop: for a managed table, check
SHOW TABLES DROPPEDand useUNDROP TABLEbefore the retention window ends. - Reading files by path to bypass the table: this skips table-level permissions and can see files that are not yet committed or are no longer referenced. Use the table name.
Exercise
You run DROP TABLE main.sales.orders_ext on an external table, and a colleague later asks you to bring the table back. What do you do, and what will not be restored?
Show answer
The data files and the _delta_log folder are still at the original path, so you run CREATE TABLE main.sales.orders_ext LOCATION '<original path>' and the schema and history are read from the log. Grants given on the old table are not restored, so someone must grant them again.
Exercise
A team writes a nightly Spark job outside Databricks that appends Parquet-compatible Delta files to a folder, and analysts in Databricks need to query the result. Should the Databricks table be managed or external, and which two Unity Catalog objects must exist before you can create it?
Show answer
External, because another system writes to the files directly and a managed table's folder is controlled by Unity Catalog. You need a storage credential and an external location that covers the folder's path, and your user needs CREATE EXTERNAL TABLE on that external location.