Connect two mysql tables

Discussion in 'MySQL' started by emi87, Oct 3, 2008.

  1. #1
    Hello,
    I need a little help with connecting two tables. I think!!
    I want to create a table in this form if it is possible:
    table name Cars with rows Name, Year, Color, Options(where i want to insert how many options I want) ..it will look like this

    Cars - name
    - year
    - color
    - options - airbag
    - full electric
    - etc.

    I need
    - the code to create such a table...or 2 tables and how to connect the 2 tables...
    - and how to output all options
    Sorry for my bad english
    Thank you very much!
     
    emi87, Oct 3, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You basically want to have 2 tables. The first for the cars and the second for the options.

    Basically something like this:

    CARS TABLE....
    car_id
    year
    color
    other fields (Not options)...

    OPTIONS TABLE...
    option_id
    car_id (FROM ABOVE TABLE)
    option_description

    You can also use a foreign key to prevent bad data in the options table. You would place a foreign key on the car_id of the options table, to the car_id of the cars table. You have to be using innodb storage engine and not myisam for foreign keys.

    http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-foreign-keys.html
     
    jestep, Oct 4, 2008 IP