Procedure for Interchanging Swap, Values Primary Key, Oracle, ORA-00001

三世轮回 提交于 2020-04-16 05:44:12

问题


TABLE_PRD

CREATE TABLE TABLE_PRD 
(
  PRODUCT_CODE_PRD VARCHAR2(10) NOT NULL 
, DESCRIPTION_PRODUCT_PRD VARCHAR2(20) 
, CONSTRAINT TABLE_PRD_PK PRIMARY KEY 
  (
    PRODUCT_CODE_PRD 
  )
  ENABLE 
);
INSERT INTO TABLE_PRD (PRODUCT_CODE_PRD, DESCRIPTION_PRODUCT_PRD) VALUES ('LS', 'Leasing');
INSERT INTO TABLE_PRD (PRODUCT_CODE_PRD, DESCRIPTION_PRODUCT_PRD) VALUES ('TG', 'Total Cost');

TABLE_POS

CREATE TABLE TABLE_POS 
(
  POSITION_CODE_POS INTEGER NOT NULL 
, SOME_TEXT_POS VARCHAR2(20) 
, CONSTRAINT TABLE_POS_PK PRIMARY KEY 
  (
    POSITION_CODE_POS 
  )
  ENABLE 
);
INSERT INTO TABLE_POS (POSITION_CODE_POS, COMMENT_POS) VALUES ('1', 'Owner');
INSERT INTO TABLE_POS (POSITION_CODE_POS, COMMENT_POS) VALUES ('2', 'Manager');
INSERT INTO TABLE_POS (POSITION_CODE_POS, COMMENT_POS) VALUES ('3', 'User');

TABLE_REL

CREATE TABLE TABLE_REL 
(
  PRODUCT_CODE_REL VARCHAR2(10) NOT NULL 
, POSITION_CODE_REL INTEGER NOT NULL 
, DESCRIPTION_REL VARCHAR2(20) 
, CREATION_DATE_REL DATE 
, CONTENT BLOB 
, CONSTRAINT TABLE_REL_FK1 FOREIGN KEY (PRODUCT_CODE_REL) REFERENCES TABLE_PRD (PRODUCT_CODE_PRD) ENABLE
, CONSTRAINT TABLE_REL_FK2 FOREIGN KEY (POSITION_CODE_REL) REFERENCES TABLE_POS (POSITION_CODE_POS) ENABLE
, CONSTRAINT TABLE_REL_PK PRIMARY KEY (PRODUCT_CODE_REL, POSITION_CODE_REL) ENABLE 
);
INSERT INTO TABLE_REL (PRODUCT_CODE_REL, POSITION_CODE_REL, DESCRIPTION_REL) VALUES ('LS', '1', 'Leasing - Owner');
INSERT INTO TABLE_REL (PRODUCT_CODE_REL, POSITION_CODE_REL, DESCRIPTION_REL) VALUES ('LS', '2', 'Leasing - Manager');
INSERT INTO TABLE_REL (PRODUCT_CODE_REL, POSITION_CODE_REL, DESCRIPTION_REL) VALUES ('LS', '3', 'Leasing - User');
INSERT INTO TABLE_REL (PRODUCT_CODE_REL, POSITION_CODE_REL, DESCRIPTION_REL) VALUES ('TG', '1', 'Total Cost - Owner');
INSERT INTO TABLE_REL (PRODUCT_CODE_REL, POSITION_CODE_REL, DESCRIPTION_REL) VALUES ('TG', '2', 'Total Cost - Manager');
INSERT INTO TABLE_REL (PRODUCT_CODE_REL, POSITION_CODE_REL, DESCRIPTION_REL) VALUES ('TG', '3', 'Total Cost - User');

Now, I need to perform a Circular/Round SWAP, changing the POSITION_CODE_REL

1 -> 3 (before of 1, is 3), 2 -> 1 (before of 2 is 1), 3 -> 2 (before of 3 is 2).

6 errors saving changes to table TABLE_REL:
Row 1: ORA-00001: unique constraint (TABLE_REL_PK) violated
Row 2: ORA-00001: unique constraint (TABLE_REL_PK) violated
Row 3: ORA-00001: unique constraint (TABLE_REL_PK) violated
Row 4: ORA-00001: unique constraint (TABLE_REL_PK) violated
Row 5: ORA-00001: unique constraint (TABLE_REL_PK) violated
Row 6: ORA-00001: unique constraint (TABLE_REL_PK) violated

I know what is the situation, The question is, Is There Some Procedure to perform the SWAP?


回答1:


Based on these Questions SQL - How to select a row having a column with max value

Oracle SQL: Update a table with data from another table

and Answers

https://stackoverflow.com/a/42007108/811293

Check This Code:

UPDATE TABLE_REL SET POSITION_CODE_REL =
    CASE 
    WHEN POSITION_CODE_REL = 1 THEN /*MAX VALUE*/ (
        SELECT POSITION_CODE_REL FROM (SELECT * FROM TABLE_REL ORDER BY POSITION_CODE_REL DESC) WHERE rownum = 1
        )
    ELSE POSITION_CODE_REL - 1 END;

Reverse

UPDATE TABLE_REL SET POSITION_CODE_REL =
    CASE 
    WHEN POSITION_CODE_REL = /*MAX VALUE*/ (
        SELECT POSITION_CODE_REL FROM (SELECT * FROM TABLE_REL ORDER BY POSITION_CODE_REL DESC) WHERE rownum = 1
        ) THEN 1 
    ELSE POSITION_CODE_REL + 1 END;


来源:https://stackoverflow.com/questions/60288050/procedure-for-interchanging-swap-values-primary-key-oracle-ora-00001

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!