【发布时间】:2014-06-25 17:38:15
【问题描述】:
我有这些表:
CREATE TABLE items(
id int,
...
)
CREATE TABLE types(
id char(1),
...
)
CREATE TABLE prices(
item int,
type char(1) NULL, --can not be null because it is in PK!
price decimal,
PRIMARY KEY (item, version),
FOREIGN KEY (item) REFERENCES items(id),
FOREIGN KEY (type) REFERENCES types(id)
)
并非所有商品的价格都不同:
INSERT INTO prices (item, type, price)
VALUES (1,'A',10.0),
VALUES (1,'B',20.0),
VALUES (1,'C',20.0),
VALUES (2,NULL,50.0),
VALUES (3,'A',10.0),
VALUES (3,'B',20.0),
VALUES (4,NULL,70.0);
如您所见,某些商品 (2,4) 只有一个价格。 这样我可以在价格和类型之间设置外键,但我不能在价格的主键中添加类型,因为它可以为空......如何解决这个问题?我需要外键和主键来键入字段,但这可以是可选的。
【问题讨论】:
标签: mysql sql nullable composite-primary-key