cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Referential Integrity (Primary Key / Foreign Key Constraint) - Azure Databricks SQL

AbhishekBreeks
New Contributor II

Hello,

Please suggest how can we implement Referential Integrity (Primary Key / Foreign Key Constraint) - between different tables defined on Azure Databricks Database.

Basically the syntax to add Primary and Foreign Key constraint in the table definitions.

Thanks in advance for your help.

Regards,

Abhishek

9 REPLIES 9

franco_patano
Databricks Employee
Databricks Employee

You can use constraints: https://docs.microsoft.com/en-us/azure/databricks/delta/delta-constraints

Franco Patano
Stragetic Data and AI Advisor

-werners-
Esteemed Contributor III

according to the docs delta only has not-null and check constraints.

PK/FK constraint support is not mentioned.

Also:

https://issues.apache.org/jira/browse/SPARK-19842

So I'm afraid that it is not yet possible.

Ruby8376
Valued Contributor

@Werner Stinckens​ Hii! I just posted similar question for my project..Can we do pk/fk constraints on delta tables now?

-werners-
Esteemed Contributor III

nothing has changed so far.

Dr_G-Guillermo
Databricks Employee
Databricks Employee

Is it sad that I was excited to see that in preview, lol

The links indicate it would be just informational, so not enforced?

image

If you are using Delta Live Table pipelines, the pipeline will check dependencies via the table_constraint using the primary and foreign key definitions and use that to update tables in the proper order to ensure referential integrity. So though not strongly enforced for performance reasons you still get the main benefit of keys when using pipelines. @Qi Su​ correct me if I'm off here.

elgeo
Valued Contributor II

Is there any alternate way you could suggest to implement and enforce primary key constraint?

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group