mysql项目案例电影,MySql电影预约系统设计

I have the following pseudo MySQL for a movie reservation system:

Table Movie:

id int auto_increment not null,

name varchar(100) not null,

.....

Table MovieSched:

movie_id foreign key refers to Movie,

sched_id int auto increment,

date & time,

max_size

Table MovieSchedSignUp:

sched_id,

user_id

Every movie schedule has a max_size of users who can sign up. To register a user, I insert a row in MovieSchedSignUp.

The problem is: How do I insert a row in MovieSchedSignUp while ensuring the schedule is not "overbooked" (sign ups <= max_size)

Specifically, to register a user for a schedule, what is the query I should use, and how do I check whether the schedule is full or not (so I can tell the user whether registration is successful)? Note that these requires some sort of atomic operation, a transaction perhaps.

What is the most efficient way to go about doing this?

解决方案

Rough idea for SQL:-

INSERT INTO Table MovieSchedSignUp (sched_id, user_id)

SELECT '$move_sched_id', '$user_id'

FROM

(

SELECT sched_id, COUNT(*) AS BookingCount

FROM MovieSchedSignUp

WHERE sched_id = '$move_sched_id'

GROUP BY sched_id

HAVING BookingCount < $max_size

)

Insert a record based on a select. The select returns 2 fixed values, and selects from a sub query that returns the schedule id if the number of bookings is less than the max number of bookings.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值