我正在构建一个SQLite数据库,我不知道如何继续这个场景。
我将使用一个真实的例子来解释我需要的东西:
我有一个列表产品,由各州的许多商店出售。不是每个Store
都会出售特定的Product
,那些做的,可能只能用一个State
或另一个出售。大多数商店在大多数州销售产品,但不是全部。
例如,假设我想在夏威夷购买吸尘器。 Joe's Hardware在18个州销售真空吸尘器,但夏威夷却没有。沃尔玛在夏威夷销售真空吸尘器,但不销售微波炉。汉堡王根本不销售吸尘器,但会在美国的任何地方给我一个Whopper。
因此,如果我在夏威夷寻找真空,我应该只得到沃尔玛。虽然其他商店可能会销售真空吸尘器,并且可能会在夏威夷销售,但他们并不同时这样做,但沃尔玛也是如此
如何在关系数据库中有效地创建这种类型的关系(具体来说,我目前正在使用SQLite,但将来需要能够转换为MySQL)。
显然,我需要Product
,Store
和State
的表格,但我对如何创建和查询相应的连接表感到茫然......
例如,如果我查询某个Product
,我将如何确定哪个Store
会在特定的State
中出售它,请记住沃尔玛可能不会在夏威夷出售真空吸尘器,但他们在那里卖茶?
我理解RD中1:1,1:n和M:n关系的基础知识,但我不知道如何处理这种复杂性,因为存在多对多的情况。
如果你能展示一些证明这一点的SQL语句(或DDL),我将非常感激。谢谢!
一种公认的常用方法是使用一个表,该表具有用于引用产品的列和用于存储的另一个列。这样的表引用表有很多名称,关联表映射表有一些名称。
您希望这些是高效的,因此请尝试通过一个数字来引用,当然这必须唯一地标识它所引用的内容。对于SQLite,默认情况下,表具有一个特殊列,通常是隐藏的,这是一个唯一的数字。它是rowid,通常是访问行的最有效方式,因为SQLite在设计时考虑了这种常见用法。
SQLite允许您为每个表创建一个列,该列是您简单提供列后跟INTEGER PRIMARY KEY
的rowid的别名,通常您可以命名列id。
因此,利用这些参考表将具有产品ID的列和用于商店的id的另一列,以满足产品/商店的每个组合。
作为示例,创建了三个表(存储产品和引用/映射表),前者使用以下方式填充: -
CREATE TABLE IF NOT EXISTS _products(id INTEGER PRIMARY KEY, productname TEXT, productcost REAL);
CREATE TABLE IF NOT EXISTS _stores (id INTEGER PRIMARY KEY, storename TEXT);
CREATE TABLE IF NOT EXISTS _product_store_relationships (storereference INTEGER, productreference INTEGER);
INSERT INTO _products (productname,productcost) VALUES
('thingummy',25.30),
('Sky Hook',56.90),
('Tartan Paint',100.34),
('Spirit Level Bubbles - Large', 10.43),
('Spirit Level bubbles - Small',7.77)
;
INSERT INTO _stores (storename) VALUES
('Acme'),
('Shops-R-Them'),
('Harrods'),
('X-Mart')
;
结果表是: -
将产品放入商店(例如)可以使用以下方式完成: -
-- Build some relationships/references/mappings
INSERT INTO _product_store_relationships VALUES
(2,2), -- Sky Hooks are in Shops-R-Them
(2,4), -- Sky Hooks in x-Mart
(1,3), -- thingummys in Harrods
(1,1), -- and Acme
(1,2), -- and Shops-R-Them
(4,4), -- Spirit Level Bubbles Large in X-Mart
(5,4), -- Spiirit Level Bubble Small in X-Mart
(3,3) -- Tartn paint in Harrods
;
_product_store_relationships将是: -
如下所示的查询将列出按商店排序的商店中的产品,然后列出产品: -
SELECT storename, productname, productcost FROM _stores
JOIN _product_store_relationships ON _stores.id = storereference
JOIN _products ON _product_store_relationships.productreference = _products.id
ORDER BY storename, productname
;
结果输出为: -
此查询将仅列出具有包含s或S的产品名称的存储(通常区分大小写),根据ASCending顺序中的productcost排序的输出,然后是storename,然后是productname: -
SELECT storename, productname, productcost FROM _stores
JOIN _product_store_relationships ON _stores.id = storereference
JOIN _products ON _product_store_relationships.productreference = _products.id
WHERE productname LIKE '%s%'
ORDER BY productcost,storename, productname
;
输出: -
2个新表state和store_state_reference
SQL可能是: -
CREATE TABLE IF NOT EXISTS _states (id INTEGER PRIMARY KEY, statename TEXT);
INSERT INTO _states (statename) VALUES
('Texas'),
('Ohio'),
('Alabama'),
('Queensland'),
('New South Wales')
;
CREATE TABLE IF NOT EXISTS _store_state_references (storereference, statereference);
INSERT INTO _store_state_references VALUES
(1,1),
(2,5),
(3,1),
(4,3)
;
如果运行以下查询: -
SELECT storename,productname,productcost,statename
FROM _stores
JOIN _store_state_references ON _stores.id = _store_state_references.storereference
JOIN _states ON _store_state_references.statereference =_states.id
JOIN _product_store_relationships ON _stores.id = _product_store_relationships.storereference
JOIN _products ON _product_store_relationships.productreference = _products.id
WHERE statename = 'Texas' AND productname = 'Sky Hook'
;
输出将是: -
没有WHERE子句: -
以下将使Stores-R-Them在所有州都有存在: -
INSERT INTO _store_state_references VALUES
(2,1),(2,2),(2,3),(2,4)
;
现在德克萨斯州的Sky Hook将导致: -
您将需要创建产品,状态和商店的组合映射表作为tbl_product_states_stores
,它将存储产品,状态和商店的映射。列将是id
,product_id
,state_id
,stores_id
。