简体   繁体   中英

Database schema for storing ints

I'm really new to databases so please bear with me.

I have a website where people can go to request tickets to an upcoming concert. Users can request tickets for either New York or Dallas. Similarly, for each of those locales, they can request either a VIP ticket or a regular ticket.

I need a database to keep track of how many people have requested each type of ticket ( VIP and NY or VIP and Dallas or Regular and NY or Regular and Dallas ). This way, I won't run out of tickets.

What schema should I use for this database? Should I have one row and then 4 columns (VIP&NY, VIP&Dallas, Regular&NY and Regular&Dallas)? The problem with this is it doesn't seem very flexible, thus I'm not sure if it's good design.

您应该有一列包含数量,一列指定类型(VIP),另一列指定城市。

To make it flexible you would do:

Table: 
    location
Columns:
    location_id  integer
    description  varchar

Table
    type
Columns:
    type_id      integer
    description  varchar

table 
    purchases
columns:
    purchase_id integer
    type_id     integer
    location_id integer

This way you can add more cities, more types and you allways insert them in purchases.

When you want to know how many you sold you count them

What you want to do is have one table with cities and one table with ticket types. Then you create a weak association with [city, ticket type, number of tickets].

That table will have 2 foreign keys, therefore "weak".

But this enables you to add or remove cities etc. And you can add a table for concerts as well and your weak table you will have another foreign key "concert".

I think this is the most correct way to do it.

CREATE TABLE `tickets` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `locale` varchar(45) NOT NULL,
   `ticket_type` varchar(45) NOT NULL
}

This is a simple representation of your table. Ideally you would have separate tables for locale and type. And your table would look like this:

CREATE TABLE `tickets` (
   `id` int(11) NOT NULL AUTO_INCREMENT,
   `locale_id` int(11) NOT NULL,
   `ticket_type_id` int(11) NOT NULL
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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