在Mysql修改表为外键的示例:country 表是父表,country_id是主键,city是子表,外键为country_id,和country表的主键country_id对应,在创建表的时候添加外键,示例:create tab
在Mysql修改表为外键的示例:
country 表是父表,country_id是主键,city是子表,外键为country_id,和country表的主键country_id对应,在创建表的时候添加外键,示例:
create table country(country_id smallint unsigned not null auto_increment,
country varchar(50) not null,
last_update timestamp not null default current_timestamp on update current_timestamp,
primary key(country_id)
)engine=INNODB default charset=utf8;
CREATE TABLE `city` (
`city_id` smallint(5) unsigned NOT NULL auto_increment,
`city` varchar(50) NOT NULL,
`country_id` smallint(5) unsigned NOT NULL,
`last_update` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (`city_id`),
KEY `idx_fk_country_id` (`country_id`),
CONSTRaiNT `fk_city_country` FOREIGN KEY (`country_id`) REFERENCES `country` (`country_id`) on delete restrict ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
在建表后添加外键的示例:
ALTER TABLE city ADD FOREIGN KEY (country_id) REFERENCES `country`(country_id);
--结束END--
本文标题: 在mysql怎么修改表为外键
本文链接: https://www.lsjlt.com/news/112510.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0