offdataai
Azure SynapseSQLDDLMicrosoft

Azure Synapse CREATE SCHEMA: Dedicated and Serverless SQL Pools

How to create schemas in Azure Synapse Analytics dedicated and serverless SQL pools, work around the lack of IF NOT EXISTS, grant permissions, move tables between schemas, and design distributed tables.

ooffdataai2 min read

In Azure Synapse Analytics a schema is a namespace inside a database, as in SQL Server. You create one with CREATE SCHEMA, in a dedicated SQL pool or in a database on the serverless SQL pool. The syntax is T-SQL, with a few Synapse-specific differences.

CREATE SCHEMA syntax

sql
CREATE SCHEMA analytics;CREATE SCHEMA staging AUTHORIZATION etl_user;

CREATE SCHEMA must be the only statement in its batch, and Synapse does not support CREATE SCHEMA IF NOT EXISTS. For re-runnable deployment scripts, check sys.schemas and run it through EXEC:

sql
IF NOT EXISTS (SELECT 1 FROM sys.schemas WHERE name = 'analytics')    EXEC('CREATE SCHEMA analytics');

Tables in a dedicated SQL pool

Dedicated SQL pools spread every table across 60 distributions, so each table needs a distribution choice:

sql
CREATE TABLE analytics.dim_customer (  customer_key INT NOT NULL,  customer_id  NVARCHAR(64) NOT NULL,  name         NVARCHAR(256),  region       NVARCHAR(64))WITH (DISTRIBUTION = REPLICATE, CLUSTERED COLUMNSTORE INDEX);​CREATE TABLE analytics.fct_orders (  order_id     NVARCHAR(64) NOT NULL,  customer_key INT NOT NULL,  order_date   DATE NOT NULL,  net_amount   DECIMAL(18, 2) NOT NULL)WITH (DISTRIBUTION = HASH(customer_key), CLUSTERED COLUMNSTORE INDEX);​ALTER TABLE analytics.dim_customer  ADD CONSTRAINT pk_dim_customer PRIMARY KEY NONCLUSTERED (customer_key) NOT ENFORCED;
  • Replicate small dimensions so joins don't move data.
  • Hash-distribute large facts on a column with many distinct values that is often joined on.
  • Keys: primary keys and unique constraints must be NONCLUSTERED NOT ENFORCED, and foreign key constraints are not supported. Test relationships in your pipeline instead.

Schemas in the serverless SQL pool

The serverless pool's master database cannot hold your objects. Create a database first, then schemas, views and external tables over files in the data lake:

sql
CREATE DATABASE lake;GOUSE lake;GOCREATE SCHEMA sales;GOCREATE VIEW sales.orders ASSELECT *FROM OPENROWSET(  BULK 'https://myaccount.dfs.core.windows.net/lake/orders/*.parquet',  FORMAT = 'PARQUET') AS rows;

Permissions, moving and dropping

sql
GRANT SELECT ON SCHEMA::analytics TO reporting_role;GRANT ALTER, INSERT, UPDATE, DELETE ON SCHEMA::staging TO etl_role;​-- Move a table from one schema to anotherALTER SCHEMA analytics TRANSFER staging.dim_product;​-- A schema must be empty before it can be droppedDROP SCHEMA staging_old;

List schemas with SELECT name FROM sys.schemas;.

Synapse and Microsoft Fabric

Microsoft is putting most new warehouse investment into Microsoft Fabric, whose Warehouse uses the same T-SQL CREATE SCHEMA. Schemas and star schemas designed for Synapse carry over largely unchanged, though distribution options differ. See the Fabric schema generator.

Frequently asked questions

Does Synapse support CREATE SCHEMA IF NOT EXISTS?

No. Check sys.schemas and run CREATE SCHEMA through EXEC, as shown above.

Why does CREATE SCHEMA fail in my script?

It must be the only statement in its batch. Put GO before and after it, or run it through EXEC.

Does Synapse support foreign keys?

Dedicated SQL pools do not support foreign key constraints, and primary keys must be NOT ENFORCED.

Generate Synapse DDL from a description

offdataai generates Synapse DDL with schemas, distribution choices and constraints, plus a dbt project, from a plain-English description or one of 174 templates. See the Synapse schema generator.

Try offdataai

Ship a warehouse from a single conversation.

Describe your domain. Get ERDs, DDL, dbt projects, and seed data for every major cloud warehouse.

Start building

Keep reading

All posts →