gpt4 book ai didi

mysql - 外键: Unique & part of Composite Primary Key

转载 作者:行者123 更新时间:2023-11-29 18:13:39 25 4
gpt4 key购买 nike

我有 table :

(优惠券)

| id |    name  |    description   |     start_at       |       end_at        |
| 1 | coupon_1 | FREE coupon ... |2017-02-01 04:00:33 | 2017-02-15 04:00:33 |

(优惠券代码)

| id | coupon_id |    code   |
| 1 | 1 | P69MFE5K |
| 13 | 1 | 75PRUE4G |

(coupon_recipient)

| coupon_id | user_id | couponcode_id |
| 1 | 4 | 1 |
| 1 | 4 | 13 |

coupon_recipient 键:

coupon_id、user_id、 couponcode_id = 复合主键

couponcode_id = 唯一索引

可以将couponcode_id作为复合主键唯一索引的一部分吗?

因为用户可以使用不同的coupon_code多次接收同一张优惠券(复合主键)

相同的coupon_code只能使用1次(唯一索引)

最佳答案

没有任何技术原因不能将 couponcode_id 作为唯一索引和主键的一部分。但是,我认为没有任何实际理由在 PK 中使用 coupon_id 和 user_id,因为它只会使表键更宽,这会对性能产生负面影响。

假设您的主键也是聚集索引键,如果 couponcode_id 在 coupon_recipient 中是唯一的,为什么不直接使用它作为主键。您可以为 coupon_id 和 user_id 创建非聚集索引。

关于mysql - 外键: Unique & part of Composite Primary Key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47204935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com