简体   繁体   English

有没有办法制作两个主键,只有一个外键引用另一个表的主键

[英]Is there a way to make two primary keys, with only one foreign key that references another tables primary key

I'm new to SQL and trying to make it so that there are two primary keys and one foreign key that references other tables primary key.我是 SQL 的新手,并试图使它有两个主键和一个引用其他表主键的外键。

I've tried adding the attribute that is a primary key that is missing the table that needs referencing and then making that a foreign key but still getting the message that "number of referencing columns must match references columns".我尝试添加作为主键的属性,该属性缺少需要引用的表,然后将其设为外键,但仍然收到“引用列数必须与引用列匹配”的消息。

if there is a solution to what I'm trying to achieve it would be much appreciated.如果我想要实现的目标有解决方案,将不胜感激。

CREATE TABLE Next_of_Kin
 (
   Employee_No       VARCHAR2(8)  NOT NULL,
   kin_Number        VARCHAR2(8)  NOT NULL,
   Name              VARCHAR2(40) NOT NULL,
   relationship      VARCHAR2(40) NOT NULL,
   contact_number    VARCHAR2(11) NOT NULL,
   PRIMARY KEY (Employee_No, Kin_Number),
   FOREIGN KEY (Employee_No, Kin_Number) REFERENCES Employee(Employee_No) 
 );

CREATE TABLE Employee 
( 
  Employee_No   VARCHAR2(8)  NOT NULL, 
  family_Name   VARCHAR2(40) NOT NULL, 
  given_Name    VARCHAR2(40) NOT NULL, 
  address       VARCHAR2(80) NOT NULL, 
  date_of_Birth DATE         NOT NULL, 
  date_Hired    DATE         NOT NULL, 
  supervisor    VARCHAR2(40) NULL, 
  PRIMARY KEY (Employee_No, Supervisor), 
  FOREIGN KEY (Employee_No,Supervisor) 
  REFERENCES Employee(Employee_No, Supervisor) 
);
  • I don't know your company, but I'm pretty sure that the employee number is unique.我不知道贵公司,但我很确定员工编号是唯一的。 Thus, the primary key of your employee table should be Employee_No , without the supervisor.因此,您的员工表的主键应该是Employee_No ,没有主管。

  • The foreign key in your employee table does not make sense.您的员工表中的外键没有意义。 Remove it.去掉它。

  • In your next of kin table, leave the primary key as it is, but make only the employee number a foreign key:在您的近亲表中,保持主键不变,但仅将员工编号设为外键:

     FOREIGN KEY (Employee_No) REFERENCES Employee(Employee_No)

Presumably supervisor references an employee number (because the supervisor is also an employee), so it should only be one column.大概supervisor引用了一个员工编号(因为主管也是员工),所以它应该只有一列。

Also, there's no reason why a next of kin entry should refer to another next of kin.此外,没有理由为什么近亲条目应该引用另一个近亲。 All you need to enforce is that the employee number refers to an existing employee.您需要强制执行的是员工编号是指现有员工。

Single-column keys can be declared as part of the column definition, which simplifies the syntax and also allows the datatype of foreign key columns to be inherited from the parent.单列键可以声明为列定义的一部分,这样可以简化语法,并且还允许从父级继承外键列的数据类型。

create table employee 
( employee_no       varchar2(8)  primary key
, family_name       varchar2(40) not null
, given_name        varchar2(40) not null
, address           varchar2(80) not null
, date_of_birth     date         not null
, date_hired        date         not null
, supervisor        references employee(employee_no)
);

create table next_of_kin
( employee_no       references employee (employee_no) not null
, kin_number        varchar2(8)  not null
, name              varchar2(40) not null
, relationship      varchar2(40) not null
, contact_number    varchar2(11) not null
, primary key (employee_no, kin_number)
);

A table can have as many unique constraints as you like, but only one primary key.一个表可以有任意多的唯一约束,但只有一个主键。 In your example though, you are trying to define one primary key with two columns, which is allowed (although not needed here).但是,在您的示例中,您尝试使用两列定义一个主键,这是允许的(尽管此处不需要)。 You can also have a foreign key with more than one column, as long as it matches a corresponding primary or unique constraint in the specified table.您还可以拥有一个包含多个列的外键,只要它与指定表中的相应主约束或唯一约束匹配即可。

It looks like you want to have EMPLOYEE_NO be the primary key on EMPLOYEE, but you also want (EMPLOYEE_NO, SUPERVISOR) to be unique.看起来您希望 EMPLOYEE_NO 成为 EMPLOYEE 的主键,但您也希望 (EMPLOYEE_NO, SUPERVISOR) 是唯一的。 Note that this unique key is unnecessary - becaue EMPLOYEE_NO is the primary key on EMPLOYEE it's guaranteed to be unique, and thus any combination of (EMPLOYEE_NO, <some other column on EMPLOYEE) will automatically be unique - but sometimes I throw in a unique key like this just as documentation, or to make it referenceable in a foreign key constraint from another table.请注意,此唯一键是不必要的 - 因为 EMPLOYEE_NO 是 EMPLOYEE 上的主键,它保证是唯一的,因此 (EMPLOYEE_NO, <EMPLOYEE 上的其他列) 的任何组合将自动是唯一的 - 但有时我会输入一个唯一键像这样作为文档,或者使它在另一个表的外键约束中可引用。

So anyways, consider using:所以无论如何,考虑使用:

 (
   Employee_No       VARCHAR2(8)  NOT NULL,
   kin_Number        VARCHAR2(8)  NOT NULL,
   Name              VARCHAR2(40) NOT NULL,
   relationship      VARCHAR2(40) NOT NULL,
   contact_number    VARCHAR2(11) NOT NULL,
   PRIMARY KEY (Employee_No, Kin_Number),
   FOREIGN KEY (Employee_No) REFERENCES Employee(Employee_No) 
 );

CREATE TABLE Employee 
( 
  Employee_No   VARCHAR2(8)  NOT NULL, 
  family_Name   VARCHAR2(40) NOT NULL, 
  given_Name    VARCHAR2(40) NOT NULL, 
  address       VARCHAR2(80) NOT NULL, 
  date_of_Birth DATE         NOT NULL, 
  date_Hired    DATE         NOT NULL, 
  supervisor    VARCHAR2(40) NULL, 
  PRIMARY KEY (Employee_No),
  UNIQUE KEY  (Employee_No, Supervisor)
);

Since (EMPLOYEE_NO, KIN_NUMBER) is the primary key on NEXT_OF_KIN it makes no sense to have a self-referencing foreign key utilizing (EMPLOYEE_NO, KIN_NUMBER) as the row would always-and-only refer to itself.由于 (EMPLOYEE_NO, KIN_NUMBER) 是 NEXT_OF_KIN 上的主键,因此使用 (EMPLOYEE_NO, KIN_NUMBER) 的自引用外键是没有意义的,因为该行将始终且仅引用自身。

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

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