Databases, Schemas, Tables & Data Types: A Beginner's Guide
Learn Snowflake's hierarchical structure: databases, schemas, tables, and data types.
20+ years shipping high-throughput database systems. Written from production experience, not tutorials.
- ✓Basic understanding of SQL (SELECT, INSERT, CREATE).
- ✓A Snowflake account (free trial available).
- ✓Familiarity with relational database concepts (tables, columns, data types).
- Snowflake organizes data in a hierarchy: Account > Database > Schema > Table.
- Databases contain schemas; schemas contain tables, views, functions, etc.
- Tables store data in rows and columns with defined data types.
- Snowflake supports standard SQL data types plus semi-structured types like VARIANT.
- Use CREATE DATABASE, CREATE SCHEMA, CREATE TABLE commands to set up your environment.
Think of Snowflake like a large office building. The building is your Snowflake account. Each floor is a database. On each floor, there are rooms (schemas). Inside each room, there are filing cabinets (tables). Each cabinet has drawers (columns) that hold specific types of documents (data types). You need to know where to put your files and what kind of files they are.
When you start working with Snowflake, the first thing you need to understand is how data is organized. Unlike traditional databases where you might just have a flat list of tables, Snowflake uses a hierarchical structure: Account → Database → Schema → Table. This structure helps you manage large amounts of data, enforce security, and organize your work logically.
Imagine you're building a data warehouse for an e-commerce company. You'll have different teams (marketing, sales, inventory) that need their own space. Snowflake's hierarchy allows you to create separate databases for each department, schemas for different projects, and tables for specific datasets. This not only keeps things tidy but also makes it easy to grant permissions at the right level.
In this tutorial, you'll learn how to create databases, schemas, and tables in Snowflake. We'll cover the essential data types you'll use daily, from simple strings and numbers to complex semi-structured data like JSON. You'll also see production-ready examples and common pitfalls to avoid. By the end, you'll be able to set up your Snowflake environment confidently and avoid the mistakes that can lead to costly production incidents.
Understanding Snowflake's Object Hierarchy
Snowflake organizes data in a multi-level hierarchy. At the top is your Snowflake account. Under the account, you can create multiple databases. Each database contains schemas, and each schema contains objects like tables, views, functions, and stored procedures. This structure is similar to a file system: account is the root, databases are folders, schemas are subfolders, and tables are files.
- Organize data logically (e.g., by department, project, or environment).
- Control access at different levels (grant usage on database, schema, or table).
- Avoid naming conflicts (same table name can exist in different schemas).
When you first log into Snowflake, you are placed in a default database and schema (usually PUBLIC). It's a common mistake to start creating tables there without realizing that PUBLIC is shared with all users who have access to the database. Always create your own schema for production work.
To see your current context, use:
SELECT CURRENT_DATABASE(), CURRENT_SCHEMA();
To change context, use:
USE DATABASE mydb; USE SCHEMA mydb.myschema;
Or you can set both at once:
USE SCHEMA mydb.myschema;
Now, let's create our first database and schema.
Creating Tables in Snowflake
Tables are the core storage objects in Snowflake. They consist of rows and columns, where each column has a name and a data type. Snowflake supports both permanent and transient tables. Permanent tables have a fail-safe period for data recovery, while transient tables do not (but are cheaper). For most production use cases, you'll use permanent tables.
To create a table, you use the CREATE TABLE statement. You must specify the table name, column names, and their data types. You can also define constraints like NOT NULL, UNIQUE, PRIMARY KEY, and FOREIGN KEY. However, Snowflake does not enforce constraints except NOT NULL and UNIQUE (if you create a unique constraint, it creates a unique index). Primary keys and foreign keys are metadata only; they are not enforced.
Here's an example of creating a sales orders table:
CREATE TABLE orders ( order_id INTEGER AUTOINCREMENT, customer_id INTEGER NOT NULL, order_date DATE NOT NULL, total_amount DECIMAL(10,2), status VARCHAR(20) DEFAULT 'PENDING', PRIMARY KEY (order_id) );
Notice the AUTOINCREMENT property for order_id – it automatically generates a unique number for each row. The DEFAULT clause sets a default value for status. The PRIMARY KEY is informational; Snowflake will not prevent duplicate order_id values unless you also create a unique constraint.
Let's create a more comprehensive table for our e-commerce schema.
Snowflake Data Types: The Essentials
Snowflake supports a wide range of data types, categorized into: - Numeric: INTEGER, DECIMAL, FLOAT, etc. - String: VARCHAR, CHAR, TEXT, etc. - Date/Time: DATE, TIME, TIMESTAMP, etc. - Semi-structured: VARIANT, OBJECT, ARRAY (for JSON, Avro, Parquet, etc.) - Others: BOOLEAN, GEOGRAPHY, etc.
- INTEGER: Whole numbers (e.g., 1, 100, -5).
- DECIMAL(p,s): Fixed-point numbers (e.g., DECIMAL(10,2) for currency).
- VARCHAR(n): Variable-length strings (e.g., VARCHAR(100)).
- DATE: Date without time (e.g., '2025-03-15').
- TIMESTAMP: Date and time (e.g., '2025-03-15 14:30:00').
- BOOLEAN: TRUE or FALSE.
- VARIANT: Any semi-structured data (JSON, etc.).
Choosing the right data type is crucial for performance and storage. For example, use DATE instead of TIMESTAMP if you don't need time, as it uses less space. Use VARCHAR instead of TEXT for shorter strings.
Let's see how to use these types in a table.
Working with Semi-Structured Data: VARIANT, OBJECT, and ARRAY
Snowflake excels at handling semi-structured data like JSON, Avro, Parquet, and XML. The key data types are: - VARIANT: Can hold any value of any other data type, including OBJECT and ARRAY. - OBJECT: A collection of key-value pairs (like a JSON object). - ARRAY: An ordered list of elements.
You can load JSON data directly into a VARIANT column. Snowflake provides powerful functions to query this data, such as: - : (colon) operator to access object fields. - [] bracket notation for array elements. - FLATTEN function to expand arrays into rows. - PARSE_JSON to convert a string to VARIANT.
Let's see an example with an ARRAY of objects.
Altering and Dropping Tables
After creating tables, you may need to modify them. Snowflake supports ALTER TABLE to add, drop, or modify columns, rename tables, and change other properties. You can also drop tables when they are no longer needed.
- Adding a column: ALTER TABLE table_name ADD COLUMN column_name data_type;
- Dropping a column: ALTER TABLE table_name DROP COLUMN column_name;
- Renaming a column: ALTER TABLE table_name RENAME COLUMN old_name TO new_name;
- Modifying a column's data type: ALTER TABLE table_name ALTER COLUMN column_name SET DATA TYPE new_type; (only if compatible)
- Renaming a table: ALTER TABLE table_name RENAME TO new_name;
- Dropping a table: DROP TABLE table_name;
Be cautious when altering tables in production. Dropping a column or changing data types can cause data loss or application errors. Always test in a development environment first.
Let's see some examples.
Best Practices for Database, Schema, and Table Design
Designing your Snowflake environment properly from the start saves headaches later. Here are some best practices:
- Use separate databases for different environments (dev, test, prod). This prevents accidental data corruption.
- Use schemas to organize by domain (e.g., sales, inventory, marketing). This makes permissions easier.
- Name objects consistently – use lowercase with underscores (e.g., customer_orders). Snowflake converts to uppercase unless quoted, but using lowercase in scripts is more readable.
- Set appropriate data types – use the smallest type that fits your data. For example, use DATE instead of TIMESTAMP if time is not needed.
- Use NOT NULL constraints for columns that must have values. This improves data quality.
- Add comments to tables and columns using COMMENT statement. This helps other developers understand the schema.
- Plan for growth – use AUTOINCREMENT for surrogate keys, but consider using sequences for high-volume tables.
- Avoid using reserved words as object names (e.g., TABLE, SELECT). If you must, use double quotes.
Let's see an example of a well-designed schema with comments.
The Case of the Missing Schema: A Snowflake Permission Disaster
- Always create a dedicated schema for each team or project.
- Never use the default PUBLIC schema for production data.
- Grant permissions at the schema level, not the database level.
- Use descriptive schema names to avoid confusion.
- Review default permissions on new databases.
SHOW TABLES;SELECT * FROM <db>.<schema>.<table> LIMIT 10;| File | Command / Code | Purpose |
|---|---|---|
| create_db_schema.sql | CREATE DATABASE IF NOT EXISTS ecommerce_analytics; | Understanding Snowflake's Object Hierarchy |
| create_tables.sql | CREATE TABLE customers ( | Creating Tables in Snowflake |
| data_types_example.sql | CREATE TABLE product_reviews ( | Snowflake Data Types |
| semi_structured.sql | CREATE TABLE customer_interactions ( | Working with Semi-Structured Data |
| alter_drop.sql | ALTER TABLE orders ADD COLUMN discount DECIMAL(5,2) DEFAULT 0.00; | Altering and Dropping Tables |
| best_practices.sql | CREATE DATABASE IF NOT EXISTS prod_ecommerce; | Best Practices for Database, Schema, and Table Design |
Key takeaways
Common mistakes to avoid
5 patternsCreating tables in the PUBLIC schema without realizing it's shared.
Using VARCHAR without specifying length (defaults to 16 MB).
Assuming PRIMARY KEY enforces uniqueness.
Using TIMESTAMP instead of DATE when time is not needed.
Forgetting to set context before querying.
Interview Questions on This Topic
What is the hierarchy of objects in Snowflake?
Frequently Asked Questions
20+ years shipping high-throughput database systems. Written from production experience, not tutorials.
That's Snowflake. Mark it forged?
4 min read · try the examples if you haven't