03. Three-Level Namespace

POST_START
# 03. Three-Level Namespace in Unity Catalog

In this lesson, you will learn how to work with the **three-level namespace** in Databricks Unity Catalog. This structure allows you to organize your data assets in a hierarchical manner, using **catalog**, **schema**, and **table** levels.

## 1. Use the Catalog

The **catalog** is the top-level namespace in Unity Catalog. It acts as a container for one or more schemas. To work within a specific catalog, you use the `USE CATALOG` command.

### ✅ Primary Operation
“`sql
USE CATALOG training;
“`

This command sets the default catalog for subsequent operations. Any schema or table referenced without a catalog name will be assumed to belong to the `training` catalog.

## 2. Use a Schema

Within a catalog, you can have multiple **schemas**. A schema is like a folder that organizes tables, views, and other objects. To work within a specific schema, you use the `USE SCHEMA` command.

### ✅ Allowed SQL Step
“`sql
USE SCHEMA sales;
“`

This command sets the default schema for subsequent operations. Any table referenced without a schema name will be assumed to belong to the `sales` schema within the `training` catalog.

## 3. Access Tables in the Three-Level Namespace

Once you have selected a catalog and a schema, you can access tables by using the fully qualified name, which consists of **catalog** > **schema** > **table**.

### ✅ Allowed SQL Step
“`sql
SELECT * FROM sales.customers;
“`

This query retrieves all data from the `customers` table located in the `sales` schema of the `training` catalog.

Alternatively, you can reference the same table using the fully qualified name:

### ✅ Allowed SQL Step
“`sql
SELECT * FROM training.sales.customers;
“`

This version explicitly specifies the catalog, schema, and table, making it useful when you want to avoid ambiguity or when working across multiple catalogs.

## 4. Practical Example: Exploring Data

Let’s walk through a practical example to reinforce your understanding of the three-level namespace.

### Step 1: Use the catalog
“`sql
USE CATALOG training;
“`

### Step 2: Use the schema
“`sql
USE SCHEMA sales;
“`

### Step 3: Query the table
“`sql
SELECT * FROM customers;
“`

This query assumes you are currently in the `training` catalog and the `sales` schema, so it accesses the `customers` table directly.

Alternatively, you could use the fully qualified name:
“`sql
SELECT * FROM training.sales.customers;
“`

Both versions are valid and will return the same result.

## 5. Summary

– The **three-level namespace** in Unity Catalog is made of **catalog**, **schema**, and **table**.
– Use `USE CATALOG training;` to set the catalog context.
– Use `USE SCHEMA sales;` to set the schema context.
– Access tables using either the short form (when in the correct context) or the fully qualified name.

By understanding and using the three-level namespace, you can better organize and access your data assets in Unity Catalog. This structure is essential for managing large-scale data environments in Databricks.

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies and similar technologies to enhance your experience on wobizdu.com, analyze site traffic, personalize content, and deliver relevant ads. Some cookies are essential for the site to function, while others help us improve performance and user experience. You may accept all cookies, decline optional ones, or customize your settings. Review our Privacy Policy to learn more.