In this post we are going to cover ALTER COLUMN in Snowflake.
Episode 103 changed which columns a table has. ALTER COLUMN changes what a column is: its type, its width, whether it will take a NULL, and what goes in it when nobody says.
It is a much smaller door than most people expect. The natural assumption is one rule — widening allowed, narrowing refused in case a row does not fit. It is three different rules, and Snowflake gives each of them its own error code.
We will start with the easy direction and work towards the one that will surprise you. Every query below was run on a real Snowflake account, and every result you see is the output it gave back.
Episode 104 of the Snowflake SQL: Zero to Hero series on TechBrothersIT.
- Let's create a table with room to spare
- Let's start with the easy direction
- Now let's narrow it again
- Can we narrow a NUMBER?
- What happens when we change the scale instead?
- Can a text column become a number?
- So here is what you actually have to write
- Now let's try NOT NULL, which behaves properly
- Now let's change a DEFAULT
- Can we at least drop a default?
- There is one exception, and it is a sequence
- One last thing: two columns in one statement
Let's create a table with room to spare
We need a table whose columns are all declared much wider than the data in them, because that is the situation in which narrowing feels obviously safe.
-- Sample data for this post.
-- Nothing in here is anywhere near its declared width, which matters later.
USE DATABASE DEMO_DB;
USE SCHEMA PUBLIC;
CREATE OR REPLACE TABLE PROFILE_T104 (
ID INT,
CODE VARCHAR(50),
QTY NUMBER(10,2),
DIGITS VARCHAR(20),
NOTE VARCHAR(100)
);
INSERT INTO PROFILE_T104 (ID, CODE, QTY, DIGITS, NOTE)
SELECT 1, 'AB12', 10.50, '100', 'first'
UNION ALL SELECT 2, 'CD34', 20.25, '250', 'second'
UNION ALL SELECT 3, 'EF56', 30.00, '375', 'third'
UNION ALL SELECT 4, 'GH78', 40.75, '4000', 'fourth';Let's see how much of each text column is actually being used.
SELECT MAX(LENGTH(CODE)) AS LONGEST_CODE,
MAX(LENGTH(DIGITS)) AS LONGEST_DIGITS,
MAX(LENGTH(NOTE)) AS LONGEST_NOTE
FROM PROFILE_T104;| LONGEST_CODE | LONGEST_DIGITS | LONGEST_NOTE |
|---|---|---|
| 4 | 4 | 6 |
Nothing is anywhere near its declared width. Now let's ask the catalog what the declarations actually say.
SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH,
NUMERIC_PRECISION, NUMERIC_SCALE, IS_NULLABLE, COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'PROFILE_T104'
ORDER BY ORDINAL_POSITION;| COLUMN_NAME | DATA_TYPE | CHARACTER_MAXIMUM_LENGTH | NUMERIC_PRECISION | NUMERIC_SCALE | IS_NULLABLE | COLUMN_DEFAULT |
|---|---|---|---|---|---|---|
| ID | NUMBER | NULL | 38 | 0 | YES | NULL |
| CODE | TEXT | 50 | NULL | NULL | YES | NULL |
| QTY | NUMBER | NULL | 10 | 2 | YES | NULL |
| DIGITS | TEXT | 20 | NULL | NULL | YES | NULL |
| NOTE | TEXT | 100 | NULL | NULL | YES | NULL |
Five columns. CODE is text with a maximum length of fifty, QTY is a number with precision ten and scale two, DIGITS is text up to twenty, and NOTE is text up to a hundred.
Have a look at ID while we are here. We declared it INT, and the catalog records precision thirty-eight with a scale of zero. In Snowflake INT is just another way of writing a number with that precision and that scale.
Let's start with the easy direction
Widening. Let's make CODE wider and give QTY more precision.
ALTER TABLE PROFILE_T104 ALTER COLUMN CODE SET DATA TYPE VARCHAR(80);| status |
|---|
| Statement executed successfully. |
ALTER TABLE PROFILE_T104 ALTER COLUMN QTY SET DATA TYPE NUMBER(20,2);| status |
|---|
| Statement executed successfully. |
Both accepted. Let's check the catalog agrees.
SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH,
NUMERIC_PRECISION, NUMERIC_SCALE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'PROFILE_T104'
AND COLUMN_NAME IN ('CODE', 'QTY')
ORDER BY COLUMN_NAME;| COLUMN_NAME | DATA_TYPE | CHARACTER_MAXIMUM_LENGTH | NUMERIC_PRECISION | NUMERIC_SCALE |
|---|---|---|---|---|
| CODE | TEXT | 80 | NULL | NULL |
| QTY | NUMBER | NULL | 20 | 2 |
CODE is eighty characters now and QTY has precision twenty with scale two. And the data?
SELECT ID, CODE, QTY FROM PROFILE_T104 ORDER BY ID;| ID | CODE | QTY |
|---|---|---|
| 1 | AB12 | 10.50 |
| 2 | CD34 | 20.25 |
| 3 | EF56 | 30.00 |
| 4 | GH78 | 40.75 |
Untouched. Four rows, same values. Widening is free, exactly as you would expect, because nothing that fitted before can stop fitting.
Now let's narrow it again
This is where the one-rule assumption breaks. Let's take CODE back down, not to the width it had, but to ten characters.
Remember the first grid on this page. The longest value in that column is four characters long. Ten is more than double what the data needs.
ALTER TABLE PROFILE_T104 ALTER COLUMN CODE SET DATA TYPE VARCHAR(10);040050 (22000): SQL compilation error: cannot change column CODE from type VARCHAR(80) to VARCHAR(10) because reducing the byte-length of a varchar is not supported.
Refused. And read the reason, because it is not the reason you would guess: reducing the byte-length of a varchar is not supported.
It did not look at the data at all. It refused on the declaration. There is no width small enough to be safe and no width large enough to be allowed, because the comparison it makes is between the old declaration and the new one.
This is worth sitting with for a second, because it kills a plan a lot of people have.
“I will declare the column as wide as Snowflake lets me now, and tighten it later once the data is clean” is a plan that does not exist here. The tightening step is not available, however clean the data gets.
Can we narrow a NUMBER?
Same request, different type. QTY is NUMBER(20,2) and the largest value in it is 40.75. Let's ask for precision eight.
ALTER TABLE PROFILE_T104 ALTER COLUMN QTY SET DATA TYPE NUMBER(8,2);| status |
|---|
| Statement executed successfully. |
Accepted. The same idea as the last section, and the opposite answer.
So numbers must be getting checked against the values. Let's prove it by asking for a precision the data cannot fit into. A NUMBER(3,2) holds at most 9.99, and we have 40.75 in there.
ALTER TABLE PROFILE_T104 ALTER COLUMN QTY SET DATA TYPE NUMBER(3,2);040051 (22000): SQL compilation error: cannot change column QTY from type NUMBER(8,2) to NUMBER(3,2) because some existing values cannot be represented using precision 3 instead of precision 8.
Refused, and read this message against the last one: some existing values cannot be represented using precision 3. That is a statement about the rows in the table.
And notice the error code. The text refusal was 040050. This one is 040051. Two different rules, and Snowflake numbers them separately.
What happens when we change the scale instead?
One more request, and this is the one with no downside at all. QTY is NUMBER(8,2). Let's ask for NUMBER(8,4) — more decimal places, nothing lost, every existing value still representable.
ALTER TABLE PROFILE_T104 ALTER COLUMN QTY SET DATA TYPE NUMBER(8,4);040052 (22000): SQL compilation error: cannot change column QTY from type NUMBER(8,2) to NUMBER(8,4) because changing the scale of a number is not supported.
Refused, with a third error code: 040052. Changing the scale of a number is not supported.
Scale does not move in either direction, even when the move is safe. So there are three rules here wearing one piece of syntax.
| What we asked for | What decided it | Code |
|---|---|---|
Widen a VARCHAR | Allowed | — |
Narrow a VARCHAR | Refused on the declaration. Never looks at the data | 040050 |
Widen a NUMBER's precision | Allowed | — |
Narrow a NUMBER's precision | Checked against the values. Allowed if they all fit | 040051 |
Change a NUMBER's scale | Refused either way. Frozen | 040052 |
Let's see where the two columns ended up.
SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH,
NUMERIC_PRECISION, NUMERIC_SCALE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'PROFILE_T104'
AND COLUMN_NAME IN ('CODE', 'QTY')
ORDER BY COLUMN_NAME;| COLUMN_NAME | DATA_TYPE | CHARACTER_MAXIMUM_LENGTH | NUMERIC_PRECISION | NUMERIC_SCALE |
|---|---|---|---|---|
| CODE | TEXT | 80 | NULL | NULL |
| QTY | NUMBER | NULL | 8 | 2 |
CODE is still eighty wide, because the narrowing never happened. QTY is precision eight, scale two, which is the one narrowing that worked.
SELECT ID, CODE, QTY FROM PROFILE_T104 ORDER BY ID;| ID | CODE | QTY |
|---|---|---|
| 1 | AB12 | 10.50 |
| 2 | CD34 | 20.25 |
| 3 | EF56 | 30.00 |
| 4 | GH78 | 40.75 |
And nothing was lost proving any of that.
Can a text column become a number?
Now the one people actually run into. The DIGITS column is text, and every value in it is a number that somebody typed as text.
Let's check that first, because if there is one bad value in there the rest of this section is pointless.
SELECT ID, DIGITS, DIGITS::NUMBER(10,0) AS AS_A_NUMBER FROM PROFILE_T104 ORDER BY ID;| ID | DIGITS | AS_A_NUMBER |
|---|---|---|
| 1 | 100 | 100 |
| 2 | 250 | 250 |
| 3 | 375 | 375 |
| 4 | 4000 | 4000 |
Every row casts. No errors, no NULL results. The data is as clean as data gets.
So let's change the column's type to match the data in it.
ALTER TABLE PROFILE_T104 ALTER COLUMN DIGITS SET DATA TYPE NUMBER(10,0);002108 (22000): SQL compilation error: cannot change column DIGITS from type VARCHAR(20) to NUMBER(10,0)
Refused. And the other direction, in case it is only one way:
ALTER TABLE PROFILE_T104 ALTER COLUMN QTY SET DATA TYPE VARCHAR(50);002108 (22000): SQL compilation error: cannot change column QTY from type NUMBER(8,2) to VARCHAR(50)
Refused as well, with the same code. Type families are closed. Text stays text and a number stays a number.
A cast working in a SELECT is not permission to change the column. They are two different questions, and the clean cast above answers the one Snowflake is not asking.
Which means fixing a type that was wrong on day one is never done in place.
So here is what you actually have to write
Three statements, and there is no shortcut. First, add a new column of the type you wanted.
ALTER TABLE PROFILE_T104 ADD COLUMN DIGITS_NUM NUMBER(10,0);| status |
|---|
| Statement executed successfully. |
Then fill it from the old one.
UPDATE PROFILE_T104 SET DIGITS_NUM = DIGITS::NUMBER(10,0);| number of rows updated | number of multi-joined rows updated |
|---|---|
| 4 | 0 |
Four rows updated, and nothing multi-joined. Then verify, always verify, before you throw anything away.
SELECT ID, DIGITS, DIGITS_NUM, SUM(DIGITS_NUM) OVER () AS TOTAL
FROM PROFILE_T104 ORDER BY ID;| ID | DIGITS | DIGITS_NUM | TOTAL |
|---|---|---|---|
| 1 | 100 | 100 | 4725 |
| 2 | 250 | 250 | 4725 |
| 3 | 375 | 375 | 4725 |
| 4 | 4000 | 4000 | 4725 |
The text and the number side by side on every row, and a total of 4725 so you can check it against the source. From here you drop the old column and rename the new one, which is Episode 103.
Add, update, verify, then rename. It is more typing than ALTER COLUMN ... SET DATA TYPE and it is the only thing that works.
Now let's try NOT NULL, which behaves properly
This section is the good news. Let's start by asking which columns will currently take a NULL.
SELECT COLUMN_NAME, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'PROFILE_T104'
ORDER BY ORDINAL_POSITION;| COLUMN_NAME | IS_NULLABLE |
|---|---|
| ID | YES |
| CODE | YES |
| QTY | YES |
| DIGITS | YES |
| NOTE | YES |
| DIGITS_NUM | YES |
Six columns, and every one of them says YES. Nullable is the default for a column nobody said anything about.
The ID column has no NULLs in it, so let's make that a promise.
ALTER TABLE PROFILE_T104 ALTER COLUMN ID SET NOT NULL;| status |
|---|
| Statement executed successfully. |
Accepted. And the promise is kept from here on:
INSERT INTO PROFILE_T104 (ID, CODE) SELECT NULL, 'XX99';100072 (22000): DML operation to table PROFILE_T104 failed on column ID with error: NULL result in a non-nullable column
NULL result in a non-nullable column. Worth noticing that this one is a different kind of refusal from everything else on this page. Code 100072 is a DML failure at write time, not a compilation error. The statement was fine. The row was not.
Now let's ask for the same promise on a dirty column
Let's deliberately put a NULL into the NOTE column first.
UPDATE PROFILE_T104 SET NOTE = NULL WHERE ID = 3;| number of rows updated | number of multi-joined rows updated |
|---|---|
| 1 | 0 |
SELECT COUNT(*) AS ROWS_TOTAL,
COUNT(NOTE) AS NOTES_PRESENT,
COUNT(*) - COUNT(NOTE) AS NOTES_NULL
FROM PROFILE_T104;| ROWS_TOTAL | NOTES_PRESENT | NOTES_NULL |
|---|---|---|
| 4 | 3 | 1 |
Four rows, three notes present, one NULL. Now the same request we just made of ID.
ALTER TABLE PROFILE_T104 ALTER COLUMN NOTE SET NOT NULL;001471 (42601): SQL compilation error: Column 'NOTE' contains null values. Not null constraint cannot be added.
Refused, and the message names the column: Column 'NOTE' contains null values. So let's fix the data.
UPDATE PROFILE_T104 SET NOTE = 'backfilled' WHERE NOTE IS NULL;| number of rows updated | number of multi-joined rows updated |
|---|---|
| 1 | 0 |
And now run the identical statement again. Nothing about the statement has changed.
ALTER TABLE PROFILE_T104 ALTER COLUMN NOTE SET NOT NULL;| status |
|---|
| Statement executed successfully. |
Accepted. That is exactly right, and it is the only thing on this page that works the way you would guess. The rule is about the data, the data moved, and the answer moved with it.
Taking the promise off again never needs permission from anything.
ALTER TABLE PROFILE_T104 ALTER COLUMN NOTE DROP NOT NULL;| status |
|---|
| Statement executed successfully. |
SELECT COLUMN_NAME, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'PROFILE_T104'
ORDER BY ORDINAL_POSITION;| COLUMN_NAME | IS_NULLABLE |
|---|---|
| ID | NO |
| CODE | YES |
| QTY | YES |
| DIGITS | YES |
| NOTE | YES |
| DIGITS_NUM | YES |
Only ID says NO. Everything else is back to YES.
Now let's change a DEFAULT
This is the one that will surprise you, so we will take it slowly and on a table of its own.
-- A second table, just for defaults.
CREATE OR REPLACE TABLE DEFAULTS_T104 (
ID INT,
STATUS VARCHAR(20) DEFAULT 'pending'
);
-- Two rows that say nothing about STATUS, so the default decides.
INSERT INTO DEFAULTS_T104 (ID) SELECT 1;
INSERT INTO DEFAULTS_T104 (ID) SELECT 2;SELECT ID, STATUS FROM DEFAULTS_T104 ORDER BY ID;| ID | STATUS |
|---|---|
| 1 | pending |
| 2 | pending |
Two rows, both pending. The default did its job and nobody had to mention the column.
Now let's change the default to queued. This is the obvious statement and it is the one everybody writes.
ALTER TABLE DEFAULTS_T104 ALTER COLUMN STATUS SET DEFAULT 'queued';000002 (0A000): Unsupported feature 'Alter Column Set Default'.
Unsupported feature 'Alter Column Set Default'.
Read that again. It is not a permissions problem, it is not a syntax error, and it is not Snowflake telling us the change would not be retroactive. The feature does not exist.
Let's confirm nothing happened.
SELECT COLUMN_NAME, COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'DEFAULTS_T104'
ORDER BY ORDINAL_POSITION;| COLUMN_NAME | COLUMN_DEFAULT |
|---|---|
| ID | NULL |
| STATUS | 'pending' |
The catalog still reads 'pending', exactly as CREATE TABLE left it. And a row written now:
INSERT INTO DEFAULTS_T104 (ID) SELECT 3;| number of rows inserted |
|---|
| 1 |
SELECT ID, STATUS FROM DEFAULTS_T104 ORDER BY ID;| ID | STATUS |
|---|---|
| 1 | pending |
| 2 | pending |
| 3 | pending |
Three rows, all pending. You cannot change a literal default. Ever. The only way to a different one is to rebuild the table.
Can we at least drop a default?
Yes, and this is where it gets properly awkward.
ALTER TABLE DEFAULTS_T104 ALTER COLUMN STATUS DROP DEFAULT;| status |
|---|
| Statement executed successfully. |
Accepted. Let's check the catalog.
SELECT COLUMN_NAME, COLUMN_DEFAULT,
COLUMN_DEFAULT IS NULL AS NO_DEFAULT_NOW
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'DEFAULTS_T104'
ORDER BY ORDINAL_POSITION;| COLUMN_NAME | COLUMN_DEFAULT | NO_DEFAULT_NOW |
|---|---|---|
| ID | NULL | TRUE |
| STATUS | NULL | TRUE |
COLUMN_DEFAULT is NULL and NO_DEFAULT_NOW says TRUE. Cleanly removed. So what does a row written after that get?
INSERT INTO DEFAULTS_T104 (ID) SELECT 4;| number of rows inserted |
|---|
| 1 |
SELECT ID, STATUS, STATUS IS NULL AS NO_DEFAULT_FIRED
FROM DEFAULTS_T104 ORDER BY ID;| ID | STATUS | NO_DEFAULT_FIRED |
|---|---|---|
| 1 | pending | FALSE |
| 2 | pending | FALSE |
| 3 | pending | FALSE |
| 4 | NULL | TRUE |
Four rows and two eras in one column. Three rows say pending because a default was there when they were written. The fourth is a real NULL, and NO_DEFAULT_FIRED marks it TRUE.
Now let's put the default back, having dropped it.
ALTER TABLE DEFAULTS_T104 ALTER COLUMN STATUS SET DEFAULT 'queued';000002 (0A000): Unsupported feature 'Alter Column Set Default'.
The same unsupported-feature error. So the door only opens one way. You can take a literal default off a column and you can never put one on.
There is one exception, and it is a sequence
A default that is a sequence rather than a literal behaves completely differently, and it is worth seeing because it comes with a surprise of its own.
-- A sequence, and a table whose default is that sequence.
CREATE OR REPLACE SEQUENCE TICKET_SEQ_T104 START = 500 INCREMENT = 1;
CREATE OR REPLACE TABLE TICKETS_T104 (
TICKET_ID INT DEFAULT TICKET_SEQ_T104.NEXTVAL,
SUBJECT VARCHAR(40)
);
INSERT INTO TICKETS_T104 (SUBJECT) SELECT 'first ticket';
INSERT INTO TICKETS_T104 (SUBJECT) SELECT 'second ticket';The sequence starts at five hundred and increments by one. Two rows have been written and neither mentioned TICKET_ID. So what did they get?
SELECT TICKET_ID, SUBJECT FROM TICKETS_T104 ORDER BY TICKET_ID;| TICKET_ID | SUBJECT |
|---|---|
| 500 | first ticket |
| 600 | second ticket |
Five hundred, and then six hundred.
We declared INCREMENT = 1 and the second row is a hundred higher than the first. That is not a bug and nothing was deleted.
Sequences hand out values in blocks, and a block that is not used up is not handed out again. Contiguity was never promised. Episode 100 found gaps in a sequence after a DELETE. This is a gap with nothing deleted at all.
Now the question this section exists for. Let's make a second sequence and point the column at it.
CREATE OR REPLACE SEQUENCE TICKET_SEQ2_T104 START = 9000 INCREMENT = 1;| status |
|---|
| Sequence TICKET_SEQ2_T104 successfully created. |
ALTER TABLE TICKETS_T104 ALTER COLUMN TICKET_ID SET DEFAULT TICKET_SEQ2_T104.NEXTVAL;| status |
|---|
| Statement executed successfully. |
Accepted. The same statement shape that was refused for a literal is accepted for a sequence.
INSERT INTO TICKETS_T104 (SUBJECT) SELECT 'third ticket';| number of rows inserted |
|---|
| 1 |
SELECT TICKET_ID, SUBJECT FROM TICKETS_T104 ORDER BY TICKET_ID;| TICKET_ID | SUBJECT |
|---|---|
| 500 | first ticket |
| 600 | second ticket |
| 9000 | third ticket |
The third row came from the new sequence. Let's ask the catalog what it has recorded.
SELECT COLUMN_NAME, COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'TICKETS_T104'
ORDER BY ORDINAL_POSITION;| COLUMN_NAME | COLUMN_DEFAULT |
|---|---|
| TICKET_ID | DEMO_DB.PUBLIC.TICKET_SEQ2_T104.NEXTVAL |
| SUBJECT | NULL |
The default is stored as a fully qualified reference to the second sequence, not as a value. That is the difference in one line. A literal default is part of the column. A sequence default is a pointer, and a pointer can be moved.
One last thing: two columns in one statement
You do not need a statement per column. Let's widen two of them at once.
ALTER TABLE PROFILE_T104 ALTER COLUMN CODE SET DATA TYPE VARCHAR(120),
COLUMN NOTE SET DATA TYPE VARCHAR(200);| status |
|---|
| Statement executed successfully. |
SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'PROFILE_T104'
AND COLUMN_NAME IN ('CODE', 'NOTE')
ORDER BY COLUMN_NAME;| COLUMN_NAME | DATA_TYPE | CHARACTER_MAXIMUM_LENGTH |
|---|---|---|
| CODE | TEXT | 120 |
| NOTE | TEXT | 200 |
CODE is a hundred and twenty wide and NOTE is two hundred. Both moved in the one statement.
And a column that is not there:
ALTER TABLE PROFILE_T104 ALTER COLUMN NOT_A_COLUMN_T104 SET DATA TYPE VARCHAR(10);000904 (42000): SQL compilation error: error line 1 at position 38 invalid identifier 'NOT_A_COLUMN_T104'
invalid identifier, at compile time, like most of this post.
Finally, let's clean up after ourselves. The sequences need dropping as well as the tables.
-- Put the schema back the way we found it.
DROP TABLE IF EXISTS PROFILE_T104;
DROP TABLE IF EXISTS DEFAULTS_T104;
DROP TABLE IF EXISTS TICKETS_T104;
DROP SEQUENCE IF EXISTS TICKET_SEQ_T104;
DROP SEQUENCE IF EXISTS TICKET_SEQ2_T104;SELECT CURRENT_DATABASE() AS THE_DATABASE,
CURRENT_SCHEMA() AS THE_SCHEMA,
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
WHERE RIGHT(TABLE_NAME, 5) = '_T104') 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.
Key points
- Widening a
VARCHARor aNUMBER's precision is free. - Narrowing a
VARCHARis refused on the declaration, with code040050. It never looks at the data, so there is no “tighten it later”. - Narrowing a
NUMBER's precision is allowed when every value fits, and refused with code040051when one does not. - A
NUMBER's scale is frozen in both directions, with code040052, even when widening it loses nothing. - Changing a column's type family is refused even when every value casts cleanly in a
SELECT. A working cast is not permission. - To fix a wrong type you add a column, update it from the old one, and verify. There is no in-place version of that.
SET NOT NULLdepends on the data and the message names the column holding theNULL. Clean the column and the identical statement is accepted.DROP NOT NULLnever depends on anything.- A literal
DEFAULTcannot be changed.ALTER COLUMN SET DEFAULTis an unsupported feature, not a permissions problem. - You can
DROP DEFAULT, and after that you can never put one back. It is one-way. - A sequence default is the one exception. Repointing a column at a different sequence is accepted, because the catalog stores a reference rather than a value.
- A sequence declared
INCREMENT = 1handed out five hundred and then six hundred. Sequences allocate in blocks and contiguity was never promised.
In the next post we will look at constraints — PRIMARY KEY, UNIQUE, FOREIGN KEY and NOT NULL — and the most important warning in Level 9.
Every query and every result on this page was run on a Snowflake account (version 10.34.101). The PROFILE_T104 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 ... ALTER COLUMN documentation.



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