In this post we are going to cover schemas in Snowflake.
A schema is the box a table lives in. A database holds schemas, a schema holds tables, and when you write a table name without saying which schema it is in, Snowflake decides for you. It uses whichever schema your session happens to be pointing at.
That last sentence is the whole post. We will build two schemas with a table of the same name in each, run one query against them twice without changing a character of it, and get different rows back both times. Then we will rename a schema, swap two schemas over in one statement, undrop a schema, and finish with a question worth knowing the answer to: can you drop the PUBLIC schema?
Every query below was run on a real Snowflake account, and every result you see is the output it gave back.
Episode 99 of the Snowflake SQL: Zero to Hero series on TechBrothersIT.
- Let's see which schemas we already have
- Let's create some sample data
- Now let's put a table called ORDERS_T99 in both of them
- Now let's run the same query twice
- Now let's spell the name out in full
- So why did nothing warn us?
- Now let's rename a schema
- Now the interesting one: SWAP WITH
- Can we get a dropped schema back?
- Can we drop the PUBLIC schema?
- And can we drop INFORMATION_SCHEMA?
- Can we create a database without a PUBLIC schema?
- One last thing: let's put everything back
Let's see which schemas we already have
Before we make anything, let's ask the database what is in it. INFORMATION_SCHEMA.SCHEMATA is a view that lists the schemas of the database you are in.
SELECT SCHEMA_NAME, SCHEMA_OWNER, IS_TRANSIENT, RETENTION_TIME, COMMENT
FROM INFORMATION_SCHEMA.SCHEMATA
ORDER BY SCHEMA_NAME;| SCHEMA_NAME | SCHEMA_OWNER | IS_TRANSIENT | RETENTION_TIME | COMMENT |
|---|---|---|---|---|
| INFORMATION_SCHEMA | NULL | NO | 1 | Views describing the contents of schemas in this database |
| PUBLIC | ACCOUNTADMIN | NO | 1 | NULL |
Two schemas, and we did not create either of them. PUBLIC is the one we have been working in all course. INFORMATION_SCHEMA is the catalog itself, which is how we are able to run this query at all.
Have a look at the owner column. PUBLIC is owned by ACCOUNTADMIN. INFORMATION_SCHEMA has no owner at all, and the grid prints that as a real NULL. That is a hint about what these two are. One of them is ours and one of them is Snowflake's.
Neither is transient and both keep one day of Time Travel. Now let's check where the session is pointing.
SELECT CURRENT_DATABASE() AS THE_DATABASE,
CURRENT_SCHEMA() AS THE_SCHEMA;| THE_DATABASE | THE_SCHEMA |
|---|---|
| DEMO_DB | PUBLIC |
DEMO_DB and PUBLIC. Remember those two words, because everything that follows is about what they do to an unqualified table name.
Let's create some sample data
We need two schemas, and a table with the same name in both of them. Here is the whole script in one piece. Copy it into a worksheet and run it, and then we will watch Snowflake answer it one statement at a time.
-- Sample data for this post.
-- Two schemas, and a table with the SAME NAME in both of them.
USE DATABASE DEMO_DB;
USE SCHEMA PUBLIC;
CREATE OR REPLACE SCHEMA STAGING_T99 COMMENT = 'episode 99 probe';
CREATE OR REPLACE SCHEMA REPORTING_T99 COMMENT = 'episode 99 probe';
CREATE OR REPLACE TABLE STAGING_T99.ORDERS_T99 (ID INT, STATUS VARCHAR);
CREATE OR REPLACE TABLE REPORTING_T99.ORDERS_T99 (ID INT, STATUS VARCHAR);
INSERT INTO STAGING_T99.ORDERS_T99 (ID, STATUS)
SELECT 1, 'RAW' UNION ALL SELECT 2, 'RAW' UNION ALL SELECT 3, 'RAW';
INSERT INTO REPORTING_T99.ORDERS_T99 (ID, STATUS)
SELECT 99, 'PUBLISHED';Let's start with the two schemas.
CREATE OR REPLACE SCHEMA STAGING_T99 COMMENT = 'episode 99 probe';| status |
|---|
| Schema STAGING_T99 successfully created. |
CREATE OR REPLACE SCHEMA REPORTING_T99 COMMENT = 'episode 99 probe';| status |
|---|
| Schema REPORTING_T99 successfully created. |
Both created, and both carry a comment so we can find them again later. Now, what is inside a schema we just made?
SELECT COUNT(*) AS TABLES_IN_A_NEW_SCHEMA
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'STAGING_T99';| TABLES_IN_A_NEW_SCHEMA |
|---|
| 0 |
Nothing. A new schema is an empty box with a name. That is all a schema ever is.
Now let's put a table called ORDERS_T99 in both of them
Here is the part that matters. We are going to create a table called ORDERS_T99 twice, in two different schemas, with different data in each.
CREATE OR REPLACE TABLE STAGING_T99.ORDERS_T99 (ID INT, STATUS VARCHAR);| status |
|---|
| Table ORDERS_T99 successfully created. |
INSERT INTO STAGING_T99.ORDERS_T99 (ID, STATUS)
SELECT 1, 'RAW' UNION ALL SELECT 2, 'RAW' UNION ALL SELECT 3, 'RAW';| number of rows inserted |
|---|
| 3 |
Three rows in the staging copy, and every one of them has a status of RAW. Now the other one.
CREATE OR REPLACE TABLE REPORTING_T99.ORDERS_T99 (ID INT, STATUS VARCHAR);| status |
|---|
| Table ORDERS_T99 successfully created. |
INSERT INTO REPORTING_T99.ORDERS_T99 (ID, STATUS) SELECT 99, 'PUBLISHED';| number of rows inserted |
|---|
| 1 |
One row in the reporting copy, with a status of PUBLISHED.
Nothing warned us that a table of that name already existed. Why would it? They are in different schemas, so they are different tables. Both statements were completely legal.
Now let's run the same query twice
First we point the session at the staging schema.
USE SCHEMA STAGING_T99;| status |
|---|
| Statement executed successfully. |
Now one query. It asks which schema it resolved against, how many rows it found, and one of the statuses. The table name in it is ORDERS_T99, with no schema in front of it.
SELECT CURRENT_SCHEMA() AS RESOLVED_AGAINST,
COUNT(*) AS ROWS_RETURNED,
MIN(STATUS) AS A_STATUS
FROM ORDERS_T99;| RESOLVED_AGAINST | ROWS_RETURNED | A_STATUS |
|---|---|---|
| STAGING_T99 | 3 | RAW |
Three rows, status RAW, resolved against the staging schema. Exactly what we put there.
Now one statement, and nothing else changes.
USE SCHEMA REPORTING_T99;| status |
|---|
| Statement executed successfully. |
And now the identical query. Same text, character for character. We are not retyping it, we are running it again.
SELECT CURRENT_SCHEMA() AS RESOLVED_AGAINST,
COUNT(*) AS ROWS_RETURNED,
MIN(STATUS) AS A_STATUS
FROM ORDERS_T99;| RESOLVED_AGAINST | ROWS_RETURNED | A_STATUS |
|---|---|---|
| REPORTING_T99 | 1 | PUBLISHED |
One row, status PUBLISHED, resolved against the reporting schema.
Same query text. Same session. Same warehouse. Different answer. The only thing that changed between those two grids is a USE SCHEMA statement, and in real life that statement was typed some minutes earlier and has been forgotten about.
There was no warning either time. Both queries were correct SQL and both returned a correct answer to the question they were actually asked. The question you think you are asking and the question Snowflake receives differ by a word you did not type.
Now let's spell the name out in full
The fix is to say which schema we mean. A fully qualified name is DATABASE.SCHEMA.TABLE, and it does not care where the session is pointing.
Let's ask for both counts in one statement, and have the query tell us where we are standing while it does it.
SELECT (SELECT COUNT(*) FROM DEMO_DB.STAGING_T99.ORDERS_T99) AS STAGING_ROWS,
(SELECT COUNT(*) FROM DEMO_DB.REPORTING_T99.ORDERS_T99) AS REPORTING_ROWS,
CURRENT_SCHEMA() AS AND_WE_ARE_STILL_IN;| STAGING_ROWS | REPORTING_ROWS | AND_WE_ARE_STILL_IN |
|---|---|---|
| 3 | 1 | REPORTING_T99 |
Three from staging and one from reporting, out of a single query, and we are still sitting in the reporting schema while it runs. Nothing about this answer depends on where the session happens to be.
Unqualified names are fine in a worksheet, where you can see the database and schema in the dropdown next to the query. They are not fine in anything you save — a script, a view, a task, a stored procedure, anything that runs when you are not watching. A file does not carry your session with it.
So why did nothing warn us?
Because there was nothing to warn us about. Let's ask the catalog for every table called ORDERS_T99, wherever it is.
SELECT TABLE_SCHEMA, TABLE_NAME, ROW_COUNT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'ORDERS_T99'
ORDER BY TABLE_SCHEMA;| TABLE_SCHEMA | TABLE_NAME | ROW_COUNT |
|---|---|---|
| REPORTING_T99 | ORDERS_T99 | 1 |
| STAGING_T99 | ORDERS_T99 | 3 |
Two rows, one per schema, with their row counts beside them. Two real tables, two different sets of rows, one name. The catalog is not confused and it is not hiding anything. It simply does not have an opinion about which one you meant.
Now let's rename a schema
ALTER SCHEMA ... RENAME TO changes a schema's name. Let's turn STAGING_T99 into LANDING_T99.
ALTER SCHEMA STAGING_T99 RENAME TO LANDING_T99;| status |
|---|
| Statement executed successfully. |
The obvious question is what happened to the table that was inside it.
SELECT TABLE_SCHEMA, TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'ORDERS_T99'
ORDER BY TABLE_SCHEMA;| TABLE_SCHEMA | TABLE_NAME |
|---|---|
| LANDING_T99 | ORDERS_T99 |
| REPORTING_T99 | ORDERS_T99 |
It went with the schema, and it is still called ORDERS_T99. We renamed the box, not the thing in it.
Now let's ask for the old name on purpose.
SELECT COUNT(*) FROM DEMO_DB.STAGING_T99.ORDERS_T99;002003 (02000): SQL compilation error: Schema 'DEMO_DB.STAGING_T99' does not exist or not authorized.
A clean compilation error. Schema DEMO_DB.STAGING_T99 does not exist or not authorized. So a rename is loud. Anything that referenced the old schema name breaks immediately and tells you why, which is the best kind of breakage there is.
While we are altering it, let's also change the comment.
ALTER SCHEMA LANDING_T99 SET COMMENT = 'renamed and re-commented';| status |
|---|
| Statement executed successfully. |
SELECT SCHEMA_NAME, COMMENT, RETENTION_TIME
FROM INFORMATION_SCHEMA.SCHEMATA
WHERE SCHEMA_NAME IN ('LANDING_T99', 'REPORTING_T99')
ORDER BY SCHEMA_NAME;| SCHEMA_NAME | COMMENT | RETENTION_TIME |
|---|---|---|
| LANDING_T99 | renamed and re-commented | 1 |
| REPORTING_T99 | episode 99 probe | 1 |
The renamed schema carries the new comment and the other one still carries the comment we created it with. Both keep one day of Time Travel.
Now the interesting one: SWAP WITH
Right now LANDING_T99 holds the three RAW rows and REPORTING_T99 holds the one PUBLISHED row. Let's exchange them in a single statement.
ALTER SCHEMA LANDING_T99 SWAP WITH REPORTING_T99;| status |
|---|
| Statement executed successfully. |
Now let's look at what is in each name.
SELECT (SELECT COUNT(*) FROM DEMO_DB.LANDING_T99.ORDERS_T99) AS LANDING_ROWS,
(SELECT COUNT(*) FROM DEMO_DB.REPORTING_T99.ORDERS_T99) AS REPORTING_ROWS,
(SELECT MIN(STATUS) FROM DEMO_DB.LANDING_T99.ORDERS_T99) AS LANDING_STATUS,
(SELECT MIN(STATUS) FROM DEMO_DB.REPORTING_T99.ORDERS_T99) AS REPORTING_STATUS;| LANDING_ROWS | REPORTING_ROWS | LANDING_STATUS | REPORTING_STATUS |
|---|---|---|---|
| 1 | 3 | PUBLISHED | RAW |
They have traded places. LANDING_T99 now holds the single PUBLISHED row and REPORTING_T99 holds the three RAW ones.
The names stayed exactly where they were and the contents moved. That is what makes this statement useful. You build a new version of a schema under a working name, swap it into place, and there is never a moment where the name people are querying does not exist.
Can we get a dropped schema back?
Let's drop one and find out.
DROP SCHEMA LANDING_T99;| status |
|---|
| LANDING_T99 successfully dropped. |
SELECT COUNT(*) FROM DEMO_DB.LANDING_T99.ORDERS_T99;002003 (02000): SQL compilation error: Schema 'DEMO_DB.LANDING_T99' does not exist or not authorized.
Gone, and the same clean error we got from the old name after the rename. Now, we know UNDROP TABLE exists and we saw UNDROP DATABASE in Episode 98. Is there an UNDROP SCHEMA?
UNDROP SCHEMA LANDING_T99;| status |
|---|
| Schema LANDING_T99 successfully restored. |
There is. Schema LANDING_T99 successfully restored. So the UNDROP family covers all three: table, schema and database.
And the table inside it?
SELECT COUNT(*) AS ROWS_RECOVERED FROM DEMO_DB.LANDING_T99.ORDERS_T99;| ROWS_RECOVERED |
|---|
| 1 |
The table came back with the schema, holding the row it had when we dropped it.
If you were expecting three rows there, look back at the swap. LANDING_T99 gave its three RAW rows away and took the single PUBLISHED row in exchange. The restored schema came back exactly as it was at the moment we dropped it, which is the whole point. Nothing was lost in the recovery.
Can we drop the PUBLIC schema?
This is the question this episode exists for, and it is not a question to try out on a database you care about. So let's make a throwaway one.
CREATE OR REPLACE DATABASE PUBLIC_PROBE_T99;| status |
|---|
| Database PUBLIC_PROBE_T99 successfully created. |
A brand new database. Does it have a PUBLIC schema?
SELECT COUNT(*) AS PUBLIC_EXISTS
FROM PUBLIC_PROBE_T99.INFORMATION_SCHEMA.SCHEMATA
WHERE SCHEMA_NAME = 'PUBLIC';| PUBLIC_EXISTS |
|---|
| 1 |
It does, and we never asked for one. Now let's drop it.
DROP SCHEMA PUBLIC_PROBE_T99.PUBLIC;| status |
|---|
| PUBLIC successfully dropped. |
PUBLIC successfully dropped. No warning, no confirmation, no special treatment. Let's see what is left.
SELECT ARRAY_AGG(SCHEMA_NAME) AS SCHEMAS_LEFT
FROM PUBLIC_PROBE_T99.INFORMATION_SCHEMA.SCHEMATA;| SCHEMAS_LEFT |
|---|
| [ "INFORMATION_SCHEMA" ] |
One schema left, and it is the catalog. The database now has no PUBLIC schema at all.
“Every database has a PUBLIC schema” is a default, not a guarantee. It is there when the database is created and it can be dropped afterwards like any other schema. Any code that writes a bare DATABASE.PUBLIC.TABLE and assumes the middle part is always available is relying on something nobody promised it.
And can we drop INFORMATION_SCHEMA?
The other schema in that database is the catalog. Let's try the same statement on it.
DROP SCHEMA PUBLIC_PROBE_T99.INFORMATION_SCHEMA;003001 (42501): SQL access control error: Insufficient privileges to operate on schema 'INFORMATION_SCHEMA'.
Refused. Read the message slowly though, because it is not the message you would expect: Insufficient privileges to operate on schema INFORMATION_SCHEMA.
That is an access control error, not a rule about what DROP SCHEMA can be pointed at. Snowflake is not saying this cannot be done. It is saying we are not the one who gets to do it — and every schema we have created in this post came back owned by ACCOUNTADMIN, so this is not a small role being told no.
Can we create a database without a PUBLIC schema?
If PUBLIC can be dropped afterwards, the next question is whether we can avoid getting one in the first place. Let's make another database and give it a schema of our own.
CREATE OR REPLACE DATABASE NO_PUBLIC_T99;| status |
|---|
| Database NO_PUBLIC_T99 successfully created. |
And a schema with WITH MANAGED ACCESS, which is a real option on CREATE SCHEMA and means grants inside it are controlled by the schema owner rather than by whoever owns each object.
CREATE OR REPLACE SCHEMA NO_PUBLIC_T99.LOCKED_T99 WITH MANAGED ACCESS;| status |
|---|
| Schema LOCKED_T99 successfully created. |
Now let's list what that database actually ended up with. SHOW SCHEMAS gives us a much wider grid than the catalog view did at the top of this post.
SHOW SCHEMAS IN DATABASE NO_PUBLIC_T99;| created_on | name | is_default | is_current | database_name | owner | comment | options | retention_time | owner_role_type | classification_profile_database | classification_profile_schema | classification_profile | object_visibility | is_nested |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2026-09-28 13:01:24.179000-07:00 | INFORMATION_SCHEMA | N | N | NO_PUBLIC_T99 | Views describing the contents of schemas in this database | 1 | NULL | NULL | NULL | NULL | false | |||
| 2026-09-28 13:01:23.989000-07:00 | LOCKED_T99 | N | Y | NO_PUBLIC_T99 | ACCOUNTADMIN | MANAGED ACCESS | 1 | ROLE | NULL | NULL | NULL | NULL | false | |
| 2026-09-28 13:01:23.737000-07:00 | PUBLIC | N | N | NO_PUBLIC_T99 | ACCOUNTADMIN | 1 | ROLE | NULL | NULL | NULL | NULL | false |
Three schemas, and we only created one of them. There is INFORMATION_SCHEMA, there is our LOCKED_T99 with MANAGED ACCESS sitting in the options column, and there is PUBLIC. It arrived anyway.
So there is no way to create a database that never has a PUBLIC schema. You either keep it or you drop it afterwards.
The empty cells in that grid and the NULL cells are two different things, and the grid marks them differently. SHOW SCHEMAS writes the INFORMATION_SCHEMA owner as an empty string, so the cell is blank and marked empty. The classification columns on the same row hold a real SQL NULL and print the word. Compare that owner cell with the first grid in this post, where INFORMATION_SCHEMA.SCHEMATA reports the same missing owner as a genuine NULL. Two views, one fact, two different ways of saying nothing.
One last thing: let's put everything back
Both probe databases were made to be thrown away, so let's throw them away.
-- Put the database back the way we found it.
DROP DATABASE IF EXISTS PUBLIC_PROBE_T99;
DROP DATABASE IF EXISTS NO_PUBLIC_T99;
USE SCHEMA DEMO_DB.PUBLIC;
DROP SCHEMA IF EXISTS DEMO_DB.LANDING_T99;
DROP SCHEMA IF EXISTS DEMO_DB.REPORTING_T99;DROP DATABASE IF EXISTS PUBLIC_PROBE_T99;| status |
|---|
| PUBLIC_PROBE_T99 successfully dropped. |
DROP DATABASE IF EXISTS NO_PUBLIC_T99;| status |
|---|
| NO_PUBLIC_T99 successfully dropped. |
Then point the session back at the schema we started in, fully qualified, because by now we have earned the habit.
USE SCHEMA DEMO_DB.PUBLIC;| status |
|---|
| Statement executed successfully. |
DROP SCHEMA IF EXISTS DEMO_DB.LANDING_T99;| status |
|---|
| LANDING_T99 successfully dropped. |
DROP SCHEMA IF EXISTS DEMO_DB.REPORTING_T99;| status |
|---|
| REPORTING_T99 successfully dropped. |
And a last check that the database we actually use is exactly as we found it.
SELECT CURRENT_DATABASE() AS THE_DATABASE,
CURRENT_SCHEMA() AS THE_SCHEMA,
(SELECT COUNT(*) FROM EVENTS_JSON) AS DEMO_DB_IS_READABLE,
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.SCHEMATA
WHERE SCHEMA_NAME LIKE '%_T99') AS PROBE_SCHEMAS_LEFT;| THE_DATABASE | THE_SCHEMA | DEMO_DB_IS_READABLE | PROBE_SCHEMAS_LEFT |
|---|---|---|---|
| DEMO_DB | PUBLIC | 4 | 0 |
DEMO_DB and PUBLIC, the course data still readable, and no probe schemas left behind.
Key points
- A table name with no schema in front of it is resolved against
CURRENT_SCHEMA(), whatever that happens to be. - Two schemas can hold a table of the same name, and nothing warns you. They are different tables.
- The identical query text returned three rows and then one row, with one
USE SCHEMAstatement between the two runs. - A fully qualified
DATABASE.SCHEMA.TABLEname does not move when the session does. Qualify anything you save: a script, a view, a task, a stored procedure. ALTER SCHEMA ... RENAME TOtakes the tables with it, and turns the old schema name into a clean compilation error.ALTER SCHEMA ... SWAP WITHexchanges the contents of two schemas in one statement. The names stay put.UNDROP SCHEMAexists, and the tables inside come back with it. SoUNDROPcovers table, schema and database.- The
PUBLICschema can be dropped, so “every database has aPUBLICschema” is a default and not a guarantee. INFORMATION_SCHEMArefuses, and it refuses on insufficient privileges rather than on a rule.- A new database arrives with a
PUBLICschema whether you want one or not. Dropping it afterwards is the only route.
Episode 100 — CREATE TABLE. NOT NULL, DEFAULT and AUTOINCREMENT, and a default that does not do what almost everyone thinks it does.
Every query and every result grid on this page was captured from a live Snowflake account (version 10.34.101) running the demo script for this episode, including the three refusals. The _T99 schemas, tables and databases are throwaway objects created and dropped by the scripts above. Every count in the text is read off one of the grids on this page. Nothing is timed and no performance claims are made anywhere in this series. Schema behaviour follows the Snowflake CREATE SCHEMA documentation.



No comments:
Post a Comment
Note: Only a member of this blog may post a comment.