日期:2014-05-16  浏览次数:20688 次

MYSQL如何创建外键并映射
现有表softtype ##软件类型表
CREATE TABLE `softtype` (
  `stid` int(6) NOT NULL,
  `typename` char(50) NOT NULL,
  PRIMARY KEY (`stid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

##软件信息表
CREATE TABLE `soft` (
  `sid` int(6) NOT NULL,
  `stid` int(6) NOT NULL,
  `aid` int(6) NOT NULL,
  `title` varchar(100) NOT NULL,
  PRIMARY KEY (`sid`),
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

##软件作者表
CREATE TABLE `author` (
  `aid` int(6) NOT NULL,
  `asite` varchar(50) NOT NULL,
  `acom` text NOT NULL,
  PRIMARY KEY (`aid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

如何在
软件信息表(soft)里创建外键stid并映射到softtype表的stid
软件信息表(soft)里创建外键aid并映射到author表的aid


------解决方案--------------------

alter table soft add FOREIGN KEY fx_stid(stid) references softtype(stid),add FOREIGN KEY fx_aid(aid) references author(aid);