好的,这是一次更新。我正在使用MySQL Workbench。这是我在前进工程师时所进入的。中途通过后,您将看到正向工程后的结果。
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
USE `mh23202` ;
-- -----------------------------------------------------
-- Table `mh23202`.`Patient`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mh23202`.`Patient` ;
CREATE TABLE IF NOT EXISTS `mh23202`.`Patient` (
`Patient Number` INT NOT NULL,
`First Name` TEXT NULL,
`Last Name` TEXT NULL,
`Date of Birth` DATE NULL,
`Phone Number` INT NULL,
`Email` TEXT NULL,
`Gender` TEXT NULL,
`Race` TEXT NULL,
`Address` TEXT NULL,
`Reason for Visit` TEXT NULL,
`Primary Doctor` TEXT NULL,
PRIMARY KEY (`Patient Number`),
CONSTRAINT `Visit Number`
FOREIGN KEY (`Patient Number`)
REFERENCES `mh23202`.`Visits` (`Visit Number`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Physician Number`
FOREIGN KEY (`Patient Number`)
REFERENCES `mh23202`.`Physician` (`Physician Number`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mh23202`.`Visits`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mh23202`.`Visits` ;
CREATE TABLE IF NOT EXISTS `mh23202`.`Visits` (
`Visit Number` INT NOT NULL,
`Date of Visit` DATE NULL,
`Time of Visit` TIME NULL,
`Reason for Visit` TEXT NULL,
PRIMARY KEY (`Visit Number`),
CONSTRAINT `Patient Number`
FOREIGN KEY (`Visit Number`)
REFERENCES `mh23202`.`Patient` (`Patient Number`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Physician Number`
FOREIGN KEY (`Visit Number`)
REFERENCES `mh23202`.`Physician` (`Physician Number`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mh23202`.`Physician`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mh23202`.`Physician` ;
CREATE TABLE IF NOT EXISTS `mh23202`.`Physician` (
`Physician Number` INT NOT NULL,
`Specialty` TEXT NULL,
`First name` TEXT NULL,
`Last Name` TEXT NULL,
`Date of Birth` DATE NULL,
`Phone Number` INT NULL,
`Email` TEXT NULL,
`Gender` TEXT NULL,
`Race` TEXT NULL,
`Address` TEXT NULL,
PRIMARY KEY (`Physician Number`),
CONSTRAINT `Visit Number`
FOREIGN KEY (`Physician Number`)
REFERENCES `mh23202`.`Visits` (`Visit Number`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mh23202`.`Medication`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mh23202`.`Medication` ;
CREATE TABLE IF NOT EXISTS `mh23202`.`Medication` (
`Prescription Number` INT NOT NULL,
`Prescription Name` TEXT NULL,
`Units per Day` TEXT NULL,
`Start Date` DATE NULL,
`Finish Date` DATE NULL,
PRIMARY KEY (`Prescription Number`),
CONSTRAINT `Patient Number`
FOREIGN KEY (`Prescription Number`)
REFERENCES `mh23202`.`Patient` (`Patient Number`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Physician Number`
FOREIGN KEY (`Prescription Number`)
REFERENCES `mh23202`.`Physician` (`Physician Number`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mh23202`.`MedicalRecord`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mh23202`.`MedicalRecord` ;
CREATE TABLE IF NOT EXISTS `mh23202`.`MedicalRecord` (
`Physician Number` INT NULL,
`Date of Visit` DATE NULL,
`Time of Visit` TIME NULL,
`Reason for Visit` TEXT NULL,
`Findings` TEXT NULL,
`Notes` TEXT NULL,
`Treatment` TEXT NULL,
`Prescriptions` TEXT NULL,
`Procedures` TEXT NULL,
INDEX `Patient Number_idx` (`Physician Number` ASC),
CONSTRAINT `Patient Number`
FOREIGN KEY (`Physician Number`)
REFERENCES `mh23202`.`Patient` (`Patient Number`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mh23202`.`Nurse`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mh23202`.`Nurse` ;
CREATE TABLE IF NOT EXISTS `mh23202`.`Nurse` (
`Nurse Number` INT NOT NULL,
`First Name` TEXT NULL,
`Last Name` TEXT NULL,
`Date of Birth` DATE NULL,
`Phone Number` INT NULL,
`Email` TEXT NULL,
`Gender` TEXT NULL,
`Race` TEXT NULL,
`Address` TEXT NULL,
PRIMARY KEY (`Nurse Number`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mh23202`.`Secretary`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mh23202`.`Secretary` ;
CREATE TABLE IF NOT EXISTS `mh23202`.`Secretary` (
`Secretary Number` INT NOT NULL,
`First Name` TEXT NULL,
`Last Name` TEXT NULL,
`Date of Birth` DATE NULL,
`Phone Number` INT NULL,
`Email` TEXT NULL,
`Gender` TEXT NULL,
`Race` TEXT NULL,
`Address` TEXT NULL,
PRIMARY KEY (`Secretary Number`),
CONSTRAINT `Visit Number`
FOREIGN KEY (`Secretary Number`)
REFERENCES `mh23202`.`Visits` (`Visit Number`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mh23202`.`ChargeNurse`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mh23202`.`ChargeNurse` ;
CREATE TABLE IF NOT EXISTS `mh23202`.`ChargeNurse` (
`Charge Nurse Number` INT NOT NULL,
`First Name` TEXT NULL,
`Last Name` TEXT NULL,
`Date of Birth` DATE NULL,
`Phone Number` INT NULL,
`Email` TEXT NULL,
`Gender` TEXT NULL,
`Race` TEXT NULL,
`Address` TEXT NULL,
PRIMARY KEY (`Charge Nurse Number`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mh23202`.`Shifts`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mh23202`.`Shifts` ;
CREATE TABLE IF NOT EXISTS `mh23202`.`Shifts` (
`Shift Number` INT NOT NULL,
`Date of Shift` DATE NULL,
`Time of Shift` TIME NULL,
PRIMARY KEY (`Shift Number`),
CONSTRAINT `Secertary Number`
FOREIGN KEY (`Shift Number`)
REFERENCES `mh23202`.`Secretary` (`Secretary Number`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Physician Number`
FOREIGN KEY (`Shift Number`)
REFERENCES `mh23202`.`Physician` (`Physician Number`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Nurse Number`
FOREIGN KEY (`Shift Number`)
REFERENCES `mh23202`.`Nurse` (`Nurse Number`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Charge Nurse Number`
FOREIGN KEY (`Shift Number`)
REFERENCES `mh23202`.`ChargeNurse` (`Charge Nurse Number`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS; `
这就是结果。
ERROR: Error 1005: Can't create table 'mh23202.visits' (errno: 121)
CREATE TABLE IF NOT EXISTS `mh23202`.`Visits` (
`Visit Number` INT NOT NULL,
`Date of Visit` DATE NULL,
`Time of Visit` TIME NULL,
`Reason for Visit` TEXT NULL,
PRIMARY KEY (`Visit Number`),
CONSTRAINT `Patient Number`
FOREIGN KEY (`Visit Number`)
REFERENCES `mh23202`.`Patient` (`Patient Number`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Physician Number`
FOREIGN KEY (`Visit Number`)
REFERENCES `mh23202`.`Physician` (`Physician Number`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 7 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
Executing SQL script in server
ERROR: Error 1005: Can't create table 'mh23202.visits' (errno: 121)
CREATE TABLE IF NOT EXISTS `mh23202`.`Visits` (
`Visit Number` INT NOT NULL,
`Date of Visit` DATE NULL,
`Time of Visit` TIME NULL,
`Reason for Visit` TEXT NULL,
PRIMARY KEY (`Visit Number`),
CONSTRAINT `Patient Number`
FOREIGN KEY (`Visit Number`)
REFERENCES `mh23202`.`Patient` (`Patient Number`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Physician Number`
FOREIGN KEY (`Visit Number`)
REFERENCES `mh23202`.`Physician` (`Physician Number`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 7 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
答案 0 :(得分:2)
您的脚本上有很多错误。其中大多数是因为您正在使用FOREIGN KEY创建表但未指定它们。例如,在下面的引用部分的create table语句中:
CONSTRAINT `Visit Number`
FOREIGN KEY ()
REFERENCES `mh23202`.`Visits` ()
空括号是每个表上的错误。对于外键,它应该具有表中的列名称和参考 mh23202
。Visits
(),它应该具有该表中的列名。
正如评论中所说,在您的脚本上只有一个创建表,其他的是从第一个创建的表。很可能您使用的工具无法正常工作,并尝试仅为一个表创建脚本。
修改强>
编辑完成后,您可以在创建表上命令字段名称之间的空格。它应该没有或应该引用。与您的第一个创建声明一样:患者编号。
答案 1 :(得分:0)
MySQL错误代码121:写入或更新时重复密钥
FOREIGN KEY问题(Visit Number
)。
答案 2 :(得分:0)
例如,您不能使用相同名称的约束。
您始终必须确保约束名称不同,否则您将遇到此错误。我在工作台上有类似的问题。我更改了解决问题的名称。