【问题标题】:mysql table with multiple primary key compound constraint具有多个主键复合约束的mysql表
【发布时间】:2011-12-15 22:43:18
【问题描述】:

对不起,如果标题不完全..有用,但我不知道如何在标题中解释我的问题。

所以基本上,我想创建一个这样的表:

预订

   day
   room
   id_client
   [other_stuff]

对于给定的一天+房间,您可以获得 id_client + 其他所有内容。而且对于给定的 id_client + day,您可以获得房间 + 其他东西。

我不完全明白我应该怎么说复合 day+room 必须是唯一的,并且复合 day+id_client 也必须是唯一的。我的数据库中确实需要这两个约束。

有人有想法吗?

谢谢。

【问题讨论】:

标签: mysql


【解决方案1】:

将一个组合定义为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)
) ;

【讨论】:

  • 我会选择最后一个,如果其他内容不止几个字段,我会为它准备另一个表格,以减少添加更多内容的影响。
【解决方案2】:
-- 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;

【讨论】:

  • 大声笑,正在发布一个类似的解决方案,这将是我的首选架构设计。
【解决方案3】:

有两种看待这个的方式......你提到的独特约束是相互排斥的吗?意思是,一个可以没有另一个而存在吗?

逻辑规定,一个房间一次只能预订一天,无论客户是谁。除非多个客户可以共享同一个房间。所以我给你两个选择。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 2013-10-06
    相关资源
    最近更新 更多