簡體   English   中英

添加一個MySQL約束,即字段作為同一表的主鍵存在

[英]Adding a MySQL constraint that a field exists as primary key of the same table

我正在創建一個具有basisId字段作為主鍵的表。 還有另一個字段parentBasis ,它將引用that.basisId等於this.parentBasis另一個元組。 我要做的是在創建表時表達此約束。

像這樣: ADD CONSTRAINT CHECK EXISTS this.parentBasis AS somewhere.basisId (顯然不是真正的MySQL)。

快速瀏覽MySQL開發頁面並沒有多大用處。 任何幫助,將不勝感激。

謝謝。

如果您使用的是InnoDB,則可以從表到自身創建一個外鍵 例如:

create table t (
    id int not null primary key,
    parent int null
);
alter table t add constraint foreign key (parent) references t(id);

那么t.parent必須為NULL或t.id值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM