create table staff(
 staffnum int(10) not null,
 staffname varchar(20) not null,
 staffage varchar(20) not null,
 stafftitle varchar(30) not null,
 staffleader varchar(20) not null,
 primary key(staffnum)
);表staff中怎样把staffleader设置为该表的外键?急啊。
这样写foreign key(staffleader) references staff(staffnum)会报错

解决方案 »

  1.   

    重新补充下,错误是这样的
    mysql> create table staff(
        -> staffnum int(10) not null,
        -> staffname varchar(20) not null,
        -> staffage varchar(20) not null,
        -> stafftitle varchar(30) not null,
        -> staffleader varchar(20) not null,
        -> storage_num int(10) not null,
        -> primary key(staffnum),
        -> foreign key(staffleader) references staff(staffnum),
        -> foreign key(storage_num) references storage(storage_num)
        -> )
        -> engine=innodb;
    ERROR 1005 (HY000): Can't create table 'facmanage.staff' (errno: 150)
      

  2.   

    staff与staffleader是一对多的关系,我想建立一个递归联系,staffnum是主键,staffleader为外键,引用“staffnum”
      

  3.   

    create table staff(
     staffnum int not null,   --int 数据类型不能指定列宽
     staffname varchar(20) not null,
     staffage varchar(20) not null,
     stafftitle varchar(30) not null,
     staffleader int not null,   --外键列数据类型必须与主键列相同
     primary key(staffnum)
    );
    go
    ALTER TABLE staff ADD CONSTRAINT
    FK_staff_staff FOREIGN KEY
    (
    staffleader
    ) REFERENCES staff
    (
    staffnum
    ) ON UPDATE  NO ACTION 
     ON DELETE  NO ACTION 
    /*
    命令已成功完成。
    */
    GO
    drop table staff
      

  4.   

    ON UPDATE  NO ACTION 
    ON DELETE  NO ACTION  是什么意思啊?