This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes JDBC and the fly likes Link Column Of two Table  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Link Column Of two Table  " Watch "Link Column Of two Table  " New topic
Author

Link Column Of two Table

anchit pancholi
Ranch Hand

Joined: Oct 14, 2010
Posts: 53
Hi,

I want to link two column of a two different table, if i update column of first table then it should automatically update to other table like :-
Master Table :- product_id, volume.
Child Table :- product_id,volume.

Master table product_id is FOREIGN KEY for child table.

is it possible if i update volume of Master table and it will automatically reflect in child table as well (and vise-versa)?

Thanks
Anchit
Zachary Pry
Greenhorn

Joined: Aug 09, 2011
Posts: 18
Hello Anchit,

If I understood your question correctly - SQL Triggers should satisfy your need.

If you are using Oracle Database - you can read Oracle Documentation Oracle PL/SQL Triggers

Not every Database out there supports triggers the same way. It would be easier to give you any further advice regarding it once you've share Database vendor you are using.


Open-source JDBC Library - Midao JDBC ( www.midao.org ).
anchit pancholi
Ranch Hand

Joined: Oct 14, 2010
Posts: 53
Hi Zachary,

Thanks for reply.

I am using MYSQL.

Thanks
Anchit.
Zachary Pry
Greenhorn

Joined: Aug 09, 2011
Posts: 18
Hello Anchit,

MySQL supports triggers as well MySQL Create Trigger.

Also here is good Tutorial.

Hope it helps.
anchit pancholi
Ranch Hand

Joined: Oct 14, 2010
Posts: 53
Hi Zachary,

Thanks for such a great help

Thanks
Anchit
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3435
    
  47

Do I understand it right, that the volume in a child record should always be the same as the volume in the master record? And then you update a child of the master record, the other children of the same master record (the child's "siblings") should be updated as well?

If this is so, the best thing to do would be to remove the volume column from the child table altogether. If you need to read the volume together with other attributes of the child table, you'll use joins. You might need to modify your business logic to some extent (modifying the volume in the child record might need a rethought), but your data model will not have a flaw that needs fixing with triggers.
 
wood burning stoves
 
subject: Link Column Of two Table
 
Similar Threads
doubt about master details relation and JDBC manner about retrieving details record
Auto fill in mysql
Autogenerated field as String datatype
Cascading non foreign key to Child Table
What effects by using a lot of foreign keys?