Sub topic 6
Schemas
This chapter assumes you know basic SQL, can run statements in a Databricks notebook or the SQL editor, and have read the earlier chapters on the metastore, the three-level namespace, and catalogs.
What a schema is
A schema is the second level of the Unity Catalog namespace. It lives inside one catalog and holds tables, views, volumes, and functions. Some people call it a database, and Databricks SQL accepts DATABASE as a synonym for SCHEMA. Unity Catalog documentation uses "schema".
A schema gives you a place to group related objects, such as everything for sales or everything for marketing. It is also a common point for setting permissions, because a privilege granted on a schema covers the objects inside it.
Creating a schema
Create a schema inside an existing catalog. You need the USE CATALOG and CREATE SCHEMA privileges on that catalog, or you must own it.
CREATE CATALOG IF NOT EXISTS dev;
CREATE SCHEMA IF NOT EXISTS dev.sales;IF NOT EXISTS makes the statement safe to run twice. Without it, the second run fails because the schema already exists.
The schema name is qualified by its catalog: dev.sales. A table created in it is addressed as dev.sales.orders.
CREATE TABLE IF NOT EXISTS dev.sales.orders (
order_id BIGINT,
customer_id BIGINT,
order_ts TIMESTAMP,
amount DECIMAL(10,2)
);Setting the current schema
Typing three-level names gets tedious. USE CATALOG and USE SCHEMA set defaults for the session, so short names resolve against them.
USE CATALOG dev;
USE SCHEMA sales;
SELECT * FROM orders;Here orders resolves to dev.sales.orders. Be careful with this in shared notebooks. If a previous cell changed the current schema, a short name can point at a different table than you expect. Fully qualified names are safer in production jobs.
Listing and inspecting schemas
Use SHOW SCHEMAS to list the schemas in a catalog.
SHOW SCHEMAS IN dev;Every catalog also has an information_schema schema that Unity Catalog maintains for you. It is read-only and describes the objects in that catalog.
Schemas and privileges
A schema is a securable object, so you can grant privileges on it. To read a table, a user needs USE CATALOG on the catalog, USE SCHEMA on the schema, and SELECT on the table. Granting SELECT on the schema instead covers every table in it, including tables created later. This is called inheritance, and a later chapter covers it in detail.
GRANT USE SCHEMA ON SCHEMA dev.sales TO `data-analysts`;
GRANT SELECT ON SCHEMA dev.sales TO `data-analysts`;The user who creates a schema becomes its owner. The owner can grant privileges on it and can drop it.
Common errors
- Schema not found. You may have omitted the catalog, so the name resolved against the wrong current catalog. Qualify the name or run
USE CATALOGfirst. - Permission denied on a table you can
SELECT. You probably lackUSE SCHEMAorUSE CATALOGon a parent object. Table privileges alone are not enough. - Wrong table returned. A short name resolved against an unexpected current schema. Check with
SELECT current_schema().
Exercise
An analyst has SELECT on dev.sales.orders, but the query fails with a permission error. Which two privileges should you check first, and on which objects?
Show answer
Check USE CATALOG on the catalog dev and USE SCHEMA on the schema dev.sales. SELECT on the table is not enough without usage privileges on both parent objects.
Exercise
After running USE CATALOG dev and USE SCHEMA sales, what full name does the short name orders refer to?
Show answer
dev.sales.orders.