简体   繁体   English

MYSQL 一对多关系

[英]One to many relationship MYSQL

I'm new to mysql and am trying to link two tables and am not sure how, i have a products table with a list of products in a restaurant, the column in this table are我是 mysql 的新手,正在尝试链接两个表,但不确定如何链接,我有一个带有餐厅产品列表的产品表,该表中的列是

  • ID(primary key) ID(主键)
  • name姓名
  • price价钱

The other table is called extras, this table contains extra things that you can add to your order but are optional, for when buying chicken you have an option of spicy and non spicy, Some products have more than one extras, for instance a product can have the option of choosing three extras.另一张表称为附加表,该表包含您可以添加到订单中但可选的附加内容,因为在购买鸡肉时,您可以选择辣和不辣,有些产品有多个附加项,例如一个产品可以可以选择三个额外的选项。

The extras table at the moment only has临时表目前只有

  • ID(primary key) ID(主键)
  • name姓名

not sure how to link the two or where to put foreign constraints.不确定如何将两者联系起来或将外部约束放在哪里。

UPDATE更新

Same extra may also belong to numerous products同样的额外费用也可能属于众多产品

With products having many extras and extras applying to many products this is actually a many to many relationship.由于产品具有许多附加功能,并且附加功能适用于许多产品,这实际上是一种多对多关系。

create table products(ID int auto_increment Primary Key,
Name varchar(50),
Price decimal(6,2));

create table extras(ID int auto_increment Primary Key,
Name varchar(50));

create table product_extras(Product int Not Null,
Extra int Not Null,
FOREIGN KEY(Product) References products(ID) ON DELETE CASCADE,
FOREIGN KEY(Extra) References extras(ID) ON DELETE CASCADE);

Something akin to this should work for you, though you may want to change the datatypes based off preference and what data actually needs to go in there.类似的东西应该对你有用,尽管你可能想根据偏好和实际需要输入哪些数据来更改数据类型。

In your case one product may have many extras(1 to many) and many products may be having same extra thing(many to 1).在您的情况下,一种产品可能有很多额外的东西(1 对多),而许多产品可能有相同的额外东西(多对 1)。 Thus, this is a many to many relationship and for such relations we need 3 tables.因此,这是一个多对多的关系,对于这种关系,我们需要 3 个表。

CREATE TABLE products ( 
    id INT PRIMARY KEY AUTO_INCREMENT, 
    name VARCHAR(50), 
    price DECIMAL(10.2)
 );

 CREATE TABLE extras (
      id INT PRIMARY KEY AUTO_INCREMENT,
      name VARCHAR(50)
 );

 CREATE TABLE products_extras (
      id INT PRIMARY KEY AUTO_INCREMENT,
      pro_id INT,
      FOREIGN  KEY(pro_id) REFERENCES products(id),
      ext_id INT,
      FOREIGN KEY(ext_id) REFERENCES extras(id)
 );

A joining/linking table usually uses many-to-many relationships by joining the 2 parent tables/primary keys to allow many products to have many extras or no extras at all.联接/链接表通常通过联接 2 个父表/主键来使用多对多关系,以允许许多产品具有许多附加项或根本没有附加项。

在此处输入图片说明

so for example:所以例如:

eg: Product IDs (primary keys) are: 1, 2, 3 Product names are: chicken wings, chicken breast, chicken fillet例如:产品 ID(主键)为:1、2、3 产品名称为:鸡翅、鸡胸肉、鸡柳

Extras IDs (primary keys) are: 1, 2, 3 Extras names are: mild, medium, hot附加 ID(主键)为:1、2、3 附加名称为:轻度、中度、热

Wings, breasts and fillet have the option of all three of the extras, so the product_extras table would end up looking something like this: Wings、breasts 和 Fillet 可以选择所有三个附加项,因此 product_extras 表最终看起来像这样:

product_extras_id | product_id | extras_id
------------------------------------------
1                 | 1          | 1
2                 | 1          | 2
3                 | 1          | 3
4                 | 2          | 1
5                 | 2          | 2
6                 | 2          | 3
7                 | 3          | 1
8                 | 3          | 2
9                 | 3          | 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM