2011-12-15 65 views
3

如果標題不完全是有用的,但我不確定如何在標題中解釋我的問題。帶有多個主鍵複合約束的mysql表

所以基本上,我想創建這樣一個表:

預訂

day 
    room 
    id_client 
    [other_stuff] 

對於給定的日子+的房間,你可以得到id_client +一切。而且對於給定的id_client +天,您可以獲得房間+其他內容。

我不完全明白我該怎麼說複合日+房間必須是唯一的,複合日+ id_client也必須是唯一的。我真的需要這兩個數據庫中的約束。

任何人有想法?

謝謝。

+0

可能重複[我怎樣才能強制化合物MySQL的獨特性?( http://stackoverflow.com/questions/1059265/how-can-i-enforce-compound-uniqueness-in-mysql) – 2011-12-15 22:47:43

回答

4

定義一個組合的PRIMARY KEY,另一個爲UNIQUE鍵:

CREATE TABLE reservation 
( day 
, room 
, id_client 
, [other_stuff] 
, PRIMARY KEY (day, room) 
, UNIQUE KEY (id_client, day) 
) ; 

或周圍的其他方法:

CREATE TABLE reservation 
( day 
, room 
, id_client 
, [other_stuff] 
, PRIMARY KEY (id_client, day) 
, UNIQUE KEY (day, room) 
) ; 

或者,如果你已經有另外一個主鍵,使它們都是獨特的:

CREATE TABLE reservation 
( reservation_id 
, day 
, room 
, id_client 
, [other_stuff] 
, PRIMARY KEY (reservation_id) 
, UNIQUE KEY (id_client, day) 
, UNIQUE KEY (day, room) 
) ; 
+0

最後一個將是我的選擇,如果其他東西超過幾個領域,我會還有其他的表格,以減少添加更多東西的影響。 – 2011-12-15 23:04:06

1
-- in MySQL 

drop database if exists mydatabase; 
create database mydatabase; 

use mydatabase; 

drop table if exists client; 
create table client 
(
    id int unsigned not null auto_increment, 
    name varchar(45) not null, 
    primary key (id) 
)engine=InnoDB default charset=utf8; 


drop table if exists room; 
create table room 
(
    id int unsigned not null auto_increment, 
    label varchar(45) not null, 
    primary key (id) 
)engine=InnoDB default charset=utf8; 



drop table if exists reservation; 
create table reservation 
(
    id int unsigned not null auto_increment, 
    id_room int unsigned, 
    id_client int unsigned, 
    day date, 
    unique(day, id_room), 
    unique(day, id_client), 
    foreign key (id_room) references room(id), 
    foreign key (id_client) references client(id), 
    primary key (id) 
)engine=InnoDB default charset=utf8; 
+0

Lol正在發佈類似的解決方案,這將是我首選的模式設計。 – 2011-12-15 23:07:39

0

有兩種方法來看待這...是你提到的獨特約束是互斥的嗎?意思是說,沒有其他人可以存在嗎?

邏輯表明一個房間可以一次預訂一天,而不管客戶。除非多個客戶可以共享同一個房間。所以我會給你兩個選擇。

# If room can be booked to multiple clients 
CREATE TABLE `reservation` (
    `id` int(11) unsigned not null auto_increment, 
    `day` varchar(25) not null, 
    `room` int(5) unsigned not null, 
    `id_client` int(11) unsigned not null, 
    PRIMARY KEY (`id`), 
    UNIQUE KEY (`room`, `day`), 
    UNIQUE KEY (`room`, `id_client`), 
)ENGINE=InnoDB DEFAULT CHARSET=utf8; 

# Room can only be booked to one client for a given day 
CREATE TABLE `reservation` (
    `id` int(11) unsigned not null auto_increment, 
    `day` varchar(25) not null, 
    `room` int(5) unsigned not null, 
    `id_client` int(11) unsigned not null, 
    PRIMARY KEY (`id`), 
    UNIQUE KEY (`room`, `day`) 
)ENGINE=InnoDB DEFAULT CHARSET=utf8; 

而且,我會用一個單獨的主鍵列,否則您的更新會更復雜,例如:

UPDATE `reservation` SET `other_stuff` = 'some value' WHERE `day` = 'Friday' AND `room` = 123; 

# Vs 

UPDATE `reservation` SET `other_stuff` = 'some value' WHERE `id` = 1;