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

How can I accomplish this with either a one-to-many or a many-to-many relationship table?


\capture.png (22.4 kB)
more ▼

asked Sep 07, 2012 at 04:30 PM in Default

jordanw44 gravatar image

50 2 2 3

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

Something like this what you are after?

      Asset INT NOT NULL
                PRIMARY KEY ,
      DefaultPrinterID INT ,
      WristbandPrinter INT

      PrinterID INT IDENTITY(1, 1)
                    NOT NULL
                    PRIMARY KEY ,
      PrinterPath VARCHAR(15)

CREATE VIEW ComputerPrinterReference
    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
more ▼

answered Sep 07, 2012 at 04:49 PM

SirSQL gravatar image

4.8k 1 3

@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.
Sep 07, 2012 at 04:56 PM jordanw44
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.
Sep 07, 2012 at 05:50 PM jordanw44
(comments are locked)
10|1200 characters needed characters left

No, something like this: alt text

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.

manytomany.png (22.5 kB)
more ▼

answered Sep 07, 2012 at 05:25 PM

Grant Fritchey gravatar image

Grant Fritchey ♦♦
103k 19 21 74

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Sep 07, 2012 at 04:30 PM

Seen: 2021 times

Last Updated: Sep 07, 2012 at 05:58 PM