您描述的数据结构不是一棵树(它要求每个节点都只有一个父节点,除了没有父节点的根节点):相反,它是一个更一般的DAG:
其他 RDBMS 原生支持分层数据和递归查询,而 MySQL 没有;相反,在 MySQL 中存储此类数据的一个很好的通用关系模型是创建一个图的传递闭包表。用作//
语句分隔符:
CREATE TABLE Products (
SKU SERIAL COMMENT 'Stock-Keeping Unit code',
Name VARCHAR(15) COMMENT 'Product name',
Description VARCHAR(255) COMMENT 'Descriptive text',
Price DECIMAL(6,2) COMMENT 'Selling price',
isAtomic BOOLEAN DEFAULT FALSE COMMENT 'Flag indicating atomicity'
)
ENGINE = InnoDB,
COMMENT = 'Properties relating to each product'
//
CREATE TABLE ProductComponents (
SKU BIGINT UNSIGNED NOT NULL COMMENT 'Stock-Keeping Unit Code',
ComponentSKU BIGINT UNSIGNED NOT NULL COMMENT 'SKU of comprised component',
PRIMARY KEY (SKU, ComponentSKU),
INDEX (ComponentSKU, SKU),
FOREIGN KEY ( SKU) REFERENCES Products (SKU),
FOREIGN KEY (ComponentSKU) REFERENCES Products (SKU)
)
ENGINE = InnoDB,
COMMENT = 'Transitive closure of the product DAG'
//
可以使用触发器在后一个表中强制执行原子性:
CREATE TRIGGER ins_atomic BEFORE INSERT ON ProductComponents
FOR EACH ROW IF
NEW.SKU <> NEW.ComponentSKU
AND (SELECT isAtomic FROM Products WHERE SKU = NEW.SKU)
THEN
SIGNAL SQLSTATE '45000' SET
MESSAGE_TEXT = 'Atomic product cannot have a component'
;
END IF//
CREATE TRIGGER upd_atomic BEFORE UPDATE ON ProductComponents
FOR EACH ROW IF
NEW.SKU <> NEW.ComponentSKU
AND (SELECT isAtomic FROM Products WHERE SKU = NEW.SKU)
THEN
SIGNAL SQLSTATE '45000' SET
MESSAGE_TEXT = 'Atomic product cannot have a component'
;
END IF//
您可能还需要一个类似的触发器来防止错误更新Products.isAtomic
:
CREATE TRIGGER upd_prod BEFORE UPDATE ON Products
FOR EACH ROW IF NEW.isAtomic AND EXISTS (
SELECT * FROM ProductComponents WHERE SKU <> ComponentSKU AND SKU = NEW.SKU
) THEN
SIGNAL SQLSTATE '45000' SET
MESSAGE_TEXT = 'Atomic product cannot have a component'
;
END IF//
对于上面描述的图表,您的数据将如下所示:
INSERT INTO Products
(SKU, isAtomic)
VALUES
( 2, TRUE ),
( 3, FALSE ),
( 5, FALSE ),
( 7, FALSE ),
( 8, FALSE ),
( 9, TRUE ),
( 10, TRUE ),
( 11, FALSE )
//
INSERT INTO ProductComponents
(SKU, ComponentSKU)
VALUES
(2,2),
(3,3), (3,8), (3,9), (3,10),
(5,5), (5,11), (5,2), (5,9), (5,10),
(7,7), (7,8), (7,9), (7,11), (7,2), (7,10),
(8,8), (8,9),
(9,9),
(10,10),
(11,11), (11,2), (11,9), (11,10)
//
然后您可以按如下方式存储可用性:
CREATE TABLE ProductAvailability (
SKU BIGINT UNSIGNED NOT NULL COMMENT 'Stock-Keeping Unit Code',
Date DATE COMMENT 'Availability date',
Quantity INT COMMENT 'Available quantity',
PRIMARY KEY (SKU, Date),
FOREIGN KEY (SKU) REFERENCES Products (SKU)
)
ENGINE = InnoDB,
COMMENT = 'Available quantities'
//
以上的一些测试数据可能是:
INSERT INTO ProductAvailability
(SKU, Date , Quantity)
VALUES
( 2, '2012-12-13', NULL),
( 2, '2012-12-15', 15),
( 9, '2012-12-13', 234),
( 9, '2012-12-14', 46),
( 9, '2012-12-15', 0),
( 10, '2012-12-13', 4),
( 10, '2012-12-14', 7),
( 10, '2012-12-15', 5)
//
您的查询将是:
SELECT p.*
FROM Products p
JOIN ProductComponents c USING (SKU)
JOIN (
SELECT p.SKU AS ComponentSKU,
COUNT(*) = DATEDIFF(@end_date, @start_date) + 1 AS available
FROM Products p LEFT JOIN ProductAvailability a
ON a.SKU = p.SKU
AND a.Quantity > 0
AND a.Date BETWEEN @start_date AND @end_date
WHERE p.isAtomic
GROUP BY p.SKU
) q USING (ComponentSKU)
GROUP BY p.SKU
HAVING NOT SUM(q.available = 0)
在sqlfiddle上查看。