Snowflake SQL Tutorial 100 - CREATE TABLE - A DEFAULT Is Not a Fallback for NULL

Snowflake SQL · Episode 100

In this post we are going to cover CREATE TABLE in Snowflake: NOT NULL, DEFAULT, AUTOINCREMENT and column comments.

CREATE TABLE is the first statement anybody learns and the one people stop thinking about. You name the columns, you give them types, and you move on. The options you can hang off those columns are where the surprises live.

We will start with the plainest table there is and add one option at a time. The middle of the post is the part worth reading twice: a DEFAULT is not a fallback for NULL. Two inserts that any reader would call the same insert put different things in the table, and the difference is whether a column name appears in a list.

Every query below was run on a real Snowflake account, and every result you see is the output it gave back.

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

Let's create some sample data

We are going to build several small tables in this post, each one asking CREATE TABLE for a bit more than the last. Here they are in one piece. Copy this into a worksheet and run it, and then we will take them one at a time.

Sample data
-- Sample data for this post.
-- Four tables, each one asking CREATE TABLE for a bit more than the last.
USE DATABASE DEMO_DB;
USE SCHEMA PUBLIC;

-- 1. the plainest table there is
CREATE OR REPLACE TABLE PLAIN_T100 (
    ID      INT,
    NAME    VARCHAR(50),
    AMOUNT  NUMBER(12,2)
);

-- 2. two columns that are not allowed to be missing
CREATE OR REPLACE TABLE REQUIRED_T100 (
    ID       INT         NOT NULL,
    NAME     VARCHAR(50) NOT NULL,
    OPTNOTE  VARCHAR(50)
);

-- 3. four defaults, one of them on a NOT NULL column
CREATE OR REPLACE TABLE DEFAULTED_T100 (
    ID               INT,
    STATUS           VARCHAR(20)  DEFAULT 'PENDING',
    QTY              INT          DEFAULT 1,
    CREATED          TIMESTAMP_NTZ DEFAULT CURRENT_TIMESTAMP(),
    REQUIRED_STATUS  VARCHAR(20)  NOT NULL DEFAULT 'NEW'
);

-- 4. two generated columns, spelled two different ways
CREATE OR REPLACE TABLE AUTO_T100 (
    A_AUTO       INT AUTOINCREMENT,
    AN_IDENTITY  INT IDENTITY,
    NOTE         VARCHAR(30)
);

Let's start with the first one, the plainest table there is. An id, a name and an amount, and nothing else asked for.

Query
CREATE OR REPLACE TABLE PLAIN_T100 (
    ID     INT,
    NAME   VARCHAR(50),
    AMOUNT NUMBER(12,2)
);
Result1 row · 1 column
status
Table PLAIN_T100 successfully created.

Now let's ask the catalog what it actually built for us.

Query
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT,
       NUMERIC_PRECISION, NUMERIC_SCALE, CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'PLAIN_T100'
ORDER BY ORDINAL_POSITION;
Result3 rows · 7 columns · scroll →
COLUMN_NAMEDATA_TYPEIS_NULLABLECOLUMN_DEFAULTNUMERIC_PRECISIONNUMERIC_SCALECHARACTER_MAXIMUM_LENGTH
IDNUMBERYESNULL380NULL
NAMETEXTYESNULLNULLNULL50
AMOUNTNUMBERYESNULL122NULL

INT became NUMBER with a precision of 38 and a scale of 0. VARCHAR(50) became TEXT with a maximum length of 50. NUMBER(12,2) stayed as we asked for it, because we asked precisely.

Now look at the IS_NULLABLE and COLUMN_DEFAULT columns. Every row says YES, and every default is NULL. Let's count that rather than read it off.

Query
SELECT COUNT(*)                          AS COLUMNS_TOTAL,
       COUNT_IF(IS_NULLABLE = 'YES')     AS NULLABLE_COLUMNS,
       COUNT_IF(COLUMN_DEFAULT IS NULL)  AS COLUMNS_WITH_NO_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'PLAIN_T100';
Result1 row · 3 columns
COLUMNS_TOTALNULLABLE_COLUMNSCOLUMNS_WITH_NO_DEFAULT
333

Three columns, three of them nullable, three of them with no default. So the state of a column when you ask for nothing is anything is allowed, including nothing.

So what goes in when we leave the columns out?

Let's insert a row that mentions only the id, and see what Snowflake does with the other two columns.

Query
INSERT INTO PLAIN_T100 (ID) SELECT 1;
Result1 row · 1 column
number of rows inserted
1

One row inserted, no complaint. Here is what is in it.

Query
SELECT ID, NAME, AMOUNT,
       NAME IS NULL   AS NAME_IS_NULL,
       AMOUNT IS NULL AS AMOUNT_IS_NULL
FROM PLAIN_T100;
Result1 row · 5 columns
IDNAMEAMOUNTNAME_IS_NULLAMOUNT_IS_NULL
1NULLNULLTRUETRUE

NAME and AMOUNT are both NULL, and the two IS NULL checks beside them say TRUE so there is no doubt about it. That row is now in the table, two thirds empty, and nothing objected.

So if you want anything other than that, you have to ask for it. The rest of this post is the asking.

Now let's make two columns required with NOT NULL

NOT NULL goes after the type. Our second table has two required columns and one optional one.

Query
CREATE OR REPLACE TABLE REQUIRED_T100 (
    ID      INT NOT NULL,
    NAME    VARCHAR(50) NOT NULL,
    OPTNOTE VARCHAR(50)
);
Result1 row · 1 column
status
Table REQUIRED_T100 successfully created.

And the catalog records it.

Query
SELECT COLUMN_NAME, IS_NULLABLE, COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'REQUIRED_T100'
ORDER BY ORDINAL_POSITION;
Result3 rows · 3 columns
COLUMN_NAMEIS_NULLABLECOLUMN_DEFAULT
IDNONULL
NAMENONULL
OPTNOTEYESNULL

NO, NO, YES. The first two columns are not allowed to be empty and the third one is. Notice that none of them has a default — that matters in a moment.

A row with both required columns filled in goes in without any trouble.

Query
INSERT INTO REQUIRED_T100 (ID, NAME) SELECT 1, 'fine';
Result1 row · 1 column
number of rows inserted
1

Is NOT NULL actually enforced?

Worth asking, because Level 8 spent a lot of time finding things that are not. Primary keys, unique constraints, foreign keys, array element types, object keys — Snowflake records all of those and enforces none of them.

So let's pass an explicit NULL into a NOT NULL column and see what happens.

Query
INSERT INTO REQUIRED_T100 (ID, NAME) SELECT 2, NULL;
Query failed — Snowflake returned an error
100072 (22000): DML operation to table REQUIRED_T100 failed on column NAME with error: NULL result in a non-nullable column

Refused. NULL result in a non-nullable column. Now let's try the other way of not supplying a value: omit the column from the insert entirely, so we never mention it at all.

Query
INSERT INTO REQUIRED_T100 (ID) SELECT 3;
Query failed — Snowflake returned an error
100072 (22000): DML operation to table REQUIRED_T100 failed on column NAME with error: NULL result in a non-nullable column

The same error, on the same column. So for a NOT NULL column with no default, passing NULL and leaving the column out are identical. Both fail, and they fail the same way.

Let's count what actually got into the table.

Query
SELECT COUNT(*)              AS ROWS_THAT_GOT_IN,
       COUNT_IF(NAME IS NULL) AS NULLS_THAT_GOT_THROUGH
FROM REQUIRED_T100;
Result1 row · 2 columns
ROWS_THAT_GOT_INNULLS_THAT_GOT_THROUGH
10

One row in, and no nulls through. NOT NULL is the constraint that genuinely refuses, and it always has been.

Remember this

Hold on to the pair we just saw — omitting a column and passing NULL into it behaving identically. It stops being true as soon as there is a default on the column, which is where the next few sections are going, and the reason it stops being true is the whole point of this post.

Now let's add some defaults

Our third table has four defaults on it. A status that defaults to PENDING, a quantity that defaults to one, a created timestamp that defaults to CURRENT_TIMESTAMP(), and a required status that is NOT NULL and has a default of NEW.

That last column is the one to watch. Keep it in mind.

Query
CREATE OR REPLACE TABLE DEFAULTED_T100 (
    ID        INT,
    STATUS    VARCHAR(20) DEFAULT 'PENDING',
    QTY       INT         DEFAULT 1,
    CREATED   TIMESTAMP_NTZ DEFAULT CURRENT_TIMESTAMP(),
    REQUIRED_STATUS VARCHAR(20) NOT NULL DEFAULT 'NEW'
);
Result1 row · 1 column
status
Table DEFAULTED_T100 successfully created.

Let's see how the catalog stores a default.

Query
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'DEFAULTED_T100'
ORDER BY ORDINAL_POSITION;
Result5 rows · 4 columns
COLUMN_NAMEDATA_TYPEIS_NULLABLECOLUMN_DEFAULT
IDNUMBERYESNULL
STATUSTEXTYES'PENDING'
QTYNUMBERYES1
CREATEDTIMESTAMP_NTZYESCURRENT_TIMESTAMP()
REQUIRED_STATUSTEXTNO'NEW'

The defaults are kept as text, exactly as we wrote them. 'PENDING' is recorded with its quotes still on. CURRENT_TIMESTAMP() is recorded with its brackets. Snowflake is storing the expression, not a value it worked out at create time.

And REQUIRED_STATUS is the only row in that grid with IS_NULLABLE set to NO, with a default sitting right beside it.

Let's omit every column that has a default

Here is the easy case. An insert that mentions the id and nothing else.

Query
INSERT INTO DEFAULTED_T100 (ID) SELECT 1;
Result1 row · 1 column
number of rows inserted
1
Query
SELECT ID, STATUS, QTY, REQUIRED_STATUS, CREATED IS NOT NULL AS CREATED_WAS_SET
FROM DEFAULTED_T100
WHERE ID = 1;
Result1 row · 5 columns
IDSTATUSQTYREQUIRED_STATUSCREATED_WAS_SET
1PENDING1NEWTRUE

Every default fired. STATUS is PENDING, QTY is one, REQUIRED_STATUS is NEW, and CREATED was set to something rather than left empty.

That is exactly what a default is for, and it is the behaviour everybody expects. Now the other one.

Now let's pass NULL into those same columns instead

Read this insert carefully. It names STATUS and QTY, and it passes NULL into both of them. Both of those columns have a default.

Query
INSERT INTO DEFAULTED_T100 (ID, STATUS, QTY) SELECT 2, NULL, NULL;
Result1 row · 1 column
number of rows inserted
1

It ran. One row inserted. And here is what it put in the table.

Query
SELECT ID, STATUS, QTY,
       STATUS IS NULL AS STATUS_IS_NULL,
       QTY IS NULL    AS QTY_IS_NULL
FROM DEFAULTED_T100
WHERE ID = 2;
Result1 row · 5 columns
IDSTATUSQTYSTATUS_IS_NULLQTY_IS_NULL
2NULLNULLTRUETRUE

STATUS is NULL. QTY is NULL. Not PENDING. Not one. The defaults did not fire at all.

Let's put the two rows beside each other.

Query
SELECT ID, STATUS, QTY, REQUIRED_STATUS
FROM DEFAULTED_T100
ORDER BY ID;
Result2 rows · 4 columns
IDSTATUSQTYREQUIRED_STATUS
1PENDING1NEW
2NULLNULLNEW

Same table, two inserts, and a reader skimming the script would call them the same insert. The only difference between them is whether a column name appears in a list.

A DEFAULT is not a fallback for NULL

A DEFAULT fires when the column is omitted from the insert. It does not fire when the column is present and the value is NULL. Those are two different things, and NULL is a value you supplied, not a value you failed to supply. So never write column = NULL meaning “use the default”. It does not mean that anywhere.

Notice that REQUIRED_STATUS is NEW on both rows. We never mentioned it in either insert, so its default fired both times.

What if the column is NOT NULL and has a default?

This is the version that will cost you a morning. REQUIRED_STATUS is NOT NULL and it has a default of NEW. Let's pass an explicit NULL into it.

There is a default sitting right there, ready to be used. So this should be fine.

Query
INSERT INTO DEFAULTED_T100 (ID, REQUIRED_STATUS) SELECT 3, NULL;
Query failed — Snowflake returned an error
100072 (22000): DML operation to table DEFAULTED_T100 failed on column REQUIRED_STATUS with error: NULL result in a non-nullable column

It failed. NULL result in a non-nullable column. The default is there, it is not used, and the row is refused.

Omit that same column and the same table takes the row happily, which is what both of the rows already in it did. Same table, same column, same intent, opposite outcome.

Let's count what the table holds after all that.

Query
SELECT COUNT(*)                               AS ROWS_IN_TABLE,
       COUNT_IF(STATUS IS NULL)               AS STATUS_NULLS,
       COUNT_IF(STATUS = 'PENDING')           AS STATUS_DEFAULTED,
       COUNT_IF(REQUIRED_STATUS = 'NEW')      AS REQUIRED_DEFAULTED
FROM DEFAULTED_T100;
Result1 row · 4 columns
ROWS_IN_TABLESTATUS_NULLSSTATUS_DEFAULTEDREQUIRED_DEFAULTED
2112

Two rows. One of them has a NULL status and one of them has the defaulted PENDING. And both of them got the NEW default on the required column, because neither insert mentioned it.

So the pair we were told to remember — omitting and passing NULL behaving identically — only held while there was no default. Add a default and the two stop being the same thing entirely.

Is a CURRENT_TIMESTAMP() default frozen or per row?

This one matters and the answer is good news. We saw that the catalog stores CURRENT_TIMESTAMP() as text. The question is when that text gets evaluated: once, at CREATE TABLE time, or every time a row goes in.

Let's add another row, a moment after the first two.

Query
INSERT INTO DEFAULTED_T100 (ID) SELECT 4;
Result1 row · 1 column
number of rows inserted
1
Query
SELECT COUNT(*)                        AS ROWS_WITH_A_TIMESTAMP,
       COUNT(DISTINCT CREATED)         AS DISTINCT_TIMESTAMPS,
       COUNT(DISTINCT CREATED) > 1     AS IT_IS_EVALUATED_PER_ROW
FROM DEFAULTED_T100
WHERE CREATED IS NOT NULL;
Result1 row · 3 columns
ROWS_WITH_A_TIMESTAMPDISTINCT_TIMESTAMPSIT_IS_EVALUATED_PER_ROW
33TRUE

Three rows carry a timestamp and all three timestamps are different. It is evaluated per row, not frozen when the table was created.

A frozen create-time default would have been a serious trap, because every row in the table would claim to have been written at the same instant. It is not frozen, so CREATED TIMESTAMP_NTZ DEFAULT CURRENT_TIMESTAMP() does the thing you wanted it to do.

Now let's try AUTOINCREMENT and IDENTITY

Snowflake accepts two spellings for a generated number column, AUTOINCREMENT and IDENTITY. Our fourth table has one of each so we can compare them.

Query
CREATE OR REPLACE TABLE AUTO_T100 (
    A_AUTO     INT AUTOINCREMENT,
    AN_IDENTITY INT IDENTITY,
    NOTE       VARCHAR(30)
);
Result1 row · 1 column
status
Table AUTO_T100 successfully created.

Let's see how the catalog describes them.

Query
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT,
       IS_IDENTITY, IDENTITY_START, IDENTITY_INCREMENT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'AUTO_T100'
ORDER BY ORDINAL_POSITION;
Result3 rows · 7 columns · scroll →
COLUMN_NAMEDATA_TYPEIS_NULLABLECOLUMN_DEFAULTIS_IDENTITYIDENTITY_STARTIDENTITY_INCREMENT
A_AUTONUMBERYESNULLYES11
AN_IDENTITYNUMBERYESNULLYES11
NOTETEXTYESNULLNONULLNULL

Both columns report IS_IDENTITY as YES, both start at one and both increment by one. The plain NOTE column reports NO and has no start or increment at all. As far as the catalog is concerned the two spellings describe the same thing.

Now let's insert three rows and mention only the note.

Query
INSERT INTO AUTO_T100 (NOTE)
SELECT 'first' UNION ALL SELECT 'second' UNION ALL SELECT 'third';
Result1 row · 1 column
number of rows inserted
3
Query
SELECT A_AUTO, AN_IDENTITY, NOTE FROM AUTO_T100 ORDER BY A_AUTO;
Result3 rows · 3 columns
A_AUTOAN_IDENTITYNOTE
11first
22second
33third

One, two, three in both generated columns, exactly as promised, and we never supplied a number.

Can we write to an AUTOINCREMENT column by hand?

It generates a value when we leave the column out. The question is whether it stops us putting our own value in. Let's name the autoincrement column in an insert and give it a number nothing would have reached on its own.

Query
INSERT INTO AUTO_T100 (A_AUTO, NOTE) SELECT 500, 'written by hand';
Result1 row · 1 column
number of rows inserted
1

One row inserted. It let us. Let's look at the table.

Query
SELECT A_AUTO, AN_IDENTITY, NOTE FROM AUTO_T100 ORDER BY A_AUTO;
Result4 rows · 3 columns
A_AUTOAN_IDENTITYNOTE
11first
22second
33third
500101written by hand

Our number is in there. So the column is not protected: it generates a value if you omit it, and it does not stop you supplying one. Those are two different promises and only the first one is made.

Now look at the middle column on that last row. AN_IDENTITY is the one column in this insert we did not mention, so Snowflake generated it — and the number it generated is not the one that comes after three.

The identity column jumped, and nothing warned us

The three rows before it hold 1, 2 and 3 in AN_IDENTITY. The hand-written row holds 101. Whatever Snowflake's generator was holding for that column, it was not four. And this is not a knock-on effect of the value we supplied to the other column: in the next section A_AUTO carries on at four and five for the rows that go in after this one. So the two generated columns do not share a counter, and the grid this episode uses to show “it let me” also shows an identity column stepping from 3 to 101 in one row. Nothing captured here says why, so nothing here will guess.

Which is the practical lesson twice over. A generated number is a unique-ish handle. It is not a counter, not a position, and not something you can predict.

Is an AUTOINCREMENT column gapless?

Let's delete a row from the middle of the table and then insert two more, and see what numbers the new rows get.

Query
DELETE FROM AUTO_T100 WHERE NOTE = 'second';
Result1 row · 1 column
number of rows deleted
1
Query
INSERT INTO AUTO_T100 (NOTE) SELECT 'fourth' UNION ALL SELECT 'fifth';
Result1 row · 1 column
number of rows inserted
2
Query
SELECT A_AUTO, NOTE FROM AUTO_T100 ORDER BY A_AUTO;
Result5 rows · 2 columns
A_AUTONOTE
1first
3third
4fourth
5fifth
500written by hand

The number we deleted is gone and it is not coming back. Nothing went back to fill the hole.

And look at which numbers the two new rows took. They are the ones that come after 3, not the ones that come after 500. The value we wrote in by hand did not move this column's counter along at all, which is the other half of what we found in the last section.

Let's put some arithmetic on that.

Query
SELECT COUNT(*)                         AS ROWS_IN_TABLE,
       MIN(A_AUTO)                      AS LOWEST_ID,
       MAX(A_AUTO)                      AS HIGHEST_ID,
       MAX(A_AUTO) - MIN(A_AUTO) + 1    AS IDS_SPANNED,
       MAX(A_AUTO) - MIN(A_AUTO) + 1 = COUNT(*) AS NO_GAPS
FROM AUTO_T100;
Result1 row · 5 columns
ROWS_IN_TABLELOWEST_IDHIGHEST_IDIDS_SPANNEDNO_GAPS
51500500FALSE

Five rows in the table. The lowest id is 1 and the highest is 500, so the ids span 500 values. NO_GAPS comes back FALSE.

Three things not to assume

So never count rows by looking at the highest id, never assume the ids are dense, and never assume no human has written one by hand. This table fails all three tests at once, and every one of those failures came from a statement Snowflake accepted without comment.

Let's set our own START and INCREMENT

The defaults are start at one, step by one. Both are options, so let's start somewhere else and step by something else.

Query
CREATE OR REPLACE TABLE AUTO_STEPPED_T100 (
    ID   INT AUTOINCREMENT START 100 INCREMENT 10,
    NOTE VARCHAR(30)
);
Result1 row · 1 column
status
Table AUTO_STEPPED_T100 successfully created.
Query
INSERT INTO AUTO_STEPPED_T100 (NOTE)
SELECT 'a' UNION ALL SELECT 'b' UNION ALL SELECT 'c' UNION ALL SELECT 'd';
Result1 row · 1 column
number of rows inserted
4
Query
SELECT ID, NOTE FROM AUTO_STEPPED_T100 ORDER BY ID;
Result4 rows · 2 columns
IDNOTE
100a
110b
120c
130d

100, 110, 120, 130. Start and increment do exactly what they say, with none of the surprises from the last two sections.

One last thing: let's document the table as we create it

COMMENT can go on a column and on the table itself, in the same statement that creates them.

Query
CREATE OR REPLACE TABLE COMMENTED_T100 (
    ID     INT COMMENT 'the surrogate key',
    AMOUNT NUMBER(12,2) COMMENT 'in the account currency'
) COMMENT = 'a table that documents itself';
Result1 row · 1 column
status
Table COMMENTED_T100 successfully created.

Let's see where the column comments went.

Query
SELECT COLUMN_NAME, DATA_TYPE, COMMENT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'COMMENTED_T100'
ORDER BY ORDINAL_POSITION;
Result2 rows · 3 columns
COLUMN_NAMEDATA_TYPECOMMENT
IDNUMBERthe surrogate key
AMOUNTNUMBERin the account currency

Both of them are in INFORMATION_SCHEMA.COLUMNS, sitting beside the columns they describe. And the table comment?

Query
SELECT TABLE_NAME, COMMENT, TABLE_TYPE, IS_TRANSIENT, ROW_COUNT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'COMMENTED_T100';
Result1 row · 5 columns
TABLE_NAMECOMMENTTABLE_TYPEIS_TRANSIENTROW_COUNT
COMMENTED_T100a table that documents itselfBASE TABLENO0

In INFORMATION_SCHEMA.TABLES, next to the table type and the row count. A base table, not transient, and empty because we never put anything in it.

DESCRIBE TABLE shows the comments too, along with everything else this post has been asking the catalog for one view at a time.

DESCRIBE TABLE
DESCRIBE TABLE COMMENTED_T100;
Result2 rows · 13 columns · scroll →
nametypekindnull?defaultprimary keyunique keycheckexpressioncommentpolicy nameprivacy domainwrite default
IDNUMBER(38,0)COLUMNYNULLNNNULLNULLthe surrogate keyNULLNULLNULL
AMOUNTNUMBER(12,2)COLUMNYNULLNNNULLNULLin the account currencyNULLNULLNULL

That is a wide grid — 13 columns for a two-column table. The type, the nullability, the default, the constraints and the comment are all in there in one place, which makes DESCRIBE TABLE the quickest way to see what a table actually promises.

Comments cost nothing and they are the only part of a table definition that tells the next person why the column exists. Write them while you are writing the CREATE TABLE, because nobody ever comes back to do it later.

And let's put the schema back

Every table in this post was made to be thrown away.

Cleanup
-- Put the schema back the way we found it.
DROP TABLE IF EXISTS PLAIN_T100;
DROP TABLE IF EXISTS REQUIRED_T100;
DROP TABLE IF EXISTS DEFAULTED_T100;
DROP TABLE IF EXISTS AUTO_T100;
DROP TABLE IF EXISTS AUTO_STEPPED_T100;
DROP TABLE IF EXISTS COMMENTED_T100;
Query
DROP TABLE IF EXISTS PLAIN_T100;
Result1 row · 1 column
status
PLAIN_T100 successfully dropped.
Query
DROP TABLE IF EXISTS REQUIRED_T100;
Result1 row · 1 column
status
REQUIRED_T100 successfully dropped.
Query
DROP TABLE IF EXISTS DEFAULTED_T100;
Result1 row · 1 column
status
DEFAULTED_T100 successfully dropped.
Query
DROP TABLE IF EXISTS AUTO_T100;
Result1 row · 1 column
status
AUTO_T100 successfully dropped.
Query
DROP TABLE IF EXISTS AUTO_STEPPED_T100;
Result1 row · 1 column
status
AUTO_STEPPED_T100 successfully dropped.
Query
DROP TABLE IF EXISTS COMMENTED_T100;
Result1 row · 1 column
status
COMMENTED_T100 successfully dropped.

And a check that nothing was left behind.

Query
SELECT COUNT(*) AS PROBE_TABLES_LEFT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC' AND RIGHT(TABLE_NAME, 5) = '_T100';
Result1 row · 1 column
PROBE_TABLES_LEFT
0

Nothing left. The schema is as we found it.

Key points

  • Every column is nullable and has no default unless you say otherwise. The plain table reported three columns, three nullable, three with no default.
  • NOT NULL is genuinely enforced, which is worth saying out loud after a level of finding constraints that are not.
  • With no default, omitting a NOT NULL column and passing NULL into it give the same error.
  • A DEFAULT fires when the column is omitted from the insert. It does not fire when you pass NULL.
  • Two inserts a reader would call the same insert put different things in the table. The difference is whether a column name appears in a list.
  • On a NOT NULL column that has a default, passing NULL is an error. The default does not rescue the row.
  • Never write column = NULL to mean “use the default”. Omit the column instead.
  • The catalog stores a default as text, exactly as you wrote it, and a CURRENT_TIMESTAMP() default is evaluated per row rather than frozen at create time.
  • AUTOINCREMENT and IDENTITY are described identically by the catalog: both IS_IDENTITY, both starting at one, both stepping by one.
  • You can write your own value into an AUTOINCREMENT column. It generates a value when you omit one; it does not stop you supplying one.
  • The two generated columns in one table do not share a counter. On the hand-written row the identity column produced 101, not four.
  • An AUTOINCREMENT column is not gapless. Delete a row and that number never returns, so never count rows by the highest id.
Next in this series

Episode 101 — CTAS, CREATE TABLE LIKE and CLONE. Three ways to make a table out of another table, what each one copies, and the one that quietly throws away every guarantee the original had.

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 _T100 tables 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, including the identity value the spoken script does not mention. Nothing is timed and no performance claims are made anywhere in this series. Column options follow the Snowflake CREATE TABLE documentation.

No comments:

Post a Comment

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