1对多关系的复合主键?

时间:2016-03-22 01:30:57

标签: sql postgresql

好吧,我总是这样做:

create table language (
  id bigserial primary key,
  code varchar(10) not null
);

create table entity (
  id bigserial primary key
);

create table entity_description (

  id bigserial primary key,

  language_id bigint not null,

  constraint fk__entity_description__language
    foreign key language_id
    references language(id),

  entity_id bigint not null,

  constraint fk__entity_description__entity
    foreign key entity_id
    references entity(id),

  name varchar(20) not null
);

但我想知道.. 为什么我不会这样做:

create table language (
  id bigserial primary key,
  code varchar(10) not null
);

create table entity (
  id bigserial primary key
);

create table entity_description (

  language_id bigint not null,

  constraint fk__entity_description__language
    foreign key language_id
    references language(id),

  entity_id bigint not null,

  constraint fk__entity_description__entity
    foreign key entity_id
    references entity(id),

  primary key (language_id, entity_id),

  name varchar(20) not null
);

我的问题是

  • 在第二个示例中是否有任何与复合主键相反的内容?
  • 有什么好处吗?
  • 在第二个示例中,(language_id, entity_id)language_identity_id是否为表格entity_description分别编入索引?如果我提取的是.. WHERE entity_description.id = x,那么这比获取.. WHERE (entity_description.language_id, entity_description.entity_id) = (x,y)要慢吗?

0 个答案:

没有答案