数据库中复合键、可延迟约束及触发器的应用与权衡
1. 复合键与外键约束
在数据库设计中,当尝试添加外键约束时,如果没有唯一性约束,会出现错误。例如,在尝试为 orders 表添加外键约束到 movie_showtimes 表时:
movies_development=# alter table orders
add constraint movie_showtimes_movie_theatre_room_start_time_fkey
foreign key (movie_id, theatre_id, room, start_time)
references movie_showtimes(movie_id, theatre_id, room, start_time);
ERROR: there is no unique constraint matching given keys for
referenced table "movie_showtimes"
为了解决这个问题,需要在 movie_showtimes 表的相关列上添加唯一约束,即 movie_id 、 theatre_id 、 room 和 start_time 。以下是 movie_showtimes 和 orders 表的定义:
超级会员免费看
订阅专栏 解锁全文

4830

被折叠的 条评论
为什么被折叠?



