How to create a mysql tables for image website?

Discussion in 'MySQL' started by eziokhan, Apr 16, 2014.

  1. #1
    Hello i am new to databases i know a bit about mysql but i am still learning and unable to create tables for my website . I have 10 main categories of images and then each main has further 3 sub categories . Please tell me how many databases and tables will i need for this? I am new to this stuff :(.
     
    Solved! View solution.
    eziokhan, Apr 16, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    One table for the categories, one for the subcategories, and one to connect them all together: image name, category-id, sub-category-id(s)
     
    PoPSiCLe, Apr 16, 2014 IP
  3. eziokhan

    eziokhan Greenhorn

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #3
    Please can you elaborate more , what columns should i create in these tables ? and relate them how .
     
    eziokhan, Apr 16, 2014 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,832
    Likes Received:
    4,541
    Best Answers:
    123
    Trophy Points:
    665
    #4
    You only need 1 database and I'd suggest you look for an existing script that does what you want. You'll learn faster by seeing what others do than by starting from scratch.

    At the bare minimum you need a category table with the following columns
    id int(11) primary key, auto increment
    parent_id(11)
    name

    then the image table
    id(11) primary key, auto increment
    category_id(11)
    name
    path
    source


    the columns you need will vary depending on how interactive you want the database to be. You might need a user_id, hell, you might need a whole user management and security setup.
     
    sarahk, Apr 16, 2014 IP
  5. #5
    Category table:
    id, category_name

    Sub-category table:
    id, sub_category_name

    Image-table:
    id, category_id, sub_category_id, image_name

    Now, if you want to add multiple sub_categories, you could do this differently, by adding a fourth table
    Image_subcategories
    id, image_id, sub_category_id
    And then repeat this for each subcategory you want that image to belong to - you would of course also remove the sub_category_id from the image table

    That way, the image-table will have a connection to the category table, and depending on your choice of one or multiple subcategories, a connection to the sub_category table
    This should be pretty straight forward to do via whatever MySQL-admin-software you have available - phpMyAdmin or similar.
     
    PoPSiCLe, Apr 16, 2014 IP
  6. eziokhan

    eziokhan Greenhorn

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #6
    thnx bro for such nice explanation, i am very new to all this , i will get deep into it :D. my head is woobling right now .:rolleyes:
     
    eziokhan, Apr 16, 2014 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    Just to clarify something about the image_subcategory table - and the line about repeating this - that was meant as "add one row for each image/subcategory", not a table pr image/subcategory, of course. It could be interpreted both ways, I saw.
     
    PoPSiCLe, Apr 17, 2014 IP
  8. eziokhan

    eziokhan Greenhorn

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #8
    category_id, sub_category_id will be set to primary key ?
     
    eziokhan, Apr 22, 2014 IP
  9. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #9
    If you're talking about the image-table, you can only have one primary key - and that will be the 'id'-column - you can index other columns depending on what you need, but mostly you'll just link tables with the corresponding IDs, so there isn't really that much need for that to begin with
     
    PoPSiCLe, Apr 22, 2014 IP
  10. eziokhan

    eziokhan Greenhorn

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #10
    you mean to say that category_id, sub_category_idw will have to set as foreign keys ?
     
    eziokhan, Apr 22, 2014 IP
  11. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #11
    You can do that, yes, if you want. They will then of course reference the primary IDs in the category-table and the sub_category-table.
     
    PoPSiCLe, Apr 22, 2014 IP
  12. eziokhan

    eziokhan Greenhorn

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #12
    thnx :)
     
    eziokhan, Apr 22, 2014 IP