Solved

foreign key possible in myisam?

Posted on 2004-08-10
9
1,724 Views
Last Modified: 2008-03-10
i know that i can use foreign key constraints in InnoDB, but my isp doesn't offer this type of db.  Is it possible to use foreign key in MYISAM?  If so how would i go about implementing this?
0
Comment
Question by:lienny
  • 5
  • 3
9 Comments
 
LVL 17

Expert Comment

by:akshah123
ID: 11768211
I don;t think so

on following page

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

it clearlly says

" At a later stage, foreign key constraints will be implemented for MyISAM tables as well. "

THus no foreign keys in MYISAM right now.

: (
0
 

Author Comment

by:lienny
ID: 11768300
so i would have to enforce my foreign key in my sql statements?
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 11773868
yes
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:lienny
ID: 11775016
can you give me an example on how i would do that, then I'll reward the points.
Thanks!
0
 
LVL 33

Accepted Solution

by:
snoyes_jw earned 125 total points
ID: 11775917
Well, foreign keys just ensure that a key exists before inserting/updating, and deletes children rows if the parent row is deleted.

So say you have tables tableA and tableB, and tableB has a foregin key on tableA.
tableA:
id

tableB:
id
AId

Before you insert or update into tableB, check tableA:
SELECT * FROM tableA WHERE id = potential_AId
If no rows are returned, don't do the insert/update.  Alternatively, you can do it like this:
INSERT INTO tableB (AId, other_field) SELECT id, other_literal_string FROM tableA WHERE id = potential_AId;

When deleting from tableA, do this:
DELETE FROM tableB WHERE AId = id_to_delete;
DELETE FROM tableA WHERE id = id_to_delete;
0
 

Author Comment

by:lienny
ID: 11776183
lets say i have tables:

employees:
id
empNo
firstName
lastName

training:
id
eid
trainDate
training

when i do an insert how would i grab that id value from the employees table and have it automatically insert into the eid field of the training table?
0
 

Author Comment

by:lienny
ID: 11776246
sorry...let me clarify...it'll be 2 seperate forms...they can enter the employees in one form and then have a link to enter the training for that employee
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 11776757
I assume the id is automatically generated using auto_increment?

You have a couple of choices.
1) If using PHP, use the mysql_insert_id() method
http://us3.php.net/manual/en/function.mysql-insert-id.php

2) Use MySQL's LAST_INSERT_ID() method
http://dev.mysql.com/doc/mysql/en/Information_functions.html

3) Structure your second query to select from the employee table:
INSERT INTO training (eid, trainDate, training) SELECT id, '2004-08-11', 'GMP Training' FROM employees WHERE empNo = 42;
0
 

Author Comment

by:lienny
ID: 11776849
thanks.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now