Introduction
Welcome to your next lesson in the "Data Governance and Security with Unity Catalog" module. In our previous session, you learned the fundamentals of navigating the Unity Catalog's three-level namespace (catalog.schema.table). We focused on using USE CATALOG, USE SCHEMA, and fully qualified names to pinpoint specific data assets.
Now that you can find your way around, it's time to start building the structure itself. This lesson directly addresses the learning outcome: Create and manage catalogs, schemas, and tables using Unity Catalog SQL commands.
By the end of this lesson, you will be able to:
- Write the SQL commands to create new catalogs and schemas.
- Create managed tables within a specified catalog and schema.
- Use
DESCRIBEandSHOWcommands to inspect the objects you've created. - Write the SQL commands to remove catalogs, schemas, and tables, understanding the crucial difference between
RESTRICTandCASCADE.
Given your extensive experience with SQL and data engineering principles, we will focus on the specific syntax and best practices within the Databricks ecosystem, providing hands-on examples you can run directly in your workspace.
The Object Creation Hierarchy
Just as navigation follows the catalog -> schema -> table path, creation does as well. You'll typically create a catalog first, then a schema within it, and finally tables inside the schema.
1. Creating and Managing Catalogs
A catalog is the top-level container for organizing your data assets. It's common to create catalogs to separate data by environment (e.g., dev, staging, prod) or by business unit (e.g., finance, marketing).
The primary command is CREATE CATALOG. It's a best practice to include the IF NOT EXISTS clause to prevent your code from failing if the catalog has already been created.
Databricks Unity Catalog: A Beginner's Guide - DataCamp
The article 'Databricks Unity Catalog: A Beginner's Guide' from DataCamp provides a clear, concise example of the CREATE CATALOG command. Please review this snippet.
In the article, locate the section 'Configuring Unity Catalog' and find the subsection 'Create Catalogs and Schemas (if needed)'. Focus on the SQL code block provided for creating a catalog.
As you saw, the basic syntax is:
CREATE CATALOG IF NOT EXISTS my_new_catalog
COMMENT 'A catalog for a specific project.';
Once created, you can verify its existence and set it as your current context. The following video demonstrates this workflow.
Create and Manage Catalog, Schema, Tables in UNITY CATALOG #4 #data #datagovernance
This video from CloudFitness provides a practical demonstration of creating a catalog and then inspecting it using SHOW and USE commands.
Watch the segment from 01:31 to 02:47. Pay attention to how the presenter runs the CREATE CATALOG command and immediately follows up with SHOW CATALOGS to verify the creation and USE CATALOG to switch context.
2. Creating and Managing Schemas
Schemas (or databases) are the second level of the namespace, residing within a catalog. They are used to group related tables, views, and functions.
To create a schema, you must specify which catalog it belongs to, using the catalog_name.schema_name dot notation.
Databricks Unity Catalog: A Beginner's Guide - DataCamp
The DataCamp guide also provides a straightforward example of the CREATE SCHEMA command.
Find the section 'Creating a schema (database) within the catalog' and review the CREATE SCHEMA syntax. Notice the use of the two-part name to specify the parent catalog.
The syntax is:
CREATE SCHEMA IF NOT EXISTS my_new_catalog.my_new_schema
COMMENT 'A schema for raw data.';
You can then use SHOW SCHEMAS to list schemas in the current or a specified catalog, and DESCRIBE SCHEMA to get more details.
Create and Manage Catalog, Schema, Tables in UNITY CATALOG #4 #data #datagovernance
The same CloudFitness video also demonstrates schema creation and inspection.
Watch the segment from 05:31 to 06:55. Observe the use of CREATE SCHEMA, SHOW SCHEMAS, and DESCRIBE SCHEMA to get metadata about the newly created schema.
3. Creating and Managing Tables
Tables are the fundamental objects that store your data. In Unity Catalog, there are two main types: managed and external. For this course and for most use cases, we will focus on managed tables, which are the default and recommended type. With managed tables, Unity Catalog fully manages the underlying data files' lifecycle, storage location, and optimizations. This simplifies data management and governance significantly.
Unity Catalog managed tables in Databricks
Before we create a table, it's important to understand why managed tables are the preferred choice in Unity Catalog. The official Databricks documentation explains the benefits.
Please read the introduction and the section titled 'Why use Unity Catalog managed tables?'. You don't need to memorize every feature, but focus on understanding that managed tables offer significant advantages in performance, cost, and operational simplicity because Unity Catalog automates tasks like optimization and cleanup.
To create a managed table, you use the standard CREATE TABLE syntax, but you must use the three-level namespace to specify its location.
Unity Catalog managed tables in Databricks
The Databricks documentation also provides the authoritative syntax for creating a managed table.
In the section 'Create a managed table', review the SQL syntax. Note the required privileges (USE CATALOG, USE SCHEMA, CREATE TABLE) and the use of the three-part name: <catalog-name>.<schema-name>.<table-name>.
Let's see this in a hands-on demonstration.
Create and Manage Catalog, Schema, Tables in UNITY CATALOG #4 #data #datagovernance
This final clip from the CloudFitness video shows the complete process: setting the context, creating a table with a defined schema, inserting data, and verifying the result.
Watch the following segments: Create and Populate Table (07:41 - 09:03): See how the CREATE TABLE command is used within a specific schema context. Note the column definitions and the simple INSERT statement to add data. Inspect Table (09:14 - 09:54): Observe the use of SHOW TABLES and DESCRIBE TABLE to confirm the table's creation and examine its structure.
4. Dropping Objects: RESTRICT vs. CASCADE
A critical part of managing objects is knowing how to remove them. The commands are simple: DROP CATALOG, DROP SCHEMA, and DROP TABLE. However, there's a crucial detail that often appears in certification exams: the behavior of RESTRICT (the default) versus CASCADE.
RESTRICT: Prevents you from dropping an object if it still contains other objects. For example, you cannotDROP CATALOG my_catalogif it still contains schemas. You must drop the child objects first.CASCADE: Deletes the specified object and all objects contained within it. For example,DROP CATALOG my_catalog CASCADEwill delete the catalog, all schemas inside it, and all tables within those schemas. This is powerful but must be used with caution.
Databricks Unity Catalog: Catalogs and Schemas
This video from Pathfinder Analytics Academy provides an exceptionally clear demonstration of the difference between RESTRICT and CASCADE when dropping objects.
Watch the segment from 06:59 to 08:38. Notice how the initial DROP CATALOG command fails because the catalog is not empty (this is the default RESTRICT behavior). Then, see how adding the CASCADE keyword makes the command succeed by recursively deleting the contents.
For managed tables specifically, Unity Catalog offers a safety net. When you drop a managed table, the underlying data isn't immediately deleted. You have a 7-day window to recover it using the UNDROP TABLE command. This is another key benefit of using managed tables.
Hands-On Practice Scenario
Now, let's bring all these commands together. In your Databricks SQL Editor or a notebook, execute the following commands step-by-step to simulate a full create-and-cleanup lifecycle. This will solidify your understanding.
Step 1: Create the hierarchy
-- Create a new catalog for our project
CREATE CATALOG IF NOT EXISTS dce_pro_practice
COMMENT 'Catalog for Data Engineer Professional course exercises.';
-- Set the new catalog as our current context
USE CATALOG dce_pro_practice;
-- Create a schema for bronze-level data
CREATE SCHEMA IF NOT EXISTS bronze_layer
COMMENT 'Schema for raw, ingested data.';
Step 2: Create and inspect a table
-- Create a simple managed table for customer data
CREATE TABLE IF NOT EXISTS bronze_layer.customers (
customer_id INT,
first_name STRING,
last_name STRING,
email STRING,
ingested_at TIMESTAMP
)
COMMENT 'Raw customer data from source systems.';
-- Verify that the table exists in the schema
SHOW TABLES IN bronze_layer;
-- Inspect the table's structure and metadata
DESCRIBE TABLE EXTENDED bronze_layer.customers;
Step 3: Clean up using DROP
First, let's try dropping the catalog without CASCADE. This command will fail because the catalog is not empty.
-- This will fail because the 'bronze_layer' schema and 'customers' table exist
DROP CATALOG dce_pro_practice;
You'll receive an error message indicating that you must use CASCADE.
Now, perform the cleanup correctly. You can either drop from the inside out or use CASCADE.
-- Option A: Drop from inside-out (manual)
-- DROP TABLE dce_pro_practice.bronze_layer.customers;
-- DROP SCHEMA dce_pro_practice.bronze_layer;
-- DROP CATALOG dce_pro_practice;
-- Option B: Use CASCADE for a top-down deletion (more common in cleanup scripts)
DROP CATALOG dce_pro_practice CASCADE;
-- Verify the catalog is gone
SHOW CATALOGS;
Running through this scenario provides the practical, hands-on experience you prefer and directly reinforces today's learning objectives.
Conclusion
In this lesson, you've moved from a navigator to an architect within Unity Catalog. You now have the fundamental SQL commands to build and dismantle the core structures that will house all your data assets.
Key Takeaways:
- You create objects following the hierarchy:
CREATE CATALOG, thenCREATE SCHEMA, thenCREATE TABLE. - Using the
IF NOT EXISTSclause is a standard best practice to make your scripts idempotent. - The three-level namespace (
catalog.schema.table) is mandatory when creating objects to define their location. - Managed tables are the Databricks-recommended standard in Unity Catalog, simplifying management and enabling powerful features like automated optimization and
UNDROP. DROP ... CASCADEis a powerful but destructive command that removes an object and all its children, while the defaultRESTRICTbehavior acts as a safety measure.
Preview of the Next Lesson:
Creating catalogs, schemas, and tables is only half the story. By default, only you (the owner) can access these new objects. To make them useful for your team, you need to grant access. In the next lesson, we will cover how to grant and revoke object permissions to principals using Unity Catalog, which is the heart of data governance.