Sub topic 6
Schemas
Prerequisites
This chapter assumes you can write basic SQL (CREATE, SELECT, INSERT). It also assumes you know the first two levels of the Unity Catalog hierarchy. A metastore holds catalogs. A catalog is the first part of a three-level name. You should be able to run SQL in a Databricks notebook or the SQL editor. Privilege inheritance gets its own chapter, so this one only touches it.
What a schema is
A schema is the second level of the Unity Catalog namespace. It lives inside one catalog and holds the data objects you work with every day: tables, views, volumes, and functions. Some Databricks documentation calls a schema a "database". In Unity Catalog the two words mean the same thing, and SQL accepts both CREATE SCHEMA and CREATE DATABASE.
A fully qualified table name has three parts: catalog.schema.table. The schema is the middle part. In dev.sales.orders, dev is the catalog, sales is the schema, and orders is the table.
Schemas group related objects. Typical groupings are a business domain (sales, marketing), a pipeline stage (bronze, silver, gold), or a project. They are also a convenient place to attach permissions, which is covered later in this chapter.
Creating a schema
Create a schema with CREATE SCHEMA. Qualify the name with its catalog so there is no doubt about where it lands.
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 IF NOT EXISTS clause makes the statement safe to re-run. Without it, creating a schema that already exists raises an error.
To create a schema you need the USE CATALOG and CREATE SCHEMA privileges on the parent catalog, or ownership of that catalog. The person who creates the schema becomes its owner.
Useful options
CREATE SCHEMA accepts a few optional clauses:
COMMENTstores a description that shows up in Catalog Explorer and inDESCRIBE SCHEMA.MANAGED LOCATIONsets a cloud storage path for the managed tables in this schema, overriding the catalog's default. Your account must have an external location that covers the path.WITH DBPROPERTIESstores key-value metadata, such as an owning team or a cost center.
CREATE SCHEMA IF NOT EXISTS dev.finance
COMMENT 'Ledger and invoice data for the finance team'
WITH DBPROPERTIES (owner_team = 'finance', cost_center = '1042');The property name owner_team here is only metadata. It does not change who owns the schema or who can use it.
Where managed data is stored
Schemas take part in deciding where the files of managed tables go. Unity Catalog looks for a managed storage location in this order and uses the first one it finds:
- The schema's own
MANAGED LOCATION. - The catalog's managed location.
- The metastore's default storage location, if the metastore has one.
If none of the three is set, creating a managed table fails because Unity Catalog has nowhere to put the files. Most teams set the location at the catalog level and leave schemas to inherit it. Set it at the schema level only when one schema needs its own storage, for example a different bucket or a different cloud account for regulatory reasons.
CREATE SCHEMA IF NOT EXISTS dev.regulated
MANAGED LOCATION 's3://example-bucket/regulated';The bucket name is a placeholder. The path must be covered by an external location you are allowed to use, or the statement fails.
Changing a schema's managed location later does not move existing tables. It only affects tables created afterward.
Choosing the current catalog and schema
Typing three-part names gets tedious. USE CATALOG and USE SCHEMA set defaults for your session so that you can refer to a table by its short name.
USE CATALOG dev;
USE SCHEMA sales;
SELECT * FROM orders;orders resolves to dev.sales.orders. You can also set both at once with USE SCHEMA dev.sales.
You can mix styles. With the defaults above, SELECT * FROM marketing.campaigns is read as dev.marketing.campaigns, because a two-part name uses the current catalog. A three-part name ignores the defaults entirely.
Do not rely on session defaults in code that other people or jobs run. A job that starts in a different default catalog will quietly read a different table or fail. Three-part names are longer but unambiguous.
Common error: wrong default
If you run SELECT * FROM orders and get TABLE_OR_VIEW_NOT_FOUND, check the current location first.
SELECT current_catalog(), current_schema();If the result is not dev and sales, the short name is resolving somewhere else. Either set the defaults or use the full name.
Inspecting schemas
List the schemas in a catalog and see the details of one:
SHOW SCHEMAS IN dev;
DESCRIBE SCHEMA EXTENDED dev.sales;DESCRIBE SCHEMA EXTENDED shows the owner, the comment, the managed location if one is set, and any properties. You can also query the catalog's information_schema:
SELECT schema_name, schema_owner, comment
FROM dev.information_schema.schemata;Each catalog has its own information_schema. The schemata view in dev lists only the schemas in dev. It shows only those that the current user has permission to see.
Schemas and privileges
A schema is a securable object. You can grant privileges on it, and those privileges apply to the objects inside it. Using a table requires access at all three levels:
| Level | Privilege needed to read a table |
|---|---|
| Catalog | `USE CATALOG` |
| Schema | `USE SCHEMA` |
| Table | `SELECT` |
USE CATALOG and USE SCHEMA act like a key to a door. They let a user reach the objects inside, but they do not let the user read any data. The data is read through SELECT.
The most common pattern grants SELECT on the whole schema, so it covers every table in it, including tables created later:
GRANT USE CATALOG ON CATALOG dev TO `data-analysts`;
GRANT USE SCHEMA ON SCHEMA dev.sales TO `data-analysts`;
GRANT SELECT ON SCHEMA dev.sales TO `data-analysts`;
SHOW GRANTS ON SCHEMA dev.sales;To take the access back:
REVOKE SELECT ON SCHEMA dev.sales FROM `data-analysts`;Revoking SELECT at the schema level removes the inherited access to every table in it. It does not remove a SELECT grant that was made directly on one table. If an analyst can still read a table after you revoke at the schema, run SHOW GRANTS on that table and look for a direct grant.
Schema-level privileges
Besides USE SCHEMA, these privileges apply at the schema level:
| Privilege | What it allows |
|---|---|
| `CREATE TABLE` | Create tables and views in the schema |
| `CREATE VOLUME` | Create volumes in the schema |
| `CREATE FUNCTION` | Create functions in the schema |
| `SELECT` | Read all tables and views in the schema |
| `MODIFY` | Insert, update, and delete rows in all tables |
| `ALL PRIVILEGES` | Every privilege above, excluding the right to grant them |
The exact list differs a little between Databricks releases, so check SHOW GRANTS and the documentation for your workspace.
Ownership
Whoever creates a schema owns it. An owner can grant privileges on the schema, drop it, and manage the objects in it. Ownership belongs best to a group, not to one person, so that the schema does not become unmanageable when that person leaves.
ALTER SCHEMA dev.sales OWNER TO `data-engineers`;You must be the current owner, or a user with the MANAGE privilege on the schema, to change the owner.
Renaming and dropping
Rename a schema with ALTER SCHEMA ... RENAME TO. Use the new name only, without the catalog:
ALTER SCHEMA dev.sales RENAME TO order_data;Renaming breaks every query, job, and view that uses the old name, and grants stay attached to the schema. Tell the downstream users before you rename.
Dropping a schema removes it from the catalog:
DROP SCHEMA dev.scratch;
DROP SCHEMA IF EXISTS dev.scratch CASCADE;A plain DROP SCHEMA fails if the schema still contains objects. CASCADE drops the schema and everything in it. This is a destructive operation. Dropping a managed table can delete its data files, though Unity Catalog keeps dropped managed tables recoverable for a limited period. External tables lose only their metadata, and the files stay in cloud storage. Check the contents first:
SHOW TABLES IN dev.scratch;Every catalog starts with a default schema
Each new catalog gets a schema called default, plus the read-only information_schema. The default schema is only a convenience. Most teams create named schemas and leave default empty, because a name such as default says nothing about what the data is.
Design guidance
- Use one schema per business domain or pipeline stage, not one schema per table.
- Use lowercase names with underscores. Unquoted names are case-insensitive, but consistent names avoid confusion across tools.
- Group objects that share the same audience. Because privileges are easiest to manage at the schema level, objects with different access rules belong in different schemas.
- Avoid putting everything in
default.
Exercises
Exercise
An analyst has USE CATALOG on dev and SELECT on the schema dev.sales, but the query SELECT * FROM dev.sales.orders fails with a permission error. What is the most likely missing privilege, and which statement would fix it?
Show answer
The analyst is missing USE SCHEMA on dev.sales. Reading a table needs USE CATALOG, USE SCHEMA, and SELECT. The fix is: GRANT USE SCHEMA ON SCHEMA dev.sales TO data-analysts; (using the analyst's own group or user name).
Exercise
You revoke SELECT on dev.sales from the group data-analysts, but members can still read dev.sales.orders. Give one likely reason and a statement that helps you find it.
Show answer
A SELECT privilege may have been granted directly on the table, or the members may get access through another group or through ownership. Run SHOW GRANTS ON TABLE dev.sales.orders; to see direct grants.
Exercise
A new schema has no MANAGED LOCATION, and its catalog has none either. Creating a managed table in it fails. Where else could a storage location come from, and what are two ways to fix this?
Show answer
The metastore's default storage location, if one is configured, is the last fallback. To fix the error, either set a managed location on the catalog or on the schema (for example with CREATE SCHEMA ... MANAGED LOCATION), or have a metastore admin configure a default location for the metastore.