Solved

Oracle table config Foreign Key

Posted on 2013-12-19
6
477 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
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.

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

856 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