1

我是 MySQL 新手,我有两个名为 Person 和 Patient 的表。我正在尝试在 Patient 中创建一个与 Person 中的主键的简单外键关系。我在网上看到的所有示例都遵循我使用的相同结构,但我不断收到错误。任何帮助是极大的赞赏!

create table PERSON(
    PatientID smallint UNSIGNED NOT NULL,
    Firstname varchar (25),
    Lastname varchar (25),
    CONSTRAINT PatientID_pk PRIMARY KEY (PatientID)
);

这是我试图添加外键的表:

CREATE TABLE PATIENT(
    PatientID smallint UNSIGNED NOT NULL, 
    DoctorID smallint UNSIGNED NOT NULL,
    FOREIGN KEY (PatientID) REFERENCES PERSON(PatientID);
4

1 回答 1

1

我想这就是你想要的

create table PERSON
(
  PersonID smallint UNSIGNED NOT NULL AUTO_INCREMENT,
  Firstname varchar (25),
  Lastname varchar (25),
  CONSTRAINT PersonID_pk PRIMARY KEY (PersonID)
);

CREATE TABLE PATIENT
(
  PatientID smallint UNSIGNED NOT NULL, 
  DoctorID smallint UNSIGNED NOT NULL,
  FOREIGN KEY (PatientID) REFERENCES PERSON(PersonID),
  FOREIGN KEY (DoctorID) REFERENCES PERSON(PersonID),
  UNIQUE KEY unique_key (PatientID, DoctorID)
);
于 2013-11-07T16:20:22.190 回答