首页 > 代码库 > Mysql外键创建

Mysql外键创建

CREATE TABLE `l1` (
  `nid` int(11) NOT NULL AUTO_INCREMENT,
  `user` char(10) NOT NULL,
  `pwd` char(10) NOT NULL,
  `s1_nid` int(11) NOT NULL,
  PRIMARY KEY (`nid`),
  KEY `l1_s1` (`s1_nid`),
  CONSTRAINT `l1_s1` FOREIGN KEY (`s1_nid`) REFERENCES `s1` (`nid`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

  

删除外键

alter table l1 drop foreign key l1_s1;

  

创建外键

alter table l1 add constraint s1_l1 foreign key (s1_nid) references s1(ni
d);

  

Mysql外键创建