从视点的关系,数据应该是足够的是
- 粒状的DBMS可以在其上执行明智约束和
- 客户代码永远解析它。
假设只有一个场地(只有一个平面布置图),企业通常会根据其部分,行和编号来识别座位。多个平面布置图和多个场地的原理是相同的。
我们假设第1节有3行,第2节有5行,第3节有4行。 (经测试,在PostgreSQL的。)
create table sections (
section_num integer primary key
check (section_num between 1 and 3)
);
insert into sections values (1), (2), (3);
create table section_rows (
section_num integer not null
references sections (section_num),
row_num integer not null
check (row_num between 1 and 5),
primary key (section_num, row_num)
);
insert into section_rows values
(1,1), (1,2), (1,3),
(2,1), (2,2), (2,3), (2,4), (2,5),
(3,1), (3,2), (3,3), (3,4);
create table seats (
section_num integer not null,
row_num integer not null,
seat_num integer not null,
primary key (section_num, row_num, seat_num),
foreign key (section_num, row_num)
references section_rows (section_num, row_num)
);
insert into seats values
(1, 1, 1), (1, 1, 2), (1, 1, 3),
(1, 2, 1), (1, 2, 2), (1, 2, 3),
(1, 3, 1), (1, 3, 2), (1, 3, 3), (1, 3, 4),
(2, 1, 1), (2, 1, 2), (2, 1, 3),
(2, 2, 1), (2, 2, 2), (2, 2, 3),
(2, 3, 1), (2, 3, 2), (2, 3, 3), (2, 3, 4),
(2, 4, 1), (2, 4, 2), (2, 4, 3), (2, 4, 4),
(2, 5, 1), (2, 5, 2), (2, 5, 3), (2, 5, 4), (2, 5, 5),
(3, 1, 1), (3, 1, 2), (3, 1, 3),
(3, 2, 1), (3, 2, 2), (3, 2, 3),
(3, 3, 1), (3, 3, 2), (3, 3, 3), (3, 3, 4),
(3, 4, 1), (3, 4, 2), (3, 4, 3), (3, 4, 4);
这最后一个表, “席位” 标识在会场每一个座位。一旦这三张桌子被填满,你就不需要改变它们,除非你拆掉座位或安装新的座位。
现在你可以把每一个卖给顾客。
create table event_sales (
-- Assumes an event identifier identifies the date and time as well
-- as the event's name.
event_id integer not null, -- references events (not shown)
section_num integer not null,
row_num integer not null,
seat_num integer not null,
customer_columns_go_here char(1) default 'x',
primary key (event_id, section_num, row_num, seat_num),
foreign key (section_num, row_num, seat_num)
references seats (section_num, row_num, seat_num)
);
insert into event_sales values
(1, 1, 1, 1, 'a'),
(1, 1, 1, 2, 'a'),
(1, 1, 1, 3, 'a'),
(1, 2, 2, 1, 'b'),
(2, 2, 1, 1, 'a'),
(2, 2, 1, 2, 'b'),
(2, 2, 1, 3, 'c'),
(2, 3, 2, 1, 'd');
所有这些表格至少在5NF。
事件1有哪些座位可用? (可能是座位应用中最常见的查询。)
select *
from seats
except
(select section_num, row_num, seat_num from event_sales where event_id = 1)
order by section_num, row_num, seat_num;
数据库设计是一个比大多数人认为它更大的话题。你不可能通过浏览几个网站来完成它。学习时避免不良习惯。我想你可能是Bill Karwin的书SQL Antipatterns: Avoiding the Pitfalls of Database Programming最好的。
您需要了解[数据库规范化](http://holowczak.com/database-normalization/)。这是一个太大的话题,要在一个SO答案中总结出来,而且你没有提供足够的信息来回答你的具体情况。 –
感谢您的资源。这足以让我加快速度吗? –
因为我不知道你现在走得有多快,或者你需要走多快,我不知道这是否会让你“加快速度”。整本教科书都写在这个话题上,所以我的猜测是不,它不会。 –