A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-create-table-constraint below:

CONSTRAINT clause | Databricks Documentation

CONSTRAINT clause

Applies to: Databricks SQL Databricks Runtime

Adds an informational primary key or an informational foreign key as part of a CREATE TABLE or CREATE MATERIALIZED VIEW statement.

To add a check constraint to a Delta Lake table use ALTER TABLE after the table has been created.

Syntax​

Use the table_constraint clause to define constraints which span multiple columns or to separate the syntax away from the column definition.

table_constraint
{ [ CONSTRAINT name ]
{ PRIMARY KEY ( key_column [ TIMESERIES ] [, ...] ) [ constraint_option [...] ] |
{ FOREIGN KEY ( foreign_key_column [, ...] )
REFERENCES parent_table [ ( parent_column [, ...] ) ]
[ foreign_key_option | constraint_option ] [...]
}
}
}

Use the column_constraintclause to define constraints specific to a single column definition.

column_constraint
{ [ CONSTRAINT name ]
{ PRIMARY KEY [ constraint_option ] [...] |
{ [ FOREIGN KEY ]
REFERENCES parent_table [ ( parent_column [, ...] ) ]
[ foreign_key_option | constraint_option ] [...]
}
}
}
constraint_option
{ NOT ENFORCED |
DEFERRABLE |
INITIALLY DEFERRED |
{ RELY | NORELY } }

foreign_key_option
{ MATCH FULL |
ON UPDATE NO ACTION |
ON DELETE NO ACTION }

For compatibility with non-standard SQL dialects you can specify ENABLE NOVALIDATE instead of NOT ENFORCED DEFERRABLE INITIALLY DEFERRED.

Parameters​

important

Databricks does not enforce primary key or foreign key constraints. Confirm key constraints before adding a primary or foreign key. Your ingest process may provide such assurance, or you can run checks against your data.

Examples​

SQL


> CREATE TABLE persons(first_name STRING NOT NULL, last_name STRING NOT NULL, nickname STRING,
CONSTRAINT persons_pk PRIMARY KEY(first_name, last_name));


> CREATE TABLE pets(name STRING, owner_first_name STRING, owner_last_name STRING,
CONSTRAINT pets_persons_fk FOREIGN KEY (owner_first_name, owner_last_name) REFERENCES persons);


> CREATE TABLE customers(customerid STRING NOT NULL PRIMARY KEY, name STRING);


> CREATE TABLE orders(orderid BIGINT NOT NULL CONSTRAINT orders_pk PRIMARY KEY,
customerid STRING CONSTRAINT orders_customers_fk REFERENCES customers);
Related articles​

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4