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 02-02-2002, 12:30 AM   #1
RecoilUK
Member
 
Registered: Jul 2001
Location: Southampton, England
Distribution: Mandrake 8.2 , Slackware 8.1, $LFS
Posts: 270

Rep: Reputation: 30
MySQL relationship question


Hi guys

At the moment i,m designing a database for a web project i,m doing, but I wanted to ask your advice on something.

Its basically about relationship modelling.

In mysql, say if you have two tables, each with a primary key(id), and you want to combine both primary keys into a third table (which would be a one to many relationship) , how to do go about preventing the same two vaules from occuring twice in the third table?

Is it simply a matter of creating a primary key out of the two fields in the third table or is there more to it than that?

Thx guys

RecoilUK
 
Old 02-02-2002, 08:18 AM   #2
te_conway
Member
 
Registered: Apr 2001
Location: MA
Distribution: redhat 7.2
Posts: 182

Rep: Reputation: 30
Not knowing exactly what you want to do I can only summize but each table should have it's own primary key independent of any other table. This is important/safe/reliable when updating tables.

Table 1 has pkey id1
Table 2 has pkey id2 and foriegn key id1 (from table1)
possible index on id1+id2
Table 3 has pkey id3 and foriegn key id2 (from table2)
possible index on id2+id3


Your join is t1.id1 = t2.id1 and t2.id2 = t3.id2

Depending on your data you may need outside joins.
 
Old 02-02-2002, 07:35 PM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
No flames here, but mysql is not a real relational database. It's more like a data store with sql queries.

Foreign key relationships setup in mysql are ignored.
 
Old 02-03-2002, 12:10 AM   #4
RecoilUK
Member
 
Registered: Jul 2001
Location: Southampton, England
Distribution: Mandrake 8.2 , Slackware 8.1, $LFS
Posts: 270

Original Poster
Rep: Reputation: 30
Hi guys

Crabboy - whats your point exactly? does this impose any limitations or special considerations to be aware of when designing tables.

te_conway - Suppose I were to put in a pkey for a table, lets say its table1, that pkey would ofcourse be unique to that table, but what if a had two further fields in that table which also have to be unique, is this possible? the two fields in question are fkeys btw.

Thx guys

RecoilUK
 
Old 02-03-2002, 05:41 AM   #5
RecoilUK
Member
 
Registered: Jul 2001
Location: Southampton, England
Distribution: Mandrake 8.2 , Slackware 8.1, $LFS
Posts: 270

Original Poster
Rep: Reputation: 30
Hi guys

Ok, let me describe what i,m trying to do, so you understand it more.

Table one is called teams_table (pkey=team_id) and table 2 is called leagues_table (pkey=league_id).

Each team must be able to enter in as many league,s as they like, but, not in the same league twice, how would you go about doing this?.

Thx guys

RecoilUK
 
Old 02-03-2002, 02:35 PM   #6
te_conway
Member
 
Registered: Apr 2001
Location: MA
Distribution: redhat 7.2
Posts: 182

Rep: Reputation: 30
Relationships have as much to do with design as the database itself. True MySQL isn't a true relational database but neither is oracle if you don't set proper relationships. Because Mysql isn't relational you can't do cascade updates, deletes, etc.

There are several ways to do what you want. If you were assigning a team to a league it would be a simple matter of having a league_id in the teams table. Since teams can join multiple leagues I would create a third table and use it for subsequent selects. If you don't, either your team or league table will have lots of duplicate info. Consider this:

[Teams]
tid name coach
--- ---------- ------------
1 Patriots Belachuk
2 Rams Martz

[Leagues]
lid name Division
--- ---------- ------------
1 NFL NFC West
2 NFL AFC East

[TeamLeague] primary key is tid(team) + lid(league)
tid lid
--- ---
1 2
1 1
2 1
2 2

Get all the data like this:
Select t.*, l.* from teams t, leagues l, teamleagues tl
Where (t.tid = tl.tid and l.lid = tl.lid)

The same thing w/o a 3rd table. Here the pkey MUST be a two field combo and if any of the content changes, it has to be updated in more than one row. You carry lots of unnecessary data and it becomes very high maintenance.

[Leagues]
lid tid name Division
--- ---------- ------------
1 1 NFL NFC West
1 2 NFL AFC East
2 1 NFL NFC West
2 2 NFL AFC East

-Tom
 
Old 02-03-2002, 04:23 PM   #7
RecoilUK
Member
 
Registered: Jul 2001
Location: Southampton, England
Distribution: Mandrake 8.2 , Slackware 8.1, $LFS
Posts: 270

Original Poster
Rep: Reputation: 30
Hi again

Thx tom, thats wat I thought actually, it was just the fact that the third table didnt have its own independant id field that was throwing me, and I wasnt sure if you could issue a joint primary key in MySQL.

Thx again, hope I can repay the favour sometime

L8rs

RecoilUK
 
Old 02-04-2002, 10:26 PM   #8
tiamat
LQ Newbie
 
Registered: Jan 2002
Location: Sweden
Distribution: Redhat
Posts: 17

Rep: Reputation: 0
Using innodb tables in mysql makes foreign keys available.

Read the manual &&|| on their site.
 
Old 02-05-2002, 12:17 AM   #9
RecoilUK
Member
 
Registered: Jul 2001
Location: Southampton, England
Distribution: Mandrake 8.2 , Slackware 8.1, $LFS
Posts: 270

Original Poster
Rep: Reputation: 30
Hi guys

Yeah thx for the info, although I think they are still in beta, so I wont be using them just yet, i,ll just join table for now.

Actually, while i,m here, I may aswell ask another question. My database design so far is based on collecting info from users and putting it into a database, which is in itself not very hard. But, the next thing I shall be working on, is designing the table structure for the actual article,s and menu driven url information for site navigation.

Does anybody have any tips on this, or know of some really good tutorial about it?

Thx again guys

RecoilUK
 
Old 02-06-2002, 09:16 AM   #10
te_conway
Member
 
Registered: Apr 2001
Location: MA
Distribution: redhat 7.2
Posts: 182

Rep: Reputation: 30
RecoilUK,

I'm not sure what you're looking for, an html editor?

-tom
 
Old 02-06-2002, 04:08 PM   #11
RecoilUK
Member
 
Registered: Jul 2001
Location: Southampton, England
Distribution: Mandrake 8.2 , Slackware 8.1, $LFS
Posts: 270

Original Poster
Rep: Reputation: 30
Hi Tom

No not really, I know html allready, what i,m looking for is advice on creating a content management system for my project.

For instance, the site is divided into three parts, the title and add banner, the left hand menu/site navigation and the content.

What I cant quite understand is how the menu links on the left hand side, control the content. With html its easy because for every link you have a seperate page, but with PHP everything is pulled out from a database, so far i,ve come to the conclusion that templates are used but i,m also not sure how this is accomplished.

If anyone can offer advice on the above, links to tutorials or even really good books that actually cover the above question, I would be most gratefull.

L8rs

RecoilUK
 
Old 02-07-2002, 07:11 AM   #12
te_conway
Member
 
Registered: Apr 2001
Location: MA
Distribution: redhat 7.2
Posts: 182

Rep: Reputation: 30
Maybe I don't understand. Php is just a way to present and store data thru web pages like cgi perl.
The content logic isn't going to change, your menu will call individual php pages. the Php page might be a form with a method of post to save the data. The post is going to reference another php page.

I would look at www.php.net and links within that page.
 
  


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
mysql error Can't connect to local MySQL server through socket '/var/lib/mysql/mysql. Dannux Linux - Software 3 03-24-2006 08:44 AM
Trust relationship abhishek124 Linux - Networking 2 06-13-2005 02:18 AM
what's their relationship? jackandking Linux - Software 2 12-17-2004 06:21 AM
Relationship between X and clients. unholy Linux - Software 1 11-24-2003 02:38 PM
relationship advice Brain Drop General 38 07-15-2003 04:17 PM

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

All times are GMT -5. The time now is 08:26 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