Preparation
SQL>
create table A (id number, soid number);
SQL>
create table B (id number);
Action
SQL>
alter table A
add constraint SOID_B_FK
foreign key (soid)
references B (id);
Error
ORA-02270: no matching unique or primary key for this column-list
Reason
Parent table (B) does not have any primary key for matching with child table (A)
Solution
SQL>
alter table B
add constraint ID_PK
primary key (id);
Showing posts with label primary key. Show all posts
Showing posts with label primary key. Show all posts
Friday, 23 March 2018
Friday, 19 January 2018
How to define primary key on existed table?
e.g.
steps;
lock table -> add new column for primary key -> update data with counter -> define sequence -> define trigger -> lastly, define primary key -> recompile invalid objects (optional)
SQL>
LOCK TABLE HR.EMPLOYEES_NEW IN EXCLUSIVE MODE NOWAIT
/
ALTER TABLE HR.EMPLOYEES_NEW
ADD (ID NUMBER)
/
DECLARE
str_exec VARCHAR2 (250);
counter NUMBER := 0;
last_seq_id NUMBER;
BEGIN
FOR cur_node IN ( SELECT ROWID, id
FROM HR.EMPLOYEES_NEW
ORDER BY HIRE_DATE ASC)
LOOP
BEGIN
str_exec :=
'update HR.EMPLOYEES_NEW set id = '
|| counter
|| ' where rowid = '
|| ''''
|| cur_node.ROWID
|| '''';
--DBMS_OUTPUT.put_line (str_exec);
EXECUTE IMMEDIATE str_exec;
COMMIT;
counter := counter + 1;
END;
END LOOP;
SELECT ID
INTO last_seq_id
FROM ( SELECT ID
FROM HR.EMPLOYEES_NEW
WHERE ID IS NOT NULL
ORDER BY ID DESC)
WHERE ROWNUM < 2;
str_exec :=
'CREATE SEQUENCE HR.EMPLOYEES_NEW_ID_SEQ START WITH '
|| last_seq_id
|| ' MAXVALUE 999999999999999999999999999999 MINVALUE '
|| last_seq_id
|| ' NOCYCLE NOCACHE NOORDER';
--DBMS_OUTPUT.put_line (str_exec);
EXECUTE IMMEDIATE str_exec;
END;
/
CREATE OR REPLACE TRIGGER HR.TRG_EMPLOYEES_NEW_ID
BEFORE INSERT
ON HR.EMPLOYEES_NEW
FOR EACH ROW
BEGIN
IF :new.ID IS NULL
THEN
:new.ID := HR.EMPLOYEES_NEW_ID_SEQ.NEXTVAL;
END IF;
END TRG_EMPLOYEES_NEW_ID;
/
ALTER TABLE HR.EMPLOYEES_NEW ADD
CONSTRAINT EMPLOYEES_NEW_PK
PRIMARY KEY (ID)
ENABLE
VALIDATE
/
EXEC SYS.UTL_RECOMP.recomp_parallel(4)
/
steps;
lock table -> add new column for primary key -> update data with counter -> define sequence -> define trigger -> lastly, define primary key -> recompile invalid objects (optional)
SQL>
LOCK TABLE HR.EMPLOYEES_NEW IN EXCLUSIVE MODE NOWAIT
/
ALTER TABLE HR.EMPLOYEES_NEW
ADD (ID NUMBER)
/
DECLARE
str_exec VARCHAR2 (250);
counter NUMBER := 0;
last_seq_id NUMBER;
BEGIN
FOR cur_node IN ( SELECT ROWID, id
FROM HR.EMPLOYEES_NEW
ORDER BY HIRE_DATE ASC)
LOOP
BEGIN
str_exec :=
'update HR.EMPLOYEES_NEW set id = '
|| counter
|| ' where rowid = '
|| ''''
|| cur_node.ROWID
|| '''';
--DBMS_OUTPUT.put_line (str_exec);
EXECUTE IMMEDIATE str_exec;
COMMIT;
counter := counter + 1;
END;
END LOOP;
SELECT ID
INTO last_seq_id
FROM ( SELECT ID
FROM HR.EMPLOYEES_NEW
WHERE ID IS NOT NULL
ORDER BY ID DESC)
WHERE ROWNUM < 2;
str_exec :=
'CREATE SEQUENCE HR.EMPLOYEES_NEW_ID_SEQ START WITH '
|| last_seq_id
|| ' MAXVALUE 999999999999999999999999999999 MINVALUE '
|| last_seq_id
|| ' NOCYCLE NOCACHE NOORDER';
--DBMS_OUTPUT.put_line (str_exec);
EXECUTE IMMEDIATE str_exec;
END;
/
CREATE OR REPLACE TRIGGER HR.TRG_EMPLOYEES_NEW_ID
BEFORE INSERT
ON HR.EMPLOYEES_NEW
FOR EACH ROW
BEGIN
IF :new.ID IS NULL
THEN
:new.ID := HR.EMPLOYEES_NEW_ID_SEQ.NEXTVAL;
END IF;
END TRG_EMPLOYEES_NEW_ID;
/
ALTER TABLE HR.EMPLOYEES_NEW ADD
CONSTRAINT EMPLOYEES_NEW_PK
PRIMARY KEY (ID)
ENABLE
VALIDATE
/
EXEC SYS.UTL_RECOMP.recomp_parallel(4)
/
Labels:
lock,
oracle,
primary key,
snapshot
Wednesday, 25 October 2017
ORA-02266 solution
Action
A table has a PK (Primary Key)
B table has a FK (Foreign Key) and referenced to A table's PK
-> User tries to truncate table A
Error
ORA-02266: unique/primary keys in table referenced by enabled foreign keys
Solution I
Disable B table's FK
SQL> alter table HR.B disable constraint FK_B;
-- e.g. schema HR
Solution II
First truncate table B
SQL> truncate table B;
Then truncate table A
SQL> truncate table A;
A table has a PK (Primary Key)
B table has a FK (Foreign Key) and referenced to A table's PK
-> User tries to truncate table A
Error
ORA-02266: unique/primary keys in table referenced by enabled foreign keys
Solution I
Disable B table's FK
SQL> alter table HR.B disable constraint FK_B;
-- e.g. schema HR
Solution II
First truncate table B
SQL> truncate table B;
Then truncate table A
SQL> truncate table A;
Labels:
foreign key,
ORA-02266,
oracle,
primary key
Subscribe to:
Posts (Atom)