DROP CONSTRAINT 子句
適用於: Databricks SQL Databricks Runtime
從數據表卸除 PRIMARY KEY、FOREIGN KEY 或 CHECK 條件約束。
語法
DROP { PRIMARY KEY [ IF EXISTS ] [ RESTRICT | CASCADE ] |
FOREIGN KEY [ IF EXISTS ] ( column [, ...] ) |
CONSTRAINT [ IF EXISTS ] name [ RESTRICT | CASCADE ] }
參數
主鍵 [ IF EXISTS ]
適用於:Databricks SQL Databricks Runtime 11.3 LTS 和更新版本僅限 Unity 目錄
從數據表卸除主鍵。
FOREIGN KEY [ IF EXISTS ] (column [, ...] )
適用於:Databricks SQL Databricks Runtime 11.3 LTS 和更新版本僅限 Unity 目錄
卸除排序數據行清單所識別的外鍵。
CONSTRAINT [ IF EXISTS ] name
卸除名稱所識別的主鍵、外鍵或檢查條件約束。 檢查條件約束只能依名稱卸除。
RESTRICT 或 CASCADE
如果您指定
RESTRICT
且主鍵是由任何外鍵參考,語句將會失敗。 如果您指定CASCADE
,卸除主鍵會導致卸除參考數據表的任何外鍵。 預設值為RESTRICT
。IF EXISTS
如果數據表
IF EXISTS
沒有相符的條件約束,則會忽略 語句。
範例
> CREATE TABLE persons(first_name STRING NOT NULL, last_name STRING NOT NULL, nickname STRING);
> ALTER TABLE persons ADD CONSTRAINT persons_pk PRIMARY KEY(first_name, last_name);
> CREATE TABLE pets(name STRING, owner_first_name STRING, owner_last_name STRING);
> ALTER TABLE pets ADD CONSTRAINT pets_persons_fk FOREIGN KEY (owner_first_name, owner_last_name) REFERENCES persons;
> ALTER TABLE pets ADD CONSTRAINT pets_name_not_cute_chk CHECK (length(name) < 20);
-- Drop the check constraint by name
> ALTER TABLE pets DROP CONSTRAINT pets_name_not_cute_chk;
-- Attempt to drop the primary key of persons by name
> ALTER TABLE persons DROP CONSTRAINT persons_pk RESTRICT;
Error: A foreign key `pets_persons_fk` depends on the primary key
-- Drop the foreign key from pets by listing the columns
> ALTER TABLE pets DROP FOREIGN KEY IF EXISTS (owner_first_name, owner_last_name);
-- Drop the primary key of persons
> ALTER TABLE persons DROP PRIMARY KEY CASCADE;