CREATE TABLE subscription (
magazine_id bigint,
user_id bigint,
PRIMARY KEY (magazine_id, user_id)
);
CREATE TABLE delivery (
magazine_id bigint,
user_id bigint,
FOREIGN KEY (subscription) REFERENCES subscription (magazine_id, user_id)
);
给定特定订阅查询交付的好方法是什么?有没有办法为列名和相应的外键分配一个方法,PRIMARY KEY (magazine_id, user_id)
以便我可以这样查询:
SELECT *
FROM subscription
JOIN delivery ON (delivery.subscription_fk = delivery.subscription_pk);
注意:我可以这样写:
SELECT *
FROM subscription
JOIN delivery ON (delivery.magazine_id = subscription.magazine_id
AND delivery.user_id = subscription.user_id);
但是,我的印象是有一种不那么冗长的方法来实现这一点。