CREATE TABLE`xh` (
`id` int(100) unsigned NOT NULL AUTO_INCREMENT COMMENT ,
`cl_id` smallint(3) unsigned NOT NULL COMMENT,
`title` varchar(100) COLLATE utf8_unicode_ci NOT NULL COMMENT ,
`details` text COLLATE utf8_unicode_ci NOT NULL COMMENT ,
`date` datetime NOT NULL COMMENT ,
`au_id` smallint(6) unsigned NOT NULL COMMENT ,
`click` int(100) unsigned NOT NULL DEFAULT '0' COMMENT ,
`reco` int(100) unsigned NOT NULL DEFAULT '0' COMMENT,
PRIMARY KEY (`id`),
KEY `fk_class` (`cl_id`),
CONSTRAINT `fk_class`FOREIGN KEY (`cl_id`) REFERENCES `fl` (`id`),
KEY `fk_author` (`au_id`),
CONSTRAINT `fk_author` FOREIGN KEY (`au_id`) REFERENCES `author` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
2. 建表后创建外键:
Alter Table `ym` Add Constraint `fk_author` Foreign Key (`au_id`) References `author` (`id`)
3. 查看表结构:
Show Create Table `ym`
ON UPDATE CASCADE(级联灶扒敏更隐枝新)
ON DELETE CASCADE(级联删除)
比如:
ALTER TABLE `ym` ADD CONSTRAINT `fk_author` FOREIGN KEY (`au_id`) REFERENCES `author` (`id`) ON UPDATE CASCADE
添加外键约束名字一档携则定不能重复隐碧如何添加外键行棚
方法一:直接在属性值后面添加
create table score(
cscore int(11),
st_id int(50) references student(id),
cs_id int(30) references classes(id),
primary key(st_id,cs_id)
)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)