snowflake.core.table.TableCollection | Snowflake Documentation (original) (raw)

class snowflake.core.table.TableCollection(schema: SchemaResource)

Bases: SchemaObjectCollectionParent[TableResource]

Represents the collection operations on the Snowflake Table resource.

With this collection, you can create, iterate through, and search for tables that you have access to in the current context.

Examples

Creating a table instance:

tables = root.databases["my_db"].schemas["my_schema"].tables new_table = Table( ... name="accounts", ... columns=[ ... TableColumn( ... name="id", ... datatype="int", ... nullable=False, ... autoincrement=True, ... autoincrement_start=0, ... autoincrement_increment=1, ... ), ... TableColumn(name="created_on", datatype="timestamp_tz", nullable=False), ... TableColumn(name="email", datatype="string", nullable=False), ... TableColumn(name="password_hash", datatype="string", nullable=False), ... ], ... ) tables.create(new_tables)

Attributes

database

The DatabaseResource this collection belongs to.

root

The Root object this collection belongs to.

Methods

create(table: Table | str, *, as_select: str | None = None, template: str | None = None, like_table: str | None = None, clone_table: str | Clone | None = None, copy_grants: bool | None = False, mode: CreateMode = CreateMode.error_if_exists) → TableResource

Create a table in Snowflake.

Parameters:

Examples

Creating a table instance:

tables = root.databases["my_db"].schemas["my_schema"].tables new_table = Table( ... name="events", ... columns=[ ... TableColumn( ... name="id", ... datatype="int", ... nullable=False, ... autoincrement=True, ... autoincrement_start=0, ... autoincrement_increment=1, ... ), ... TableColumn(name="category", datatype="string"), ... TableColumn(name="event", datatype="string"), ... ], ... comment="store events/logs in here", ... ) tables.create(new_tables)

Cloning a Table instance:

tables = root.databases["my_db"].schemas["my_schema"].tables tables.create("new_table", clone_table="original_table_name")

Cloning a Table instance in a different database and schema:

tables = root.databases["my_db"].schemas["my_schema"].tables tables.create("new_table", clone_table="database_name.schema_name.original_table_name")

Notes

Not currently implemented:

create_async(table: Table | str, *, as_select: str | None = None, template: str | None = None, like_table: str | None = None, clone_table: str | Clone | None = None, copy_grants: bool | None = False, mode: CreateMode = CreateMode.error_if_exists) → PollingOperation[TableResource]

An asynchronous version of iter().

Refer to PollingOperation for more information on asynchronous execution and the return type.

items() → ItemsView[str, T]

iter(*, like: str | None = None, starts_with: str | None = None, limit: int | None = None, from_name: str | None = None, history: bool = False, deep: bool = False) → Iterator[Table]

Iterate through Table objects from Snowflake, filtering on any optional ‘like’ pattern.

Parameters:

Examples

Showing all tables that you have access to see in a schema:

tables = my_schema.tables.iter()

Showing information of the exact table you want to see:

tables = my_schema.tables.iter(like="my-table-name")

Showing tables starting with ‘my-table-name-‘:

tables = my_schema.tables.iter(like="my-table-name-%")

Using a for loop to retrieve information from iterator:

for table in table: print(table.name, table.kind)

iter_async(*, like: str | None = None, starts_with: str | None = None, limit: int | None = None, from_name: str | None = None, history: bool = False, deep: bool = False) → PollingOperation[Iterator[Table]]

An asynchronous version of iter().

Refer to PollingOperation for more information on asynchronous execution and the return type.

keys() → KeysView[str]

values() → ValuesView[T]