Sub topic 8
Managed and external tables
This chapter assumes you can write basic SQL, know that tables are backed by files in cloud object storage, and have already created a catalog and schema in Unity Catalog. You will learn how managed and external tables differ, how to create each, and how to choose between them.
Two ways to store a table
Every table in Unity Catalog has a data location. What differs is who controls that location.
- A managed table keeps its files in storage that Unity Catalog manages. You do not choose a path. Unity Catalog places the files under the managed storage location of the table's schema, catalog, or metastore, and it owns the file layout and lifecycle.
- An external table keeps its files at a path you choose in cloud storage. Unity Catalog records the table's metadata and governs access to it, but it does not own the files.
Both kinds use the same three-level name and the same GRANT and REVOKE statements. The difference shows up in storage and in what happens when you drop the table.
Creating a managed table
A managed table is the default. If you leave out LOCATION, you get a managed table.
CREATE CATALOG IF NOT EXISTS dev;
CREATE SCHEMA IF NOT EXISTS dev.sales;
CREATE TABLE IF NOT EXISTS dev.sales.orders (
order_id BIGINT,
customer_id BIGINT,
order_ts TIMESTAMP,
amount DECIMAL(10,2)
);Creating an external table
An external table needs an explicit LOCATION.
CREATE TABLE dev.sales.raw_orders (
order_id BIGINT,
payload STRING
)
LOCATION 's3://example-bucket/raw/orders';The bucket name is a placeholder. This statement only works if the path is covered by an external location that you are allowed to use. The storage chapter covers external locations. If no external location covers the path, the statement fails with a permission error.
Checking which kind you have
DESCRIBE EXTENDED shows the table type and the storage location.
DESCRIBE EXTENDED dev.sales.orders;In the output, look at the Type row, which reads MANAGED or EXTERNAL, and at the Location row. You can also query table_type in dev.information_schema.tables.
What happens on DROP TABLE
This is the main practical difference.
| Action | Managed table | External table |
|---|---|---|
| Metadata in the catalog | Removed | Removed |
| Data files | Deleted by Unity Catalog, after a retention period during which you can undo the drop | Left untouched in your storage |
| Who else can write the files | Only through Unity Catalog | Anyone with direct storage access |
Choosing between them
Prefer managed tables unless you have a reason not to. Unity Catalog controls access to the files, so every read goes through governance, and it can handle file layout and cleanup for you.
Choose external tables when:
- Other tools outside Databricks must read or write the same files directly.
- The data already exists at a path that you cannot or do not want to move.
- Your organization requires that the files stay at a specific storage location and outlive the table definition.
Common errors
- Permission error on
CREATE TABLE ... LOCATION. No external location covers the path, or you lack the privilege to create an external table on it. - Expecting data to disappear after dropping an external table. The files stay in storage.
- Pointing two external tables at the same path. Unity Catalog does not allow overlapping external table paths, because two tables would then compete to manage the same files.
Exercise
You run DROP TABLE on dev.sales.raw_orders, which was created with a LOCATION clause. What happens to the files at s3://example-bucket/raw/orders, and how would you confirm beforehand that the table is external?
Show answer
The table's metadata is removed from Unity Catalog, but the files stay in the bucket, because the table is external. Before dropping it, run DESCRIBE EXTENDED dev.sales.raworders and check that the Type row reads EXTERNAL and that the Location row shows the bucket path. You can also query tabletype in dev.information_schema.tables.