有没有一种方法可以配对外键来匹配?
例如,user_id和subs_id fk对在产品表中不匹配,但允许添加。
表:用户
| user_id (PK) | first_name | last_name |
| -------------| ------------|------------|
| 000001 | David | Hawk |
| 000002 | Ali | Abdullah |表:订阅
| user_id (FK) | subs_id (PK) | subs_status | total_cycles |
| -------------| -------------|-------------|--------------|
| 000001 | ABC_123456 | ACTIVE | 4 |
| 000002 | CDE_654321 | CANCELLED | 8 | 表:产品
| user_id (FK) | subs_id (FK) | product | plan | product-key (PK) |
| -------------| ---------------|-------------|--------|------------------|
| **000001** | **CDE_654321** | Product-A | Pro | A5CD-8Z62-X2D4 |
| **000002** | **ABC_123456** | Product-B | Plus | WFE7-71W4-Z64D | 发布于 2021-09-14 08:59:47
如果我理解正确的话,您有两个外键,一个在user_id上,一个在subs_id上。相反,您需要在两者的组合上使用单个外键:
ALTER TABLE product
ADD CONSTRAINT prodcut_subscription_fk
FOREIGN KEY (user_id, subs_id)
REFERENCES subscription(user_id, subs_id)https://stackoverflow.com/questions/69174769
复制相似问题