I am trying to set one column in DB as primary key but I always get this error:
Enforced unique constraints are not supported in Azure SQL Data Warehouse. To create an unenforced unique constraint you must include the NOT ENFORCED syntax as part of your statement.
While doing R & D,I found that there is no concept of primary keys & foreign keys in Azure SQL data warehouse then how can we accomplish this in it.
Any ideas?
A primary key is a column or a group of columns that uniquely identifies each row in a table. You create a primary key for a table by using the PRIMARY KEY constraint. Each table can contain only one primary key. All columns that participate in the primary key must be defined as NOT NULL .
A primary key can be defined on one column or the combination of multiple columns known as a composite primary key. A primary key cannot exceed 16 columns and a total key length of 900 bytes. The primary key uniquely identifies each row in a table. It is often defined on the identity column.
In case you really need to create a primary key constraint in a table, Synapse pool can do that but it will not validate data already in the table. Only new data (insert/update) will be checked for duplication.
Azure SQL Data Warehourse doesn't support Primary Key.
https://docs.microsoft.com/en-us/azure/sql-data-warehouse/sql-data-warehouse-tables-overview
< Quote >
Azure SQL Data Warehouse supports these table constraints:
Source: https://docs.microsoft.com/en-us/azure/sql-data-warehouse/sql-data-warehouse-table-constraints
I've just signed up for StackOverflow RSS feeds on Synapse. The first feed was this post. I thought it was a recent one but just notice it is from 2 years ago, @Lurifaxel just posted an answer 4 days ago. I'll learn how to work around here ... eventually. :)
To the topic, database constraints like primary keys and foreign keys are resources to prevent bad client code to mess up data consistency. This is very popular (and almost mandatory) on OLTP databases.
In OLAP systems, where you load data either in batches or streams, you usually don't want this because it slows down the ingestion process. You usually rely on stage tables and CTAS techniques that will render the consistent version of your table.
OLTP transactions should not be considered in OLAP / BI system like Synapse. Which makes primary keys and foreign key constraints just irrelevant and not necessary.
In case you really need to create a primary key constraint in a table, Synapse pool can do that but it will not validate data already in the table. Only new data (insert/update) will be checked for duplication.
Alter a table to create an unenforced and non clustered primary key like this:
-- Schema: sales
-- Table: customer
-- primary key column: customerid
ALTER TABLE sales.customer
ADD CONSTRAINT PK_customer_customerid PRIMARY KEY NONCLUSTERED (customerid) NOT ENFORCED;
Keep in mind that you'll have to make sure that there aren't any rows with duplicate values manually! See the docs for further information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With