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
- So what goes in when we leave the columns out?
- Now let's make two columns required with NOT NULL
- Is NOT NULL actually enforced?
- Now let's add some defaults
- Let's omit every column that has a default
- Now let's pass NULL into those same columns instead
- What if the column is NOT NULL and has a default?
- Is a CURRENT_TIMESTAMP() default frozen or per row?
- Now let's try AUTOINCREMENT and IDENTITY
- Can we write to an AUTOINCREMENT column by hand?
- Is an AUTOINCREMENT column gapless?
- Let's set our own START and INCREMENT
- One last thing: let's document the table as we create it
- And let's put the schema back
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 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.
CREATE OR REPLACE TABLE PLAIN_T100 (
ID INT,
NAME VARCHAR(50),
AMOUNT NUMBER(12,2)
);| status |
|---|
| Table PLAIN_T100 successfully created. |
Now let's ask the catalog what it actually built for us.
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;| COLUMN_NAME | DATA_TYPE | IS_NULLABLE | COLUMN_DEFAULT | NUMERIC_PRECISION | NUMERIC_SCALE | CHARACTER_MAXIMUM_LENGTH |
|---|---|---|---|---|---|---|
| ID | NUMBER | YES | NULL | 38 | 0 | NULL |
| NAME | TEXT | YES | NULL | NULL | NULL | 50 |
| AMOUNT | NUMBER | YES | NULL | 12 | 2 | NULL |
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.
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';| COLUMNS_TOTAL | NULLABLE_COLUMNS | COLUMNS_WITH_NO_DEFAULT |
|---|---|---|
| 3 | 3 | 3 |
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.
INSERT INTO PLAIN_T100 (ID) SELECT 1;| number of rows inserted |
|---|
| 1 |
One row inserted, no complaint. Here is what is in it.
SELECT ID, NAME, AMOUNT,
NAME IS NULL AS NAME_IS_NULL,
AMOUNT IS NULL AS AMOUNT_IS_NULL
FROM PLAIN_T100;| ID | NAME | AMOUNT | NAME_IS_NULL | AMOUNT_IS_NULL |
|---|---|---|---|---|
| 1 | NULL | NULL | TRUE | TRUE |
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.
CREATE OR REPLACE TABLE REQUIRED_T100 (
ID INT NOT NULL,
NAME VARCHAR(50) NOT NULL,
OPTNOTE VARCHAR(50)
);| status |
|---|
| Table REQUIRED_T100 successfully created. |
And the catalog records it.
SELECT COLUMN_NAME, IS_NULLABLE, COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'REQUIRED_T100'
ORDER BY ORDINAL_POSITION;| COLUMN_NAME | IS_NULLABLE | COLUMN_DEFAULT |
|---|---|---|
| ID | NO | NULL |
| NAME | NO | NULL |
| OPTNOTE | YES | NULL |
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.
INSERT INTO REQUIRED_T100 (ID, NAME) SELECT 1, 'fine';| 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.
INSERT INTO REQUIRED_T100 (ID, NAME) SELECT 2, NULL;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.
INSERT INTO REQUIRED_T100 (ID) SELECT 3;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.
SELECT COUNT(*) AS ROWS_THAT_GOT_IN,
COUNT_IF(NAME IS NULL) AS NULLS_THAT_GOT_THROUGH
FROM REQUIRED_T100;| ROWS_THAT_GOT_IN | NULLS_THAT_GOT_THROUGH |
|---|---|
| 1 | 0 |
One row in, and no nulls through. NOT NULL is the constraint that genuinely refuses, and it always has been.
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.
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'
);| status |
|---|
| Table DEFAULTED_T100 successfully created. |
Let's see how the catalog stores a default.
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;| COLUMN_NAME | DATA_TYPE | IS_NULLABLE | COLUMN_DEFAULT |
|---|---|---|---|
| ID | NUMBER | YES | NULL |
| STATUS | TEXT | YES | 'PENDING' |
| QTY | NUMBER | YES | 1 |
| CREATED | TIMESTAMP_NTZ | YES | CURRENT_TIMESTAMP() |
| REQUIRED_STATUS | TEXT | NO | '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.
INSERT INTO DEFAULTED_T100 (ID) SELECT 1;| number of rows inserted |
|---|
| 1 |
SELECT ID, STATUS, QTY, REQUIRED_STATUS, CREATED IS NOT NULL AS CREATED_WAS_SET
FROM DEFAULTED_T100
WHERE ID = 1;| ID | STATUS | QTY | REQUIRED_STATUS | CREATED_WAS_SET |
|---|---|---|---|---|
| 1 | PENDING | 1 | NEW | TRUE |
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.
INSERT INTO DEFAULTED_T100 (ID, STATUS, QTY) SELECT 2, NULL, NULL;| number of rows inserted |
|---|
| 1 |
It ran. One row inserted. And here is what it put in the table.
SELECT ID, STATUS, QTY,
STATUS IS NULL AS STATUS_IS_NULL,
QTY IS NULL AS QTY_IS_NULL
FROM DEFAULTED_T100
WHERE ID = 2;| ID | STATUS | QTY | STATUS_IS_NULL | QTY_IS_NULL |
|---|---|---|---|---|
| 2 | NULL | NULL | TRUE | TRUE |
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.
SELECT ID, STATUS, QTY, REQUIRED_STATUS
FROM DEFAULTED_T100
ORDER BY ID;| ID | STATUS | QTY | REQUIRED_STATUS |
|---|---|---|---|
| 1 | PENDING | 1 | NEW |
| 2 | NULL | NULL | NEW |
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 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.
INSERT INTO DEFAULTED_T100 (ID, REQUIRED_STATUS) SELECT 3, NULL;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.
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;| ROWS_IN_TABLE | STATUS_NULLS | STATUS_DEFAULTED | REQUIRED_DEFAULTED |
|---|---|---|---|
| 2 | 1 | 1 | 2 |
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.
INSERT INTO DEFAULTED_T100 (ID) SELECT 4;| number of rows inserted |
|---|
| 1 |
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;| ROWS_WITH_A_TIMESTAMP | DISTINCT_TIMESTAMPS | IT_IS_EVALUATED_PER_ROW |
|---|---|---|
| 3 | 3 | TRUE |
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.
CREATE OR REPLACE TABLE AUTO_T100 (
A_AUTO INT AUTOINCREMENT,
AN_IDENTITY INT IDENTITY,
NOTE VARCHAR(30)
);| status |
|---|
| Table AUTO_T100 successfully created. |
Let's see how the catalog describes them.
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;| COLUMN_NAME | DATA_TYPE | IS_NULLABLE | COLUMN_DEFAULT | IS_IDENTITY | IDENTITY_START | IDENTITY_INCREMENT |
|---|---|---|---|---|---|---|
| A_AUTO | NUMBER | YES | NULL | YES | 1 | 1 |
| AN_IDENTITY | NUMBER | YES | NULL | YES | 1 | 1 |
| NOTE | TEXT | YES | NULL | NO | NULL | NULL |
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.
INSERT INTO AUTO_T100 (NOTE)
SELECT 'first' UNION ALL SELECT 'second' UNION ALL SELECT 'third';| number of rows inserted |
|---|
| 3 |
SELECT A_AUTO, AN_IDENTITY, NOTE FROM AUTO_T100 ORDER BY A_AUTO;| A_AUTO | AN_IDENTITY | NOTE |
|---|---|---|
| 1 | 1 | first |
| 2 | 2 | second |
| 3 | 3 | third |
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.
INSERT INTO AUTO_T100 (A_AUTO, NOTE) SELECT 500, 'written by hand';| number of rows inserted |
|---|
| 1 |
One row inserted. It let us. Let's look at the table.
SELECT A_AUTO, AN_IDENTITY, NOTE FROM AUTO_T100 ORDER BY A_AUTO;| A_AUTO | AN_IDENTITY | NOTE |
|---|---|---|
| 1 | 1 | first |
| 2 | 2 | second |
| 3 | 3 | third |
| 500 | 101 | written 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 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.
DELETE FROM AUTO_T100 WHERE NOTE = 'second';| number of rows deleted |
|---|
| 1 |
INSERT INTO AUTO_T100 (NOTE) SELECT 'fourth' UNION ALL SELECT 'fifth';| number of rows inserted |
|---|
| 2 |
SELECT A_AUTO, NOTE FROM AUTO_T100 ORDER BY A_AUTO;| A_AUTO | NOTE |
|---|---|
| 1 | first |
| 3 | third |
| 4 | fourth |
| 5 | fifth |
| 500 | written 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.
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;| ROWS_IN_TABLE | LOWEST_ID | HIGHEST_ID | IDS_SPANNED | NO_GAPS |
|---|---|---|---|---|
| 5 | 1 | 500 | 500 | FALSE |
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.
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.
CREATE OR REPLACE TABLE AUTO_STEPPED_T100 (
ID INT AUTOINCREMENT START 100 INCREMENT 10,
NOTE VARCHAR(30)
);| status |
|---|
| Table AUTO_STEPPED_T100 successfully created. |
INSERT INTO AUTO_STEPPED_T100 (NOTE)
SELECT 'a' UNION ALL SELECT 'b' UNION ALL SELECT 'c' UNION ALL SELECT 'd';| number of rows inserted |
|---|
| 4 |
SELECT ID, NOTE FROM AUTO_STEPPED_T100 ORDER BY ID;| ID | NOTE |
|---|---|
| 100 | a |
| 110 | b |
| 120 | c |
| 130 | d |
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.
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';| status |
|---|
| Table COMMENTED_T100 successfully created. |
Let's see where the column comments went.
SELECT COLUMN_NAME, DATA_TYPE, COMMENT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'COMMENTED_T100'
ORDER BY ORDINAL_POSITION;| COLUMN_NAME | DATA_TYPE | COMMENT |
|---|---|---|
| ID | NUMBER | the surrogate key |
| AMOUNT | NUMBER | in the account currency |
Both of them are in INFORMATION_SCHEMA.COLUMNS, sitting beside the columns they describe. And the table comment?
SELECT TABLE_NAME, COMMENT, TABLE_TYPE, IS_TRANSIENT, ROW_COUNT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'COMMENTED_T100';| TABLE_NAME | COMMENT | TABLE_TYPE | IS_TRANSIENT | ROW_COUNT |
|---|---|---|---|---|
| COMMENTED_T100 | a table that documents itself | BASE TABLE | NO | 0 |
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 COMMENTED_T100;| name | type | kind | null? | default | primary key | unique key | check | expression | comment | policy name | privacy domain | write default |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ID | NUMBER(38,0) | COLUMN | Y | NULL | N | N | NULL | NULL | the surrogate key | NULL | NULL | NULL |
| AMOUNT | NUMBER(12,2) | COLUMN | Y | NULL | N | N | NULL | NULL | in the account currency | NULL | NULL | NULL |
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.
-- 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;DROP TABLE IF EXISTS PLAIN_T100;| status |
|---|
| PLAIN_T100 successfully dropped. |
DROP TABLE IF EXISTS REQUIRED_T100;| status |
|---|
| REQUIRED_T100 successfully dropped. |
DROP TABLE IF EXISTS DEFAULTED_T100;| status |
|---|
| DEFAULTED_T100 successfully dropped. |
DROP TABLE IF EXISTS AUTO_T100;| status |
|---|
| AUTO_T100 successfully dropped. |
DROP TABLE IF EXISTS AUTO_STEPPED_T100;| status |
|---|
| AUTO_STEPPED_T100 successfully dropped. |
DROP TABLE IF EXISTS COMMENTED_T100;| status |
|---|
| COMMENTED_T100 successfully dropped. |
And a check that nothing was left behind.
SELECT COUNT(*) AS PROBE_TABLES_LEFT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC' AND RIGHT(TABLE_NAME, 5) = '_T100';| 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 NULLis genuinely enforced, which is worth saying out loud after a level of finding constraints that are not.- With no default, omitting a
NOT NULLcolumn and passingNULLinto it give the same error. - A
DEFAULTfires when the column is omitted from the insert. It does not fire when you passNULL. - 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 NULLcolumn that has a default, passingNULLis an error. The default does not rescue the row. - Never write
column = NULLto 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. AUTOINCREMENTandIDENTITYare described identically by the catalog: bothIS_IDENTITY, both starting at one, both stepping by one.- You can write your own value into an
AUTOINCREMENTcolumn. 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
AUTOINCREMENTcolumn is not gapless. Delete a row and that number never returns, so never count rows by the highest id.
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.