以下是我将如何根据提供的数据构建表以反映约束:
SQL DDL:
CREATE TABLE Products
(
product_name CHAR(1) NOT NULL UNIQUE
);
CREATE TABLE ProductPrices
(
product_name CHAR(1) NOT NULL
REFERENCES Products (product_name),
product_price DECIMAL(19, 4) NOT NULL
CHECK (product_price > 0),
UNIQUE (product_name, product_price)
);
CREATE TABLE MediaZones
(
zone_name CHAR(1) NOT NULL UNIQUE
);
CREATE TABLE Versions
(
product_name CHAR(1) NOT NULL,
product_price DECIMAL(19, 4) NOT NULL,
FOREIGN KEY (product_name, product_price)
REFERENCES ProductPrices (product_name, product_price),
zone_name CHAR(1) NOT NULL
REFERENCES MediaZones (zone_name),
UNIQUE (product_name, zone_name)
);
SQL DML(成功 = 好):
INSERT INTO Products (product_name) VALUES ('A');
INSERT INTO MediaZones (zone_name)
VALUES ('A'), ('B'), ('C'),
('D'), ('E'), ('F'),
('G'), ('H'), ('I');
INSERT INTO ProductPrices (product_name, product_price)
VALUES ('A', 1),
('A', 2),
('A', 3);
SQL DML(失败 = 好):
INSERT INTO Versions (product_name, product_price, zone_name)
VALUES ('A', 1, 'G');
INSERT INTO Versions (product_name, product_price, zone_name)
VALUES ('A', 1, 'A');
INSERT INTO Versions (product_name, product_price, zone_name)
VALUES ('A', 1, 'Z');
INTO Versions (product_name, product_price, zone_name)
VALUES ('A', 2, 'A');
etc etc