2

我正在尝试重新定义我的数据库设计,添加一些额外的表,因此在我的表 Departaments 和 Jobs 中没有重复的名称......

但是我收到了与外键有关的错误 121,你能解释一下为什么会出现这个问题吗?

我在 sqlfiddle 中这样做

我正在尝试做这样的事情,但在 sqlfiddle 中没有运气,我目前正在工作

 CREATE TABLE Employee(
          EmployeeID  INTEGER      NOT NULL PRIMARY KEY,
          Name        VARCHAR(30)  NOT NULL,
          Sex         CHAR(1)      NOT NULL,
          Address     VARCHAR(80)  NOT NULL,
          Security    VARCHAR(15)  NOT NULL          
        );

        CREATE TABLE Departments  (
            DeptID   INTEGER     NOT NULL PRIMARY KEY,
            DeptName VARCHAR(30) NOT NULL
        );

如果我取消注释代码,我得到 Schema Creation Failed: Can't create table 'db_2_2bf4a.project-employee' (errno: 121):

        CREATE TABLE `Dept-Employee`(
          EmployeeID   INTEGER NOT NULL,          
          DeptID       INTEGER NOT NULL,
          CONSTRAINT fk_DeptID     FOREIGN KEY (DeptID)  REFERENCES Departments(DeptID),
          CONSTRAINT fk_EmployeeID FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID)
        );

       CREATE TABLE `Dept-Manager`(
          EmployeeID   INTEGER NOT NULL,          
          DeptID       INTEGER NOT NULL,
          CONSTRAINT fk_DeptID     FOREIGN KEY (DeptID)  REFERENCES Departments(DeptID),
          CONSTRAINT fk_EmployeeID FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID)
        );

        CREATE TABLE Jobs (
            JobID            INTEGER      NOT NULL PRIMARY KEY,
            JobName          VARCHAR(30)  NOT NULL,
            JobSalary        DOUBLE(15,3) NOT NULL default '0.000', 
            JobSalaryperDay  DOUBLE(15,3) NOT NULL default '0.000', 
            DeptID           INTEGER      NOT NULL
        );

        CREATE TABLE `Jobs-Employee`(
          EmployeeID   INTEGER NOT NULL,
          JobID        INTEGER NOT NULL,
          CONSTRAINT fk_JobID      FOREIGN KEY (JobID)      REFERENCES Jobs(JobID),
          CONSTRAINT fk_EmployeeID FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID)
        );

        CREATE TABLE Project(
          ProjectID    INTEGER NOT NULL PRIMARY KEY,
          ProjectDesc   VARCHAR(200) NOT NULL,
          StartDate     DATE NOT NULL,
          EndDate       DATE NOT NULL, 
          DaysOfWork    INTEGER NOT NULL,
          NoEmployees   INTEGER NOT NULL,
          EstimatedCost DOUBLE(15,3) NOT NULL default '0.000', 
          RealCost      DOUBLE(15,3) NOT NULL default '0.000' 
        );


        CREATE TABLE `Project-Employee`(
          ProjectID    INTEGER NOT NULL,
          EmployeeID   INTEGER NOT NULL,
          Note         VARCHAR(200),
          DaysWork     INTEGER NOT NULL,
          CONSTRAINT fk_ProjectID  FOREIGN KEY (ProjectID)  REFERENCES Project(ProjectID),
          CONSTRAINT fk_EmployeeID FOREIGN KEY (EmployeeID) REFERENCES Employee(EmployeeID)
        );

所以在我不知道这个查询是否正确之后

INSERT INTO `Departments` VALUES (1, 'Outsourcing');
    INSERT INTO `Departments` VALUES (2, 'Technician');
    INSERT INTO `Departments` VALUES (3, 'Administrative');

    INSERT INTO `Jobs` VALUES (1, 'welder'    ,500.550,16.7 ,2);
    INSERT INTO `Jobs` VALUES (2, 'turner'    ,500.100,16.67,2);
    INSERT INTO `Jobs` VALUES (3, 'assistant' ,650.100,21.67,2);
    INSERT INTO `Jobs` VALUES (4, 'supervisor',800.909,26.70,3);
    INSERT INTO `Jobs` VALUES (5, 'manager'   ,920.345,30.68,3);
    INSERT INTO `Jobs` VALUES (6, 'counter'   ,520.324,17.35,1);

    INSERT INTO `Dept-Employee` (10,1);
    INSERT INTO `Dept-Employee` (10,2);
    INSERT INTO `Dept-Employee` (10,3);
    INSERT INTO `Dept-Employee` (10,1);
    INSERT INTO `Dept-Employee` (10,3);

    INSERT INTO `Jobs-Employee` (10,3);
    INSERT INTO `Jobs-Employee` (10,3);
    INSERT INTO `Jobs-Employee` (10,4);
    INSERT INTO `Jobs-Employee` (10,6);
    INSERT INTO `Jobs-Employee` (10,5);

    INSERT INTO `Employee` VALUES (10, 'Joe',  'M', 'Anywhere', '927318344');
    INSERT INTO `Employee` VALUES (20, 'Moe',  'M', 'Anywhere', '827318322');
    INSERT INTO `Employee` VALUES (30, 'Jack', 'M', 'Anywhere', '927418343');
    INSERT INTO `Employee` VALUES (40, 'Marge','F', 'Evererre', '127347645');
    INSERT INTO `Employee` VALUES (50, 'Greg' ,'M', 'Portland', '134547633');


    INSERT INTO `Project` VALUES (1, 'The very first', '2008-7-04' , '2008-7-24' , 20, 5, 3000.50, 2500.00);
    INSERT INTO `Project` VALUES (2, 'Second one pro', '2008-8-01' , '2008-8-30' , 30, 5, 6000.40, 6100.40);


    INSERT INTO `Project-Employee` VALUES (1, 10, 'Worked all days'    , 20);
    INSERT INTO `Project-Employee` VALUES (1, 20, 'Worked just in defs', 11);
    INSERT INTO `Project-Employee` VALUES (1, 30, 'Worked just in defs', 17);
    INSERT INTO `Project-Employee` VALUES (1, 40, 'Contability '       , 8);
    INSERT INTO `Project-Employee` VALUES (1, 50, 'Managed the project', 8);
4

1 回答 1

5

原因是CONSTRAINT您提供的名称已经存在。尝试更改约束名称。

例如fk_EmployeeIDon已经Dept-Manager 存在Dept-Employee

于 2012-11-07T06:37:10.327 回答