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

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

Related

How do i resolve ORA-00904: "CI"."CATALOG_ITEM_ID": invalid identifier Error at Line: 2 Column: 52

I need to create a query that will display title, publisher, ISBN, release date, number of pages, and whether it's carried in any library ("Yes" or "No") For each book in the catalog and sort results by title. I can't have any duplicates either.
The query I have is
select distinct ci.title, ci.publisher, ci.RELEASE_DATE, b.ISBN, b.pages from
catalog_item ci, book b
left join physical_item pi on pi.CATALOG_ITEM_ID = ci.CATALOG_ITEM_ID
left join branch b on b.branch_id = pi.branch_id
left join library l on b.library_id=l.library_id
order by ci.TITLE
;
I get the following error
ORA-00904: "CI"."CATALOG_ITEM_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 2 Column: 52
I don't see how it is invalid
My DDL is as follows:
CREATE TABLE CUSTOMER (Customer_ID NUMBER PRIMARY KEY, Customer_Firstname
VARCHAR2(30), Customer_Lastname VARCHAR2(30), Customer_Street VARCHAR2(30),
Customer_City VARCHAR2(30), Customer_State VARCHAR2(20), Customer_Zip
VARCHAR2(10));
CREATE TABLE BRANCH (Branch_ID NUMBER PRIMARY KEY, Branch_Name VARCHAR2(30),
Branch_Phone VARCHAR2 (30), Branch_Address VARCHAR2(30), LIBRARY_ID NUMBER);
CREATE TABLE PHYSICAL_ITEM (Physical_Item_ID NUMBER PRIMARY KEY, Branch_ID NUMBER,
Catalog_Item_ID NUMBER, Copy_Number NUMBER, Date_Purchased DATE);
CREATE TABLE CATALOG_ITEM (Catalog_Item_ID NUMBER PRIMARY KEY, Title VARCHAR2 (30),
Description VARCHAR2(30), Publisher VARCHAR2 (30), Release_Date DATE, Type
VARCHAR2(30));
CREATE TABLE DVD (Catalog_Item_ID NUMBER, Length VARCHAR2(30));
CREATE TABLE BOOK (Catalog_Item_ID NUMBER, ISBN VARCHAR2(13), Pages NUMBER);
CREATE TABLE TRANSACTION (Transaction_ID NUMBER PRIMARY KEY, Date_checkout DATE,
Date_Due DATE, Date_Returned DATE, Library_Card_ID NUMBER, Physical_Item_ID NUMBER);
CREATE TABLE LIBRARY (Library_ID NUMBER PRIMARY KEY, Library_Name VARCHAR2 (30),
Library_Phone VARCHAR2(30), Library_Address VARCHAR2(30));
CREATE TABLE LIBRARY_CARD (Library_Card_ID NUMBER PRIMARY KEY, Library_ID NUMBER,
Customer_ID NUMBER, Card_Number VARCHAR2(30), PIN VARCHAR2(8), Date_Expire DATE);
ALTER TABLE BRANCH ADD CONSTRAINT Library_ID_FK FOREIGN KEY (Library_ID) REFERENCES
LIBRARY (Library_ID);
ALTER TABLE PHYSICAL_ITEM ADD CONSTRAINT Branch_ID_FK FOREIGN KEY (Branch_ID)
REFERENCES BRANCH (Branch_ID);
ALTER TABLE PHYSICAL_ITEM ADD CONSTRAINT Catalog_Item_ID_FK FOREIGN KEY
(Catalog_Item_ID) REFERENCES CATALOG_ITEM (Catalog_Item_ID);
ALTER TABLE TRANSACTION ADD CONSTRAINT Library_Card_ID_FK FOREIGN KEY
(Library_Card_ID) REFERENCES LIBRARY_CARD (Library_Card_ID);
ALTER TABLE TRANSACTION ADD CONSTRAINT Physical_Item_ID_FK FOREIGN KEY
(Physical_Item_ID) REFERENCES PHYSICAL_ITEM (Physical_Item_ID);
ALTER TABLE LIBRARY_CARD ADD CONSTRAINT Library_ID_FK1 FOREIGN KEY (Library_ID)
REFERENCES LIBRARY (Library_ID);
ALTER TABLE LIBRARY_CARD ADD CONSTRAINT Customer_ID_FK FOREIGN KEY (Customer_ID)
REFERENCES CUSTOMER (Customer_ID);
ALTER TABLE DVD ADD CONSTRAINT Catalog_Item_ID_FK1 FOREIGN KEY (Catalog_Item_ID)
REFERENCES CATALOG_ITEM (Catalog_Item_ID);
ALTER TABLE BOOK add CONSTRAINT Catalog_Item_ID_FK2 FOREIGN KEY (Catalog_Item_ID)
REFERENCES CATALOG_ITEM (Catalog_Item_ID);

ORA-02291: integrity constraint violated - parent key not found

SO here is my Database:
CREATE TABLE courses (
ccode VARCHAR2(10) CONSTRAINT courses_PK PRIMARY KEY,
cname VARCHAR2(50),
coursefee NUMBER(6)
);
CREATE TABLE faculty (
fcode VARCHAR2(5) CONSTRAINT faculty_PK PRIMARY KEY,
name VARCHAR2(50)
);
CREATE TABLE batches (
bcode varchar2(5) CONSTRAINT batches_PK PRIMARY KEY,
ccode varchar2(5) CONSTRAINT batches_ccode_FK REFERENCES COURSES(ccode),
fcode varchar2(5) CONSTRAINT batches_fcode_FK REFERENCES FACULTY(fcode),
stdate date CONSTRAINT batches_stdate_nn not null,
enddate date,
timing number(1) CONSTRAINT batches_timing_chk check( timing in (1,2,3) ),
CONSTRAINT batches_date_chk check ( stdate <= enddate)
);
CREATE TABLE students (
rollno number(5) CONSTRAINT students_PK PRIMARY KEY,
bcode varchar2(5) CONSTRAINT students_bcode_FK REFERENCES batches(bcode),
name varchar2(30),
gender char(1) CONSTRAINT students_gender_chk check( upper(gender) in ('M','F')),
dj date,
phone varchar2(10),
email varchar2(30)
);
Insert Table:
INSERT INTO batches VALUES('A0001','A0016','A0031','08-05-13','08-06-17',1);
INSERT INTO batches VALUES('B0002','E0017','B0032','08-05-13','08-06-26',2);
INSERT INTO batches VALUES('C0003','C0018','C0033','08-06-27','08-06-28',2);
INSERT INTO batches VALUES('D0004','D0019','D0034','08-06-27','08-08-11',3);
INSERT INTO batches VALUES('E0005','E0020','E0035','08-07-01','08-08-09',2);
INSERT INTO batches VALUES('F0006','F0021','F0036','08-07-21','08-08-11',1);
INSERT INTO batches VALUES('G0007','V0022','G0037','08-08-31','08-09-21',3);
INSERT INTO batches VALUES('H0008','H0023','H0038','08-09-17','08-10-14',3);
INSERT INTO batches VALUES('I0009','I0024','I0039','08-09-23','08-10-20',2);
INSERT INTO batches VALUES('J0010','P0025','J0040','08-10-04','08-11-26',1);
INSERT INTO batches VALUES('K0011','F0026','K0041','08-10-10','08-11-17',2);
INSERT INTO batches VALUES('L0012','C0027','L0042','08-11-02','09-02-23',2);
INSERT INTO batches VALUES('M0013','M0028','M0043','08-12-09','09-01-02',3);
INSERT INTO batches VALUES('N0014','N0029','N0044','08-12-11','09-01-08',3);
INSERT INTO batches VALUES('O0015','O0030','O0045','08-12-15','09-01-12',2);
INSERT INTO students VALUES(001,'A0001','Andrew Johnson','M','08-01-10','677-0323','AndyJ#hotmail.com');
INSERT INTO students VALUES(002,'B0002','Dionne Peterson','F','08-01-10','324-9705','dionnepet#gmail.com');
INSERT INTO students VALUES(003,'C0003','Sydney Peters','F','08-01-10','624-9664','SydersP#yahoo.com');
INSERT INTO students VALUES(004,'D0004','Tsian Figaro','F','08-09-01','310-4957','tsianfigaro#hotmail.com');
INSERT INTO students VALUES(005,'E0005','Jewel Forbes','F','08-09-01','666-5948','Jewel#gmail.com');
INSERT INTO students VALUES(006,'F0006','Jason Fitzpatrick','M','08-01-02','657-4958','JasonFitz#hotmail.com');
INSERT INTO students VALUES(007,'G0007','Ali Maharaj','M','08-01-02','674-4437','AliMaharaj#gmail.com');
INSERT INTO students VALUES(008,'H0008','Mariah Sunderstone','F','08-01-02','707-4056','mariahS#yahoo.com');
INSERT INTO students VALUES(009,'I0009','Ariah Baron','F','08-01-02','673-9685','AriBar#yahoo.com');
INSERT INTO students VALUES(010,'J0010','Joe Zheng ','M','08-09-01','348-4567','Zhengdo#hotmail.com');
INSERT INTO students VALUES(011,'K0011','Donald Yen','M','08-09-01','328-4749','DonnieYen#gmail.com');
INSERT INTO students VALUES(012,'L0012','Jackson Chang','M','08-08-27','345-6847','JackieChang#yahoo.com');
INSERT INTO students VALUES(013,'M0013','Scott Smith','M','08-01-02','702-9903','Scottsmith#hotmail.com');
INSERT INTO students VALUES(014,'N0014','Michelle Shareef','F','08-02-27','321-0589','michelleShareef#gmail.com');
INSERT INTO students VALUES(015,'O0015','Jean Louis Pierre ','M','08-09-01','428-8378','JLPierre#gmail.com');
The error that keeps coming up for batches Table is :
ORA-02290: check constraint (EJAY.BATCHES_DATE_CHK) violated
ANd the error that comes up for students Table is:
ORA-02291: integrity constraint violated - parent key not found
What I'm confused about is that it only shows up for some rows in the tables. faculty and courses are fine, it's just these two.
If the answers is obvious, I apologize, I'm a beginner. Please help
Insert in Batches is failing because you have your dates mixed up; they're dd-mm-yy.
For example, INSERT INTO batches VALUES('D0004','D0019','D0034','08-06-27','08-08-11',3); fails the test stdate <= enddate.
The insert in Students is failing because you're referring to a value in Batches that does not exist (because the inserts failed?).

Employee/History - Part of composite key as foreign key

I've got 2 entities:
1) EMPLOYEES (Parent)
CREATE TABLE EMPLOYEES (
employee_id NUMBER (3) NOT NULL,
first_name VARCHAR (20) NOT NULL,
last_name VARCHAR (20) NOT NULL,
job_title VARCHAR (20) NOT NULL,
employee_type VARCHAR (1) NOT NULL,
salary NUMBER (5),
hourly_pay NUMBER (5,2),
bonus_pay NUMBER (5,2),
CONSTRAINT employee_pk PRIMARY KEY(employee_id));
2) EMPLOYEE_HISTORY (Child)
CREATE TABLE EMPLOYEE_HISTORY (
start_date DATE NOT NULL,
employee_id NUMBER (3) NOT NULL,
end_date DATE,
job_title VARCHAR (10) NOT NULL,
hourly_rate NUMBER (5,2) NOT NULL,
CONSTRAINT employee_history_pk PRIMARY KEY(start_date, employee_id));
I'm trying to create:
ALTER TABLE employee_history
ADD CONSTRAINT employee_history_fk
FOREIGN KEY (employee_id)
REFERENCES employee_history(employee_id);
When I do this, I get an error
ORA-02270: no matching unique or primary key for this column-list
My guess is that I cannot create the constraint on just employee_id because I have a composite key in my child table. I understand when an employee gets put into the database, the parent table is filled out and the "start date" should be filled out along with everything else. However, I do not understand how this would work if I had start_date in my parent table as well. I would be able to create my constraint, yes, but how will I be able to keep a record of changes in start_date if my start_date was inputted at the time of when the employee was entered into the database.I thought about using job_title as a primary key instead of start_date because it's present in both tables, but what happens when an employee gets promoted and demoted again? Won't a duplicate value constraint come up when the same employee_id and job_title is getting inserted?
Your references clause needs to reference the parent table. Not the child table
ALTER TABLE employee_history
ADD CONSTRAINT employee_history_fk
FOREIGN KEY (employee_id)
REFERENCES employee(employee_id); -- employee not employee_history
The SQL you posted is trying to create a self-referential foreign key where employee_history is both the parent and the child. That doesn't make sense in this case.

oracle change a column to unique

Table has been created in system this way
CREATE TABLE INSTANCES
(
DM INTEGER NOT NULL,
INSTANCEID VARCHAR2(512) NOT NULL,
INSTANCENAME VARCHAR2(64) NOT NULL UNIQUE,
HOSTNAME VARCHAR2(32) NOT NULL,
CONSTRAINT PK_INSTANCES PRIMARY KEY (INSTANCEID, HOSTNAME)
);
The new crete table statement is as below:
CREATE TABLE INSTANCES
(
DM INTEGER NOT NULL,
INSTANCEID VARCHAR2(512) NOT NULL UNIQUE,
INSTANCENAME VARCHAR2(64) NOT NULL UNIQUE,
HOSTNAME VARCHAR2(32) NOT NULL,
CONSTRAINT PK_INSTANCES PRIMARY KEY (INSTANCEID, HOSTNAME)
);
The differnce is INSTANCEID has UNIQUE in it. How do i Alter the table? I used the below statement and it did not work for me.
ALTER TABLE INSTANCES ADD CONSTRAINT ab UNIQUE ( INSTANCEID);
It gave an error:
ALTER TABLE INSTANCES ADD CONSTRAINT ab UNIQUE ( INSTANCEID)
Error report:
SQL Error: ORA-02261: such unique or primary key already exists in the table
02261. 00000 - "such unique or primary key already exists in the table"
*Cause: Self-evident.
*Action: Remove the extra key.
Please help me to Alter the table as required above. Thanks!
Here is the output of SELECT con.constraint_name, col.column_name, con.constraint_type
FROM user_cons_columns col
JOIN user_constraints con ON (col.constraint_name = con.constraint_name)
WHERE col.table_name = 'INSTANCES';
"CONSTRAINT_NAME","COLUMN_NAME","CONSTRAINT_TYPE"
"SYS_C0016531","DM","C"
"SYS_C0016532","INSTANCEID","C"
"SYS_C0016533","INSTANCENAME","C"
"SYS_C0016534","HOSTNAME","C"
"PK_INSTANCES","HOSTNAME","P"
"PK_INSTANCES","INSTANCEID","P"
"SYS_C0016536","INSTANCENAME","U"
You have already stated that INSTANCEID is supposed to be UNIQUE, so a constraint has been created.
CREATE TABLE INSTANCES
(
DM INTEGER NOT NULL,
INSTANCEID VARCHAR2(512) NOT NULL UNIQUE, -- UNIQUE constraint
INSTANCENAME VARCHAR2(64) NOT NULL UNIQUE,
HOSTNAME VARCHAR2(32) NOT NULL,
CONSTRAINT PK_INSTANCES PRIMARY KEY (INSTANCEID, HOSTNAME)
);
Edit: Ok, after reading your comment, try this:
SELECT con.constraint_name, col.column_name, con.constraint_type
FROM user_cons_columns col
JOIN user_constraints con ON (col.constraint_name = con.constraint_name)
WHERE col.table_name = 'INSTANCES'
AND con.constraint_type = 'U'
;
It will list UNIQUE constraints and associated columns for INSTANCE table. Please check if there is a unique constraint on the INSTANCEID column (and if that constraint has no other associated columns).
Example at SQLFiddle: http://sqlfiddle.com/#!4/43b43/6
Edit #2: creating named constraints, all options:
-- CREATE TABLE - "In Line" Constraints
CREATE TABLE ports (
ID NUMBER CONSTRAINT PORT_ID_PK PRIMARY KEY,
NAME VARCHAR2(20)
);
CREATE TABLE ports (
ID NUMBER,
NAME VARCHAR2(20) CONSTRAINT NAME_NN NOT NULL
);
CREATE TABLE ports (
ID NUMBER,
NAME VARCHAR2(20) CONSTRAINT NAME_UQ UNIQUE
);
CREATE TABLE ports (
ID NUMBER,
STATUS NUMBER CONSTRAINT PROPER_STATUS_CK
CHECK (STATUS IN (4, 5))
);
CREATE TABLE ships (
SHIP_ID NUMBER,
NAME VARCHAR2(20),
HOME_PORT_ID NUMBER CONSTRAINT SHIP_PORT_FK
REFERENCES PORTS (ID)
);
-- CREATE TABLE - "Out of Line" Constraints
CREATE TABLE ports (
ID NUMBER,
NAME VARCHAR2(20),
CONSTRAINT PORT_ID_PK PRIMARY KEY (ID)
);
-- NOT NULL constraints can not be created "Out of Line"!
CREATE TABLE ports (
ID NUMBER,
NAME VARCHAR2(20),
CONSTRAINT NAME_UQ UNIQUE (NAME)
);
CREATE TABLE ports (
ID NUMBER,
STATUS NUMBER,
CONSTRAINT PROPER_STATUS_CK
CHECK (STATUS IN (4, 5))
);
CREATE TABLE ships (
SHIP_ID NUMBER,
NAME VARCHAR2(20),
HOME_PORT_ID NUMBER,
CONSTRAINT SHIP_PORT_FK FOREIGN KEY
(HOME_PORT_ID) REFERENCES PORTS (ID)
);
-- ALTER TABLE - "In Line" Constraints
ALTER TABLE PORTS MODIFY ID
CONSTRAINT PORT_ID_PK PRIMARY KEY;
ALTER TABLE PORTS MODIFY NAME
CONSTRAINT NAME_NN NOT NULL;
ALTER TABLE PORTS MODIFY NAME
CONSTRAINT NAME_UQ UNIQUE;
ALTER TABLE SHIPS MODIFY HOME_PORT_ID
CONSTRAINT SHIP_PORT_FK REFERENCES PORTS (ID);
-- ALTER TABLE - "Out of Line" Constraints
ALTER TABLE PORTS ADD CONSTRAINT
PORT_ID_PK PRIMARY KEY (ID);
-- NOT NULL constraints can not be created "Out of Line"!
ALTER TABLE PORTS ADD CONSTRAINT
NAME_UQ UNIQUE (NAME);
ALTER TABLE PORTS ADD
CONSTRAINT PROPER_STATUS_CK
CHECK (STATUS IN (4, 5));
ALTER TABLE SHIPS ADD CONSTRAINT SHIP_PORT_FK
FOREIGN KEY (HOME_PORT_ID)
REFERENCES PORTS (ID);
NOT NULL constraints cannot be create of out line.

Insert query showing an error in oracle10g

I created a table in oracle10g using following query......
CREATE TABLE "EMPLOYEESTASKS"
( "EMPLOYEEID" NUMBER,
"TASKDATE" VARCHAR2(40),
"STATUS" NUMBER,
"CUSTOMERID" NUMBER,
"ADDRESS" VARCHAR2(400) NOT NULL ENABLE,
"TASKTIME" VARCHAR2(40) NOT NULL ENABLE,
"VISITDATE" VARCHAR2(40),
"VISITTIME" VARCHAR2(40),
CONSTRAINT "EMPLOYEESTASKS_PK" PRIMARY KEY ("EMPLOYEEID", "TASKDATE", "TASKTIME") ENABLE,
CONSTRAINT "EMPLOYEESTASKS_FK" FOREIGN KEY ("EMPLOYEEID")
REFERENCES "EMPLOYEES" ("ID") ON DELETE CASCADE ENABLE
)
Table was created successfully... but the problem is while iam trying to insert a row into the table it is showing the error
ORA-01722: invalid number
The query i used is ,
insert into employeestasks values(12305,'30-11-2011','09:00',0,45602,'Sarpavaram Junction ,kakinada',null,null)
What is that invalid number..??
It look like your columns in the table are ordered employeeid, taskdate, status, and you're trying to insert '09:00' into status, which is a number. This is no good. You need to use the same order of columns or specify which value is for which column.
Also, you really like capslock, huh?

Resources