交换交换过程,值主键,Oracle,ORA-00001
Procedure for Interchanging Swap, Values Primary Key, Oracle, ORA-00001
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');
现在,我需要执行 Circular/Round SWAP,更改 POSITION_CODE_REL
1 -> 3
(1之前是3),2 -> 1
(2之前是1),3 -> 2
(3之前是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
我知道是什么情况,问题是,是否有一些程序可以执行 SWAP?
基于这些问题
SQL - How to select a row having a column with max value
Oracle SQL: Update a table with data from another table
和答案
检查此代码:
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;
反转
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;
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');
现在,我需要执行 Circular/Round SWAP,更改 POSITION_CODE_REL
1 -> 3
(1之前是3),2 -> 1
(2之前是1),3 -> 2
(3之前是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
我知道是什么情况,问题是,是否有一些程序可以执行 SWAP?
基于这些问题 SQL - How to select a row having a column with max value
Oracle SQL: Update a table with data from another table
和答案
检查此代码:
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;
反转
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;