从表
CREATE TABLE `student1` (
`id` INT NOT NULL AUTO_INCREMENT COMMENT'id',
`greadid` INT(11) NOT NULL COMMENT 'gradeid',
`pwd` VARCHAR(23) COMMENT '密码',
`name` VARCHAR(32) NOT NULL COMMENT '名字',
`add` VARCHAR(32) DEFAULT NULL COMMENT '地址',
PRIMARY KEY (`id`),
KEY `as` (`greadid`),
CONSTRAINT `as` FOREIGN KEY (`greadid`) REFERENCES `greads` (`gradeid`)
)ENGINE=INNODB DEFAULT CHARSET=utf8
主表
CREATE TABLE greads(
`gradeid` INT(11) NOT NULL AUTO_INCREMENT COMMENT'年级id',
`gradename` VARCHAR(32) NOT NULL COMMENT'年级名字',
PRIMARY KEY(`gradeid`)
)ENGINE=INNODB DEFAULT CHARSET=utf8
ALTER TABLE student1
ADD CONSTRAINT `xc` FOREIGN KEY (`greadid`) REFERENCES `greads` (`gredeid`);
只有删除从表才能删主表!!!
标签:COMMENT,VARCHAR,greadid,3.1,外键,KEY,sql,NULL,id From: https://www.cnblogs.com/zhenlee/p/17877576.html