2010-05-30 121 views
1

我爲使用一個「項目」表的架構師構建了一個非常基本的php/mysql站點。 該網站展示了他所從事的各種項目。我應該如何設計我的MYSQL表格?

每個項目都包含一段文字和一系列圖像。

原始項目表(創建語法):

CREATE TABLE `projects` (
    `project_id` int(11) NOT NULL auto_increment, 
    `project_name` text, 
    `project_text` text, 
    `image_filenames` text, 
    `image_folder` text, 
    `project_pdf` text, 
    PRIMARY KEY (`project_id`) 
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1; 

客戶現在需要以下內容,我不知道如何處理擴展在我的數據庫。 我的懷疑是我需要一張額外的桌子。

每個項目現在都有'網頁'。

網頁或者包含...

  • 一個圖像
  • 一文的 「作品」
  • 一個圖像和文本的一塊。

每個頁面可以使用三種佈局之一。

由於每個項目目前沒有超過4個文本(一個非常冒險的假設),我已經擴展了原始表以適應一切。

新項目表的嘗試(創建語法):

CREATE TABLE `projects` (
    `project_id` int(11) NOT NULL AUTO_INCREMENT, 
    `project_name` text, 
    `project_pdf` text, 
    `project_image_folder` text, 
    `project_img_filenames` text, 
    `pages_with_text` text, 
    `pages_without_img` text, 
    `pages_layout_type` text, 
    `pages_title` text, 
    `page_text_a` text, 
    `page_text_b` text, 
    `page_text_c` text, 
    `page_text_d` text, 
    PRIMARY KEY (`project_id`) 
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1; 

在試圖瞭解更多關於MYSQL表結構剛纔我看到an intro to normalizationA Simple Guide to Five Normal Forms in Relational Database Theory。我會繼續閱讀! 在此先感謝

回答

3

正常化是你的朋友。

你會想要移動到關係型兩表設計。

CREATE TABLE projects (
    project_id int not null primary key auto_increment, 
    project_name varchar(128), 
    -- ... 
); 

CREATE TABLE pages (
    page_id int not null primary key auto_increment, 
    project_id int not null, 
    pagetext text, 
    image varchar(128), 
    -- ... 
); 

現在每個項目可以有任意數量的頁面。

如果客戶再回來,說:「每個頁面都可以有0-N圖片」,你會想要第三個表,其中包含一個外鍵page_id(就像頁面表有一個project_id外鍵)

+0

好啊,所以它在PHP中我將通過project_id鏈接表格 – 2010-05-30 21:59:28

+0

@ yaya3:不,你可以在SQL中連接表格 – 2010-05-30 22:28:15

+0

@keenan:yeh也許我應該說'在表格設計之外,在我的代碼'而不是'PHP'? – 2010-05-30 22:53:07