简体   繁体   English

在SQL中创建并构造一个复杂的座位结构?

[英]Create and structure a complex seating structure in SQL?

I am trying to create a data structure for stadium seating in mySQL for a booking system. 我正在尝试在预订系统的mySQL中为体育场座位创建数据结构。 I want to be able to query the database, to iterate over blocks to create a graphic representation and be able to book seats. 我希望能够查询数据库,遍历数据块以创建图形表示形式并能够预定座位。 The situation is: 情况是:

  • Many blocks of seats, we'll say A to G. 座位很多,我们将说A到G。
  • 'x' amounts of rows of seats A,B,C… varying by block. 座位A,B,C…的“ x”行数量逐块变化。
  • 'y' numbers of seats, varying by row (A: 35,B:32 etc) 'y'座位数,依行而异(A:35,B:32等)
  • Each seat would have a status (Booked/Available) and a customer ref. 每个席位都会有一个状态(已预订/可用)和一个客户参考。 (a foreign key). (外键)。

I am guessing that there will be one 'main' table with a row for every seat, and columns for block, row, position, status, customer etc. With other tables eg rows to use as foreign keys. 我猜想会有一个“主”表,每个席位都有一行,而各栏分别代表行,行,位置,状态,客户等。还有其他表,例如行用作外键。

The problem is I obviously don't want to manually insert every single seat, so how can I do this automatically by inputting how many seats are in a row, then auto-generate seats? 问题是我显然不想手动插入每个席位,因此如何通过输入连续多少个席位然后自动生成席位来自动执行此操作?

Finally, how can I re-use this, so it could be used more than once for more events? 最后,我该如何重用它,以便在更多事件中可以多次使用它? Adding a record for every seat in every event will be resource demanding. 在每个事件中为每个席位添加记录将需要大量资源。

So, what is a way to structure this data, and what would I use to make my life simpler and easily generated? 那么,什么是构造这些数据的方法?我将使用什么方法使我的生活更简单,更容易生成?

The program is in java, maybe a function is needed to build the structure? 该程序在Java中,也许需要一个函数来构建结构?

You can create tables like 您可以创建如下表

   Table definition                      | Sample data
   Block - Id, Name                      | (1, 'A'), (2, 'B')
    Row - Id, BlockId, Name              | (1 , 1, 'A'), (2 , 1, 'B')
    Seat - Id, rowId, Name               | (1 , 1, 'A1'), (2 , 1, 'A2')

    Event - Id, Name, Date               | (1 , 'Musical Concert', '2016-07/05')

   EventSeats - EventId, SeatId, Status  | (1 , 1, 1), (1 , 2, 0)

PS Status 1 - Booked, Status 0 - Available PS状态1-已预订,状态0-可用

If you create models like above you can insert all of the seats of a rows in a block. 如果您像上面那样创建模型,则可以将一个行的所有座位插入一个块中。

Also you can book the seats for each event. 您也可以预订每个活动的座位。 The booking information such as the customer name etc can be stored in the EventSeat table. 预订信息(例如客户名称等)可以存储在EventSeat表中。 Hope this would answer your final question. 希望这能回答您的最后一个问题。

Now let me answer your first question. 现在,让我回答您的第一个问题。 "I don't want to manually insert every single seat" “我不想手动插入每个座位”

It's up to you to implement the logic. 实施逻辑取决于您。 What you can do is you can get the no of blocks, no of rows in each block and no of seats on each row from the user input. 您可以做的是从用户输入获取块数,每个块中无行以及每行中无席位。

And loop through it and programmatically insert the data within loop. 并循环遍历,并以编程方式在循环内插入数据。 Hope you got the idea. 希望你有主意。

I am just giving you the pseudo code here, that's given in the comment to give more readability. 我只是在这里为您提供伪代码,该注释中提供了伪代码以提高可读性。

Java code Java代码

int newEventId = 2; //e.g
EventSeat eventSeat = null
//Loop through all the blocks
for(Block block in Blocks){

    //Get all rows of each blocks
    for(Row row in block.rows){

        //Get all seats of each row
        for(Seat seat in row.seats){
            //Create each entry for one seat
            eventSeat = new EventSeat();
            eventSeat.setSeatId(seat.getSetId());
            eventSeat.setStatus(0);
            .....
            session.save(eventSeat)
        }
    }
} 

Stored prcedure 储存程序

CREATE PROCEDURE dbo.fillEventSeat @eventId INT
AS

DECLARE @seatCounter INT  @MaxSeatId INT, @seatId INT

SELECT @seatCounter = MIN(Id) , @MaxSeatId = MAX(Id) 
FROM Seat

WHILE(@seatCounter IS NOT NULL
      AND @seatCounter <= @MaxSeatId)

    SELECT @seatId = Id
    FROM Seat WHERE Id = @seatCounter

   INSERT INTO EventSeat(eventId, seatId, status) VALUES (@eventId, @seatId, 0);

   SET @seatCounter  = @seatCounter  + 1        
END
GO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM