预订网站的数据库表

发布于 2024-12-19 11:47:10 字数 1422 浏览 2 评论 0原文

我们正在建立一个预订系统。共有三个模块

  1. 客户
  2. 客房
  3. 预订

客户可以预订一间或多间房间并预订不同的日期。预订房间时,我想搜索在日期 A 和日期 B 之间可以预订哪些房间。

表格(可能的解决方案) 客户(ID、姓名、......) 房间(id,房间号,房间类型,......) bookings(id, room_id, fromDate, toDate)

目前我有这样的表

CREATE TABLE IF NOT EXISTS `bookings` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `checkin` datetime NOT NULL,
  `checkout` datetime NOT NULL,
  `advance` int(11) NOT NULL,
  `amount` int(11) NOT NULL,
  `booking_details_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


CREATE TABLE IF NOT EXISTS `bookings_rooms` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `booking_id` int(11) NOT NULL,
  `room_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;


CREATE TABLE IF NOT EXISTS `customers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `age` int(11) NOT NULL,
  `address` varchar(255) NOT NULL,
  `nationality_id` int(11) NOT NULL,
  `mobile` int(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `rooms` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `number` int(4) NOT NULL,
  `category_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

需要帮助设计数据库,这是一个更好的方法。

We are building an Reservation booking system. There are three modules

  1. Customers
  2. Rooms
  3. Bookings

A customer can book one or many rooms and for varying dates. While booking a room(s) i want to search for which rooms are available to be booked between date A and date B.

Tables ( likely solution)
customers(id, name, .....)
rooms(id, roomNo, roomType, ....)
bookings(id, room_id, fromDate, toDate)

Presently i have the tables like this

CREATE TABLE IF NOT EXISTS `bookings` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `checkin` datetime NOT NULL,
  `checkout` datetime NOT NULL,
  `advance` int(11) NOT NULL,
  `amount` int(11) NOT NULL,
  `booking_details_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


CREATE TABLE IF NOT EXISTS `bookings_rooms` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `booking_id` int(11) NOT NULL,
  `room_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;


CREATE TABLE IF NOT EXISTS `customers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `age` int(11) NOT NULL,
  `address` varchar(255) NOT NULL,
  `nationality_id` int(11) NOT NULL,
  `mobile` int(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `rooms` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `number` int(4) NOT NULL,
  `category_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

Need help to design the database and which is a better approach.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

关于从前 2024-12-26 11:47:10

这些模式结构已经允许您找到两个日期之间的空闲房间。

以下查询将检查在开始时间之前预订的房间是否会在开始时间之前空闲,或者对于那些在开始时间之后预订的房间,它会检查他们是否也在结束日期之后办理入住。

select *
from rooms r
where id not in (
        select room_id
        from bookings_rooms br
            join bookings b on (br.booking_id=b.id)
        where (checkin < :dateA and checkout > :dateA)
            or (checkin > :dateA and checkin < :dateB)
    )

These schema structures already allow you to find the free rooms between two dates.

The following query will check if rooms booked before start-time will be free before start-time or, for those ones booked after, it checks if they're checkin is following end-date too.

select *
from rooms r
where id not in (
        select room_id
        from bookings_rooms br
            join bookings b on (br.booking_id=b.id)
        where (checkin < :dateA and checkout > :dateA)
            or (checkin > :dateA and checkin < :dateB)
    )
揪着可爱 2024-12-26 11:47:10
  1. 外键名称存在混淆 - booking_id 逻辑上应该是 bookings_id

  2. 每个表都有重复的键 -您可以删除所有 booking_details_id 等,因为它们扮演 id 的角色

  3. 您为每个表重复的键拥有 - 您可以删除所有 booking_details_id 等,因为它们扮演 id

    customers 不以任何方式引用

  4. 您实现房间的方式 - 很好,但一切都取决于您对它们的处理方式。也许你可以用稍微不同的方式设计它,以便你可以轻松地获取空房间列表(你可以从这个结构中获取空闲房间列表,但性能并不壮观)

  1. there is a confusion in the name of foreign keys - booking_id should be logically bookings_id

  2. you have for each table duplicate keys - you can remove all booking_details_id etc as they play the role of id

  3. customers are not referenced in any way

  4. the way you implement rooms - it's fine but all depends on what you do with them. Possibly you could design it in a bit different way so that you can get easily the list of empty rooms (you can get the list of free rooms from this structure, but the performance is not spectacular)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文