x

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?

Thanks!


\capture.png (22.4 kB)
more ▼

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

avatar image

jordanw44
50 2 2 5

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

2 answers: sort voted first

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
more ▼

answered Sep 07, 2012 at 04:49 PM

avatar image

SirSQL
4.9k 4 5

@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

avatar image

Grant Fritchey ♦♦
137k 20 43 81

(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.

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

SQL Server Central

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

Topics:

x428
x111

asked: Sep 07, 2012 at 04:30 PM

Seen: 3874 times

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

Copyright 2016 Redgate Software. Privacy Policy