简体   繁体   English

外键可以引用组合的唯一键吗?

[英]Can a Foreign key reference a combined Unique key?

Is there a way to reference a foreign key to a 2row-unique key? 有没有办法将外键引用到2行唯一键?

CREATE TABLE factura( 

num_factura varchar(10), 

ID varchar(10), 

UNIQUE (num_factura,ID)

);

CREATE TABLE detalle_factura(

clave_factura varchar(30),

FOREIGN KEY(clave_factura) references factura(num_factura,ID) 

--I know this is not the right way to do it!


);

I don't know if this makes sense, but if you get it, please help me. 我不知道这是否有意义,但是如果您明白了,请帮助我。

To start with: If a table contains a column called ID, I expect this to be the unique ID that identifies a record. 首先,如果表包含名为ID的列,则我希望它是标识记录的唯一ID。 Often the ID is used as the table's primary key. 通常,ID用作表的主键。

You can also have natural keys. 您也可以使用自然键。 As your table is called factura and there is a column num_factura, I would think that this is the unique factura number issued. 由于您的表称为factura,并且有一列num_factura,我认为这是发出的唯一的factura号。

If this is so, then UNIQUE (num_factura,ID) would not be helpful, as you want the single attributes to be unique, not the pair of them. 如果是这样,则UNIQUE(num_factura,ID)将无济于事,因为您希望单个属性是唯一的,而不是一对。 With the UNIQUE constraint given thus, you could still have duplicate ID and duplicate num_factura. 这样,在给出UNIQUE约束的情况下,您仍然可以具有重复的ID和重复的num_factura。 Use this instead: 使用此代替:

CREATE TABLE factura
( 
  num_factura varchar(10) not null, 
  ID varchar(10) not null, 
  UNIQUE KEY (num_factura),
  UNIQUE KEY (ID)
);

(You can also replace on of the UNIQUE KEY keywords with PRIMARY KEY . It doesn't matter which. You would usually use the one which you reference in other tables.) (您也可以使用PRIMARY KEY替换UNIQUE KEY关键字中的一个。这无关紧要。您通常会使用在其他表中引用的那个。)

Now you build your database either ID based or natuaral key based. 现在,您可以构建基于ID或基于自然密钥的数据库。

Here is an example for the ID concept: 这是ID概念的示例:

create table factura
( 
  id int not null auto_increment,
  num_factura varchar(10) not null, 
  ...
  primary key (id),
  unique key (num_factura)
);

create table detalle_factura
(
  id int not null auto_increment,
  id_faktura int,
  ...
  primary key (id),
  foreign key(id_faktura) references factura(id)
);

And here is an example for the natural key concept: 这是自然键概念的示例:

create table factura
( 
  num_factura varchar(10) not null, 
  id int not null auto_increment, -- In case you want an additional id for the table (for logging maybe). It is not needed.
  ...
  primary key (num_factura),
  unique key (id) -- In case the table shall have this column.
);

create table detalle_factura
(
  faktura_num varchar(10),
  faktura_sub_num varchar(10),
  id int not null auto_increment, -- Again: Only if you want this additional ID for some purpose.
  ...
  primary key (faktura_num, faktura_sub_num),
  foreign key(faktura_num) references factura(faktura_num),
  unique key (id) -- ditto
);

It's up to you which concept to use. 由您决定要使用哪种概念。 The ID concept has its strength in its technical approach - you can rather easily change the database structure. ID概念在其技术方法上具有优势-您可以轻松地更改数据库结构。 The natural key concept is strong when it comes to data integrity and easy data access. 当涉及到数据完整性和易于访问的数据时,自然键概念很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM