[英]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. 同样,对于这些语言环境中的每个语言环境,他们都可以请求VIP票证或普通票。
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
). 我需要一个数据库来跟踪有多少人要求每种票证( VIP and NY
或VIP and Dallas
或Regular and NY
或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)? 我应该一行然后四列(VIP&NY,VIP&Dallas,Regular&NY和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". 该表将具有2个外键,因此为“弱”。
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.