create table Employee
(
ssn  INTEGER not null unique,
Name varchar(30) not null,
     Address  varchar(30) not null,
     Age  INTEGER not null,
Sex varchar(10) not null,
Position varchar(20) not null,
     primary key(ssn)
)ENGINE=InnoDB;
create table Department
(
DId  INTEGER not null unique,
     Dname varchar(15) not null,
ManagerId INTEGER not null unique,
     primary key(DId)
);
create table Works_In
(
ssn INTEGER not null unique,
DId INTEGER not null unique,
since  varchar(15) not null,
primary key(ssn,DId),
FOREIGN KEY(ssn,DId) REFERENCES Employee(ssn)  ON DELETE CASCADE ON UPDATE CASCADE
FOREIGN KEY(DId) REFERENCES Department(DId) ON DELETE CASCADE ON UPDATE CASCADE为什么 
)ENGINE=InnoDB;
在Works_In中给Employee和Department加两个外键
这样为什么不行?
ERROR 1005 (HY000) at line 62: Can't create table './HumanResources/Works_In.frm' (errno: 150)
应该怎么样写?

解决方案 »

  1.   

    主键是唯一的,不能用两个的吧, 而且要非空。主键不能做外键使用!!楼主缺乏sql的基础知识,建议去mysql网站上看看。
      

  2.   

    太粗心了。当您的问题得到解答后请及时结贴.
    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.htmlcreate table Works_In
    (
    ssn INTEGER not null unique,
    DId INTEGER not null unique,
    since  varchar(15) not null,
    primary key(ssn,DId),
    FOREIGN KEY(ssn,DId) REFERENCES Employee(ssn)  ON DELETE CASCADE ON UPDATE CASCADE
    FOREIGN KEY(DId) REFERENCES Department(DId) ON DELETE CASCADE ON UPDATE CASCADE为什么 
    )ENGINE=InnoDB;mysql> create table Works_In
        -> (
        ->  ssn             INTEGER         not null unique,
        ->  DId             INTEGER         not null unique,
        ->  since   varchar(15)     not null,
        ->  primary key(ssn,DId),
        ->  FOREIGN KEY(DId)        REFERENCES Employee(ssn)        ON DELETE CASCADE ON UPDATE CASCADE,
        ->  FOREIGN KEY(DId)        REFERENCES Department(DId)      ON DELETE CASCADE ON UPDATE CASCADE
        -> )ENGINE=InnoDB;
    Query OK, 0 rows affected (0.13 sec)mysql>
      

  3.   

    create table Employee
    (
    ssn INTEGER not null unique,
    Name varchar(30) not null,
        Address varchar(30) not null,
        Age INTEGER not null,
    Sex varchar(10) not null,
    Position varchar(20) not null,
        primary key(ssn)
    )ENGINE=InnoDB;
    create table Department
    (
    DId INTEGER not null unique,
        Dname varchar(15) not null,
    ManagerId INTEGER not null unique,
        primary key(DId)
    );
    create table Works_In
    (
    ssn INTEGER not null unique,
    DId INTEGER not null unique,
    since varchar(15) not null,
    primary key(ssn,DId),
    FOREIGN KEY(ssn) REFERENCES Employee(ssn) ON DELETE CASCADE ON UPDATE CASCADE
    FOREIGN KEY(DId) REFERENCES Department(DId) ON DELETE CASCADE ON UPDATE CASCADE为什么
    )ENGINE=InnoDB;
    在Works_In中给Employee和Department加两个外键
    这样为什么不行?
    ERROR 1005 (HY000) at line 62: Can't create table './HumanResources/Works_In.frm' (errno: 150)
    应该怎么样写?
      

  4.   

    但是3楼不好意思,您也打错了,不是
    ->  FOREIGN KEY(DId)        REFERENCES Employee(ssn)        ON DELETE CASCADE ON UPDATE CASCADE,
    而是
    FOREIGN KEY(ssn) REFERENCES Employee(ssn) ON DELETE CASCADE ON UPDATE CASCADE 
    问题依然存在
      

  5.   

    请你什么都不要改,直接复制下面语句来执行! 在#2楼已经贴给你一遍了。请认真对待别人给你的回复,否则是浪费大家时间!create table Works_In
    (
    ssn INTEGER not null unique,
    DId INTEGER not null unique,
    since varchar(15) not null,
    primary key(ssn,DId),
    FOREIGN KEY(ssn) REFERENCES Employee(ssn) ON DELETE CASCADE ON UPDATE CASCADE,
    FOREIGN KEY(DId) REFERENCES Department(DId) ON DELETE CASCADE ON UPDATE CASCADE
    )ENGINE=InnoDB;
      

  6.   

    对不起,我非常感谢您的回复,但是确实是如实地覆盖你的代码问题依然存在
    create table Employee
    (
    ssn  INTEGER not null unique,
    Name varchar(30) not null,
         Address  varchar(30) not null,
         Age  INTEGER not null,
    Sex varchar(10) not null,
    Position varchar(20) not null,
         primary key(ssn)
    )ENGINE=InnoDB;
    create table Department
    (
    DId  INTEGER not null unique,
         Dname varchar(15) not null,
    ManagerId INTEGER not null unique,
         primary key(DId)
    );create table Works_In

    ssn INTEGER not null unique, 
    DId INTEGER not null unique, 
    since varchar(15) not null, 
    primary key(ssn,DId), 
    FOREIGN KEY(ssn) REFERENCES Employee(ssn) ON DELETE CASCADE ON UPDATE CASCADE, 
    FOREIGN KEY(DId) REFERENCES Department(DId) ON DELETE CASCADE ON UPDATE CASCADE 
    )ENGINE=InnoDB;
    完整代码就是这样,第三个表是完全复制您的代码的。