Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 14

Managed vs. External Tables: Data Control

Recommended resources

Hello! Welcome back to our module on "Managing Tables with Delta Lake."

In our last lesson, we explored the "why" behind Delta Lake, covering the core reliability and performance features like ACID transactions, schema enforcement, and time travel. You learned that these features are all enabled by the transaction log, which turns a standard data lake into a robust Lakehouse.

Today, we transition from theory to practice. This lesson directly addresses the learning outcome: Differentiate between managed and external tables in terms of data lifecycle and location. This is a fundamental concept for any data engineer working in Databricks. We will focus on two key questions:

  1. Location: Where does the data for a table physically live?
  2. Lifecycle: What happens to that data when you DROP the table?

Understanding this distinction is crucial for effective data governance, cost management, and designing flexible data architectures. Let's get started.

1. Managed vs. External Tables: The Core Concepts

In Databricks, every table has two primary components:

  • Metadata: The "data about the data," such as the table name, schema (column names and data types), and location of the data files. This is stored in a metastore, which in modern Databricks is managed by Unity Catalog.
  • Data: The actual data files, typically stored in Parquet format in cloud object storage.

The difference between managed and external tables boils down to who is responsible for managing these two components, particularly the data.

Databricks Unity Catalog table types

The official Databricks documentation provides the most precise definitions for managed and external tables. It also includes a helpful comparison chart.

Please read the sections 'Managed tables' and 'External tables' for clear definitions. Then, review the 'Comparison of table types' chart, focusing on the columns for Managed and External tables. Pay close attention to the rows for 'Data lifecycle management', 'Storage location', and 'Data deleted on DROP TABLE'.

Key Differences at a Glance

Based on the documentation, here is a summary of the main distinctions:

Feature Managed Table External Table
Responsibility Databricks/Unity Catalog manages both metadata and data. Databricks manages metadata; you manage the data.
Data Location Stored in a location managed by Unity Catalog (e.g., within the schema or catalog's storage). You do not specify a path. Stored in a location you specify in your cloud storage (e.g., an S3 bucket or ADLS container).
Data Lifecycle When the table is dropped, the underlying data files are also deleted (after a retention period). When the table is dropped, only the metadata is removed. The data files remain in your cloud storage.
Default Choice Yes. This is the recommended type for most use cases. No. Use for specific scenarios like sharing data with external tools.

The following diagrams provide a great visual summary of these differences.

Managed vs. External Delta Tables Data Location
This diagram illustrates the storage location difference. Managed table data resides in a Databricks-managed location (`/user/hive/warehouse`), while external table data resides in a user-defined cloud storage path (e.g., an ADLS container).
Managed vs. External Tables Comparison
This table provides a side-by-side comparison, highlighting that for managed tables, data is deleted on `DROP`, whereas for external tables, it is not.

2. Hands-On: Creating and Inspecting Tables

Now, let's put this into practice. Open a new Databricks notebook and make sure it's attached to a running cluster. We will use SQL to create one of each table type.

Creating a Managed Table

A managed table is the default. Notice that we do not specify a LOCATION in the CREATE TABLE statement. Databricks handles this for us.

Run the following SQL command in a notebook cell:

CREATE OR REPLACE TABLE main.default.managed_table (
  id INT,
  name STRING
);

INSERT INTO main.default.managed_table VALUES (1, 'Alpha'), (2, 'Bravo');

(Note: We are using the main.default catalog and schema, which should be available in your workspace. You can replace this with your own catalog/schema if you prefer.)

Creating an External Table

For an external table, we must provide the LOCATION clause, pointing to a path in your cloud storage where you want the table's data to be stored.

Run the following SQL command in a notebook cell:

CREATE OR REPLACE TABLE main.default.external_table (
  id INT,
  name STRING
)
LOCATION 'dbfs:/mnt/my_data/external_table'; -- Replace with a path you have access to

INSERT INTO main.default.external_table VALUES (3, 'Charlie'), (4, 'Delta');

Important: You might need to change the LOCATION path to a location where you have write permissions. A safe bet is usually within your user directory, like dbfs:/Users/your.email@company.com/external_table.

Inspecting Table Properties

How can we verify that these tables were created as we intended? The DESCRIBE EXTENDED command is perfect for this.

The following video demonstrates how to create both table types and then use DESCRIBE EXTENDED to inspect their properties, confirming their type and location.

13 Managed & External Tables in Unity Catalog vs Legacy Hive Metastore | UNDROP Tables in Databricks

This video shows the practical steps of creating managed and external tables and then inspecting their properties to see the difference.

Watch the segment from 03:41 to 06:15. The presenter first creates a managed table, then an external table with a LOCATION clause. Pay close attention to the output of the DESCRIBE EXTENDED command for each table, specifically the 'Table Type' and 'Location' fields.

Now, try it yourself. Run these commands in your notebook:

-- For the managed table
DESCRIBE EXTENDED main.default.managed_table;

Look for the Type: MANAGED and note the Location path. It will point to a directory managed by Databricks.

-- For the external table
DESCRIBE EXTENDED main.default.external_table;

Here, you'll see Type: EXTERNAL and the Location will be the exact path you specified.


3. The DROP TABLE Experiment: Data Lifecycle in Action

Now for the crucial test: what happens when we drop these tables? This reveals the core difference in data lifecycle management.

The behavior here has a modern twist with Unity Catalog that is essential to know for the certification exam.

13 Managed & External Tables in Unity Catalog vs Legacy Hive Metastore | UNDROP Tables in Databricks

This part of the video is critical. It demonstrates what happens when you drop a managed table in Unity Catalog and introduces the UNDROP feature.

Watch the segment from 06:15 to 09:14. The presenter drops the managed table and explains why the data files are not immediately deleted in Unity Catalog. You'll learn about the retention period and how to use UNDROP TABLE to recover a table.

Let's replicate this.

Step 1: Drop the Tables
Run the following commands in your notebook:

DROP TABLE main.default.managed_table;
DROP TABLE main.default.external_table;

Step 2: Verify the Data Lifecycle

  • External Table: The table definition is gone from the metastore, but the data files remain untouched. You can verify this by listing the files at the location you specified. Run this Python command in a new cell:

    
    
    
    # Remember to use the same path you used in the LOCATION clause
    dbutils.fs.ls('dbfs:/mnt/my_data/external_table')
    

    You will see the Parquet files for your table data are still there. You are responsible for cleaning them up if they are no longer needed.

  • Managed Table (with Unity Catalog): As the video explained, when you DROP a managed table in Unity Catalog, the data is not immediately deleted. It is retained for a default period of 7 days. This is a safety feature that allows you to recover accidentally dropped tables using the UNDROP TABLE command. After the retention period, Databricks will garbage-collect and delete the data files automatically.


Conclusion

You have now seen firsthand the fundamental differences between managed and external tables in Databricks. This knowledge is not just theoretical; it directly impacts how you design and manage your data pipelines.

Key Takeaways

  • Managed Tables are the default and recommended choice. Databricks manages the entire lifecycle of both the metadata and the data. Use them when the data's lifecycle is fully contained within Databricks.
  • External Tables give you direct control over the data files and their lifecycle. Use them when you need to share underlying data with other systems, or when you are registering data that already exists in your cloud storage.
  • Location: You specify the LOCATION for an external table; Databricks chooses the location for a managed table.
  • Lifecycle (DROP TABLE):
    • DROP EXTERNAL TABLE removes only metadata; the data files remain.
    • DROP MANAGED TABLE (in Unity Catalog) removes the metadata and marks the data for deletion after a retention period, during which it can be recovered with UNDROP.

Next Steps

In this lesson, you used the CREATE TABLE command to define tables. In our next lesson, "Create and drop managed and external Delta tables using SQL," we will delve deeper into the syntax and options available for creating tables, solidifying your ability to manage the schema and structure of your Delta tables effectively.