答案 0 :(得分:0)
我认为将会有很多,因为多个客户可以关注商家,每个商家都与多个客户相关。要规范化此关系,您可以创建一个新表-将其命名为customer_merchants或类似的名称。它只有两列,customer_id和merchant_id。
每次有客户跟随商人时,都会在customer_merchant中创建一个新行以反映这一事实。
现在您在客户和customer_merchants之间具有一对多关系,而在商家和customer_merchant之间具有一对多关系。
这是一个有关数据库规范化的网页,可以帮助您了解更多信息:-
https://www.w3schools.in/dbms/database-normalization/
希望有帮助。
答案 1 :(得分:0)
这是经典的“多通”关系。
由于关系是在不同的模型之间(例如,一个客户不能跟随另一个客户),因此您不必构造多态表;只需有“关注者”作为联接。
答案 2 :(得分:0)
我怀疑我可能没有完全理解您所有设计的含义,所以我用我能做的第五种标准形式进行设计。
我认为客户,用户和商人都是角色。 例如,语义线索之一是“性别”是人的财产,而不是所谓的“用户”。 (“用户”是一个人扮演的角色)
另一种好的做法是将所有地址数据存储在一个表中。 每个角色(客户,用户和商人)通过使用与“人”表的PK-FK关系而链接到地址表。
我假设一种产品可以有很多变体,这就是为什么变体有自己的表格。
SQL DDL基于T-SQL(对不起,我不会说PostGres,但是this wiki应该可以帮助您解决问题)
因此,这里有两个人工制品:第五范式的逻辑模型和该逻辑模型的SQL DDL。
SQL DDL
CREATE SCHEMA Orders
GO
CREATE TABLE Orders.Person
(
personNr int NOT NULL,
addressNr int NOT NULL,
firstName nchar(30) NOT NULL,
gender nchar(6) NOT NULL,
lastName nchar(30) NOT NULL,
CONSTRAINT Person_PK PRIMARY KEY(personNr)
)
GO
CREATE TABLE Orders.Address
(
addressNr int NOT NULL,
city nchar(50) NOT NULL,
countryCode nchar(2) NOT NULL,
postalCode nchar(12) NOT NULL,
street nchar(50) NOT NULL,
CONSTRAINT Address_PK PRIMARY KEY(addressNr)
)
GO
CREATE TABLE Orders.Customer
(
customerNr int NOT NULL,
orderNr int NOT NULL,
stripeID nchar(30) NOT NULL,
CONSTRAINT Customer_PK PRIMARY KEY(customerNr)
)
GO
CREATE TABLE Orders.Merchant
(
merchantNr int NOT NULL,
creationDate datetime NOT NULL,
merchantName nchar(30) NOT NULL,
CONSTRAINT Merchant_PK PRIMARY KEY(merchantNr)
)
GO
CREATE TABLE Orders."User"
(
userNr int NOT NULL,
email nchar(50) NOT NULL,
password nchar(50) NOT NULL,
userName nchar(20) NOT NULL,
CONSTRAINT User_PK PRIMARY KEY(userNr)
)
GO
CREATE TABLE Orders."Order"
(
orderNr int NOT NULL,
orderDate datetime NOT NULL,
orderStatus nchar(10) NOT NULL,
CONSTRAINT Order_PK PRIMARY KEY(orderNr)
)
GO
CREATE TABLE Orders.Product
(
productNr int NOT NULL,
createdDate datetime NOT NULL,
productName nchar(40) NOT NULL,
productStatus nchar(10) NOT NULL,
unitPrice decimal(6,2) NOT NULL,
CONSTRAINT Product_PK PRIMARY KEY(productNr)
)
GO
CREATE TABLE Orders.OrderHasProductInQuantity
(
orderNr int NOT NULL,
productNr int NOT NULL,
quantity smallint NOT NULL,
CONSTRAINT OrderHasProductInQuantity_PK PRIMARY KEY(orderNr, productNr, quantity)
)
GO
CREATE TABLE Orders.Variant
(
variantNr int NOT NULL,
imageURL nchar(200) NOT NULL,
title nchar(20) NOT NULL,
variantPrice decimal(6,2) NOT NULL,
CONSTRAINT Variant_PK PRIMARY KEY(variantNr)
)
GO
CREATE TABLE Orders.PersonLikesProduct
(
personNr int NOT NULL,
productNr int NOT NULL,
CONSTRAINT PersonLikesProduct_PK PRIMARY KEY(personNr, productNr)
)
GO
CREATE TABLE Orders.PersonFollowsMerchant
(
merchantNr int NOT NULL,
personNr int NOT NULL,
CONSTRAINT PersonFollowsMerchant_PK PRIMARY KEY(personNr, merchantNr)
)
GO
ALTER TABLE Orders.Person ADD CONSTRAINT Person_FK FOREIGN KEY (addressNr) REFERENCES Orders.Address (addressNr) ON DELETE NO ACTION ON UPDATE NO ACTION
GO
ALTER TABLE Orders.Customer ADD CONSTRAINT Customer_FK1 FOREIGN KEY (orderNr) REFERENCES Orders."Order" (orderNr) ON DELETE NO ACTION ON UPDATE NO ACTION
GO
ALTER TABLE Orders.Customer ADD CONSTRAINT Customer_FK2 FOREIGN KEY (customerNr) REFERENCES Orders.Person (personNr) ON DELETE NO ACTION ON UPDATE NO ACTION
GO
ALTER TABLE Orders.Merchant ADD CONSTRAINT Merchant_FK FOREIGN KEY (merchantNr) REFERENCES Orders.Person (personNr) ON DELETE NO ACTION ON UPDATE NO ACTION
GO
ALTER TABLE Orders."User" ADD CONSTRAINT User_FK FOREIGN KEY (userNr) REFERENCES Orders.Person (personNr) ON DELETE NO ACTION ON UPDATE NO ACTION
GO
ALTER TABLE Orders.OrderHasProductInQuantity ADD CONSTRAINT OrderHasProductInQuantity_FK1 FOREIGN KEY (orderNr) REFERENCES Orders."Order" (orderNr) ON DELETE NO ACTION ON UPDATE NO ACTION
GO
ALTER TABLE Orders.OrderHasProductInQuantity ADD CONSTRAINT OrderHasProductInQuantity_FK2 FOREIGN KEY (productNr) REFERENCES Orders.Product (productNr) ON DELETE NO ACTION ON UPDATE NO ACTION
GO
ALTER TABLE Orders.Variant ADD CONSTRAINT Variant_FK FOREIGN KEY (variantNr) REFERENCES Orders.Product (productNr) ON DELETE NO ACTION ON UPDATE NO ACTION
GO
ALTER TABLE Orders.PersonLikesProduct ADD CONSTRAINT PersonLikesProduct_FK1 FOREIGN KEY (personNr) REFERENCES Orders.Person (personNr) ON DELETE NO ACTION ON UPDATE NO ACTION
GO
ALTER TABLE Orders.PersonLikesProduct ADD CONSTRAINT PersonLikesProduct_FK2 FOREIGN KEY (productNr) REFERENCES Orders.Product (productNr) ON DELETE NO ACTION ON UPDATE NO ACTION
GO
ALTER TABLE Orders.PersonFollowsMerchant ADD CONSTRAINT PersonFollowsMerchant_FK1 FOREIGN KEY (personNr) REFERENCES Orders.Person (personNr) ON DELETE NO ACTION ON UPDATE NO ACTION
GO
ALTER TABLE Orders.PersonFollowsMerchant ADD CONSTRAINT PersonFollowsMerchant_FK2 FOREIGN KEY (merchantNr) REFERENCES Orders.Merchant (merchantNr) ON DELETE NO ACTION ON UPDATE NO ACTION
GO