LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-02-2006, 09:36 AM   #1
anickless
LQ Newbie
 
Registered: Nov 2005
Location: Edmonton
Distribution: Debian sarge & woody
Posts: 29

Rep: Reputation: 15
Sql table relationship question


I just have a basic SQL problem that i can't seem to solve.
For my company i am trying to create a database that keeps track of users the computer they are useing and the monitor of that computer.

that's been done and is o.k but here is the problem the relationship between the computer table and the monitor table is a 1-N relationship one computer can be assosiated with many monitors the foreign key or rather the link between the two table is the monitor.

i need to be able to add multiple monitors of the same type e.g we buy viewsonic so in theory if i query the monitors table all the monitor type would come back "viewsonic"

in my current setup once i add one monitor of type "viewsonic" to the computer table i cannot add for the second type the same name. how can i make it so i can add multiples of the same type?
 
Old 03-02-2006, 04:14 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
You need to add third table (ok, that's one of the option, but it first the situation good). Rename current 'monitor' to 'monitor model'. The new one will be called 'monitor' and has a rather simple structure: key (primary, unique), monitor serial (or any other id that identifies *one* monitor) and foreign key - monitor type.

So, computer table will have 'monitor id' foreign key - key from new 'monitor table'.
 
Old 03-02-2006, 04:24 PM   #3
anickless
LQ Newbie
 
Registered: Nov 2005
Location: Edmonton
Distribution: Debian sarge & woody
Posts: 29

Original Poster
Rep: Reputation: 15
Let me see if i understand you:

table: Computer
cpu
monitor_id(pk)

monitor
monitor_id(fk)
serial_number(pk)
monitor_type(pk)

monitor model
monitor_type(fk)
manufacturer
purchase_date


is this correct? are you sure about the associations?

Last edited by anickless; 03-02-2006 at 04:27 PM.
 
Old 03-02-2006, 06:40 PM   #4
Vagrant
Member
 
Registered: Nov 2001
Posts: 75

Rep: Reputation: 15
No, that doesn't look right. I don't really understand exactly what you are going for but this may be it.

table: User
user_id (pk)
... other info

table: Computer
cpu
cpu_id(pk)

table: Monitor
monitor_id(pk)
serial_number
monitor_type
purchase_date
manuf

table: cpu_monitor_pair
cpu_monitor_pair_id (pk)
monitor_id (fk) references Monitor
cpu_id (fk) references Computer

table: equipment_in_use
cpu_monitor_pair_id (fk) (pk)
user_id (fk) (pk)
 
Old 03-03-2006, 09:26 AM   #5
anickless
LQ Newbie
 
Registered: Nov 2005
Location: Edmonton
Distribution: Debian sarge & woody
Posts: 29

Original Poster
Rep: Reputation: 15
ok Vagrant this is what i am trying to do:

i want to keep try for administrative purposes to keep track of the users in our office, their passwords, email address and computer they are using. realitive to the computer they are using i want to keep track of specifics like cpu, ram, motherboard, serail_number, monitor used. i know i need another table to hold similar infomation for the monitors.
 
Old 03-03-2006, 05:21 PM   #6
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Quote:
Originally Posted by anickless
Let me see if i understand you:

table: Computer
cpu
monitor_id(pk)

monitor
monitor_id(fk)
serial_number(pk)
monitor_type(pk)

monitor model
monitor_type(fk)
manufacturer
purchase_date


is this correct? are you sure about the associations?
In this case it should be different... 'monitor' beeing instance, where 'model' beeing just a model. Purchase date moved to 'monitor' (it's instance-specific).

table: Computer
cpu
monitor_id(fk)

monitor
monitor_id(pk)
serial_number
monitor_type(fk)
purchase_date
Note: serial number may be id, but SNs are not always ints and don't have a 'standard' form, so integer id, separate from SN may be a good idea.

monitor model
monitor_type(pk)
manufacturer
color, parameters etc
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL statement to get the last row in a table oulevon Programming 11 04-03-2009 08:30 PM
SQL question: Need to add a column from another table Hivemind Programming 3 09-19-2005 06:02 PM
PHP script to check filetypes and put images into SQL table benrose111488 Programming 5 03-02-2005 01:57 AM
SQL Question jacksmash Programming 7 01-18-2004 10:10 PM
MySQL relationship question RecoilUK Programming 11 02-07-2002 07:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration