[英]Is there a way to create a unique constraint for 2 tables?
目前,我有以下 3 個表。
CREATE TABLE customer (
id SERIAL PRIMARY KEY
);
CREATE TABLE google_subscription (
fk_customer_id INTEGER NOT NULL UNIQUE,
CONSTRAINT fk_customer_id_constraint
FOREIGN KEY(fk_customer_id)
REFERENCES customer(id)
ON DELETE RESTRICT
);
CREATE TABLE apple_subscription (
fk_customer_id INTEGER NOT NULL UNIQUE,
CONSTRAINT fk_customer_id_constraint
FOREIGN KEY(fk_customer_id)
REFERENCES customer(id)
ON DELETE RESTRICT
);
google_subscription
有fk_customer_id
引用customer
表id
。apple_subscription
有fk_customer_id
引用customer
表id
。 我想知道,是否有可能創建一個約束,例如customer
表id
只能在google_subscription
或apple_subscription
中找到,但不能同時在兩者中找到?
不,那是不可能的。 一個約束不能跨越多個表。 相反,您可以擁有另一個表subscriptions
,您可以在customer(id)
上擁有唯一索引。 或者,您可以在客戶表中設置另一列,該列一次僅包含 1 個訂閱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.