Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set any column as primary key in Azure SQL data warehouse

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?

like image 895
shweta_kaushish Avatar asked Apr 20 '18 11:04

shweta_kaushish


People also ask

Can a column be a primary key?

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 .

Which column can be set as primary key?

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.

Can we create primary key in Azure synapse?

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.


4 Answers

Azure SQL Data Warehourse doesn't support Primary Key.

https://docs.microsoft.com/en-us/azure/sql-data-warehouse/sql-data-warehouse-tables-overview

like image 197
DEEPAK LAKHOTIA Avatar answered Sep 18 '22 00:09

DEEPAK LAKHOTIA


< Quote >

Azure SQL Data Warehouse supports these table constraints:

  • PRIMARY KEY is only supported when NONCLUSTERED and NOT ENFORCED are both used.
  • UNIQUE constraint is only supported with NOT ENFORCED is used.

Source: https://docs.microsoft.com/en-us/azure/sql-data-warehouse/sql-data-warehouse-table-constraints

like image 42
AecorSoft Avatar answered Sep 20 '22 00:09

AecorSoft


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.

like image 35
Armando Lacerda Avatar answered Sep 22 '22 00:09

Armando Lacerda


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.

like image 25
Lurifaxel Avatar answered Sep 19 '22 00:09

Lurifaxel