LinuxQuestions.org
Visit Jeremy's Blog.
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-03-2005, 10:33 PM   #1
czarherr
Member
 
Registered: Sep 2003
Location: Suwon, Korea
Distribution: Slackware 14
Posts: 288

Rep: Reputation: 32
mysql relationships question


Im very new to mysql, and I'm trying to do a small database to link campers to addresses and specific camps and such. I know about setting primary keys and such, but one point eludes me. How does mysql actually link info in different tables properly? I have a name table, an address table, a camp table, and so on, but when I enter data into each of these tables seperately, how does mysql know what camp a person is in? I have the primary key name_id, and in every other table, I have this as a secondary key, as my manual explains to do, but it never explains how mysql will know what data goes together. Sorry this is so vague, I know so little I don't really know what it is I don't know. I've read lots of material on this, but nothing really covers this simple question.
 
Old 03-03-2005, 11:21 PM   #2
KurtAKARebuild
LQ Newbie
 
Registered: Feb 2005
Location: Australia
Distribution: Debian, Reformed Slackwareist
Posts: 20

Rep: Reputation: 0
This url from the MySQL tutorial gives a brief intro to implicit joins

http://dev.mysql.com/doc/mysql/en/multiple-tables.html

You could search the MySQL for more details about joins

http://dev.mysql.com/doc/mysql/en/index.html

but just quickly something like

SELECT * FROM name LEFT JOIN address ON name.name_id = address.name_id;

might be what your looking for.

the ON clause tells MySQL to associate the data.
 
Old 03-04-2005, 12:57 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You should look into foreign keys eg:
FOREIGN KEY (Name) REFERENCES Names (Name)

This is added to the create statement on child tables of the Names table (eg Addresses table). When you add a row to Addresses, the DB will only allow it if a row exists on the Names table with the same vale for the Name column on both tables.
ie
FOREIGN KEY (col_on_this_table) REFERENCES other_table (col_on_other_table)
 
  


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
Nessus Plugin Variable Relationships BoarderX Linux - Software 1 04-20-2005 07:06 PM
Samba 3.0 Alpha 23 Adding Trusted Relationships Vishi Linux - Networking 0 05-14-2003 02:46 AM
MySQL question Glover Linux - Software 3 08-22-2002 07:13 PM
MySQL Relationships Question? RecoilUK Linux - General 1 08-02-2001 06:05 PM

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

All times are GMT -5. The time now is 02:41 PM.

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