Snowflake Tables
A table in Snowflake is a database object. we can see the list of tables under database and Schema:
Show all tables in snowflake account.
show tables:
We have 4 Types of tables in Snowflake:
- Permanent Table
- Transient Table
- Temporary Table
- External Table
1. permanent Table :
These are standard, regular database tables. These are default tables and no need of any additional syntax to create them. Data stored in permanent tables consume space and Snowflake bills for it. These permanent tables comes with additional features like time travel and file safe.
create table <table name> (fileld datatype, filed2 datatype, field3 datatype);
id integer,
last_name string,
first_name string,
company string,
email string,
workphone string,
cellphone string,
streetaddress string,
city string,
postalcode string);
2. Transient Table :
The transient tables will exist till you explicitly drop them and they will be available to other users based on the privileges you provide them. They will have lower level of data protection than permanent tables. They will not have fail safe. They have very limited time travel period. They are best suited where the data is not critical and when we have option to recover them from external sources..
create transient table <table name> (fileld datatype, filed2 datatype, field3 datatype);
create or replace transient table customer_table (
id integer,
last_name string,
first_name string,
company string,
email string,
workphone string,
cellphone string,
streetaddress string,
city string,
postalcode string);
3. Temporary Table :
These are specific to the session. These are stored in no-permanent, Transitory data. These tables are not visible to other sessions or users. once the session ends, the data will be purged completely and not recoverable. Though they will be dropped automatically, Snowflake recommends to drop them explicitly to prevent any unexpected storage charges. .
It is session specific. Time Travel is 24 Hrs. Data_retention_time_in_days =1 (if session is not closed). Fail_safe = 0 on session close it will drop automatically
create temporary table <table name> (fileld datatype, filed2 datatype, field3 datatype);
create or replace temporary table customer_table (
id integer,
last_name string,
first_name string,
company string,
email string,
workphone string,
cellphone string,
streetaddress string,
city string,
postalcode string);
4. External Table. (Reading data from external Sources) :
An external table is a Snowflake feature that allows you to query data stored in an external stage as if the data were inside a table in Snowflake. The external stage is not part of Snowflake, So Snowflake does not store or manage the stage.
External Tables are database objects so we need to set context like role, warehouse, database and schema. Since we are not storing data in snowflake, we will not have any storage charges in this external tables.
- Table store file level metadata like FileName, Version, Identifier and related properties.
- These table are read-only tables and no DML operations are allowed.
- can be used to query data , and join operations.
- Views can be created on top of these tables.
- Quiring data against External tables are slow when we compare them to Native tables.
- Time travel is not supported
- When you want to do some adhoc analysis.
- when you don't want to read and load complete data into snowflake table.
- When you want to do POCs.
- when there is some static data that needs to be used by multiple people / teams and when it is not needed to be loaded in to tables.
Steps to create External table from WebUI :
Assume that we have configured the external stage already
- go to database {my_database}
- under Stages select Create stage
- This prompt us to select one among 4 options
- Snowflake Managed
- Existing S3 Location
- Existing Microsoft Azure Location
- Existing Google Cloud Platform
- Once you select it, it prompts you to provide external stage location details based on the provider you selected. (we need to provide a name to stage in this step)
- Now go to worksheet and follow the steps as below
It is hard to write this type of query each time when you want to read data. So it is always better to write create external table with schema so we can query the table as normal table.
Comments
Post a Comment