Sub topic 5
Catalogs
This chapter assumes you know basic SQL, can run statements in a Databricks notebook or the SQL editor, and understand the idea of the metastore from the previous chapters.
What a catalog is
A catalog is the first level of the three-level namespace in Unity Catalog. It sits directly under the metastore and holds schemas. Every table, view, volume, and function lives inside a schema, and every schema lives inside a catalog. A fully qualified table name therefore starts with the catalog: catalog.schema.table.
Catalogs are the coarsest unit you can use to organize and secure data. Most teams use them to separate environments (such as dev and prod), business units, or data domains.
The diagram shows two catalogs that each contain a schema named sales. That is allowed, because a schema name only has to be unique within its catalog. dev.sales and prod.sales are different objects.
Creating a catalog
Use CREATE CATALOG. Adding IF NOT EXISTS makes the statement safe to run more than once.
CREATE CATALOG IF NOT EXISTS dev;
CREATE SCHEMA IF NOT EXISTS dev.sales;
CREATE TABLE IF NOT EXISTS dev.sales.orders (
order_id BIGINT,
customer_id BIGINT,
order_ts TIMESTAMP,
amount DECIMAL(10,2)
);The first statement creates the catalog dev. The next two create a schema and a managed table inside it, so the table is addressed as dev.sales.orders. To run CREATE CATALOG you need the CREATE CATALOG privilege on the metastore. The user who creates the catalog becomes its owner.
Listing catalogs
To see which catalogs exist in the metastore, run SHOW CATALOGS. The result only includes catalogs you have some privilege on or own.
SHOW CATALOGS;Setting the current catalog
Typing the catalog name in every query gets tedious. USE CATALOG sets the default catalog for your session. USE SCHEMA does the same for the schema. After both, you can use a short table name.
USE CATALOG dev;
USE SCHEMA sales;
SELECT * FROM orders;Here orders resolves to dev.sales.orders. If you set only the schema, such as USE SCHEMA sales, Unity Catalog looks for sales in the current catalog. If no current catalog is set, the workspace default applies.
Catalogs and storage
A catalog can have its own managed storage location. Managed tables created in the catalog store their data there. If you don't set one, the catalog uses the metastore's storage. A separate location per catalog is useful when prod data must live in a different bucket or account from dev data. Storage locations are covered in a later chapter.
Catalogs and privileges
Privileges granted on a catalog apply to everything below it. A user needs USE CATALOG on a catalog to reach any object inside it. This makes the catalog a convenient place to apply broad rules, for example giving analysts read access to every table in prod. Inheritance is covered in detail in a later chapter.
Common errors
- Catalog not found. The name is misspelled, the catalog doesn't exist, or you lack any privilege on it. Run
SHOW CATALOGSto check what you can see. - Permission denied on CREATE CATALOG. You need the
CREATE CATALOGprivilege on the metastore. - Table not found with a short name. The current catalog or schema isn't what you expect. Use the full three-level name, or set
USE CATALOGandUSE SCHEMAfirst.
Exercise
You run USE CATALOG dev; and then SELECT * FROM sales.orders;. Which table is queried, and what happens if you have no USE CATALOG privilege on dev?
Show answer
The query reads dev.sales.orders, because the short two-part name is resolved against the current catalog. If you lack USE CATALOG on dev, the statement fails with a permission error, even if you hold SELECT on the table.