栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在MySQL中创建具有N:M关系的表?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在MySQL中创建具有N:M关系的表?

多对一(产品只能有一家商店)

create table store(    id int unsigned not null auto_increment,    store_name varchar(30) not null,    primary key(id));Query OK, 0 rows affected (0.02 sec)create table product(    id int unsigned not null auto_increment,    store_id int unsigned not null,    product_name varchar(30) not null,    price float not null,    primary key(id),    constraint product_store foreign key (store_id) references store(id));Query OK, 0 rows affected (0.02 sec)

多对多(产品可以在许多商店中使用)

create table store(    id int unsigned not null auto_increment,    store_name varchar(30) not null,    primary key(id));Query OK, 0 rows affected (0.04 sec)create table product(    id int unsigned not null auto_increment,    store_id int unsigned not null,    product_name varchar(30) not null,    price float not null,    primary key(id));Query OK, 0 rows affected (0.01 sec)create table product_store (    product_id int unsigned not null,    store_id int unsigned not null,    ConSTRAINT product_store_store foreign key (store_id) references store(id),    ConSTRAINT product_store_product foreign key (product_id) references product(id),    ConSTRAINT product_store_unique UNIQUE (product_id, store_id))Query OK, 0 rows affected (0.02 sec)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/456381.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号