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
- Let's add a plain column
- Now let's add a column with a DEFAULT
- Can we add a NOT NULL column to a table that has rows?
- What happens when we add a column that is already there?
- Now let's rename a column
- Let's drop a column, and then try to get it back
- Now let's rename the whole table
- Let's publish a rebuilt table with SWAP WITH
- So where did the comment go?
- One last thing: comments are the cheapest documentation there is
- So which of these break things loudly?
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 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.
SELECT ID, NAME, PRICE FROM WIDGETS_T103 ORDER BY ID;| ID | NAME | PRICE |
|---|---|---|
| 1 | bolt | 2.50 |
| 2 | nut | 1.25 |
| 3 | washer | 0.75 |
| 4 | bracket | 9.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.
ALTER TABLE WIDGETS_T103 ADD COLUMN CATEGORY VARCHAR(30);| 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?
SELECT ID, NAME, PRICE, CATEGORY,
CATEGORY IS NULL AS THE_NEW_COLUMN_IS_NULL
FROM WIDGETS_T103 ORDER BY ID;| ID | NAME | PRICE | CATEGORY | THE_NEW_COLUMN_IS_NULL |
|---|---|---|---|---|
| 1 | bolt | 2.50 | NULL | TRUE |
| 2 | nut | 1.25 | NULL | TRUE |
| 3 | washer | 0.75 | NULL | TRUE |
| 4 | bracket | 9.00 | NULL | TRUE |
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.
ALTER TABLE WIDGETS_T103 ADD COLUMN IN_STOCK BOOLEAN DEFAULT TRUE;| 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.
SELECT ID, NAME, IN_STOCK,
IN_STOCK IS NULL AS STILL_NULL
FROM WIDGETS_T103 ORDER BY ID;| ID | NAME | IN_STOCK | STILL_NULL |
|---|---|---|---|
| 1 | bolt | TRUE | FALSE |
| 2 | nut | TRUE | FALSE |
| 3 | washer | TRUE | FALSE |
| 4 | bracket | TRUE | FALSE |
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.
INSERT INTO WIDGETS_T103 (ID, NAME, PRICE) SELECT 5, 'screw', 0.40;| number of rows inserted |
|---|
| 1 |
SELECT ID, NAME, IN_STOCK FROM WIDGETS_T103 ORDER BY ID;| ID | NAME | IN_STOCK |
|---|---|---|
| 1 | bolt | TRUE |
| 2 | nut | TRUE |
| 3 | washer | TRUE |
| 4 | bracket | TRUE |
| 5 | screw | TRUE |
Five rows, and IN_STOCK is TRUE on every single one of them. The old rows and the new row agree completely.
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.
ALTER TABLE WIDGETS_T103 ADD COLUMN SUPPLIER VARCHAR(40) NOT NULL;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.
ALTER TABLE WIDGETS_T103 ADD COLUMN SUPPLIER VARCHAR(40) NOT NULL DEFAULT 'unknown';| status |
|---|
| Statement executed successfully. |
Accepted. And the rows that were already there?
SELECT ID, NAME, SUPPLIER FROM WIDGETS_T103 ORDER BY ID;| ID | NAME | SUPPLIER |
|---|---|---|
| 1 | bolt | unknown |
| 2 | nut | unknown |
| 3 | washer | unknown |
| 4 | bracket | unknown |
| 5 | screw | unknown |
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.
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;| COLUMN_NAME | DATA_TYPE | IS_NULLABLE | COLUMN_DEFAULT |
|---|---|---|---|
| ID | NUMBER | YES | NULL |
| NAME | TEXT | YES | NULL |
| PRICE | NUMBER | YES | NULL |
| CATEGORY | TEXT | YES | NULL |
| IN_STOCK | BOOLEAN | YES | TRUE |
| SUPPLIER | TEXT | NO | '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.
ALTER TABLE WIDGETS_T103 ADD COLUMN NAME VARCHAR(50);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.
ALTER TABLE WIDGETS_T103 ADD COLUMN IF NOT EXISTS NAME VARCHAR(50);| 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.
ALTER TABLE WIDGETS_T103 RENAME COLUMN PRICE TO UNIT_PRICE;| status |
|---|
| Statement executed successfully. |
Accepted. Now, did the data move?
SELECT ID, NAME, UNIT_PRICE FROM WIDGETS_T103 ORDER BY ID;| ID | NAME | UNIT_PRICE |
|---|---|---|
| 1 | bolt | 2.50 |
| 2 | nut | 1.25 |
| 3 | washer | 0.75 |
| 4 | bracket | 9.00 |
| 5 | screw | 0.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?
SELECT ID, PRICE FROM WIDGETS_T103;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.
ALTER TABLE WIDGETS_T103 RENAME COLUMN UNIT_PRICE TO NAME;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.
ALTER TABLE WIDGETS_T103 DROP COLUMN CATEGORY;| status |
|---|
| Statement executed successfully. |
Accepted. Is it still in the catalog?
SELECT COUNT(*) AS CATEGORY_STILL_LISTED
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'WIDGETS_T103'
AND COLUMN_NAME = 'CATEGORY';| CATEGORY_STILL_LISTED |
|---|
| 0 |
Zero. INFORMATION_SCHEMA.COLUMNS does not list it any more.
And a query that asks for it?
SELECT ID, CATEGORY FROM WIDGETS_T103;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.
ALTER TABLE WIDGETS_T103 UNDROP COLUMN CATEGORY;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.
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?
ALTER TABLE WIDGETS_T103 DROP COLUMN NOT_A_COLUMN_T103;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:
ALTER TABLE WIDGETS_T103 DROP COLUMN IF EXISTS NOT_A_COLUMN_T103;| 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.
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'WIDGETS_T103'
ORDER BY ORDINAL_POSITION;| COLUMN_NAME | DATA_TYPE | IS_NULLABLE |
|---|---|---|
| ID | NUMBER | YES |
| NAME | TEXT | YES |
| UNIT_PRICE | NUMBER | YES |
| IN_STOCK | BOOLEAN | YES |
| SUPPLIER | TEXT | NO |
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.
ALTER TABLE WIDGETS_T103 RENAME TO PARTS_T103;| status |
|---|
| Statement executed successfully. |
Accepted. Did the rows come with it?
SELECT COUNT(*) AS ROWS_UNDER_THE_NEW_NAME FROM PARTS_T103;| ROWS_UNDER_THE_NEW_NAME |
|---|
| 5 |
Five rows under the new name. Everything came along.
And the old name?
SELECT COUNT(*) FROM WIDGETS_T103;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?
SELECT TABLE_NAME, COMMENT, ROW_COUNT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'PARTS_T103';| TABLE_NAME | COMMENT | ROW_COUNT |
|---|---|---|
| PARTS_T103 | the original widgets table | 5 |
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 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.
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;| PARTS_ROWS | REBUILT_ROWS | PARTS_COLUMNS | REBUILT_COLUMNS |
|---|---|---|---|
| 5 | 2 | 5 | 4 |
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.
ALTER TABLE PARTS_T103 SWAP WITH PARTS_REBUILT_T103;| status |
|---|
| Statement executed successfully. |
Let's run exactly the same comparison again.
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;| PARTS_ROWS | REBUILT_ROWS | PARTS_COLUMNS | REBUILT_COLUMNS |
|---|---|---|---|
| 2 | 5 | 4 | 5 |
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:
SELECT ID, NAME, UNIT_PRICE FROM PARTS_T103 ORDER BY ID;| ID | NAME | UNIT_PRICE |
|---|---|---|
| 101 | rebuilt bolt | 2.75 |
| 102 | rebuilt nut | 1.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?
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;| TABLE_NAME | COMMENT | ROW_COUNT |
|---|---|---|
| PARTS_REBUILT_T103 | the original widgets table | 5 |
| PARTS_T103 | the rebuilt table, published by SWAP | 2 |
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.
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC'
AND COLUMN_NAME = 'REBUILT_ON'
ORDER BY TABLE_NAME;| TABLE_NAME | COLUMN_NAME |
|---|---|
| PARTS_T103 | REBUILT_ON |
One row. REBUILT_ON was created on the rebuilt table, and after the swap it sits on PARTS_T103.
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.
ALTER TABLE PARTS_T103 SWAP WITH NO_SUCH_TABLE_T103;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:
ALTER TABLE PARTS_T103 SET COMMENT = 'the live parts table';| status |
|---|
| Statement executed successfully. |
And a comment on a single column:
ALTER TABLE PARTS_T103 ALTER COLUMN NAME COMMENT 'the part name as printed';| status |
|---|
| Statement executed successfully. |
Now one query gives us both levels of documentation at once.
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;| COLUMN_NAME | COLUMN_COMMENT | TABLE_COMMENT |
|---|---|---|
| ID | NULL | the live parts table |
| NAME | the part name as printed | the live parts table |
| UNIT_PRICE | NULL | the live parts table |
| REBUILT_ON | NULL | the 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
ALTER TABLE PARTS_T103 UNSET COMMENT;| status |
|---|
| Statement executed successfully. |
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';| TABLE_NAME | COMMENT | COMMENT_IS_NULL_NOW |
|---|---|---|
| PARTS_T103 | NULL | TRUE |
COMMENT is NULL and the check beside it says TRUE. Cleanly removed.
Finally, let's clean up after ourselves.
-- 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;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;| THE_DATABASE | THE_SCHEMA | PROBE_TABLES_LEFT | DEMO_DB_IS_READABLE |
|---|---|---|---|
| DEMO_DB | PUBLIC | 0 | 10 |
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 for | What happened |
|---|---|
ADD COLUMN with no default | Accepted. Existing rows are NULL |
ADD COLUMN with a DEFAULT | Accepted, and it backfills the existing rows |
ADD COLUMN ... NOT NULL on a populated table | Refused unless it has a non-null default |
ADD COLUMN that already exists | Refused. IF NOT EXISTS makes it a no-op |
RENAME COLUMN | Accepted. The data stays, the old name becomes an error |
RENAME COLUMN onto a taken name | Refused |
DROP COLUMN | Accepted. Gone from the catalog immediately |
DROP COLUMN that was never there | Refused. IF EXISTS makes it a no-op |
UNDROP COLUMN | No such syntax |
RENAME TO | Accepted. Rows and comment travel, old name becomes an error |
SWAP WITH | Accepted. Rows, columns and comment exchange |
SWAP WITH a table that does not exist | Refused |
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 COLUMNwith no default leaves the existing rowsNULL.ADD COLUMNwith aDEFAULTbackfills them. I predicted the opposite and the query name in the demo file still says so.- A
NOT NULLcolumn cannot be added to a table that has rows unless it has a non-null default, and then it backfills too. RENAME COLUMNleaves the data exactly where it was. The old column name becomesinvalid identifierat compile time.DROP COLUMNremoves the column from the catalog at once, and there is noUNDROP COLUMN. It is not a permissions problem or an expired window. The syntax does not exist.ADD COLUMN IF NOT EXISTSandDROP COLUMN IF EXISTSturn the two mistakes that should be idempotent into clean no-ops.RENAME TOcarries the rows and the table comment to the new name, and turns the old name into an error.SWAP WITHexchanges 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 WITHthe safe way to publish a rebuilt table. - One query against
INFORMATION_SCHEMA.COLUMNSgives 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.
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.