Integrity Constraint violated error when trying to insert into tables - sql

I need a new set of eyes to help me understand what I'm doing wrong here!
I created these tables:
CREATE TABLE bankkund(
PNR VARCHAR2(11) PRIMARY KEY,
FNAMN VARCHAR2(25) NOT NULL,
ENAMN VARCHAR2(25) NOT NULL,
PASSWD VARCHAR2(16) NOT NULL,
UNIQUE(PASSWD));
CREATE TABLE konto(
KNR NUMBER(8)PRIMARY KEY,
KTNR NUMBER(6)NOT NULL,
REGDATUM DATE NOT NULL,
SALDO NUMBER(10,2),
FOREIGN KEY(ktnr) REFERENCES kontotyp(ktnr));
CREATE TABLE kontoägare(
RADNR NUMBER(9)PRIMARY KEY,
PNR VARCHAR2(11)NOT NULL,
KNR NUMBER(8)NOT NULL,
FOREIGN KEY(pnr) REFERENCES bankkund(pnr),
FOREIGN KEY(knr) REFERENCES konto(knr));
then this sequence:
create sequence radnr_seq
start with 1
increment by 1;
and then whenever I want to insert the following values I get an integrity constraint error:
INSERT INTO kontoägare(radnr,pnr,knr)
VALUES(radnr_seq.NEXTVAL,'540126-1111',123);
INSERT INTO kontoägare(radnr,pnr,knr)
VALUES(radnr_seq.NEXTVAL,'691124-4478',123);
INSERT INTO kontoägare(radnr,pnr,knr)
VALUES(radnr_seq.NEXTVAL,'540126-1111',5899);
INSERT INTO kontoägare(radnr,pnr,knr)
VALUES(radnr_seq.NEXTVAL,'691124-4478',8896);
COMMIT;
What am I missing??

The error you're seeing is expected. Table "kontoägare" has 2 foreign keys: FOREIGN KEY(pnr) REFERENCES bankkund(pnr)and FOREIGN KEY(knr) REFERENCES konto(knr)). This means that the values for "pnr" need to exist in table "bankkund" and the value for "knr" needs to exist in "konto". If either of those values do not exist when you do the insert, an integrity constraint error will be raised.

Related

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

I have encountered some problem with SQL foreign key.
Here are my table and insert SQL.
create table passenger_card2
(
phone char(20) primary key,
name char(20)
);
create table card
(
card_num char(20) primary key,
balance number(10,2),
cvn char(20)
);
create table passenger_card1
(
sin integer primary key,
user_id char(20) not null unique,
phone char(20),
card_num char(20) unique,
foreign key(phone) references passenger_card2,
foreign key (card_num) references card
);
And here are my INSERT statements:
INSERT INTO PASSENGER_CARD2 VALUES ( '111222333' , 'Ace');
INSERT INTO CARD VALUES ( '1000' , '100.1' , '110');
INSERT INTO PASSENGER_CARD1 VALUES ('100', 'aaaa', '111222333', '1000');
However, I get an error when I tried to insert PASSENGER_CARD1 data:
SQL ORA-02291: integrity constraint violated - parent key not found
I am not sure why my foreign key is wrong?
I am not sure if this is going to be right but you should make the table 2 first before you create the first table. The database is confused because it wouldnt make sense telling them that theres gonna be a foreign key in the second table but the table isnt created. Run the code for the second table first and then run the code for the first table.

ERROR: violates foreign key constraint, key is not present in parent table (but it is??)

I know this question has been asked many times, but none of the answers have solved my issue.
I am creating a database for a uni assignment, using PostgreSQL through pgadmin 4, and I have a table named "staff" populated with staff members with a primary key of "staffid". I then have another table named "client_international", which includes a foreign key of "staffid" which relates to the staff tables primary key.
When trying to insert into the client table, I am getting the following error:
ERROR: insert or update on table "client_international" violates foreign key constraint "intclient_staff_fkey"
DETAIL: Key (staffid)=(100000024) is not present in table "staff".
SQL state: 23503
I am certain that that '100000024' key is in the staff table.. yet I still get the error. Any suggestions? Below I will paste the code I used to create the staff and client tables, in case anyone notices an error in them.
Staff table:
CREATE SEQUENCE staff_seq
start 100000000
increment 1;
CREATE TABLE staff
(
staffid integer default nextval('staff_seq'),
firstname varchar(20) NOT NULL,
lastname varchar(20) NOT NULL,
"position" varchar(20) NOT NULL,
mobile varchar(20) NOT NULL,
email varchar(100) NOT NULL,
"location" integer NOT NULL,
CONSTRAINT staff_pkey PRIMARY KEY (staffid)
);
Client table:
CREATE SEQUENCE client_seq
start 200000000
increment 1;
CREATE TABLE client
(
clientid integer default nextval('client_seq'),
company varchar(100) NOT NULL,
sector varchar(100) NOT NULL,
pointofcontact varchar(20) NOT NULL,
mobile varchar(20) NOT NULL,
email varchar(100) NOT NULL,
approvalstatus boolean default (false),
"location" integer NOT NULL,
staffid integer NOT NULL,
CONSTRAINT client_pkey PRIMARY KEY (clientid)
);
CREATE TABLE client_international
(
CONSTRAINT client_international_pkey PRIMARY KEY (clientid)
) INHERITS ("client");
ALTER TABLE client
ADD CONSTRAINT client_location_fkey FOREIGN KEY ("location") REFERENCES "location" (locationid),
ADD CONSTRAINT client_staff_fkey FOREIGN KEY (staffid) REFERENCES staff (staffid);
ALTER TABLE client_international
ADD CONSTRAINT intclient_location_fkey FOREIGN KEY ("location") REFERENCES "location" (locationid),
ADD CONSTRAINT intclient_staff_fkey FOREIGN KEY (staffid) REFERENCES staff (staffid);
I get the error when running the following statements:
INSERT INTO client_international(company, sector, pointofcontact, mobile, email, approvalstatus, "location", staffid)
VALUES ('Moores Dogs', 'Border Patrol', 'Carol Moore', '07911 653453', 'jenkinsj#k9solutions.co.uk', 'false', '500000001', '100000024');
Here's a screenshot of the entry in the staff table, showing that it's definitely in there:
Foreign keys aren't "inherited".
Quote from the manual
A serious limitation of the inheritance feature is that [...] foreign key constraints only apply to single tables, not to their inheritance children. This is true on both the referencing and referenced sides of a foreign key constraint.
(emphasis mine)
So what you are trying to do, simply isn't supported.

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

so I'm writing some code in Oracle and have established the following tables:
CREATE TABLE users
(
user_id NUMBER NOT NULL,
email_address VARCHAR2(50) NOT NULL UNIQUE,
first_name VARCHAR2(10) NOT NULL,
last_name VARCHAR2(10) NOT NULL,
CONSTRAINT users_pk PRIMARY KEY (user_id)
)
CREATE TABLE product
(
product_id NUMBER,
product_name VARCHAR2(50) NOT NULL,
CONSTRAINT product_pk PRIMARY KEY (product_id)
)
CREATE TABLE downloads
(
download_id NUMBER,
user_id NUMBER NOT NULL,
product_id NUMBER NOT NULL,
download_date DATE NOT NULL,
filename VARCHAR2(50) NOT NULL,
CONSTRAINT downloads_pk PRIMARY KEY (download_id),
CONSTRAINT downloads_fk
FOREIGN KEY(user_id) REFERENCES users (user_id),
CONSTRAINT downloads_fk2
FOREIGN KEY(product_id) REFERENCES product(product_id)
)
CREATE SEQUENCE user_id_seq
CREATE SEQUENCE download_id_seq
CREATE SEQUENCE product_id_seq
The downloads table connects the users and product table, containing the foreign keys user_id and product_id. I am trying to insert data into the downloads table via the following code:
INSERT INTO downloads (download_id,user_id,product_id,download_date,filename)
VALUES(download_id_seq.NEXTVAL,1,2, SYSDATE, 'one_horse_town.mp3')
INSERT INTO downloads (download_id,user_id,product_id,download_date,filename)
VALUES(download_id_seq.NXTVAL, 2, 1, SYSDATE, 'pedals_are_falling.mp3')
INSERT INTO downloads (download_id,user_id,product_id,download_date,filename)
VALUES(download_id_seq.NEXTVAL, 2, 2, SYSDATE, 'random_song.mp3')
Oracle then gives me the following error:
Error report - ORA-02291: integrity constraint (BC29369.DOWNLOADS_FK2) violated - parent key not found.
I am not sure why this is coming up, as it seems to me that I have clearly labeled the primary and foreign key relationships as I should. Does anyone know how to fix this? Thank you in advance!
The constraints
CONSTRAINT downloads_fk FOREIGN KEY(user_id) REFERENCES users (user_id)
and
CONSTRAINT downloads_fk2 FOREIGN KEY(product_id) REFERENCES product(product_id)
means that the user_id values 1 and 2, the product_id values 1 and 2 which you are trying to insert into downloads table must already exist in users table and product table.
If users and product tables are not populated with these values (1 and 2) before you insert those values into downloads table, you will get this error as the integrity constraints will be violated.
You are creating an orphan record with no parent record. This is precisely what you were ensuring not to happen when you created those two constraints.

(SQL) integrity constraint violated - parent key not found

I did look up for solutions for this problem but i still get the same error..
I'm trying to insert values into PART and MANUFACTURER tables. Initially, i inserted values into MANUFACTURER without knowing the fact i need to deal with the parent table i.e. PART. So, i did the PART then the MANUFACTURER but still not working :(.
These are the tables:
PART(PNum, PName, PUnitPrice, ComponentOf)
primary key (PNum)
foreign key (ComponentOf) references PART(PNum)
MANUFACTURER(MName, MAddress, MPhone)
primary key (MName)
candidate key (MPhone)
candidate key (MAddress)
PART-MANUFACTURED(MDate, PNum, MName, Quantity)
primary key (MName, PNum, MDate)
foreign key (PNum) references PART(PNum)
foreign key (MName) references MANUFACTURER(MName)
CUSTOMER(CNum, CName, CType)
primary key (CNum)
domain constraint ctype in ('INDIVIDUAL', 'INSTITUTION')
ORDERS(CNum, PNum, OrderDate, OrderQuantity)
primary key (CNum, PNum, OrderDate)
foreign key (CNum) references CUSTOMER(CNum)
foreign key (PNum) references PART(PNum)
Create statements:
CREATE TABLE PART(PNum VARCHAR(25) NOT NULL, PName VARCHAR(75) NOT NULL, PUnitPrice NUMBER(7,2) NOT NULL, ComponentOf VARCHAR(25), PRIMARY KEY(PNum), FOREIGN KEY(ComponentOf) REFERENCES PART(PNum));
Table created.
SQL> CREATE TABLE MANUFACTURER(MName VARCHAR(50) NOT NULL, MAddress VARCHAR(100) NOT NULL, MPhone VARCHAR(25) NOT NULL, PRIMARY KEY(MName), CONSTRAINT UK_MADDRESS Unique(MAddress), CONSTRAINT UK_MPHONE UNIQUE(MPhone));
Table created.
SQL> CREATE TABLE PARTMANUFACTURED(MDate DATE NOT NULL, PNum VARCHAR(25) NOT NULL, MName VARCHAR(50) NOT NULL, QUANTITY NUMBER(10) NOT NULL, PRIMARY KEY(MName, PNum, MDate), FOREIGN KEY(PNum) REFERENCES PART(PNum), FOREIGN KEY(MName) REFERENCES MANUFACTURER(MName));
Table created.
SQL> CREATE TABLE CUSTOMER(CNum VARCHAR(25) NOT NULL, CName VARCHAR(75) NOT NULL, CType VARCHAR(20) NOT NULL, PRIMARY KEY(CNum), CHECK(Ctype in('INDIVIDUAL','INSTITUTION')));
Table created.
SQL> CREATE TABLE ORDERS(CNum VARCHAR(25) NOT NULL, PNum VARCHAR(25) NOT NULL, OrderDate DATE NOT NULL, OrderQuantity NUMBER(7,2) NOT NULL, PRIMARY KEY(CNum, PNum, OrderDate), FOREIGN KEY(CNum) REFERENCES CUSTOMER(CNum), FOREIGN KEY(PNum) REFERENCES PART(PNum));
Isn't the PNum already the primary or parent key? and PART table is the parent table? since, other tables have the PNum as foreign key.. i really don't get it..
anyone knows and can help me with it, is greatly appreciated. thanks :)
The error with your insert statement INSERT INTO PART VALUES('S001', 'System-economy', 1100, 'Null') is that you are trying to insert a string 'NULL' rather than an actual NULL for the column ComponentOf in the PART table.
The problem with the string 'NULL' is that you have a FOREIGN KEY constraint on ComponentOf that references the column PNum, which means that all the values in the column ComponentOf must also be in PNum. However, there is no value 'NULL' in PNum so that's why it threw the error. An actual NULL works since it means that it is not referencing anything.
The value inserted for ComponentOf has to match an existing PNum in the PARTS table. Your key is their to ensure you don't have any "orphaned" components.
If you try to insert 'Null' (a string value as mentioned in the comments) then it can't find the "parent". However, null is allowed since it means that particular part is not a component of any other part, i.e. it doesn't have a "parent".

Removing all references from a tuple using Oracle/Access

I created a simple databse using Oracle that has several tables and a few constraints, i am using Access 2007 to interact with the database.
My problem is that I have a table that is called "Customer" which holds several fields, most notably a Primary Key called CUSTID.
I have another table called "Order" which uses the Primary Key in "Customer" as a Foreign Key.
Obviously if i try to delete a customer that is being used in "Order" i get an error as it's being used. This means i have to delete the particular order that references the Customer and then i can delete the Custome record.
I know that "cascade" should delete everything associated with it, my question is how can i do that from Access? I have limited knowledge of databases but enough to create one etc..
This is more of a validation thing, it's just a hassle to delete certain tuples before i can remove another.
This is the database created for Oracle:
--Used to create a "Clean" slate
DROP TABLE ITEM CASCADE CONSTRAINTS;
DROP TABLE CUSTOMER CASCADE CONSTRAINTS;
DROP TABLE CORDER CASCADE CONSTRAINTS;
DROP TABLE FORUM CASCADE CONSTRAINTS;
CREATE TABLE ITEM
(
ITEMID NUMBER(4) NOT NULL,
NAME CHAR(15) NOT NULL,
CATEGORY CHAR(15) NOT NULL,
PRICE NUMBER(8) NOT NULL,
CONSTRAINT ITEM_PK PRIMARY KEY (ITEMID)
);
INSERT INTO ITEM VALUES (1000,'CARROT SEEDS','PACKET SEEDS',2.99);
INSERT INTO ITEM VALUES (2250,'ROSES','FLOWERS',5.99);
INSERT INTO ITEM VALUES (3300,'TOMATOES','PACKET SEEDS',2.99);
INSERT INTO ITEM VALUES (4050,'POTATOES','PACKET SEEDS',1.99);
CREATE TABLE CUSTOMER
(
CUSTID NUMBER(4) NOT NULL,
FNAME CHAR(10) NOT NULL,
LNAME CHAR(10) NOT NULL,
ADDRESS CHAR(40) NOT NULL,
CITY CHAR(15) NOT NULL,
PCODE CHAR(7) NOT NULL,
CNUMBER NUMBER(11) NOT NULL,
CONSTRAINT CUSTOMER_PK PRIMARY KEY (CUSTID)
);
INSERT INTO CUSTOMER VALUES (1010,'JAMIE','KEELING','149 OLD MANSFIELD ROAD','DERBY','DE214SA',07500966490);
INSERT INTO CUSTOMER VALUES (2020,'HELEN','DARLINGTON','27 MOORPARK AVENUE','ROCHDALE','OL113JQ',07890189802);
INSERT INTO CUSTOMER VALUES (3030,'STEVEN','SEGAL','123 FAKE STREET','OHIO','SE095BG',01559345467);
INSERT INTO CUSTOMER VALUES (4040,'BRUCE','WAYNE','17 LAKEVIEW CRESCENT','CHICAGO','MN432BD',07500966490);
CREATE TABLE CORDER
(
ORDERID NUMBER(4) NOT NULL,
CUSTID NUMBER(4) NOT NULL,
SHIPADD CHAR(40) NOT NULL,
SHIPPCODE CHAR(7) NOT NULL,
SHIPDATE DATE,
ITEMID NUMBER(4) NOT NULL,
QUANTITY NUMBER(3) NOT NULL,
TOTAL NUMBER(8) NOT NULL,
CONSTRAINT ORDER_PK PRIMARY KEY (ORDERID),
CONSTRAINT FK_CUSTOMER FOREIGN KEY (CUSTID) REFERENCES CUSTOMER(CUSTID),
CONSTRAINT FK_ITEM FOREIGN KEY (ITEMID) REFERENCES ITEM(ITEMID)
);
INSERT INTO CORDER VALUES (1000,1010,'149 OLD MANSFIELD ROAD','DE214SA','12-JAN-07',1000,100,100.00);
INSERT INTO CORDER VALUES (2000,2020,'27 MOORPARK AVENUE','OL113JQ','04-NOV-10',2250,200,100.00);
INSERT INTO CORDER VALUES (3000,3030,'123 FAKE STREET','SE095BG','30-OCT-08',3300,150,100.00);
INSERT INTO CORDER VALUES (4000,4040,'17 LAKEVIEW CRESCENT','MN432BD','25-JUL-07',4050,125,100.00);
CREATE TABLE FORUM
(
FORUMID NUMBER(4) NOT NULL,
TITLE CHAR(30) NOT NULL,
THREADNAME CHAR(30) NOT NULL,
POSTER CHAR(20) NOT NULL,
POSTDATE DATE,
CONSTRAINT FORUM_PK PRIMARY KEY (FORUMID)
);
INSERT INTO FORUM VALUES (1001,'GENERAL CHAT','BEGINNER QUESTIONS','JAMIE KEELING', '25-NOV-09');
INSERT INTO FORUM VALUES (2002,'OFF TOPIC','FAVOURITE BAND','HELEN DARLINGTON', '12-JAN-09');
INSERT INTO FORUM VALUES (3003,'GENERAL CHAT','WHEN TO HARVEST?','BRUCE WAYNE', '02-NOV-08');
INSERT INTO FORUM VALUES (4004,'OFF TOPIC','WHERE DO YOU LIVE?','STEVEN SEGAL', '13-JAN-08');
Standard SQL way to create foreign key with ON DELETE CASCADE is following. I don't know if Access supports this, but Oracle does have support for ON DELETE CASCADE.
alter table CORDER
add foreign key FK_CUSTOMER
references CUSTOMER(custid)
on delete cascade
If you are re-creating tables, you can also define the foreign keys with on delete cascade in table creation statement. i.e. replace this
CONSTRAINT FK_CUSTOMER FOREIGN KEY (CUSTID) REFERENCES CUSTOMER(CUSTID)
with this
CONSTRAINT FK_CUSTOMER FOREIGN KEY (CUSTID) REFERENCES CUSTOMER(CUSTID) ON DELETE CASCADE