?
Solved

Primary Foreign key creation on Oracle tables

Posted on 2013-12-26
2
Medium Priority
?
763 Views
Last Modified: 2013-12-26
I have my main table POLICY_DATA and heres the script

CREATE TABLE POLICY_DATA_LINK
(
  POL_LINK_ID    NUMBER,
  POL_LINK_URL   VARCHAR2(100 BYTE),
  POL_LINK_PATH  VARCHAR2(255 BYTE),
  POL_DETAIL_ID  NUMBER
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
MONITORING;


CREATE UNIQUE INDEX POLICY_DATA_LINK_PK ON POLICY_DATA_LINK
(POL_LINK_ID)
LOGGING
NOPARALLEL;


ALTER TABLE POLICY_DATA_LINK ADD (
  CONSTRAINT POLICY_DATA_LINK_PK
  PRIMARY KEY
  (POL_LINK_ID)
  USING INDEX POLICY_DATA_LINK_PK
  ENABLE VALIDATE);

Then I created a details table POLICY_DATA_DETAILS and heres the script

CREATE TABLE POLICY_DATA_DETAILS
(
  POL_DETAIL_ID     NUMBER                      NOT NULL,
  POL_DATA_ID       NUMBER                      NOT NULL,
  POL_DETAIL_TITLE  VARCHAR2(100 BYTE),
  POL_DETAIL_DESC   CLOB
)
LOB (POL_DETAIL_DESC) STORE AS (
  TABLESPACE  ANET_DATA
  ENABLE      STORAGE IN ROW
  CHUNK       8192
  RETENTION
  NOCACHE
  LOGGING
      STORAGE    (
                  INITIAL          64K
                  MINEXTENTS       1
                  MAXEXTENTS       UNLIMITED
                  PCTINCREASE      0
                  FREELISTS        1
                  FREELIST GROUPS  1
                  BUFFER_POOL      DEFAULT
                 ))
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
MONITORING;


CREATE OR REPLACE TRIGGER POLICY_DETAIL_TRG
BEFORE INSERT
ON POLICY_DATA_DETAILS
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
DECLARE
  N NUMBER;
BEGIN
-- For Toad:  Highlight column TALENT_ID
  Select POLICY_SEQ.nextval into n from dual;
  :new.POL_DETAIL_ID := N;
END POLICY_DETAIL_TRG;
/


ALTER TABLE POLICY_DATA_DETAILS ADD (
  FOREIGN KEY (POL_DATA_ID)
  REFERENCES POLICY_DATA (POL_ID)
  ON DELETE CASCADE
  ENABLE VALIDATE);


I need to create another table that has link info from the POLICAY_DATA_DETAILS table so I was thinking about creating a POLICY_DATA_LINK table. Id like to create this table with a foreign key(POL_DETAIL_ID) to the details table but was having a problem. Here is what I have so far...

CREATE TABLE POLICY_DATA_LINK
(
  POL_LINK_ID    NUMBER,
  POL_LINK_URL   VARCHAR2(100 BYTE),
  POL_LINK_PATH  VARCHAR2(255 BYTE),
  POL_DETAIL_ID  NUMBER
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
MONITORING;


CREATE UNIQUE INDEX POLICY_DATA_LINK_PK ON POLICY_DATA_LINK
(POL_LINK_ID)
LOGGING
NOPARALLEL;


ALTER TABLE POLICY_DATA_LINK ADD (
  CONSTRAINT POLICY_DATA_LINK_PK
  PRIMARY KEY
  (POL_LINK_ID)
  USING INDEX POLICY_DATA_LINK_PK
  ENABLE VALIDATE);

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 Comments
 

Accepted Solution

by:
jknj72 earned 0 total points
ID: 39740151
I was able to figure it out...

ALTER TABLE POLICY_DATA_LINK ADD (
  CONSTRAINT POLICY_DATA_LINK_PK
  PRIMARY KEY
  (POL_LINK_ID)
  USING INDEX POLICY_DATA_LINK_PK
  ENABLE VALIDATE);

ALTER TABLE POLICY_DATA_LINK ADD (
  FOREIGN KEY (POL_DETAIL_ID)
  REFERENCES POLICY_DATA_DETAILS (POL_DETAIL_ID)
  ON DELETE CASCADE
  ENABLE VALIDATE);
0
 

Author Closing Comment

by:jknj72
ID: 39740153
I figure it out
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
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
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
Suggested Courses

762 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