Results 1 to 7 of 7

Thread: Database Design

  1. #1

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Database Design

    I need help designing my database for my knowledge base on my website. I need a way to store the following:

    ID
    Title
    Body
    Author
    Primary Category
    Sub Category
    Pictures

    Can you store pictures in a MySQL database and if so, how do you retreive them for use? Also, how should I setup the primary and sub categories? Just as text? Since I want this database searchable, should I make everything but ID and Pictures full text? I'm still new at this. I got a book but it sucks

  2. #2
    Addicted Member techwizz's Avatar
    Join Date
    Apr 2005
    Location
    U.S.A.
    Posts
    246

    Talking Re: Database Design

    Here is the code to create a MySQL table named table1 .. i didnt know what to call the table.. lol

    Code:
    CREATE TABLE `table1` (
      `ID` int(6) unsigned NOT NULL auto_increment,
      `Title` text NOT NULL,
      `Body` text NOT NULL,
      `Authur` text NOT NULL,
      `PrimaryCat` text NOT NULL,
      `SubCat` text NOT NULL,
      `Picture` text NOT NULL,
      PRIMARY KEY  (`ID`)
    ) TYPE=MyISAM AUTO_INCREMENT=1 ;
    MySQl doesnt store images but stores image locations or names as text. Then the php echos an image... and ask MySQL for the name. PHP builds the picture frame.. MySQL moves the frame so you can see the pic. That might be a dumb analogy... but hey... its Free. Lol

    Here is possibly a better deffinition / explanation in this post link.



  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Database Design

    I would say eliminate the primary category and sub category field from your table and put them in a separate table called kb_categories. You should also have an additional table for Categories too, so it will look something like this:
    Code:
    +-------------+
    | kb_articles |
    |=============|
    | id {PK}     |-|------|
    | title       |        |
    | body        |        |       
    | author      |        |
    | pictures    |        | 
    +-------------+        |
                           |
                           |
    +------------------+   |
    | kb_categories    |   |
    |==================|   |
    | aritcle_iD {PK}  |3--|    
    | category_iD {PK} |3----|
    +------------------+     |
                             |
                             | 
    +---------------+        |
    | categories    |        |
    |===============|        |
    | id            |-|------|
    | category_name |
    +---------------+
    PK - denotes a primary key.

    Instead of using the names we use a reference to the record in another table.

    The reason I recommend this is that it does not constrain your articles to only two categories, each article can now belong to as many categories you like. You may want to keep the primary category field in you kb_articles table though, if you want each article to have a main category.

    Another tip for MySql daabases, when naming fields and tables, always use lower case letters separated by underscores for each word as Mysql on linux is case sensitive.

    P.s: I've moved this to the DB forum so some more expert eyes can have a look.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4
    Hyperactive Member The_Duck's Avatar
    Join Date
    May 2005
    Location
    Leamington, UK
    Posts
    351

    Re: Database Design

    If you have a webserver might I suggest phpmyadmin (http://www.phpmyadmin.net/home_page/) - you will need php as well

    This will make it much easier for you to create and edit your database tables

    If you don't have a webserver I would recommend abyss web server (http://www.aprelium.com/) (Version x1)

    Good Luck!
    Last edited by The_Duck; May 28th, 2005 at 03:43 AM.

  5. #5

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Database Design

    I understand how to create tables and such. I really needed to know like, do I make categories at text fields and then use a delimiter like a comma to seperate each one and parse in PHP or is there another way? I needed to get my database down so I could start coding my Knowledge base

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Database Design

    Using a delimited text field to contain references is a bad idea, as it will slow everything down dramatically. I would recommend using a table structure more along the lines that visualAd suggested, with all ID fields as numerics.

    Instead of using the table kb_categories you could just add an extra field to the categories table for the sub-category name. eg:
    Code:
    kb_articles
      article_id {PK}     
      category_id {FK}
      title       
      body      
      author    
      pictures  
    
    categories
      category_id {PK}
      category_name
      sub_category_name
    This allows very little duplication of data (the category_name will be repeated for each sub-category that belongs to it). It does mean however that if you change the name of a category it needs to be updated for all sub-categories.

    A design which eliminates duplication is this:
    Code:
    kb_articles
      article_id {PK}     
      sub_category_id {FK}
      title       
      body      
      author    
      pictures  
    
    sub_categories
      sub_category_id {PK}
      category_id {FK}
      sub_category_name
    
    categories
      category_id {PK}
      category_name
    Which is better/faster is up to how you intend to use the data, but unless you have a reason not to I would recommend the second of these desgins.

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Database Design

    I described the other way, which is better in post #3. Using a delimiter to separate each category creates he following issues:
    • Redundant / extra processing by the application (i.e: the PHP script) to separate and group categories.
    • How do you check a category is valid?
    • How would you rename a category?
    • A type could result in to separate categories which are meant to be one category.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width