2017-04-02 12 views
0
-- MySQL Script generated by MySQL Workbench 
-- Sun Apr 2 17:50:54 2017 
-- Model: New Model Version: 1.0 
-- MySQL Workbench Forward Engineering 

SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0; 
SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0; 
SET @[email protected]@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 [email protected]_SQL_MODE; 
SET [email protected]_FOREIGN_KEY_CHECKS; 
SET [email protected]_UNIQUE_CHECKS; 

這是我的SQL文件,我得到錯誤後,我運行SQL文件MySQL和創建外鍵的

錯誤代碼:1215無法添加外鍵約束

17:52 :08創建表如果不存在SintalDBServis
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_idxFK_User_Commited ASC,ASC FK_User_Done),INDEX FK_Facility_Servis_fkn_idxFK_Facility_AD ASC),約束 FK_User_Servis_fkn外鍵(FK_User_CommitedFK_User_Done)參考文獻SintalDBUserPK_UserPK_User)ON DELETE NO ACTION ON UPDATE NO ACTION,
約束FK_Facility_Servis_fkn外鍵(FK_Facility_AD) 參考SintalDBFacilityPK_Facility_AD)ON DELETE NO 行動UPDATE NO ACTION)ENGINE = InnoDB的默認字符集 = UTF8 COLLATE = utf8_slovenian_ci錯誤代碼:1215無法添加外鍵約束0.063秒

回答

2

檢查:Adding foreign key on multiple columns

我認爲你必須添加兩個外鍵,以便引用同一列。 您可以替換您的代碼並進行測試。

-- ----------------------------------------------------- 
-- 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; 
+0

我同意。 '外鍵(FK_User_Commited,FK_User_Done)參考文獻SintalDB.User(PK_User,PK_User)'沒有意義,在多個層次上。 –