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

Modernizing AppSec: The Role of DevSecOps Consulting Services

Modern engineering teams deliver software faster than ever, releasing code multiple times a day across complex multi-cloud environments. However, rapid release cycles often create significant security challenges…

Read More

A Complete Overview of DevOps Support Services and Their Business Value

Introduction Running a modern software environment involves much more than writing code and releasing applications. Engineering teams must continuously manage cloud resources, deployment pipelines, containers, security controls,…

Read More

A Practical Guide to Evaluating DevOps Trainers and Training Programs

Introduction DevOps has changed considerably from being mainly associated with deployment automation and collaboration between development and operations teams. Today, engineering organizations work across cloud platforms, containers,…

Read More

Essential DataOps Testing Techniques for Reliable Modern Pipelines

Introduction The ingestion job extracted raw files and loaded them without crashing, but an upstream application updated its checkout flow. You must verify both the pipeline code…

Read More

How DataOps Improves Collaboration Across Teams in Modern Organizations

Introduction In modern organizations, data is often called the most valuable asset. Yet, the teams responsible for gathering, processing, analyzing, and acting on that data frequently operate…

Read More

Best Countries for Dental Tourism: Comparing Costs, Safety, and Quality Care

Navigating the world of international healthcare can feel overwhelming, especially when facing extensive dental work or rising domestic treatment costs. Millions of patients worldwide actively research cross-border…

Read More

Leave a Reply