簡體   English   中英

有沒有辦法為 2 個表創建唯一約束?

[英]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
);
  1. google_subscriptionfk_customer_id引用customerid
  2. apple_subscriptionfk_customer_id引用customerid

我想知道,是否有可能創建一個約束,例如customerid只能在google_subscriptionapple_subscription中找到,但不能同時在兩者中找到?

不,那是不可能的。 一個約束不能跨越多個表。 相反,您可以擁有另一個表subscriptions ,您可以在customer(id)上擁有唯一索引。 或者,您可以在客戶表中設置另一列,該列一次僅包含 1 個訂閱。

暫無
暫無

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

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