從視點的關係,數據應該是足夠的是
- 粒狀的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答案中總結出來,而且你沒有提供足夠的信息來回答你的具體情況。 –
感謝您的資源。這足以讓我加快速度嗎? –
因爲我不知道你現在走得有多快,或者你需要走多快,我不知道這是否會讓你「加快速度」。整本教科書都寫在這個話題上,所以我的猜測是不,它不會。 –