Can you have multiple schemas in a single Snowflake database? If yes, how do you specify which schema to use in queries?
Yes, you can have multiple schemas within a single Snowflake database. Schemas are used to logically group and organize database objects, such as tables, views, and functions. Multiple schemas help you manage and categorize your objects more efficiently.
When executing queries, you can specify the schema to use by qualifying the object names with the schema name. This ensures that Snowflake knows which schema to look in when searching for the specified objects. To specify a schema in queries, you use the following syntax:
```sql
sqlCopy code
SELECT column1, column2, ...
FROM schema_name.table_name;
```
- **`schema_name`**: The name of the schema where the object resides.
- **`table_name`**: The name of the table or other object you want to query.
Here's an example of how to specify a schema in a query:
```sql
sqlCopy code
SELECT customer_name, order_date, total_amount
FROM sales_data.sales_orders;
```
In this example, the **`sales_orders`** table is accessed within the **`sales_data`** schema.
If you do not specify a schema explicitly in your query, Snowflake will assume that you are referring to an object within the default schema for your session. You can set the default schema for your session using the **`USE SCHEMA`** command:
```sql
sqlCopy code
USE SCHEMA schema_name;
```
This command sets the default schema for your session, so you don't need to specify the schema name in every query. However, even with a default schema, you can still fully qualify object names with schema names if needed.
Having multiple schemas within a single database provides flexibility for organizing your data and database objects based on your needs, projects, or teams.