Solved

Oracle table config Foreign Key

Posted on 2013-12-19
6
449 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
  • 2
  • 2
  • 2
6 Comments
 
LVL 34

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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 34

Accepted Solution

by:
johnsone earned 500 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

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.

Join & Write a Comment

This article describes some very basic things about SQL Server filegroups.
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

743 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