Sub topic 10
Unity Catalog: catalogs, schemas, and tables
This chapter assumes you know basic SQL, can read short Python snippets, and have met the Delta Lake table format from the previous chapter. You also need to know what a workspace is. By the end you will be able to address any table by its three-level name, explain where the data physically lives, and read the permission rules that guard it.
Why a catalog layer exists
A Delta table is a folder of Parquet files plus a transaction log in cloud object storage. Storage alone does not tell you who may read the folder, what the table is called, or which team owns it. Unity Catalog is the Databricks governance layer that supplies those answers. It keeps a central record of data objects, controls access to them, and logs how they are used.
Unity Catalog sits at the account level, not inside a single workspace. Several workspaces can attach to the same metastore, so they all see the same tables and the same permissions.
The object hierarchy
Unity Catalog organizes objects in a tree.
- Metastore is the top-level container for metadata and permissions. You normally have one per cloud region in an account.
- Catalog is the first level of organization. Teams often use catalogs to separate environments (dev, prod) or business units.
- Schema (also called a database) groups related objects inside a catalog.
- Table, view, volume, function, and model are the objects inside a schema. Tables and views hold tabular data. Volumes hold files that are not tables, such as images or CSV drops.
The three-level name
Every table is addressed as catalog.schema.table. The name is the same from SQL, Python, a job, or a dashboard, so there is one way to refer to a table everywhere.
CREATE SCHEMA IF NOT EXISTS main.tutorial;
CREATE TABLE IF NOT EXISTS main.tutorial.orders (
order_id BIGINT,
customer_id BIGINT,
amount DECIMAL(10, 2),
order_ts TIMESTAMP
);
INSERT INTO main.tutorial.orders VALUES
(1, 101, 25.00, current_timestamp()),
(2, 102, 40.50, current_timestamp()),
(3, 101, 15.25, current_timestamp());This assumes you have permission to create objects in the main catalog. Because no storage path was given, the table is a managed Delta table, which the next sections explain.
Read the same table from a notebook attached to compute. The spark session is predefined in notebooks.
from pyspark.sql import functions as F
df = spark.table("main.tutorial.orders")
totals = (
df.groupBy("customer_id")
.agg(F.sum("amount").alias("total_amount"))
)
display(totals)Setting defaults
Typing three parts every time gets tedious. You can set a current catalog and schema, and then use shorter names.
USE CATALOG main;
USE SCHEMA tutorial;
SELECT * FROM orders;A one-part name resolves against the current schema. A two-part name schema.table resolves against the current catalog. Resolution depends on session state, so shared code such as jobs is easier to reason about when it uses full three-level names.
Managed and external tables
Unity Catalog supports two ways to store a table.
| Property | Managed table | External table |
|---|---|---|
| Who decides file location | Unity Catalog | You, with a `LOCATION` clause |
| Storage used | Managed storage at the metastore, catalog, or schema level | A path you register as an external location |
| On `DROP TABLE` | Metadata and data are removed (the data is recoverable for a limited period with `UNDROP`) | Only metadata is removed. The files stay |
| Typical use | Default choice for new tables | Data that other systems also write or read directly |
Managed storage can be set at the metastore, catalog, or schema level. The most specific setting wins. This lets one team's catalog write to its own storage location. Managed tables are the default recommendation because Unity Catalog can handle file layout and maintenance for you.
Seeing history and metadata
Because the table is Delta, each change creates a new version in the transaction log. You can inspect the log and read older versions.
DESCRIBE HISTORY main.tutorial.orders;
SELECT * FROM main.tutorial.orders VERSION AS OF 0;Unity Catalog also exposes metadata as queryable tables in an information_schema that exists in every catalog.
SELECT table_schema, table_name, table_type
FROM main.information_schema.tables
WHERE table_schema = 'tutorial';You can also run SHOW CATALOGS, SHOW SCHEMAS IN main, and SHOW TABLES IN main.tutorial to list what exists. Results include only the objects you are allowed to see.
Permissions and inheritance
Access is denied by default. A principal (a user, a group, or a service principal) can use an object only if it is the owner or has been granted a privilege.
Privileges are inherited downward. A privilege granted on a catalog applies to every schema and table in it, including ones created later. A grant on a schema applies to every table in that schema.
Reading a table needs more than SELECT. The principal also needs the right to traverse the parents:
USE CATALOGon the catalog.USE SCHEMAon the schema.SELECTon the table, or on a parent that passes it down.
GRANT USE CATALOG ON CATALOG main TO `data-analysts`;
GRANT USE SCHEMA, SELECT ON SCHEMA main.tutorial TO `data-analysts`;After these two statements, members of data-analysts can query every table in main.tutorial. They cannot modify tables, because MODIFY was not granted. They cannot create tables, because CREATE TABLE was not granted either.
Edge cases and common errors
- Case and characters. Unity Catalog stores object names in lowercase, so
Ordersandordersare the same table. Names with spaces or special characters need backticks. - The legacy
hive_metastorecatalog. Older workspaces expose a catalog calledhive_metastorefor tables created before Unity Catalog. Those tables do not get Unity Catalog permissions or lineage. Migrating them is a separate task. - Wrong default catalog. A bare table name that works in one workspace can fail in another because the default catalog differs. Use full names in shared code.
- Compute without access. Unity Catalog needs compute that supports it. Serverless compute and SQL warehouses do. A cluster must run in an access mode that supports Unity Catalog, such as standard or dedicated.
- Ownership. The creator of an object owns it and can grant on it. If that person leaves, ownership should be moved to a group so grants remain manageable.
Exercise
A user in group reporting has SELECT on main.sales.invoices, but the query fails with a permission error. Name the two other privileges you should check, and say what object each is granted on.
Show answer
Check USE CATALOG on the catalog main and USE SCHEMA on the schema main.sales. SELECT on the table is not enough without both parent privileges.
Exercise
You run DROP TABLE on an external table whose files are in s3://company-data/raw/events/. What happens to the files, and how does this differ from a managed table?
Show answer
The files stay in the storage path, and only the table's metadata is removed from Unity Catalog. For a managed table, DROP TABLE also removes the data, although it can be recovered for a limited time with UNDROP.