[英]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.