How to set up a variable 2d array in a database?

Discussion in 'Databases' started by mj23, Nov 12, 2006.

  1. #1
    How would you set up a variable 2d array in a database?

    My problem visualizing the setup for this may be because I have quite a bit of experience with Microsoft Excel using "lookup" functions and I'm just starting out with databases.

    In excel you can have a table (x, y) with x columns and y rows. The number of columns (x) and rows (y) may vary from table to table.

    How would I set up something like this in a database? Do I need to create a separate table for every widget? There will be MANY widgets so creating a separate table seems cumbersome.

    Using an example table composed of WIDGETS. I want to be able to lookup the number of bolts each widget has. The number of bolts depends on x and y. The 2 widgets below would belong to the WIDGETS table, but you can see each widgets 2d array varies in size. The goal (for example) would be to lookup Widget 1, with a "y" value of 'B' and an "x" value of '5'.

    WIDGET #1
    -------------A-----B-----C-----D
    2 wheels----4-----8----10----16
    5 wheels----8----10----10----18
    8 wheels----9----10----10----n/a

    WIDGET #2
    -------------A-----B-----C
    2 wheels----3-----4-----5
    4 wheels----5-----6-----7
    6 wheels----7-----8-----9
     
    mj23, Nov 12, 2006 IP
  2. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #2
    TABLE "widgets"

    widget_id, widget_name


    TABLE "bolts"

    id, widget_id, x, y, number_of_bolts


    Using your example it would look like this:

    
    [b]widget_id[/b]	[b]widget_name[/b]
    1		widget #1
    2		widget #2
    
    
    [b]id[/b]		[b]widget_id[/b]	[b]x[/b]	[b]y[/b]	[b]number_of_bolts[/b]
    1		1		2	A	4
    2		1		2	B	8
    3		1		2	C	10
    ...
    
    Code (markup):

    "SELECT number_of_bolts FROM bolts WHERE widget_id = 1 AND y = 'B' AND x = 5"
     
    SoKickIt, Nov 13, 2006 IP