Question

Multiple foreign keys in one table referencing the same column in a second table

Asked by: sedberg1

Honestly, this is a school project I can't figure out a small part.  I'm using Oracle 10g Personal Edition and always use SQLPLUS.  There are two tables, PROCEDURES and TRANSACTIONS.  

The columns are:
PROCEDURES:
PROCEDURE# (primary key)
DESCRIPTION (varchar2)
AMOUNT (number)

TRANSACTIONS:
CASE# (primary key)
PATIENT#
TRANSDATE
PROCEDURE#1 (foreign key to PROCEDURE# in PROCEDURES)
PROCEDURE#2 (foreign key to PROCEDURE# in PROCEDURES)
PROCEDURE#3 (foreign key to PROCEDURE# in PROCEDURES)
AMOUNT#1 (AMOUNT from PROCEDURE#1)
AMOUNT#2 (AMOUNT from PROCEDURE#2)
AMOUNT#3 (AMOUNT from PROCEDURE#3)
TOTAL (SUM of AMOUNT#1 - AMOUNT#3)

So I have everything done and created and all my constraints.  What I'm trying to do is get AMOUNT#1, AMOUNT#2, and AMOUNT#3 to automatically populate with the appropriate amount from the PROCEDURES table.  Is this possible?  I'm only about beginner-intermediate with my knowledge of SQL.  Can I include this in the CREATE TABLE statement for TRANSACTIONS?

CREATE TABLE PROCEDURES
	(PROCEDURE# CHAR(4) PRIMARY KEY,
	DESCRIPTION VARCHAR2(50),
	AMOUNT NUMBER(10))
 
CREATE TABLE TRANSACTIONS
	(CASE# NUMBER(5) PRIMARY KEY,
	PATIENT# NUMBER(5),
	TRANSDATE DATE DEFAULT SYSDATE NOT NULL,
	PROCEDURE#1 CHAR(6),
	PROCEDURE#2 CHAR(6),
	PROCEDURE#3 CHAR(6),
	AMOUNT#1 NUMBER(10),
                     AMOUNT#2 NUMBER(10),
                      AMOUNT#3 NUMBER(10),
	TOTAL NUMBER(10),
	CONSTRAINT TRANSACTIONS_PATIENT#_FK FOREIGN KEY (PATIENT#) REFERENCES PATIENTS(PATIENT#),
	CONSTRAINT TRANSACTIONS_PROCEDURES#1_FK FOREIGN KEY (PROCEDURE#1) REFERENCES PROCEDURES(PROCEDURE#),
	CONSTRAINT TRANSACTIONS_PROCEDURES#2_FK FOREIGN KEY (PROCEDURE#2) REFERENCES PROCEDURES(PROCEDURE#),
	CONSTRAINT TRANSACTIONS_PROCEDURES#3_FK FOREIGN KEY (PROCEDURE#3) REFERENCES PROCEDURES(PROCEDURE#));

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-03-05 at 18:44:19ID24204477
Tags

Oracle 10g

Topics

SQL Query Syntax

,

Oracle 10.x

,

Oracle CRM

Participating Experts
4
Points
500
Comments
8

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. sqlplus
    I work with oracle 8 on solaris2.6 I have sqlplus installed under user oracle and only from there it works. when another user tries to run this executable it says: Message file sp1<lang>.msb not found Error 6 initializing SQL*Plus. How can I install sqlplus so all the u...
  2. Foreign Key Constraint
    Is there any way to prevent a Value from being deleted in a table that is being used as a foreign key. Example: if a table called DEPT has a primary key of DeptNo (Dept list all the valid DeptNos) and the Employee table has a column DeptNo which it specifies Employee.DeptN...
  3. varchar2
    what is the history of varchar2.I mean why it is varchar2 rather varchar.
  4. Oracle SQLPlus configuration
    hi, just a simple question about how to make SQLPlus dump complete date columns. I already used the command ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY/MM/DD HH24:MI:SS' to format correctly the dates, but the result set truncated the dates to 9 (or 10) characters. what should...
  5. How Convert varchar2 to clob in oracle 8.1.7
    Hello everybody, I need to convert a column tpye from Varchar2 to Clob. This table is referenced by other tables and I have to retain the data of this table. I am using Oracle 8.1.7 Can anybody help me. Thanks in advance. Regards, Deepak
  6. how can i change a varchar2 column to a number column
    im having two tables for eg;. 1. ------------ CREATE TABLE offrdet1 ( ang_nr VARCHAR2(12) NOT NULL, ver_nr NUMBER(2,0) NOT NULL, ver_besh VARCHAR2(40) NOT NULL, gesc_jahr NUMBER(4,0) NOT NULL, curr VARCHAR2(3) NOT NULL, ges_kalk VARCHAR2(20) NULL, ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: quincydudePosted on 2009-03-05 at 19:22:30ID: 23813579

Do u mean you want to have some setting like the 'default' thing that once user insert a record with values PROCEDURE#1,2,3 then the db will retrieve the corresponding AMOUNT#1,2,3 from those table for you?
I have never heard of such feature but if there's one it would be great.

Oh wait, I think you can achieve the same functionality  using trigger.
Use the after insert trigger
http://www.techonthenet.com/oracle/triggers/after_insert.php

Hope this helps.

 

by: k_murli_krishnaPosted on 2009-03-05 at 19:35:22ID: 23813622

I could not understand why you have split into 3 foreign keys in TRANSACTIONS tables pointing to same primary key in PROCEDURES table. And then again 2 AMOUNT columns for 1 in PROCEDURES. I suppose the foreign key columns are declared as NULL. As per me it should be PROCEDURE# single column as foreign key. Also is AMOUNT = TOTAL = AMOUNT#1+ AMOUNT#2 + AMOUNT#3.  If this is so then TOTAL column is dangerously redundant with a different name.

You can define BEFORE/AFTER INSERT/UPDATE/DELETE triggers on PROCEDURES table so that when ever one such operation occurs then within the trigger body/definition you will write a calcualted INSERT/UPDATE/DELETE on TRANSACTIONS table. Then again trigger can be FOR EACH ROW (mostly this is the one used) and FOR EACH STATEMENT. The trigger can call a procedure/function(this you can use in queries). The other way round is keep the entire set of operations i.e. let us say INSERT in PROCEDURES and TRANSACTIONS in a single transaction (logical unit of work) on whose completion you will COMMIT else if error rollback the whole thing. You need to set auto-commit false for this.

The third way is keep the entire set of operations in a SQL script or OS embedded SQL file and schedule jobs to execute periodically. This is only if it is this way.

 

by: k_murli_krishnaPosted on 2009-03-05 at 19:38:50ID: 23813633

The AFTER triggers are not mutative i.e. if you carry out a INSERT on same table again in the trigger or the procedure it calls, the trigger is not called recursively but goes through and ends unlike BEFORE triggers. This is since on COMMIT only the trigger is called again in case of AFTER trigger and COMMIT occurs only when  the triggers exits.

 

by: sedberg1Posted on 2009-03-05 at 20:07:43ID: 23813764

The amount is how much a particular procedure costs.  So in TRANSACTIONS, if I have three procedures, there are three costs.  And the TOTAL in TRANSACTIONS is the sum of those three amounts (one for each procedure).

I'm still a student and what you wrote though I'm sure it's correct, went WAY over my head.  I haven't learned about triggers yet.  I think really the easiest way for me to do this is to just have one procedure in the TRANSACTIONS table.  However, I'll have to manually put in the amount in TRANSACTIONS since I don't understand how to do the triggers.

 

by: dportasPosted on 2009-03-05 at 23:38:49ID: 23814521

>> if I have three procedures, there are three costs.

And if you have 4 or more procedures? Repeating arrays of values like yours are usually best represented as rows in an additional table. So for example you could create a table for Transaction amounts with ONE amount column and populate that with three rows.

 

by: sedberg1Posted on 2009-03-06 at 06:38:59ID: 23816924

ok.

 

by: k_murli_krishnaPosted on 2009-03-06 at 09:28:54ID: 23818897

Please visit these links:
Coding Triggers
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14251/adfns_triggers.htm
Triggers
http://www.sc.ehu.es/siwebso/KZCC/Oracle_10g_Documentacion/server.101/b10743/triggers.htm
Oracle 10g Training - Triggers And Triggering Events
http://www.exforsys.com/tutorials/oracle-10g/oracle-10g-triggers-and-triggering-events.html

One more point: Apart from BEFORE and AFTER trigger one can have INSTEAD OF trigger. Also, with a clause while creating trigger i.e. REFERENCES OLD AS OLD NEW AS NEW for INSERT/UPDATE access new value using :NEW.<column> and for DELETE/UPDATE access old value using :OLD.<column>.

 

by: dkaislaPosted on 2009-03-09 at 00:32:51ID: 23833643

It is best if you have another table for procedures availed in a case, where you will have one entry per procedure# availed by a case#. This is so that you can have a dynamic number of procedures availed by a case#.
You need not save the amount of each procedure availed by the patient for it can be derived from the PROCEDURES table. saving the amount into another table will cause redundancy.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...