question

jordanw44 avatar image
jordanw44 asked

Many-to-Many Relationship Table

I made a post yesterday here. After many hours, a severe headache, and some tylenol I have decided to try and see if I can get some help on this many-to-many relationship that I was suggested to try and use. I decided to define my tables a little better to hopefully provide you guys with some more and clearer information. Here is a screenshot:
![alt text][1]
How can I accomplish this with either a one-to-many or a many-to-many relationship table? Thanks!
[1]: /storage/temp/392-\capture.png
querytable
\capture.png (21.9 KiB)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

SirSQL avatar image
SirSQL answered
Something like this what you are after? CREATE TABLE Computer ( Asset INT NOT NULL PRIMARY KEY , DefaultPrinterID INT , WristbandPrinter INT ) CREATE TABLE Printer ( PrinterID INT IDENTITY(1, 1) NOT NULL PRIMARY KEY , PrinterPath VARCHAR(15) ) CREATE VIEW ComputerPrinterReference AS SELECT c.Asset AS Asset , p.PrinterPath AS DefaultPrinter , p2.PrinterPath AS WristbandPrinter FROM Computer c INNER JOIN Printer p ON c.DefaultPrinterID = p.PrinterID INNER JOIN Printer p2 ON c.WristbandPrinter = p2.PrinterID GO
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

jordanw44 avatar image jordanw44 commented ·
@SirSQL on line 3, 4, and 5 you have "c.Aset AS Asset" etc... What exactly are you doing with this? It looks like you are declaring variables almost but I'm not sure if I am even on the right path.
0 Likes 0 ·
jordanw44 avatar image jordanw44 commented ·
I see definite benefits in re-organizing my database like this. I would have to change a couple things around in one of my programs insert statments but this could be tremendously better in the end. I can see myself needing to know what computers are mapped to each printer as well so I will study up on my pivot's.
0 Likes 0 ·
Grant Fritchey avatar image
Grant Fritchey answered
No, something like this: ![alt text][1] Then you only put computer information into the computer table. You only put printer information into the printer table. The relationship table defines if it's a default wristband, hoozymawatchit or anything else. Then, the ComputerPrinter table brings them all together. This way you can get listings any way you want them. I will tell you, the one wrinkle is, if you want to output printers across the page for each computer, you'll need to use a type of query called a pivot. I leave that to you for additional homework. [1]: /storage/temp/393-manytomany.png

manytomany.png (22.0 KiB)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.