Solved

foreign key support for NDBCLUSTER

Posted on 2008-10-22
5
1,022 Views
Last Modified: 2008-11-19
Hi,
   i read that innoDB engine supports foreign key,but not clustering.
and NDBclustering engine will support clustering but not foerign key.
however,
i wanted to use both clustering and foreign key support in my application.
please tell.is there any other way to do it.
Regds,
vijaya
0
Comment
Question by:vijji_lakshmi
  • 3
  • 2
5 Comments
 
LVL 50

Expert Comment

by:Steve Bink
Comment Utility
The NDB engine does not explicitly support foreign keys, but it does not disallow the functionality if you choose to implement it yourself.  If your database-layer API does the work in verifying adherence to a foreign key schema, you can effectively duplicate that functionality.  For example, instead of relying on MySQL to return an error if a DELETE would result in orphaned child rows, your API should do the check with an additional query.

It is a bit more work, and it will slow down your application a little, but it can be done.
0
 

Author Comment

by:vijji_lakshmi
Comment Utility
can you please provide with the examples link.where can i refer how they done.
0
 
LVL 50

Expert Comment

by:Steve Bink
Comment Utility
Your absolute best option is to gain a firm understanding of how foreign keys work.  Once you grasp the concepts, duplicating their actions through your own queries is relatively simple.  I've given some hints and tips below, but be sure to read through the foreign key sections in the MySQL manual:

http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html

First, you have to decide your FK strategy.  Will you cascade changes, or restrict them, etc.?  There are three aspects you need to consider.  All of the examples below consider two tables: table1 (parent) and table2 (child)

For delete queries, you will need to check for children.  If you are cascading, then your queries are relatively simple:

DELETE FROM table1 WHERE id=1
DELETE FROM table2 WHERE FK_id=1

Ideally, these queries would be in a transaction so that they both succeed or both fail.  If only one succeeds, you'll be left with an orphan somewhere.  If you are restricting, then you have to check for a child first.  If a child exists, cancel the delete query:

<?
$query = "SELECT * FROM table2 WHERE FK_id=1";
$result=mysql_query($query);
$check_value=mysql_num_rows($result);
if ((int)$check_value > 0) {
  // children exist...do not delete
} else {
  // no children exist
  $query = "DELETE FROM table1 WHERE id=1";
}
?>

For INSERT queries, you'll need to check for the existence of a parent.  Normal FKs do not allow for inserting a child with a valid parent.  Since you're doing this manually, you theoretically *could* allow it, but you'd be left with an orphan.  The check might look something like this:

<?
$query = "SELECT * FROM table1 WHERE id=1";
$result=mysql_query($query);
$check_value=mysql_num_rows($result);
if ((int)$check_value > 0) {
  // parent exists
  $query = "INSERT INTO table2 (field1,field2) VALUES ('value1','value2')";
  $result=mysql_query($query);
} else {
  // no parent exists
}
?>

For update queries, it gets a little more complex, but it's the same basic idea.  For every change you need to push down to a child, create a query to do that job.  Again, all the foreign key queries you create would (ideally) be part of the same transaction to prevent your constraints from breaking.
0
 

Author Comment

by:vijji_lakshmi
Comment Utility
i read that there is an alreadu existing NDB API for supporting foreign key.
can you please let me know how to use it with java??
please reply me soon it is very urgent.
0
 
LVL 50

Accepted Solution

by:
Steve Bink earned 500 total points
Comment Utility
There is an existing API for NDB.  You can find it here:

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

But it does seem to have support for foreign keys any more than the docs say it is supposed to.  Have you seen another API capable of supporting foreign keys in NDB?
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

10 Experts available now in Live!

Get 1:1 Help Now