0
我要統計訪問信息數據,信息有兩個不同的表, 我設計的表使用類型告訴不同的表, 如如何設計表方便插入和選擇,以及良好的設計
CREATE TABLE visitor(
visitorId BIGINT UNSIGNED NOT NULL,
informationId BIGINT UNSIGNED NOT NULL,
visitCount BIGINT UNSIGNED NOT NULL,
type -- "news" or "tech"
PRIMARY KEY (visitorId , informationId),
);
我知道它是多態關聯,並且表不是跟隨3NF, 傳遞依賴是當一個非關鍵字段的值依賴於另一個非關鍵字段的值。 類型列取決於visitedInformation, 我試圖拆表,
CREATE TABLE newVisit (
informationId BIGINT UNSIGNED NOT NULL,
visitorId BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (issue_id, comment_id),
FOREIGN KEY (informationId) REFERENCES news(id),
FOREIGN KEY (visitorId) REFERENCES Visitor(id)
);
CREATE TABLE newVisit (
informationId BIGINT UNSIGNED NOT NULL,
visitorId BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (issue_id, comment_id),
FOREIGN KEY (informationId) REFERENCES news(id),
FOREIGN KEY (visitorId) REFERENCES Visitor(id)
);
CREATE TABLE visitor(
visitorId BIGINT UNSIGNED NOT NULL,
informationId BIGINT UNSIGNED NOT NULL,
visitCount BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (visitorId , informationId),
);
但外鍵visitorId,不能識別訪問者的行, 我應該使用表的設計?
CREATE TABLE newVisit (
informationId BIGINT UNSIGNED NOT NULL,
visitorId BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (issue_id, comment_id),
foreign key (informationId , visitorId)
references visitor(informationId , visitorId),
);
我不認爲該表是方便插入數據,我不得不加入newVisit和訪客接入,但如果我使用的第一個設計,我剛剛加入一個表。