2015-09-18 27 views
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和訪客接入,但如果我使用的第一個設計,我剛剛加入一個表。

回答

0

盡我所能,你有兩個實體表,訪客和新聞。現在你想跟蹤......究竟是什麼?哪些新聞...項目?...每個訪問者訪問?聽起來不祥。

如果這是正確的,那麼你想讓許多訪問者與許多新聞事件關聯。這是一個簡單的交集表:

create table NewsVisits(
    VisitorID int not null, 
    InfoID  int not null, 
    VisitCount int not null default 1, -- to allow same visitor to access same info many times??? 
    constraint PK_NewsVisits primary key(VisitorID, InfoID), 
    constraint FK_NewsVisits_Visitor foreign key(VisitorID) 
     references Visitors(ID), 
    constraint FK_NewsVisits_Info foreign key(InfoID) 
     references News(ID) 
); 

所以,當訪問者訪問一個新聞條目,插入信息 - 除非該訪問者已經訪問的新聞項目已經在這種情況下你遞增訪問計數。