Snowflake SQL Tutorial 103 - ALTER TABLE - ADD, DROP, RENAME and SWAP WITH

Snowflake SQL · Episode 103

In this post we are going to cover ALTER TABLE in Snowflake: adding columns, dropping them, renaming them, renaming the whole table, and swapping two tables over in one statement.

ALTER TABLE is how you change a table that already exists. The table keeps its data and its name, and you change something about its shape.

This is where a schema changes, which is where other people's queries break. So the question worth asking of every statement on this page is not only whether it works. It is whether the things that used to work break loudly or quietly.

We will start by adding a column and build it up step by step. Every query below was run on a real Snowflake account, and every result you see is the output it gave back.

Episode 103 of the Snowflake SQL: Zero to Hero series on TechBrothersIT.

Let's create a table we can change

We need something to alter. The script below creates a WIDGETS_T103 table with an ID, a name and a price, and puts four rows into it.

Notice the COMMENT on the table. We are going to follow that comment around for the rest of the post, and where it ends up is the most surprising thing here.

Sample data
-- Sample data for this post.
-- A small table with a comment on it, because we are going to follow that
-- comment around for the rest of the page.
USE DATABASE DEMO_DB;
USE SCHEMA PUBLIC;

CREATE OR REPLACE TABLE WIDGETS_T103 (
    ID     INT,
    NAME   VARCHAR(50),
    PRICE  NUMBER(10,2)
)
COMMENT = 'the original widgets table';

INSERT INTO WIDGETS_T103 (ID, NAME, PRICE)
SELECT 1, 'bolt',    2.50
UNION ALL SELECT 2, 'nut',     1.25
UNION ALL SELECT 3, 'washer',  0.75
UNION ALL SELECT 4, 'bracket', 9.00;

Let's look at what we just made.

Query
SELECT ID, NAME, PRICE FROM WIDGETS_T103 ORDER BY ID;
Result4 rows · 3 columns
IDNAMEPRICE
1bolt2.50
2nut1.25
3washer0.75
4bracket9.00

Four rows and three columns. A bolt, a nut, a washer and a bracket.

Let's add a plain column

The simplest change there is. We add a column and say nothing else about it.

Query
ALTER TABLE WIDGETS_T103 ADD COLUMN CATEGORY VARCHAR(30);
Result1 row · 1 column
status
Statement executed successfully.

Accepted. Now the question that matters: what did the rows that were already in the table get put in that column?

Query
SELECT ID, NAME, PRICE, CATEGORY,
       CATEGORY IS NULL AS THE_NEW_COLUMN_IS_NULL
FROM WIDGETS_T103 ORDER BY ID;
Result4 rows · 5 columns
IDNAMEPRICECATEGORYTHE_NEW_COLUMN_IS_NULL
1bolt2.50NULLTRUE
2nut1.25NULLTRUE
3washer0.75NULLTRUE
4bracket9.00NULLTRUE

NULL on every row, and the THE_NEW_COLUMN_IS_NULL check says TRUE four times.

Which is what you would expect. There was nothing to put there, so Snowflake put nothing there.

Now let's add a column with a DEFAULT

Same statement, with a DEFAULT on the end. A boolean IN_STOCK column that defaults to TRUE.

Query
ALTER TABLE WIDGETS_T103 ADD COLUMN IN_STOCK BOOLEAN DEFAULT TRUE;
Result1 row · 1 column
status
Statement executed successfully.

Accepted too. And here is where I was wrong, so let me say what I expected before I show you the result.

I expected the existing rows to be NULL, the same as the last section, and the default to apply only to rows written from now on. A default that fills in old rows and a default that fills in future rows are two very different promises.

Query
SELECT ID, NAME, IN_STOCK,
       IN_STOCK IS NULL AS STILL_NULL
FROM WIDGETS_T103 ORDER BY ID;
Result4 rows · 4 columns
IDNAMEIN_STOCKSTILL_NULL
1boltTRUEFALSE
2nutTRUEFALSE
3washerTRUEFALSE
4bracketTRUEFALSE

TRUE on every row, and STILL_NULL is FALSE on every row. It backfilled. All four rows that were already in the table got the default value.

I was so sure of the other answer that I wrote the next query to show the two groups disagreeing. Let's insert a new row and run it anyway.

Query
INSERT INTO WIDGETS_T103 (ID, NAME, PRICE) SELECT 5, 'screw', 0.40;
Result1 row · 1 column
number of rows inserted
1
Query
SELECT ID, NAME, IN_STOCK FROM WIDGETS_T103 ORDER BY ID;
Result5 rows · 3 columns
IDNAMEIN_STOCK
1boltTRUE
2nutTRUE
3washerTRUE
4bracketTRUE
5screwTRUE

Five rows, and IN_STOCK is TRUE on every single one of them. The old rows and the new row agree completely.

About that query name

The query is still called so_the_old_rows_and_the_new_row_disagree and the name is staying. It is the record of a prediction that was wrong, and renaming it would hide the evidence.
What is true is the pair of grids above it. A column added with no default leaves the existing rows NULL. A column added with a default backfills them.

Can we add a NOT NULL column to a table that has rows?

Let's ask for a SUPPLIER column that is not allowed to be empty, on a table that already has five rows in it.

Query
ALTER TABLE WIDGETS_T103 ADD COLUMN SUPPLIER VARCHAR(40) NOT NULL;
Query failed — Snowflake returned an error
002200 (22023): SQL compilation error: Non-nullable column 'SUPPLIER' cannot be added to non-empty table 'WIDGETS_T103' unless it has a non-null default value.

Refused, and the message says exactly why: a non-nullable column cannot be added to a non-empty table unless it has a non-null default to fall back on.

Which is fair. The rows are already there and Snowflake has nothing legal to put in them. So let's give it something.

Query
ALTER TABLE WIDGETS_T103 ADD COLUMN SUPPLIER VARCHAR(40) NOT NULL DEFAULT 'unknown';
Result1 row · 1 column
status
Statement executed successfully.

Accepted. And the rows that were already there?

Query
SELECT ID, NAME, SUPPLIER FROM WIDGETS_T103 ORDER BY ID;
Result5 rows · 3 columns
IDNAMESUPPLIER
1boltunknown
2nutunknown
3washerunknown
4bracketunknown
5screwunknown

unknown on all five rows. The default backfilled again, which is the only way the NOT NULL promise could be true the moment the statement finished.

Let's look at the shape of the table after all that adding.

Query
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'WIDGETS_T103'
ORDER BY ORDINAL_POSITION;
Result6 rows · 4 columns
COLUMN_NAMEDATA_TYPEIS_NULLABLECOLUMN_DEFAULT
IDNUMBERYESNULL
NAMETEXTYESNULL
PRICENUMBERYESNULL
CATEGORYTEXTYESNULL
IN_STOCKBOOLEANYESTRUE
SUPPLIERTEXTNO'unknown'

Six columns now. Look at the last row: SUPPLIER is TEXT, IS_NULLABLE is NO, and the default is recorded in the catalog as 'unknown'. The default is not a one-off thing that happened during the ALTER. It is part of the column now.

What happens when we add a column that is already there?

Let's try to add a NAME column to a table that already has one.

Query
ALTER TABLE WIDGETS_T103 ADD COLUMN NAME VARCHAR(50);
Query failed — Snowflake returned an error
001430 (42601): SQL compilation error:
column 'NAME' already exists

Refused. The column already exists, and Snowflake says so rather than quietly doing nothing.

Sometimes doing nothing is exactly what you want, though. If a deployment script runs twice you would rather the second run was a no-op than an error. That is what IF NOT EXISTS is for.

Query
ALTER TABLE WIDGETS_T103 ADD COLUMN IF NOT EXISTS NAME VARCHAR(50);
Result1 row · 1 column
status
Statement executed successfully.

Accepted, and it did nothing at all. Same table, same columns. That is the idempotent form of the statement.

Now let's rename a column

PRICE is a vague name. Let's call it UNIT_PRICE instead.

Query
ALTER TABLE WIDGETS_T103 RENAME COLUMN PRICE TO UNIT_PRICE;
Result1 row · 1 column
status
Statement executed successfully.

Accepted. Now, did the data move?

Query
SELECT ID, NAME, UNIT_PRICE FROM WIDGETS_T103 ORDER BY ID;
Result5 rows · 3 columns
IDNAMEUNIT_PRICE
1bolt2.50
2nut1.25
3washer0.75
4bracket9.00
5screw0.40

It did not move anywhere. The same prices, under the new heading. A column rename changes the name and nothing else.

So what happens to a query that still asks for the old name?

Query
SELECT ID, PRICE FROM WIDGETS_T103;
Query failed — Snowflake returned an error
000904 (42000): SQL compilation error: error line 1 at position 11
invalid identifier 'PRICE'

invalid identifier 'PRICE', at compile time, immediately. Nothing runs and nothing comes back wrong.

That is the good case, and it is worth being clear about why. Anything in your warehouse that named PRICE is now broken, and it tells you the next time it runs. It does not quietly return something else.

One more thing to try. Let's rename onto a name that is already taken.

Query
ALTER TABLE WIDGETS_T103 RENAME COLUMN UNIT_PRICE TO NAME;
Query failed — Snowflake returned an error
002002 (42710): SQL compilation error:
Object 'WIDGETS_T103.NAME' already exists.

Refused as well. You cannot end up with two columns called the same thing.

Let's drop a column, and then try to get it back

CATEGORY is the column we added and never filled in. Let's drop it.

Query
ALTER TABLE WIDGETS_T103 DROP COLUMN CATEGORY;
Result1 row · 1 column
status
Statement executed successfully.

Accepted. Is it still in the catalog?

Query
SELECT COUNT(*) AS CATEGORY_STILL_LISTED
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'WIDGETS_T103'
  AND COLUMN_NAME = 'CATEGORY';
Result1 row · 1 column
CATEGORY_STILL_LISTED
0

Zero. INFORMATION_SCHEMA.COLUMNS does not list it any more.

And a query that asks for it?

Query
SELECT ID, CATEGORY FROM WIDGETS_T103;
Query failed — Snowflake returned an error
000904 (42000): SQL compilation error: error line 1 at position 11
invalid identifier 'CATEGORY'

invalid identifier again. Loud, the same way the renamed column was loud.

So is there an UNDROP COLUMN?

There is an UNDROP TABLE, which we used in Episode 102. It would be reasonable to expect a column version of it. Let's ask.

Query
ALTER TABLE WIDGETS_T103 UNDROP COLUMN CATEGORY;
Query failed — Snowflake returned an error
001003 (42000): SQL compilation error:
syntax error line 1 at position 25 unexpected 'UNDROP'.

syntax error ... unexpected 'UNDROP'. Read that message carefully, because it is not what people assume.

There is no UNDROP COLUMN

This is not a permissions problem and it is not a Time Travel window that has closed. Snowflake does not recognise the word in this position at all. There is no such statement.
A dropped column comes back by restoring the whole table to a point in time, or not at all.

Let's finish the pattern. What about dropping a column that was never there?

Query
ALTER TABLE WIDGETS_T103 DROP COLUMN NOT_A_COLUMN_T103;
Query failed — Snowflake returned an error
001431 (42601): SQL compilation error:
column 'NOT_A_COLUMN_T103' does not exist

Refused, and the message names the column it could not find. And the idempotent form:

Query
ALTER TABLE WIDGETS_T103 DROP COLUMN IF EXISTS NOT_A_COLUMN_T103;
Result1 row · 1 column
status
Statement executed successfully.

Accepted, and it did nothing. Same as ADD COLUMN IF NOT EXISTS, on the other side of the statement.

Here is where the table ended up after all the adding and dropping.

Query
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'WIDGETS_T103'
ORDER BY ORDINAL_POSITION;
Result5 rows · 3 columns
COLUMN_NAMEDATA_TYPEIS_NULLABLE
IDNUMBERYES
NAMETEXTYES
UNIT_PRICENUMBERYES
IN_STOCKBOOLEANYES
SUPPLIERTEXTNO

Five columns. PRICE is now UNIT_PRICE, CATEGORY is gone, and SUPPLIER is the one column that is not nullable.

Now let's rename the whole table

Same idea, one level up. WIDGETS_T103 becomes PARTS_T103.

Query
ALTER TABLE WIDGETS_T103 RENAME TO PARTS_T103;
Result1 row · 1 column
status
Statement executed successfully.

Accepted. Did the rows come with it?

Query
SELECT COUNT(*) AS ROWS_UNDER_THE_NEW_NAME FROM PARTS_T103;
Result1 row · 1 column
ROWS_UNDER_THE_NEW_NAME
5

Five rows under the new name. Everything came along.

And the old name?

Query
SELECT COUNT(*) FROM WIDGETS_T103;
Query failed — Snowflake returned an error
002003 (42S02): SQL compilation error:
Object 'WIDGETS_T103' does not exist or not authorized.

Object 'WIDGETS_T103' does not exist or not authorized. Loud again. Anything pointing at the old table name fails the next time it runs.

Now the thing I wanted to know. The comment we put on the table at the very beginning — where is it?

Query
SELECT TABLE_NAME, COMMENT, ROW_COUNT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'PARTS_T103';
Result1 row · 3 columns
TABLE_NAMECOMMENTROW_COUNT
PARTS_T103the original widgets table5

Still there, word for word, sitting on the new name, with five rows beside it. The comment travelled with the rename.

So a rename is safe in one particular sense. Nothing goes quiet. Everything that referenced the old name breaks visibly and immediately, which is the opposite of what the next statement does.

Let's publish a rebuilt table with SWAP WITH

This is the reason people reach for ALTER TABLE in production at all.

The situation is this. There is a live table that reports read. You have rebuilt it off to one side, and now you want the new version to be the live one. Dropping and renaming leaves a window where the name does not resolve, and a report that runs in that window fails.

Let's build the replacement first. It is deliberately a different shape from the live table — four columns instead of five, with a REBUILT_ON date the live one has never had, and different IDs.

The rebuilt table
-- The table we are going to publish, built off to one side.
-- It is deliberately a different shape from the live one.
CREATE OR REPLACE TABLE PARTS_REBUILT_T103 (
    ID          INT,
    NAME        VARCHAR(50),
    UNIT_PRICE  NUMBER(10,2),
    REBUILT_ON  DATE
)
COMMENT = 'the rebuilt table, published by SWAP';

INSERT INTO PARTS_REBUILT_T103 (ID, NAME, UNIT_PRICE, REBUILT_ON)
SELECT 101, 'rebuilt bolt', 2.75, '2026-06-01'::DATE
UNION ALL SELECT 102, 'rebuilt nut', 1.40, '2026-06-01'::DATE;

Here are the two of them side by side before we touch anything.

Query
SELECT (SELECT COUNT(*) FROM PARTS_T103)         AS PARTS_ROWS,
       (SELECT COUNT(*) FROM PARTS_REBUILT_T103) AS REBUILT_ROWS,
       (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_NAME = 'PARTS_T103')         AS PARTS_COLUMNS,
       (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_NAME = 'PARTS_REBUILT_T103') AS REBUILT_COLUMNS;
Result1 row · 4 columns
PARTS_ROWSREBUILT_ROWSPARTS_COLUMNSREBUILT_COLUMNS
5254

The live table has five rows and five columns. The rebuilt one has two rows and four columns. They are completely different tables that happen to sit in the same schema.

Now one statement.

Query
ALTER TABLE PARTS_T103 SWAP WITH PARTS_REBUILT_T103;
Result1 row · 1 column
status
Statement executed successfully.

Let's run exactly the same comparison again.

Query
SELECT (SELECT COUNT(*) FROM PARTS_T103)         AS PARTS_ROWS,
       (SELECT COUNT(*) FROM PARTS_REBUILT_T103) AS REBUILT_ROWS,
       (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_NAME = 'PARTS_T103')         AS PARTS_COLUMNS,
       (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_NAME = 'PARTS_REBUILT_T103') AS REBUILT_COLUMNS;
Result1 row · 4 columns
PARTS_ROWSREBUILT_ROWSPARTS_COLUMNSREBUILT_COLUMNS
2545

The numbers have changed places. The live name now has two rows and four columns, and the rebuilt name has five rows and five columns. They exchanged.

And the published name, the one your reports read:

Query
SELECT ID, NAME, UNIT_PRICE FROM PARTS_T103 ORDER BY ID;
Result2 rows · 3 columns
IDNAMEUNIT_PRICE
101rebuilt bolt2.75
102rebuilt nut1.40

The rebuilt bolt and the rebuilt nut, under the old name. The new data is live, and there was never a moment when PARTS_T103 did not resolve. That is the whole point of the statement.

So where did the comment go?

Here is the question I actually built this demo to answer. The comment was on the original table. The original table's rows are now sitting under the rebuilt name. Did the comment follow the name, or did it follow the data?

Query
SELECT TABLE_NAME, COMMENT, ROW_COUNT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC'
  AND TABLE_NAME IN ('PARTS_T103', 'PARTS_REBUILT_T103')
ORDER BY TABLE_NAME;
Result2 rows · 3 columns
TABLE_NAMECOMMENTROW_COUNT
PARTS_REBUILT_T103the original widgets table5
PARTS_T103the rebuilt table, published by SWAP2

Read the two rows carefully. PARTS_REBUILT_T103 is described as the original widgets table and has five rows. PARTS_T103 is described as the rebuilt table and has two rows.

The comment followed the data. Not the name.

The same thing happened to the column that only one of the two tables ever had.

Query
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC'
  AND COLUMN_NAME = 'REBUILT_ON'
ORDER BY TABLE_NAME;
Result1 row · 2 columns
TABLE_NAMECOLUMN_NAME
PARTS_T103REBUILT_ON

One row. REBUILT_ON was created on the rebuilt table, and after the swap it sits on PARTS_T103.

The name stays, everything else travels

So the name is the only thing that stayed put. The rows, the columns and the comment all moved with the table they belonged to.
That is exactly what you want when you are publishing a rebuilt table, and it is worth knowing before you go looking for your table comment and find it on the wrong object.

One refusal to finish the section. Let's swap with a table that does not exist.

Query
ALTER TABLE PARTS_T103 SWAP WITH NO_SUCH_TABLE_T103;
Query failed — Snowflake returned an error
002003 (42S02): SQL compilation error:
Table 'DEMO_DB.PUBLIC.NO_SUCH_TABLE_T103' does not exist or not authorized.

Refused, at compile time, with the fully qualified name of the table it could not find.

One last thing: comments are the cheapest documentation there is

We have been following one comment around all post. Let's look at how you set them, because this is the piece of ALTER TABLE most worth taking away.

A comment on the table:

Query
ALTER TABLE PARTS_T103 SET COMMENT = 'the live parts table';
Result1 row · 1 column
status
Statement executed successfully.

And a comment on a single column:

Query
ALTER TABLE PARTS_T103 ALTER COLUMN NAME COMMENT 'the part name as printed';
Result1 row · 1 column
status
Statement executed successfully.

Now one query gives us both levels of documentation at once.

Query
SELECT C.COLUMN_NAME, C.COMMENT AS COLUMN_COMMENT,
       (SELECT T.COMMENT FROM INFORMATION_SCHEMA.TABLES T
        WHERE T.TABLE_SCHEMA = 'PUBLIC' AND T.TABLE_NAME = 'PARTS_T103')
         AS TABLE_COMMENT
FROM INFORMATION_SCHEMA.COLUMNS C
WHERE C.TABLE_SCHEMA = 'PUBLIC' AND C.TABLE_NAME = 'PARTS_T103'
ORDER BY C.ORDINAL_POSITION;
Result4 rows · 3 columns
COLUMN_NAMECOLUMN_COMMENTTABLE_COMMENT
IDNULLthe live parts table
NAMEthe part name as printedthe live parts table
UNIT_PRICENULLthe live parts table
REBUILT_ONNULLthe live parts table

Four rows, one per column. NAME carries the column comment we just wrote. The other three columns have no comment, and those cells are a real NULL rather than an empty piece of text — the grid above draws those two things differently, so you can tell.

And every row carries the table comment beside it, because the table comment applies to all of them. If you do nothing else from this post, do this one. It costs a line per column and it answers the question everybody asks about somebody else's table.

And taking a comment off again

Query
ALTER TABLE PARTS_T103 UNSET COMMENT;
Result1 row · 1 column
status
Statement executed successfully.
Query
SELECT TABLE_NAME, COMMENT, COMMENT IS NULL AS COMMENT_IS_NULL_NOW
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'PARTS_T103';
Result1 row · 3 columns
TABLE_NAMECOMMENTCOMMENT_IS_NULL_NOW
PARTS_T103NULLTRUE

COMMENT is NULL and the check beside it says TRUE. Cleanly removed.

Finally, let's clean up after ourselves.

Cleanup
-- Put the schema back the way we found it.
DROP TABLE IF EXISTS PARTS_T103;
DROP TABLE IF EXISTS PARTS_REBUILT_T103;
DROP TABLE IF EXISTS WIDGETS_T103;
Query
SELECT CURRENT_DATABASE()                 AS THE_DATABASE,
       CURRENT_SCHEMA()                   AS THE_SCHEMA,
       (SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
        WHERE RIGHT(TABLE_NAME, 5) = '_T103') AS PROBE_TABLES_LEFT,
       (SELECT COUNT(*) FROM CUSTOMERS)   AS DEMO_DB_IS_READABLE;
Result1 row · 4 columns
THE_DATABASETHE_SCHEMAPROBE_TABLES_LEFTDEMO_DB_IS_READABLE
DEMO_DBPUBLIC010

No probe tables left, and the demo database still reads. Back where we started.

So which of these break things loudly?

All of them, as it turns out, and that is the good news of this post. Every mistake above was refused at compile time with a message that named the problem.

What we asked forWhat happened
ADD COLUMN with no defaultAccepted. Existing rows are NULL
ADD COLUMN with a DEFAULTAccepted, and it backfills the existing rows
ADD COLUMN ... NOT NULL on a populated tableRefused unless it has a non-null default
ADD COLUMN that already existsRefused. IF NOT EXISTS makes it a no-op
RENAME COLUMNAccepted. The data stays, the old name becomes an error
RENAME COLUMN onto a taken nameRefused
DROP COLUMNAccepted. Gone from the catalog immediately
DROP COLUMN that was never thereRefused. IF EXISTS makes it a no-op
UNDROP COLUMNNo such syntax
RENAME TOAccepted. Rows and comment travel, old name becomes an error
SWAP WITHAccepted. Rows, columns and comment exchange
SWAP WITH a table that does not existRefused

Nine of those statements were refused, and every refusal was a SQL compilation error. Nothing was accepted and then quietly did something else.

That is worth saying out loud after Level 8, where a great deal was accepted and almost nothing was enforced. ALTER TABLE tells you when you are wrong.

Key points

  • ADD COLUMN with no default leaves the existing rows NULL. ADD COLUMN with a DEFAULT backfills them. I predicted the opposite and the query name in the demo file still says so.
  • A NOT NULL column cannot be added to a table that has rows unless it has a non-null default, and then it backfills too.
  • RENAME COLUMN leaves the data exactly where it was. The old column name becomes invalid identifier at compile time.
  • DROP COLUMN removes the column from the catalog at once, and there is no UNDROP COLUMN. It is not a permissions problem or an expired window. The syntax does not exist.
  • ADD COLUMN IF NOT EXISTS and DROP COLUMN IF EXISTS turn the two mistakes that should be idempotent into clean no-ops.
  • RENAME TO carries the rows and the table comment to the new name, and turns the old name into an error.
  • SWAP WITH exchanges rows, columns and the comment in one statement, with no window where either name fails to resolve.
  • The comment followed the data, not the name. So did the column only one of the two tables had. The name is the only thing that stays put.
  • That is what makes SWAP WITH the safe way to publish a rebuilt table.
  • One query against INFORMATION_SCHEMA.COLUMNS gives you the column comments and the table comment together.
  • Every mistake in this post was caught at compile time. Nothing was accepted and then quietly did the wrong thing.
Next in this series

In the next post we will look at ALTER COLUMN: changing a column's type, its width, its nullability and its default. There are three different rules hiding behind one piece of syntax, and a DEFAULT you cannot change at all.

Every query and every result on this page was run on a Snowflake account (version 10.34.101). The WIDGETS_T103 data is made up for teaching. Nothing here is timed and no performance claims are made anywhere in this series. The statement reference is the Snowflake ALTER TABLE documentation.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.