首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > Mysql >

MYSQL怎么创建外键并映射

2012-08-07 
MYSQL如何创建外键并映射现有表softtype ##软件类型表CREATE TABLE `softtype` (`stid` int(6) NOT NULL,`

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);

热点排行