Sub topic 3
Managed and External Tables
This chapter assumes you know basic SQL, can run cells in a Databricks notebook, and have compute attached. It also assumes you know that Unity Catalog organizes data as catalog, schema, and table. Together these form a three-level name such as main.sales.orders.
Two kinds of Delta table
Every Delta table in Unity Catalog is either managed or external. Both store data as Parquet files plus a transaction log. The difference is who controls where those files live and what happens when you drop the table.
- A managed table stores its files in a location that Unity Catalog chooses and controls. Unity Catalog owns the data's lifecycle.
- An external table stores its files in a cloud storage path that you choose. Unity Catalog records the table's metadata and permissions but does not own the files.
Managed tables
A managed table is the default. If you omit a LOCATION clause, you get one.
CREATE TABLE IF NOT EXISTS main.sales.orders (
order_id BIGINT NOT NULL,
customer_id BIGINT,
amount DECIMAL(10,2)
);Unity Catalog places the files under the storage root of the schema, the catalog, or the metastore, using the most specific one that is set. You never see or manage the file path in normal work.
Managed tables are the recommended choice for most data. Because Databricks controls the files, it can run maintenance such as optimization for you. Access goes through Unity Catalog permissions.
When you drop a managed table, Unity Catalog removes the metadata and also deletes the underlying data. Unity Catalog keeps a dropped managed table recoverable for a limited period (7 days at the time of writing). You can bring it back with UNDROP TABLE during that window.
External tables
An external table needs a LOCATION that points at cloud storage. Before you can use a path, an administrator must register it in Unity Catalog. This takes two objects:
- A storage credential, which holds the identity Unity Catalog uses to reach the storage.
- An external location, which ties a storage path to a credential.
You also need the CREATE EXTERNAL TABLE privilege on that external location.
CREATE TABLE main.sales.orders_ext (
order_id BIGINT,
customer_id BIGINT,
amount DECIMAL(10,2)
)
USING DELTA
LOCATION 's3://my-company-lake/sales/orders_ext';The path above is an example. Replace it with a path covered by one of your external locations. If the path is not covered, the statement fails with a permissions error.
When you drop an external table, Unity Catalog removes only the metadata. The files stay in storage. If you create a new table at the same location, it reads the existing Delta log and sees the old data.
Choosing between them
| Question | Managed | External |
|---|---|---|
| Who picks the file path? | Unity Catalog | You |
| What does `DROP TABLE` delete? | Metadata and data | Metadata only |
| Can other tools read the files directly? | Not as a supported route | Yes, if they have storage access |
| Typical use | Default for new tables | Data shared with outside systems or already in place |
Use an external table when other systems must read or write the same files, or when the data already exists at a known path. Otherwise, use a managed table.
Common errors
- Creating an external table at a path with no matching external location produces an access or permission error. Ask an administrator to register the location.
- Assuming
DROP TABLEon an external table cleans up storage. It does not, so the files keep costing money until you delete them. - Pointing two external tables at the same path. Both write to one transaction log, and changes made through one table appear in the other. Avoid this.
To check, run DESCRIBE EXTENDED main.sales.orders and read the Type row. It shows MANAGED or EXTERNAL.
Exercise
You run DROP TABLE on an external table and then create a new table at the same LOCATION. What data does the new table contain, and why?
Show answer
It contains the old data. Dropping an external table removes only the Unity Catalog metadata, so the Delta files and transaction log stay in storage. The new table reads that existing log and sees the same rows.