cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Community Discussions
Connect with fellow community members to discuss general topics related to the Databricks platform, industry trends, and best practices. Share experiences, ask questions, and foster collaboration within the community.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Primary key and not null

Shree23
New Contributor III

Hi Expert,

 

how we can get primary key and not null and cluster index in table creation

%Sql

create table table1 values (id int , product char)

 

expected output

create table table1 values (id int  not null primary key, product char) and cluster index

 

 

6 REPLIES 6

Kaniz_Fatma
Community Manager
Community Manager

Hi @Shree23To create a table with a primary key that is not null and a clustered index, you can use the following SQL commands:

First, create the table with the NOT NULL constraint and PRIMARY KEY:

sql
CREATE TABLE table1 (
 id INT NOT NULL PRIMARY KEY,
 product CHAR
);

Next, to create a clustered index, use the ALTER TABLE command with the CLUSTER BY clause:

sql
ALTER TABLE table1 CLUSTER BY (id);

Please note that the clustering command is specific to Databricks Delta and may not work in other SQL environments.

Shree23
New Contributor III

it is giving error message   Dataanalysisexception:table constraint are only supported in unity catalog

Shree23
New Contributor III

Hi @Shree23, The error message youโ€™re encountering, โ€œDataAnalysisException: Table constraints are only supported in Unity Catalog,โ€ indicates that youโ€™re trying to create a table with a primary key constraint, but this feature is only available within the Unity Catalog in Databricks

 

Letโ€™s break it down:

 

Unity Catalog: This is a specific catalog in Databricks that supports features like primary keys and foreign keys. Itโ€™s essential for enforcing constraints on tables.

Primary Key Constraints: These constraints ensure that specific columns in a table contain unique values. In your case, youโ€™re trying to define a primary key for the persons table using the first_name and last_name columns.

Error Message: Since primary key constraints are only supported within the Unity Catalog, youโ€™ll need to ensure that your table creation script is executed within this context.

Here are some steps to troubleshoot:

  • Verify that youโ€™re indeed creating the table inside the Unity Catalog.
  • Confirm that Unity Catalog is enabled for your workspace.
  • If youโ€™re still facing issues, consider raising a support case with Databricks for further investigation.

Remember, primary keys and foreign keys are informational in Databricksโ€”they provide useful metadata.... If you need additional assistance, feel free to ask! ๐Ÿš€

Shree23
New Contributor III

Suggestion please 

Shree23
New Contributor III

sugggestion pls

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!