我的数据库设计是否正确?

Is my database design correct?

我叫 tables: 1.商城 2. 商店 3.产品

商城显然可以有多个商店,商店可以有多个产品。那么这是否应该是一种识别关系,因为没有商场就没有商店,没有商店就没有产品(至少我不希望他们这样)?

另外让我感到困惑的是,如果我使用 MySQL Workbench 创建此设计(即使我在我的项目中使用 SQLite),它将在产品中创建 3 个主键 table,其中 2 个引用了之前的 table。产品 table 不应该只引用商店 table 因为这是之前的步骤吗?

我将如何在这样的数据库设计中查询具有特定名称并且存在于具有商店 "Store 1" 和 "Store 2" 的商场中的产品?

谢谢!

您必须避免在数据库中重复依赖。您具有以下数据库结构。

Mall --> (1:n) Store --> (1:n) Product 

并且根据您的设计,没有商店就不会存在产品的依赖性。没有商店,商场就不能包含产品,对吧?

Mall -->  (1:n) Product  {Cannot exist}

因此,将商城外键添加到产品 table 是没有意义的。 这是您的数据库结构的示例 SQL 语句。

    create table if not exists mall (
mall_id int(11) AUTO_INCREMENT PRIMARY KEY,
mall_name varchar(255) NOT NULL
)

create table if not exists store (
store_id int(11) AUTO_INCREMENT PRIMARY KEY,
store_name varchar(255) NOT NULL,
mall_id int(11) ,
CONSTRAINT 'mall_Id_FK' FOREIGN KEY (mall_id) REFERENCES mall(mall_Id)  ON UPDATE CASCADE  ON DLETE CASCADE
);

create table if not exists product (
product_id int(11) AUTO_INCREMENT PRIMARY KEY,
product_name varchar(255) NOT NULL,
store_id int(11) ,

CONSTRAINT 'store_Id_FK' FOREIGN KEY (store_id) REFERENCES store(store_id) ON UPDATE CASCADE  ON DLETE CASCADE
);

另外关于你的问题,你如何根据商店和商场查询产品数据:

How would I query in a database design like this for a product that has a specific name and it exists in a mall that has stores "Store 1" and "Store 2"?

    SELECT a.product_id, a.product_name 
from product a ,
store b,
 mall c 
where
 a.store_id = b.store_id and 
 b.mall_id= c.mall_id and
 c.mall_name = 'Mall1' and
 b.store_name IN ('Store1' ,'Store2') and 
 a.product_name = 'Product1';

此return特定商城中特定店铺的产品详情。