How to Create a Database in Snowflake: Syntax and Example
To create a database in Snowflake, use the
CREATE DATABASE command followed by the database name. For example, CREATE DATABASE my_database; creates a new database named my_database.Syntax
The basic syntax to create a database in Snowflake is:
CREATE DATABASE <database_name>;- creates a new database with the specified name.- You can optionally add
IF NOT EXISTSto avoid errors if the database already exists.
sql
CREATE DATABASE database_name; -- Optional to avoid error if database exists CREATE DATABASE IF NOT EXISTS database_name;
Example
This example creates a database named sales_db. It shows how to create the database and confirms success.
sql
CREATE DATABASE sales_db;Output
Database SALES_DB successfully created.
Common Pitfalls
Common mistakes when creating a database in Snowflake include:
- Not using a unique database name, which causes an error.
- Forgetting the semicolon
;at the end of the command. - Trying to create a database that already exists without
IF NOT EXISTS, which causes an error.
Always check if the database name is available and use IF NOT EXISTS to avoid errors.
sql
/* Wrong: Missing semicolon and duplicate name error */ CREATE DATABASE sales_db; /* Right: Use semicolon and IF NOT EXISTS */ CREATE DATABASE IF NOT EXISTS sales_db;
Quick Reference
| Command | Description |
|---|---|
| CREATE DATABASE | Creates a new database with the given name. |
| CREATE DATABASE IF NOT EXISTS | Creates a database only if it does not already exist. |
| DROP DATABASE | Deletes the specified database. |
Key Takeaways
Use the CREATE DATABASE command followed by the database name to create a database in Snowflake.
Add IF NOT EXISTS to avoid errors if the database already exists.
Always end SQL commands with a semicolon ; to avoid syntax errors.
Choose unique database names to prevent conflicts.
Check for existing databases before creating new ones to avoid errors.
