?
Solved

Oracle table config Foreign Key

Posted on 2013-12-19
6
Medium Priority
?
495 Views
Last Modified: 2013-12-19
Im looking to create a Foreign Key relationship and I want to be able to do a Cascading delete from the main table to the Main_Details table.
This is my primary table script. I have a trigger adding the ID value on every insert. I want to create a table that holds the detail data with the foreign key to the POL_ID from main table

Heres the main table

CREATE TABLE ANET.POLICY_DATA
(
  POL_ID          NUMBER,
  POL_TITLE       VARCHAR2(100 BYTE),
  POL_NUMBER      VARCHAR2(10 BYTE),
  POL_SUPERSEDES  VARCHAR2(100 BYTE),
  POL_EFF_DATE    DATE,
  POL_MANUAL      VARCHAR2(100 BYTE),
  POL_SOURCE      VARCHAR2(100 BYTE),
  POL_SUBJECT     VARCHAR2(100 BYTE),
  POL_APPLIES_TO  VARCHAR2(100 BYTE),
  POL_SIGNATURE   VARCHAR2(100 BYTE),
  CREATE_DATE     DATE                          DEFAULT SYSDATE,
  CREATE_BY       VARCHAR2(50 BYTE)             DEFAULT USER
)

I have a Details table below that Id like to have the ability to delete a row from the table above and it Delete the row from this details table. I have a column POL_ID that I want as the Foreign Key
Here is what I have thus far...

CREATE TABLE POLICY_DATA_DETAILS
(
  POL_DETAILS_ID    NUMBER,
  POL_DATA_ID       NUMBER,
  POL_DETAILS_DESC  VARCHAR2(50 BYTE)
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
MONITORING;


Any help would be appreciated
Thanks
0
Comment
Question by:jknj72
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 35

Expert Comment

by:johnsone
ID: 39729460
A little confused on what you posted.  I don't see POL_ID in the detail table.  Assuming you are going to add it, then this should do what you want:

alter table policy_data_details
add foreign key (pol_id)
references policy_data(pol_id)
on delete cascade;

Open in new window

0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 39729464
you'll need to have a foreign key constraint, something like this in your create statement for your detail table:

CREATE TABLE POLICY_DATA_DETAILS
(
  POL_DETAILS_ID    NUMBER,
  POL_DATA_ID       NUMBER,
  POL_DETAILS_DESC  VARCHAR2(50 BYTE),
  POL_ID NUMBER,
 CONSTRAINT POL_DETAILS_FK FOREIGN KEY (POL_ID) REFERENCES ANET.POLICY_DATA(POL_ID) ON DELETE CASCADE

)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
MONITORING;
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 39729469
use johnsone's solution if you've already created the table and if you're unable to re-create it. If not, you may use "my" statement ;-)
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

Author Comment

by:jknj72
ID: 39729491
johnson; POL_DATA_ID  is what I named the column in the Details table that I wanted to use as the foreign key. If I change that in your post than it should work correct?
0
 
LVL 35

Accepted Solution

by:
johnsone earned 2000 total points
ID: 39729502
Correct.  The statement would be:

alter table policy_data_details
add foreign key (pol_data_id)
references policy_data(pol_id)
on delete cascade;

Open in new window

0
 

Author Closing Comment

by:jknj72
ID: 39729558
Thanks johnson I appreciate your help.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

752 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