user1092796
user1092796

Reputation: 35

mysql table with multiple primary key compound constraint

I'm sorry if the title isn't exactly.. useful, but I wasn't sure how to explain my issue in a title.

So basically, I want to create a table like that :

reservation

   day
   room
   id_client
   [other_stuff]

For a given day+room, you can get the id_client + everything else. And also for a given id_client + day you can get the room + other stuff.

I don't exactly understand how am I supposed to say that the compound day+room must be unique AND the compound day+id_client must also be unique. I really need both of those constraint in my database.

Anyone has an idea ?

Thanks.

Upvotes: 3

Views: 173

Answers (3)

Djahid Bekka
Djahid Bekka

Reputation: 516

-- 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;

Upvotes: 1

Mike Purcell
Mike Purcell

Reputation: 19989

There are two ways of looking at this... are the unique constraints you mention mutually exclusive? Meaning, can one exist without the other?

Logic dictates that a room can be booked to one day at a time, regardless of client. Unless multiple clients can share the same room. So I will give you two alternatives.

# 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;

Also, I would use a separate primary key column, otherwise your updates will be more complex, for example:

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

# Vs

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

Upvotes: 0

ypercubeᵀᴹ
ypercubeᵀᴹ

Reputation: 115550

Define one combination an PRIMARY KEY and the other as UNIQUE key:

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

or the other way around:

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

Or, if you already have another Primary Key, make them both unique:

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

Upvotes: 4

Related Questions