我这几天正在mySQL工作台上的一个程序上工作。我创建了以下表格:
create table if not exists customers
(
customerId varchar(10) primary key,
customerAFM varchar(9) not null,
first_name text not null,
surname text not null,
sex varchar(8),
birthdate date,
address text,
phone_number varchar(10)
) engine = innodb;
create table if not exists employees
(
employeeId varchar(5) primary key,
first_name text not null,
surname text not null,
specialty text not null,
address text,
phone_number varchar(10)
) engine = innodb;
create table if not exists rooms
(
room_number int primary key,
capacity int not null,
category text
) engine = innodb;
create table if not exists accomodation
(
accomodationId varchar(5) primary key,
room_number int not null, #constraint acc_num foreign key references rooms(room_number),
start_date date,
end_date date,
accomodation_price float,
foreign key (room_number) references rooms (room_number)
);
create table if not exists accomodation_customers
(
accomodationId varchar(5) primary key,
customerId varchar(5) not null unique key,
foreign key (accomodationId) references accomodation (accomodationId),
foreign key (customerId) references customers (customerId)
);
create table if not exists products
(
productId varchar(15) primary key,
product_name text,
product_price int
) engine = InnoDB;
create table if not exists charges
(
customerId varchar(5) primary key,
productId varchar(15) not null unique key,
charge_date datetime not null unique key,
employeeId varchar(5) not null,
quantity float,
foreign key (customerId) references customers (customerId),
foreign key (productId) references products (productId),
foreign key (employeeId) references employees (employeeId)
);
create table if not exists availability
(
registrationId int auto_increment primary key,
all_rooms int,
available_rooms int,
completeness float
);
现在,我想要做的是使用触发器进行后续限制: 一定不能与许多客户呆在同一个房间,以克服房间的容量。例如,如果房间是双人间,那么留在这个房间的人数不可超过2人。
我怎样才能让它真实?
答案 0 :(得分:0)
评论可能太长了。
您可能仍想查看架构设计:
1)表“可用性”是您可能不需要的。
2)你真的想为住宿ID或customerId使用varchar(5)吗?
3)表“accomodation_customers”不适用于一对多或多对多关系,您可能希望将其更改为:
create table if not exists accomodation_customers
(
accomodationId varchar(5) not null,
customerId varchar(5) not null ,
primary key (accomodationId, customerId),
foreign key (accomodationId) references accomodation (accomodationId),
foreign key (customerId) references customers (customerId)
);
然后,您可以查看指定日期的每个房间的可用性和状态:
set @date:=date_add(now(), INTERVAL 2 DAY);
select room_number, capacity, (select count(customerid) from accomodation a
join accomodation_customers r
on a.accomodationId = r.accomodationId
where a.start_date < cast(@date as date)
and a.end_date > @date
and a.room_number = rooms.room_number
) occupied
from rooms;
如果你在触发器中执行此操作,则可以在那里抛出异常,如果占用的空间大于任何房间的容量,则终止该事务。
预订/保留逻辑通常非常复杂,您无法通过一个触发器解决所有问题。在添加任何住宿之前,您需要检查可用性。您可以在住宿表中添加“人数”栏,以简化操作:
set @date:=date_add(now(), INTERVAL 2 DAY);
select room_number, capacity, (select sum(number_of_persons) from accomodation a
where a.start_date < cast(@date as date)
and a.end_date > @date
and a.room_number = rooms.room_number
) occupied from rooms;
并且可以在“住宿表”上添加触发器。
同样,酒店通常不会让不同的客户共用一个房间。从逻辑上讲,容量和“人数”通常为“1”。