当前位置: 首页 > 知识库问答 >
问题:

MySql和创建外键

郑宜民
2023-03-14
    -- MySQL Script generated by MySQL Workbench
-- Sun Apr  2 17:50:54 2017
-- Model: New Model    Version: 1.0
-- MySQL Workbench Forward Engineering

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';

-- -----------------------------------------------------
-- Schema SintalDB
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Schema SintalDB
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `SintalDB` DEFAULT CHARACTER SET utf8 COLLATE utf8_slovenian_ci ;
USE `SintalDB` ;

-- -----------------------------------------------------
-- Table `SintalDB`.`User`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SintalDB`.`User` (
  `PK_User` INT NOT NULL,
  `Name` VARCHAR(15) NOT NULL,
  `Surname` VARCHAR(25) NOT NULL,
  `Phone_Nr` VARCHAR(13) NOT NULL,
  `Email` VARCHAR(90) NOT NULL,
  `Car_Reg_Nr` VARCHAR(10) NULL,
  `Username` VARCHAR(20) NOT NULL,
  `Password` VARCHAR(20) NOT NULL,
  `Servis_Input_Rights` TINYINT(1) NOT NULL,
  `Servis_Output_Rights` TINYINT(1) NOT NULL,
  `Servis_Delete_Rights` TINYINT(1) NOT NULL,
  `Admin_Rights` TINYINT(1) NOT NULL,
  PRIMARY KEY (`PK_User`))
ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_slovenian_ci;


-- -----------------------------------------------------
-- Table `SintalDB`.`City`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SintalDB`.`City` (
  `PK_City` INT NOT NULL,
  `Name` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`PK_City`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_slovenian_ci;


-- -----------------------------------------------------
-- Table `SintalDB`.`Company`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SintalDB`.`Company` (
  `PK_Company` INT NOT NULL,
  `Name` VARCHAR(25) NOT NULL,
  `Address` VARCHAR(45) NOT NULL,
  `Address_Nr` VARCHAR(4) NOT NULL,
  `FK_City` INT NOT NULL,
  PRIMARY KEY (`PK_Company`),
  INDEX `FK_City_Company_fkn_idx` (`FK_City` ASC),
  CONSTRAINT `FK_City_Company_fkn`
    FOREIGN KEY (`FK_City`)
    REFERENCES `SintalDB`.`City` (`PK_City`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_slovenian_ci;


-- -----------------------------------------------------
-- Table `SintalDB`.`Facility`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SintalDB`.`Facility` (
  `PK_Facility_AD` VARCHAR(6) NOT NULL,
  `Name` VARCHAR(60) NOT NULL,
  `Address` VARCHAR(45) NOT NULL,
  `Address_Nr` VARCHAR(4) NOT NULL,
  `FK_City` INT NOT NULL,
  `FK_Company_Affiliation` INT NOT NULL,
  PRIMARY KEY (`PK_Facility_AD`),
  INDEX `FK_City_Facility_fkn_idx` (`FK_City` ASC),
  INDEX `FK_Company_Affiliation_Facility_fkn_idx` (`FK_Company_Affiliation` ASC),
  CONSTRAINT `FK_City_Facility_fkn`
    FOREIGN KEY (`FK_City`)
    REFERENCES `SintalDB`.`City` (`PK_City`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_Company_Affiliation_Facility_fkn`
    FOREIGN KEY (`FK_Company_Affiliation`)
    REFERENCES `SintalDB`.`Company` (`PK_Company`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_slovenian_ci;


-- -----------------------------------------------------
-- Table `SintalDB`.`Servis`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SintalDB`.`Servis` (
  `PK_Servis_Nr` VARCHAR(11) NOT NULL,
  `FK_Facility_AD` VARCHAR(6) NOT NULL,
  `Description` VARCHAR(255) NOT NULL,
  `Date_Recived` DATETIME NOT NULL,
  `FK_User_Commited` INT NULL,
  `Date_Commited` DATETIME NULL,
  `FK_User_Done` INT NULL,
  `Date_Done` DATETIME NULL,
  PRIMARY KEY (`PK_Servis_Nr`),
  INDEX `FK_User_Servis_fkn_idx` (`FK_User_Commited` ASC, `FK_User_Done` ASC),
  INDEX `FK_Facility_Servis_fkn_idx` (`FK_Facility_AD` ASC),
  CONSTRAINT `FK_User_Servis_fkn`
    FOREIGN KEY (`FK_User_Commited` , `FK_User_Done`)
    REFERENCES `SintalDB`.`User` (`PK_User` , `PK_User`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_Facility_Servis_fkn`
    FOREIGN KEY (`FK_Facility_AD`)
    REFERENCES `SintalDB`.`Facility` (`PK_Facility_AD`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_slovenian_ci;


-- -----------------------------------------------------
-- Table `SintalDB`.`ServisNote`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SintalDB`.`ServisNote` (
  `PK_ServisNote` INT NOT NULL,
  `Note` VARCHAR(255) NOT NULL,
  `FK_User_Note` INT NOT NULL,
  `Date_Created` DATETIME NOT NULL,
  `FK_Servis_Nr` VARCHAR(11) NOT NULL,
  PRIMARY KEY (`PK_ServisNote`),
  INDEX `FK_Servis_ServisNote_fkn_idx` (`FK_Servis_Nr` ASC),
  INDEX `FK_User_ServisNote_fkn_idx` (`FK_User_Note` ASC),
  CONSTRAINT `FK_Servis_ServisNote_fkn`
    FOREIGN KEY (`FK_Servis_Nr`)
    REFERENCES `SintalDB`.`Servis` (`PK_Servis_Nr`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_User_ServisNote_fkn`
    FOREIGN KEY (`FK_User_Note`)
    REFERENCES `SintalDB`.`User` (`PK_User`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_slovenian_ci;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

这是我的sql文件,我得到错误后,我运行sql文件

共有1个答案

笪涛
2023-03-14

检查以下内容:在多列上添加外键

我认为您必须添加两个外键以引用同一列。您可以为此替换代码并进行测试

-- -----------------------------------------------------
-- Table `SintalDB`.`Servis`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `SintalDB`.`Servis` (
  `PK_Servis_Nr` VARCHAR(11) NOT NULL,
  `FK_Facility_AD` VARCHAR(6) NOT NULL,
  `Description` VARCHAR(255) NOT NULL,
  `Date_Recived` DATETIME NOT NULL,
  `FK_User_Commited` INT NULL,
  `Date_Commited` DATETIME NULL,
  `FK_User_Done` INT NULL,
  `Date_Done` DATETIME NULL,
  PRIMARY KEY (`PK_Servis_Nr`),
  INDEX `FK_User_Servis_fkn_idx` (`FK_User_Commited` ASC, `FK_User_Done` ASC),
  INDEX `FK_Facility_Servis_fkn_idx` (`FK_Facility_AD` ASC),
  CONSTRAINT `FK_User_Servis_fkn`
    FOREIGN KEY (`FK_User_Commited`)
    REFERENCES `SintalDB`.`User` (`PK_User` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_User_Servis_2_fkn`
    FOREIGN KEY (`FK_User_Done`)
    REFERENCES `SintalDB`.`User` (`PK_User`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `FK_Facility_Servis_fkn`
    FOREIGN KEY (`FK_Facility_AD`)
    REFERENCES `SintalDB`.`Facility` (`PK_Facility_AD`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_slovenian_ci;
 类似资料:
  • 我在创建mysql数据库中现有表的外键时遇到了一些问题。 我不想创建一个名为的新表来引用它,使用以下方法: 但我发现了错误: 为了获得更多的信息,我做了: 在我看来,这两个列的类型似乎是匹配的,因为它们都是varchar(45)。(我还尝试将列设置为not null,但这没有解决问题)所以我猜问题一定是。但我不太清楚这意味着什么,也不知道如何检查/修复它。有人有什么建议吗?是什么意思?

  • 我试图在两个表之间创建一个外键约束,但在执行alter命令之后,mysql创建了索引而不是外键。 我使用的是hibernate,所以最初我认为这是hibernate的问题,但当我直接在mysql上执行查询时,行为是一样的。 更改表person添加约束FK9ircw28d19mdg5pu8yfg1qs8p外键(Address_Id)引用地址(Address_Id) 运行此命令后,我会在mysql数据

  • 问题内容: 如何创建外键 从表tGeoAnswers列’locationId’ 表tLocations列“ ID”? 我正在尝试找到的这段代码,但出现以下错误: “ FK_Answer_Location”列的定义必须包含数据类型 问题答案: 否则,假设您要添加名为的列。

  • 本文向大家介绍MySQL中外键的创建、约束以及删除,包括了MySQL中外键的创建、约束以及删除的使用技巧和注意事项,需要的朋友参考一下 前言 在MySQL 3.23.44版本后,InnoDB引擎类型的表支持了外键约束。 外键的使用条件: 1.两个表必须是InnoDB表,MyISAM表暂时不支持外键(据说以后的版本有可能支持,但至少目前不支持); 2.外键列必须建立了索引,MySQL 4.1.2以后

  • 方法正在工作,我已经创建了其他简单的表没有问题。 我用的是MySQL服务器8.0 这是对表的测试,然后我将创建一个脚本来创建它们。我一直有这个错误: 无法创建表,SQLException:无法添加外键约束 但我不知道怎么了。 plhd--11/>ConnectionImpl.execSQL(ConnectionImpl.java:2570)[23:51:36 WARN]:在com.mysql.jd

  • 我试图在MySQL中创建一个有两个外键的表,这两个外键引用了其他两个表中的主键,但我得到一个errno:150错误,它将无法创建该表。 下面是所有3个表的SQL: 如有任何帮助,将不胜感激。