mutating table ORA-04091 oracle(trying to deny update on condition) - oracle

I'm trying to rewrite the trigger so it doesn't give a mutating table (ORA-04091)error. The table and trigger definition are as follows (commented part of the trigger giving the mutating table exception )
CREATE TABLE "COPYREAL"."PL_EDUCPLANS"
( "PLANID" NUMBER(10,0),
"STUDYFORM" NUMBER(2,0) NOT NULL ENABLE,
"SKILLCODE" NUMBER(2,0) NOT NULL ENABLE,
"YEARBEGIN" NUMBER(4,0) NOT NULL ENABLE,
"SPECCODE" VARCHAR2(10 CHAR) NOT NULL ENABLE,
"SEMESTERS" NUMBER(2,0) NOT NULL ENABLE,
"BIFURCATE_SEMESTER" NUMBER(2,0),
"CHAIRID" NUMBER(4,0),
"LESS10" NUMBER(1,0),
"CURATOR_CHAIR" NUMBER(4,0),
"SCHOOL_DISCS" NUMBER(1,0),
"MARKSYSTEMID" NUMBER(4,0) NOT NULL ENABLE,
"SKILL" VARCHAR2(50 CHAR),
"VKR_WEEKS" NUMBER(2,0),
"TOTALHOURS_GOS" NUMBER(5,0),
"NORM_LEARN_TIME" NUMBER(3,0),
"SKILL_ENG" VARCHAR2(50 CHAR),
"FIS_ITEM_UID" NUMBER(6,0),
"TOTALCOST" NUMBER(10,0),
"TOTALCOST_STR" VARCHAR2(800 CHAR),
"FORWP" NUMBER(1,0) DEFAULT 0 NOT NULL ENABLE,
"ISOLD" NUMBER(1,0),
"COMMENTARY" VARCHAR2(500 CHAR),
CONSTRAINT "PK_PL_EDUCPLANS" PRIMARY KEY ("PLANID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "SYSTEM" ENABLE,
CONSTRAINT "FK_PL_EDUCPLANS_CHAIRID" FOREIGN KEY ("CHAIRID")
REFERENCES "COPYREAL"."RB_DEPARTMENTS" ("CODE") ENABLE,
CONSTRAINT "FK_CURATORCHAIR_DEP" FOREIGN KEY ("CURATOR_CHAIR")
REFERENCES "COPYREAL"."RB_DEPARTMENTS" ("CODE") ENABLE,
CONSTRAINT "FK_PL_EDUCPLANS_RB_SKILLS" FOREIGN KEY ("SKILLCODE")
REFERENCES "COPYREAL"."RB_SKILLS" ("CODE") ENABLE,
CONSTRAINT "FK_PL_EDUCPLANS_RB_SPECS" FOREIGN KEY ("SPECCODE")
REFERENCES "COPYREAL"."RB_SPECIALITY" ("CODE") ENABLE,
CONSTRAINT "FK_PL_EDUCPLANS_RB_STUDYFORMS" FOREIGN KEY ("STUDYFORM")
REFERENCES "COPYREAL"."RB_STUDYFORMS" ("CODE") ENABLE,
CONSTRAINT "FK_PLEDUCPLANS_SCMARKSYSTEMS" FOREIGN KEY ("MARKSYSTEMID")
REFERENCES "COPYREAL"."SC_MARKSYSTEMS" ("MARKSYSTEMID") ENABLE
);
The trigger is:
create or replace TRIGGER "COPYREAL".tr_pl_educplans
before insert or update or delete
on pl_educPlans
for each row
DECLARE
l_flag NUMBER(1);
begin
IF :NEW.FORWP = 0 THEN
if inserting or updating then
delete from pl_processed_plans where planid=:new.planid;
DELETE FROM PL_PROCESSED_PLANS_B WHERE PLANID=:NEW.PLANID;
/* select case when exists(
SELECT 1
FROM PL_EDUCPLANS EP
where EP.FORWP=0 AND EP.YEARBEGIN=:NEW.YEARBEGIN AND EP.STUDYFORM=:NEW.STUDYFORM AND ep.SKILLCODE=:NEW.SKILLCODE and ep.SPECCODE=:NEW.SPECCODE
) then 1 else 0 end
into l_flag from Dual;
IF L_FLAG = 1 THEN
RAISE_APPLICATION_ERROR(-20001, 'Plan exists!');
end if;
*/
end if;
if deleting then
delete from pl_processed_plans where planid=:old.planid;
delete from pl_processed_plans_b where planid=:old.planid;
delete from pl_plan_activities_mt where planid=:old.planid;
delete from pl_plan_activities_b where planid=:old.planid;
END IF;
end if;
end;
insert runs as it should (giving the exception "plan exists" when trying to insert the duplicate value for a regular plan), that is once you run the update on the table (obvously you cannot select from tje same table that fired the trigger in the first place)
basically what I am trying to achieve here is to enforce the following business logic
you cannot have 2 regular plans(the plan is considered regular when it has forwp attribute value of 0)) that have the same studyform,skillcode,speccode and yearbegin values for each other and you can have as many as you want irregular plans that have the same studyform,skillcode,speccode and yearbegin values for each other.
an irregular plan (the plan is considered irrregular when it has forwp attribute value of 1)
before the introduction of the forwp attribute the previous business logic was enforced by a unique constraint(studyform,skillcode,speccode,yearbegin ) on the pl_educplans table and I'm not too sure how to enforce the rule now
I've read the suggestions given on https://oracle-base.com/articles/9i/mutating-table-exceptions but I'm not sure how to apply them to my case
is enforcing such business logic rule even possible with a trigger? or should such check be done on the application level?

Related

Trigger on same table in oracle

I have the table employee_leave :
/
I need to create a trigger so that when a manager ( is_manager='Y') is deleted from this table, the manager's subordinate/employee with the managers employee_id as his/her employee_manager_id, should have employee_manager_id set as null on deletion.
How can i write this and what kind of triggers would help the same table would be updated after deletion.
You do not need a trigger, or any code at, all for this. Create a FK from EMPLOYEE_MANAGER_ID to EMPLOYEE_MANAGER_ID with on delete set null option.
CREATE TABLE "EMPLOYEE_LEAVE"
( "EMPLOYEE_ID" NUMBER NOT NULL ENABLE,
"EMPLOYEE_NAME" VARCHAR2(100) NOT NULL ENABLE,
"EMPLOYEE_EMAIL" VARCHAR2(100) NOT NULL ENABLE,
"EMPLOYEE_MANAGER_ID" NUMBER,
"EMPLOYEE_USERNAME" VARCHAR2(400),
"EMPLOYEE_LEAVE_NORMAL" NUMBER NOT NULL ENABLE,
"EMPLOYEE_LEAVE_LOP" NUMBER,
"EMPLOYEE_IS_MANAGER" CHAR(1) NOT NULL ENABLE,
"EMPLOYEE_IS_ADMIN" CHAR(1) NOT NULL ENABLE,
"EMPLOYEE_CONTACT_NO" VARCHAR2(100),
"EMPLOYEE_PASSWORD" VARCHAR2(100),
CONSTRAINT "EMPLOYEE_PK" PRIMARY KEY ("EMPLOYEE_ID")
USING INDEX ENABLE
, constraint empmgr_2_emp foreign key (employee_manager_id)
references employee_leave(employee_id) on delete set null
);

Where to put sql file in spring boot app for it to generate H2 database

It is not clear where to put the SQL file in order for my H2 database to be initialized.
in my application-h2.properties file I have:
# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2
# Datasource
#spring.datasource.url=jdbc:h2:file:~/test
spring.datasource.url=jdbc:h2:mem:testdb;Mode=Oracle
spring.datasource.platform=h2
spring.datasource.username=user
spring.datasource.password=user
spring.jpa.hibernate.ddl-auto=none
spring.datasource.continue-on-error=true
spring.datasource.initialization-mode=always
spring.datasource.driver-class-name=org.h2.Driver
spring.profiles.active=h2
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
My SQL file is pure oracle SQL generated from sqlDeveloper. I tried to cut and paste it in the H2 console but it didn't accept it. I am hoping this way will work.
------------------------update 1---------------------------
schema.sql
schema.sql]: CREATE SEQUENCE "foo"."ADDRESSID_SEQ" MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE; nested exception is org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "CREATE SEQUENCE ""foo"".""ADDRESSID_SEQ"" MINVALUE[*] 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE"; SQL statement:
data.sql
data.sql]: CREATE SEQUENCE "foo"."ADDRESSID_SEQ" MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE; nested exception is org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "CREATE SEQUENCE ""foo"".""ADDRESSID_SEQ"" MINVALUE[*] 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE"; SQL statement:
CREATE SEQUENCE "foo"."ADDRESSID_SEQ" MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE [42000-148]
Error
Error creating bean with name 'entityManagerFactory': Post-processing of FactoryBean's singleton object failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #3
This is the same sql file, I just renamed it. It has creates and inserts. However if I name it schema.sql or data.sql it still fails on the third line. It doesn't appear to fail when creating a user or schema.
------------------Update 2----------------
CREATE USER foo ifentified by foo;
CREATE SCHEMA foo;
CREATE TABLE foo.ADDRESS
(ADDRESS_ID NUMBER(22,0),
CUSTOMER_ID NUMBER(*,0),
COMPANY_NAME VARCHAR2(100 BYTE),
ADDITIONAL_ADDRESS_INFO VARCHAR2(100 BYTE),
STREET VARCHAR2(100 BYTE),
ADDITIONAL_STREET_INFO VARCHAR2(100 BYTE),
HOUSE_NUMBER VARCHAR2(20 BYTE),
ZIP VARCHAR2(20 BYTE),
CITY VARCHAR2(50 BYTE),
STATE VARCHAR2(20 BYTE),
COUNTRY_CODE CHAR(2 BYTE),
PHONE VARCHAR2(20 BYTE),
CREATED_AT TIMESTAMP (6) DEFAULT CURRENT_TIMESTAMP,
MODIFIED_AT TIMESTAMP (6) DEFAULT CURRENT_TIMESTAMP,
VALIDATED_AT TIMESTAMP (6),
VALIDATION_RESULT VARCHAR2(100 CHAR)
) SEGMENT CREATION DEFERRED
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
NOCOMPRESS LOGGING
COMMENT ON COLUMN foo.ADDRESS.ADDRESS_ID IS 'primary key for table address';
COMMENT ON COLUMN foo.ADDRESS.CUSTOMER_ID IS 'foreign key for table customer';
COMMENT ON COLUMN foo.ADDRESS.CREATED_AT IS 'initially created at';
COMMENT ON COLUMN foo.ADDRESS.MODIFIED_AT IS 'date of last modification';
Error
Syntax error in SQL statement "CREATE TABLE FOO.ADDRESS (ADDRESS_ID NUMBER(22,0), CUSTOMER_ID NUMBER(*[*],0), COMPANY_NAME VARCHAR2(100 BYTE), ADDITIONAL_ADDRESS_INFO VARCHAR2(100 BYTE), STREET VARCHAR2(100 BYTE), ADDITIONAL_STREET_INFO VARCHAR2(100 BYTE), HOUSE_NUMBER VARCHAR2(20 BYTE), ZIP VARCHAR2(20 BYTE), CITY VARCHAR2(50 BYTE), STATE VARCHAR2(20 BYTE), COUNTRY_CODE CHAR(2 BYTE), PHONE VARCHAR2(20 BYTE), CREATED_AT TIMESTAMP (6) DEFAULT CURRENT_TIMESTAMP, MODIFIED_AT TIMESTAMP (6) DEFAULT CURRENT_TIMESTAMP, VALIDATED_AT TIMESTAMP (6), VALIDATION_RESULT VARCHAR2(100 CHAR) ) SEGMENT CREATION DEFERRED PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING COMMENT ON COLUMN FOO.ADDRESS.ADDRESS_ID IS 'primary key for table address' "; expected "long"; SQL statement:
You should name the file data.sql and save it in src/main/resources folder. In this location the file will be detected and executed automatically. This, in the case that you want to leave default schema generation (as defined by your #Entity annotated classes).
If you want to generate also the schema manually you can create the file schema.sql where you put all the details for creating the schema.
edited answer after update 2.
you cannot use "
CREATE SEQUENCE "foo"."ADDRESSID_SEQ" ...
so try instead (foo is the schema-name)
CREATE SEQUENCE foo.ADDRESSID_SEQ START WITH 1 INCREMENT BY 1 MINVALUE 1 NOMAXVALUE NOCYCLE CACHE 20;
do'nt known if you can use NOORDER
use BIGINT instead of NUMBER(...) see ...expected "long"... in error.

How can this merge query matching with same columns result in primary key violation?

I have a tricky issue at hand where a merge query as below :-
MERGE INTO table_destination D USING table_source S
ON (D.id = S.id AND D.name_s = S.name_s AND D.seqno = S.seqno AND D.type_s = S.type_s)
WHEN NOT MATCHED THEN INSERT (D.class_v, D.id, D.name_s, D.seqno, D.VALID, D.IFSC_CODE, D.CREATOR, D.APPROVER, D.type_s) VALUES (S.class_v, S.id, S.name_s, S.seqno, S.VALID, S.IFSC_CODE, S.CREATOR, S.APPROVER,S.type_s)
WHEN MATCHED THEN UPDATE SET D.VALID = S.VALID
gives a primary key violation as this - unique constraint (schema_1.TBL_BRANCH_PK1) violated
The DDL of the destination table is as follows:-
CREATE TABLE table_destination
( "id" VARCHAR2(3 BYTE),
"name_s" VARCHAR2(3 BYTE),
"seqno" VARCHAR2(3 BYTE),
"NAME" VARCHAR2(50 BYTE),
"type_s" VARCHAR2(3 BYTE) NOT NULL ENABLE,
"IFSC_CODE" VARCHAR2(11 BYTE),
"VALID" NUMBER(1,0),
"CREATOR" VARCHAR2(22 BYTE),
"APPROVER" VARCHAR2(22 BYTE),
CONSTRAINT "TBL_BRANCH_PK1" PRIMARY KEY ("id", "name_s", "seqno", "type_s")
);
Clearly the Pk that is being violated consists of the columns that are being checked for a match in the merge query.
Another thing to note is that this query can be run from two sessions at the same time, but this shall not be an issue as merge must obtain lock on the row before actually updating it. Please help. Thanks in advance.
You have indeed constraint on destination table but there is no information about constraint in source table. If in source table there are two records with same key both will try to insert and constraint fails.
create table t1 (id number);
create table t2 (id number, constraint tpk primary key (id));
insert into t1 values (1);
insert into t1 values (1);
commit;
merge into t2
using t1
on (t2.id = t1.id)
when not matched then insert values (t1.id);
SQL Error: ORA-00001: unique constraint (TPK) violated

Sample data - "Issue while executing stored procedure which consists both update and insert statements"

Below are the sample table and file details for the question which I have asked on "Issue while executing stored procedure which consists both update and insert statements". Below are the steps I am following before executing the Procedure.
I will get a file from the Vendor which contains the data in the below format.
6437,,01/01/2017,3483.92,,
14081,,01/01/2017,8444.23,,
I am loading these data to the table NMAC_PTMS_NOTEBK_SG. In the above file 1st column will be the asset.
I am updating the table with extra column with name lse_id with respect to that asset. Now the NMAC_PTMS_NOTEBK_SG table will have the data in the below format.
LSE_ID AST_ID PRPRTY_TAX_DDCTN_CD LIEN_DT ASES_PRT_1_AM ASES_PRT_2_AM
5868087 5049 Null 01-01-2017 3693.3 NULL
Now my procedure will start. In my procedure the logic should be in a way I need to take the lse_id from NMAC_PTMS_NOTEBK_SG and compare the same in MJL table (here lse_id = app_lse_s). Below is the structure for MJL table.
CREATE TABLE LPR_LP_TEST.MJL
(
APP_LSE_S CHAR(10 BYTE) NOT NULL,
DT_ENT_S TIMESTAMP(3) NOT NULL,
DT_FOL_S TIMESTAMP(3),
NOTE_TYPE_S CHAR(4 BYTE) NOT NULL,
PRCS_C CHAR(1 BYTE) NOT NULL,
PRIO_C CHAR(1 BYTE) NOT NULL,
FROM_S CHAR(3 BYTE) NOT NULL,
TO_S CHAR(3 BYTE) NOT NULL,
NOTE_TITLE_S VARCHAR2(41 BYTE) NOT NULL,
INFO_S VARCHAR2(4000 BYTE),
STAMP_L NUMBER(10) NOT NULL,
PRIVATE_C CHAR(1 BYTE),
LSE_ACC_C CHAR(1 BYTE),
COL_STAT_S CHAR(4 BYTE),
INFO1_S VARCHAR2(250 BYTE),
INFO2_S VARCHAR2(250 BYTE),
INFO3_S VARCHAR2(250 BYTE),
INFO4_S VARCHAR2(250 BYTE),
NTBK_RSN_S CHAR(4 BYTE)
)
TABLESPACE LPR_LP_TEST
PCTUSED 0
PCTFREE 25
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 64K
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
MONITORING;
CREATE UNIQUE INDEX LPR_LP_TEST.MJL_IDX0 ON LPR_LP_TEST.MJL
(APP_LSE_S, DT_ENT_S)
LOGGING
TABLESPACE LPR_LP_TEST
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
)
NOPARALLEL;
CREATE OR REPLACE TRIGGER LPR_LP_TEST."MT_MJL_AIUD"
AFTER INSERT OR UPDATE OR DELETE ON mjl
BEGIN
mpkg_trig_mjl.mp_mjl_aiud;
END mt_mjl_aiud;
/
CREATE OR REPLACE TRIGGER LPR_LP_TEST."MT_MJL_AIUDR"
AFTER INSERT OR UPDATE OR DELETE ON mjl FOR EACH ROW
BEGIN
mpkg_trig_mjl.mp_mjl_aiudr (INSERTING, UPDATING, DELETING,
:NEW.app_lse_s, :NEW.prcs_c, :NEW.note_type_s,
:OLD.app_lse_s, :OLD.prcs_c, :OLD.note_type_s);
END mt_mjl_aiudr;
/
CREATE OR REPLACE TRIGGER LPR_LP_TEST."MT_MJL_BIUD"
BEFORE INSERT OR UPDATE OR DELETE ON mjl
BEGIN
mpkg_trig_mjl.mp_mjl_biud;
END mt_mjl_biud;
/
CREATE OR REPLACE TRIGGER LPR_LP_TEST."MT_MJL_OBIUR"
BEFORE INSERT OR UPDATE ON mjl FOR EACH ROW
BEGIN
IF INSERTING THEN
:NEW.stamp_l := mpkg_util.mp_time_ticker;
ELSE
IF :OLD.stamp_l > 999999990 THEN
:NEW.stamp_l := 1;
ELSE
:NEW.stamp_l := :OLD.stamp_l + 1;
END IF;
END IF;
END mt_mjl_obiur;
/
Below is the procedure I am using which you have provided in previous post and it is almost working good for me.
CREATE OR REPLACE PROCEDURE LPR_LP_TEST.SP_PTMS_NOTES
(
p_app_lse_s IN mjl.app_lse_s%TYPE,
--p_dt_ent_s IN mjl.dt_ent_s%TYPE,
--p_note_type_s IN mjl.note_type_s%TYPE,
--p_prcs_c IN mjl.prcs_c%TYPE,
--p_prio_c IN mjl.prio_c%TYPE,
--p_note_title_s IN mjl.note_title_s%TYPE,
--p_info1_s IN mjl.info1_s%TYPE,
--p_info2_s IN mjl.info2_s%TYPE
)
AS
--v_rowcount_i number;
--v_lien_date mjl.info1_s%TYPE;
--v_lien_date NMAC_PTMS_NOTEBK_SG.LIEN_DT%TYPE;
--v_asst_amount mjl.info2_s%TYPE;
v_app_lse_s mjl.app_lse_s%TYPE;
BEGIN
v_app_lse_s := trim(p_app_lse_s);
-- I hope this dbms_output line is for temporary debug purposes only
-- and will be removed in the production version!
dbms_output.put_line(app_lse_s);
merge into mjl tgt
using (select lse_s app_lse_s,
sysdate dt_ent_s,
'SPPT' note_type_s,
'Y' prcs_c,
'1' prio_c,
'Property Tax Assessment' note_title_s,
lien_dt info1_s,
ases_prt_1_am info2_s
from nmac_ptms_notebk_sg
where lse_id = v_app_lse_s) src
on (trim(tgt.app_lse_s) = trim(src.app_lse_s))
-- and tgt.dt_ent_s = src.dt_ent_s)
when matched then
update set --tgt.dt_ent_s = src.dt_ent_s,
tgt.note_title_s = src.note_title_s,
tgt.info1_s = src.info1_s,
tgt.info2_s = src.info2_s
where --tgt.dt_ent_s != src.dt_ent_s
tgt.note_title_s != src.note_title_s
or tgt.info1_s != src.info1_s
or tgt.info2_s != src.info2_s
when not matched then
insert (tgt.app_lse_s,
tgt.dt_ent_s,
tgt.note_type_s,
tgt.prcs_c,
tgt.prio_c,
tgt.from_s,
tgt.to_s,
tgt.note_title_s,
tgt.info1_s,
tgt.info2_s)
values (src.app_lse_s,
src.dt_ent_s,
src.note_type_s,
src.prcs_c,
src.prio_c,
src.from_s,
src.to_s,
src.note_title_s,
src.info1_s,
src.info2_s);
commit;
end;
Now the logic should be I need to pass lse_id from the file which I
have already saved to the procedure.
If the lse_id which I am passing is matching with the app_lse_s in
the mjl table then I need to update that row and some of the harcoded
fields which I am doing it correclty.
If the lse_id is not matching then I have to insert a new row for that
lease and the hardcoded fields.
The issue which I am facing is the dt_ent_s in the mjl table is a
unique constraint.
Please let me know if the above is making any sense to you...
"The issue which I am facing is the dt_ent_s in the mjl table is a unique constraint."
Actually it's not, it's part of a compound unique key. So really your ON clause should match on
on (tgt.app_lse_s = src.app_lse_s
and tgt.dt_ent_s = src.dt_ent_s)
Incidentally, the use of trim() in the ON clause is worrying, especially trim(tgt.app_lse_s). If you're inserting values with trailing or leading spaces your "unique key" will produce multiple hits when you trim them. You should trim the spaces when you load the data from the file and insert trimmed values in your table.
"ORA-00001: unique constraint (LPR_LP_TEST.MJL_IDX0) violated"
MJL_IDX0 must me a unique index. That means you need to include its columns in any consideration of unique records.
Clearly there is a difference between your straight INSERT logic and your MERGE INSERT logic. You need to compare the two statements and figure out what the difference is.

Conflict creating table with Index and Primary Key

I'm a veteran SQL Server dev, recently moved to a project requiring Oracle and I'm confused by the error [ORA-02260: table can have only one primary key] I'm getting on Oracle 11.
I'm attempting to create a reference table, with an index and a primary key.
However, getting errors that my column Partner_ID is already declared. I know I'm missing something simple, but the docs and other sources I've viewed here have not given me a clue. Please help me understand what I'm doing wrong.
Thank you
ALTER TABLE REF_PARTNER
DROP PRIMARY KEY CASCADE;
DROP TABLE REF_PARTNER CASCADE CONSTRAINTS;
CREATE TABLE REF_PARTNER
(
PARTNER_ID NUMBER(10) PRIMARY KEY NOT NULL,
GLOBAL_APPID VARCHAR2(256 BYTE) NOT NULL,
FRIENDLY_NAME VARCHAR2(256 BYTE) NOT NULL,
CREATE_DTS DATE,
MODIFIED_DTS DATE,
LAST_MODIFIED_USER VARCHAR2(40 BYTE)
)
TABLESPACE DATA_1
PCTUSED 0
PCTFREE 5
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 1M
NEXT 1M
MAXSIZE UNLIMITED
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
)
LOGGING
NOCOMPRESS
NOCACHE
MONITORING;
BEGIN
EXECUTE IMMEDIATE 'DROP SEQUENCE PARTNER_SEQ';
EXCEPTION WHEN OTHERS THEN NULL;
END;
CREATE SEQUENCE PARTNER_SEQ START WITH 1 INCREMENT BY 1 MINVALUE 1 NOMAXVALUE NOCYCLE CACHE 200;
--CREATE UNIQUE INDEX REF_PARTNER_IDX ON REF_PARTNER
--(PARTNER_ID)
--LOGGING
--TABLESPACE INDEX_1
--PCTFREE 10
--INITRANS 2
--MAXTRANS 255
--STORAGE (
-- INITIAL 64K
-- NEXT 64K
-- MAXSIZE UNLIMITED
-- MINEXTENTS 1
-- MAXEXTENTS UNLIMITED
-- PCTINCREASE 0
-- BUFFER_POOL DEFAULT
-- );
--ALTER TABLE REF_PARTNER ADD (
-- CONSTRAINT REF_PARTNER_PK
-- PRIMARY KEY
-- (PARTNER_ID)
-- USING INDEX REF_PARTNER_PK
-- ENABLE VALIDATE);
A assume the error you get is
ORA-01408: such column list already indexed.
This is because you create the table with partner_id as the primary key. This automatically creates a unique index on partner_id.
There is no need to create a unique key on partner_id after you declared it to be the primary key.

Resources