2011-12-02 43 views
1

三個表格。獨特的指數跨加入?

事件有許多種族

種族有多個註冊

註冊記憶上有「bib_number」

我需要做的是確保參賽號是唯一對該事件的整數。

最好的解決方案我已經是進行非規範化事項標識到註冊...然後我可以只添加一個唯一的密鑰,以註冊:

UNIQUE KEY `index_bib_number_event_id` (`bib_number`, `event_id`) 

....但我寧願避免可能的話

下面是表:

CREATE TABLE `registrations` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `race_id` int(11) NOT NULL, 
    `bib_number` int(11) DEFAULT NULL, 
    PRIMARY KEY (`id`), 
) 

CREATE TABLE `races` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `event_id` int(11) DEFAULT NULL, 
    PRIMARY KEY (`id`) 
} 

CREATE TABLE `events` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY (`id`) 
} 
+0

不知道這是可能的(這就是爲什麼這是一個評論而不是答案),但你有沒有嘗試在視圖上創建一個唯一的索引? –

+0

圍棋號碼是賽事獨有的,而不是比賽? –

+0

@Catcall,OP希望確保Bib編號對*整個*事件是唯一的,而不是*比賽。 –

回答

0

你看..你已經非規範化的registration表,因爲bib_number不依賴於PK(ID) 。所以,你可以隨意移動event_idregistration,但我寧願再次看看數據庫模型,並試圖找出它是好的。可能你在設計db時錯過了一些東西。

0

沿着這些線的東西應該工作。我相信你可以修復MySQL的語法。

我省略了自動編號,因爲它們可以隱藏真正發生的事情。所有表格都至少有5NF,如果您使用自動增量數字,則可能會忽略這一點。

create table events (
    event_id integer primary key 
); 

create table races (
    event_id integer not null references events (event_id), 
    race_id integer not null, 
    primary key (event_id, race_id) 
); 

create table registrations (
    event_id integer not null, 
    race_id integer not null, 

    foreign key (event_id, race_id) 
    references races (event_id, race_id), 

    registration_id integer not null, 
    primary key (event_id, race_id, registration_id), 

    bib_number integer not null, 

    unique (event_id, bib_number) 
); 

下面是一些示例數據。

-- Two events. 
insert into events values (1); 
insert into events values (2); 

-- Three races in each event. 
insert into races values (1,1); 
insert into races values (1,2); 
insert into races values (1,3); 
insert into races values (2,1); 
insert into races values (2,2); 
insert into races values (2,3); 

-- Some registrations. 
insert into registrations values (1, 1, 1, 51); 
insert into registrations values (1, 1, 2, 52); 
insert into registrations values (1, 1, 3, 53); 
insert into registrations values (1, 1, 4, 54); 
insert into registrations values (1, 2, 1, 61); 
insert into registrations values (1, 2, 2, 62); 
insert into registrations values (1, 2, 3, 63); 
insert into registrations values (1, 2, 4, 64); 
insert into registrations values (1, 3, 1, 71); 
insert into registrations values (1, 3, 2, 72); 
insert into registrations values (1, 3, 3, 73); 
insert into registrations values (1, 3, 4, 74); 

-- These bib numbers were already used, but not in event 2. 
insert into registrations values (2, 1, 1, 51); 
insert into registrations values (2, 1, 2, 52); 
insert into registrations values (2, 1, 3, 53); 
insert into registrations values (2, 1, 4, 54); 
+0

'主鍵(event_id,race_id,registration_id)'似乎是多餘的。 「(event_id,race_id)」或「(registration_id)」可以是「註冊」表的主鍵。它是OP架構中的後者,但是,當您將'event_id'列添加到'registrations'表中時,專用ID列可能完全沒有必要。你怎麼看? –

+0

@AndriyM:這很難說,因爲除了圍脖號碼之外我們沒有任何真實的數據。規範化,候選鍵的識別,功能依賴 - 這些都與真實屬性和真實數據有關,而不是代理ID號碼。就目前而言,{event_id,race_id}不足以標識註冊中的一行,但{event_id,race_id,bib_number}是。 (另外,你可以添加其他真實的屬性到「事件」中,這樣​​它只能在2NF而不是在5NF中。) –

+0

嗯,你當然是對的,我想知道我是怎麼想出來的這是一個愚蠢的想法。我可能有不同的想法,但是,如果是這樣,我現在不記得它是什麼。也許我想'race_id'是多餘的而不是'registration_id',但我不確定。無論如何,抱歉打擾你。 –