Databricks: Step-by-Step Commands: Managed vs. External Table in Databricks

Below is a complete workflow—with working SQL and Python code—demonstrating how to create, manage, insert, read, and delete data for both Managed and External tables in Databricks. After each step, commands using dbutils.fs are used to check underlying file storage differences, highlighting the distinction between managed and external tables.


1. Create a Managed Table

SQL:

sqlCREATE TABLE my_catalog.my_schema.managed_example (
    id INT,
    name STRING
);

2. Create an External Table

Suppose the external data location is /dbfs/FileStore/external_table_data/.

SQL:

sqlCREATE TABLE my_catalog.my_schema.external_example (
    id INT,
    name STRING
)
USING PARQUET
LOCATION '/dbfs/FileStore/external_table_data/';

3. Insert Sample Data (Both Tables)

SQL:

sqlINSERT INTO my_catalog.my_schema.managed_example VALUES (1, 'Alice'), (2, 'Bob');
INSERT INTO my_catalog.my_schema.external_example VALUES (3, 'Carol'), (4, 'Dave');

4. Check Table Data using SQL

SQL:

sqlSELECT * FROM my_catalog.my_schema.managed_example;
SELECT * FROM my_catalog.my_schema.external_example;

5. Inspect Underlying Files with dbutils (Python)

python# Managed table storage location (internally managed, path can be found using SQL below)
display(spark.sql("DESCRIBE DETAIL my_catalog.my_schema.managed_example"))

# External table (path known, list files directly)
dbutils.fs.ls("/FileStore/external_table_data/")

6. Delete Table Data

Delete all rows:

sqlDELETE FROM my_catalog.my_schema.managed_example;
DELETE FROM my_catalog.my_schema.external_example;

Or drop the tables entirely:

sqlDROP TABLE my_catalog.my_schema.managed_example;
DROP TABLE my_catalog.my_schema.external_example;

7. Check Data/Files Again

SQL:

sqlSELECT * FROM my_catalog.my_schema.managed_example;
SELECT * FROM my_catalog.my_schema.external_example;

Python:

python# Managed table storage usually deleted after DROP (internally managed).
# If not dropped, use DESCRIBE DETAIL to review location and check DBFS.
# External table storage persists after DROP unless deleted manually:
dbutils.fs.ls("/FileStore/external_table_data/")

Difference Between Managed and External Table in Databricks

  • Managed Table: Databricks manages both metadata and files. Dropping the table deletes both the metadata and the data files from storage automatically. Path is internal.
  • External Table: You specify a storage path. Dropping the table only deletes the metadata; files in the specified external location remain. You manage storage lifecycle yourself. Path is externalized (e.g., /dbfs/FileStore/external_table_data/).

dbutils.fs lets you inspect the file system. With managed tables, files are auto-managed and cleaned up. For external tables, you must manually manage/remove files as needed.


Summary:

  • Use SQL to create, insert, query, and delete tables and data.
  • Use Python (dbutils.fs) to inspect file storage differences.
  • Managed tables are fully handled by Databricks; external tables give you full control of file storage and persistence.

Related Posts

Complete Guide to Car and Motorcycle Rentals in Mexico

Finding flexible transportation in Mexico’s major cities and tourist destinations no longer relies exclusively on traditional rental agencies with rigid corporate fleets. Whether you need a vehicle…

Read More

Exploring Alternative Vehicle Rentals and Sharing Markets Nationwide

You open a search box and type “motorcycle rentals near me.” What comes back depends entirely on where you live. In a major city, you might see…

Read More

Practical Advice for Renting and Sharing Vehicles Across Africa

Finding the right vehicle for a trip in Africa often comes down to one question: where do you look? A traveler landing in Cape Town may want…

Read More

Running a Rental Business or Planning a Trip? How Modern Platforms Are Changing European Mobility

Stepping out onto the open road across Europe—whether carving through alpine switchbacks on a touring motorcycle, zipping through historic city streets on a scooter, or embarking on…

Read More

MyEventsNow USA Helps Event Fans and Hosts Meet Across America

Six friends. One group chat. No plan for Saturday night. One wants live music. One wants a comedy show. One wants a spot that’s close. This happens…

Read More

Predictive Analytics Tools for DataOps Workflows: An Honest Guide

Data teams react to failures. A run breaks, an alert fires, and someone wakes up to fix it. Predictive analytics flips that order. You use your own…

Read More

Leave a Reply