Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,471
There are 567 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

relations in a simple mysql databse

Thread title: relations in a simple mysql databse
Closed Thread    
    Thread tools Search this thread Display Modes  
01-12-2005, 11:33 AM
#1
derek lapp is offline derek lapp
Status: design rockstar
Join date: Jan 2005
Location: guelph, ontario
Expertise:
Software:
 
Posts: 2,246
iTrader: 0 / 0%
 

derek lapp is on a distinguished road

  Old  relations in a simple mysql databse

i've been going through setting up a databse for my website, and i've run into a few things that don't make much sense; they're very impractical.

say i was setting up a projects databse and it looked like so
Code:
Table Name: projects
project_id | project_name | client_name |

Table Name: clients
client_id | client_name | etc
so my intention is to print out this information. say a client changes it's name, or merges or for some reason i'm to alter the client_name field.

i initally thought to use foreign keys to establish a good relationship between the two tables, so it would be like 'including' client_name from the clients table. i started reading about how only the InnoDB table type supports foreign keys, and the blurb i read said InnoDB is the "industrial strength" databse, so it seems really impartical to break out the maximum databse only to make this one simple relation.

since reading that, it seemed the only way yo create the relation was through something like manual entry. so client_1 is "john doe inc" so, ro the other table, i'd select 1 for his is from the drop down, and that name would be put into the project table. but say "john doe' changes the name, i'd have to go edit both tables . . . which really isn't a relationship at all.

all my googling and searching other forums hasn't helped the situation. i've only found isuues when using the foreign key on InnoDB, instructions on how to use it with InnoDB, or something that didn't make any sense to me.

so, how does one create a stable relationship between two tables through a regular MyISAM databse? forgien keys was my only idea and switching to innoDB seems very impractical, since it says it's not supported by default. after searchin through the index for relationships i was able to read that using them is a good idea and ie encouraged, but nothig more (educational writing at its finest). it said something about keys, and after looking up keys, it was just as useless.

little help? i've been lloking at this for the last three hours, and i've found nothing.

01-13-2005, 12:04 AM
#2
DateinaDash is offline DateinaDash
Status: The BidMaster
Join date: Nov 2004
Location: England
Expertise:
Software:
 
Posts: 10,821
iTrader: 0 / 0%
 

DateinaDash is on a distinguished road

  Old

Hey theory, not sure what package this is but i am quite familar with ms access as i did a course on it.

From what i can see you don't have any relationship in your tables (anything linking projects and clients) You are currently using client name, which isn't a unique identifer...what if several clients share the same name?

The solution is to create a unique key for both tables and this will be the primary key and thus create the relationship, something like "Client ID".

Hope that works

01-13-2005, 12:37 PM
#3
derek lapp is offline derek lapp
Status: design rockstar
Join date: Jan 2005
Location: guelph, ontario
Expertise:
Software:
 
Posts: 2,246
iTrader: 0 / 0%
 

derek lapp is on a distinguished road

  Old

yeah, for the tables i'm actually building i have clientId a the primarky key for the client table, but project_id is the primary key for projets.

after finally getting to speak ot some gurus, i've discovered, there isn't much i can do about it, in the sence i thought i could - it would function like a php include, so it would call 'clients' and plug in the name.

what i ahve found though is when running the edit script, i can call both tables and do a left join on the two fields and it will edit them both @ the same time. that or i've misunderstood why i'd be calling them with a select call in my php script.

on a side note, each client should have a unique name - if i do repeat business a new field doesn't get made, and the name refers to business name, not personal - IE talkfreelance not rob.

01-13-2005, 04:18 PM
#4
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

Personally I would structure the tables differently. You don't know all that much about databases so I won't bore you with technical terms, instead I'll just show you how I would do it (maybe).

Code:
Table Name: project
project_id | project_name | etc

Table Name: client
client_id | client_name | etc

Table Name: project_client
procli_id | procli_project | procli_client
This means that your project data is completely separate from your client data, and vice versa. It also allows you to have multiple clients per project (it can happen!). Simple JOIN commands can be used to relate the project_client table to the project and client tables if and when necessary.

01-13-2005, 04:21 PM
#5
tres is offline tres
Status: Sin Binner
Join date: Jan 2005
Location:
Expertise:
Software:
 
Posts: 5
iTrader: 0 / 0%
 

tres is on a distinguished road

  Old

What you have is a classic one to many relationship. In standared MySQL, relationships are not enforced by the database.

1) Use your (unique) primary_key as your foreign keys. i.e.. ad client_id to projects. MySQL will enforce a unique index and not let you add duplicate records.

2) If you merge two client accounts, all you have to do is update the client_id of the affected project records.

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed