Hi I am developing a database in Oracle SQL dev, that is trying to access foriegn keys from another table. I am currently working on the ItemOrdered table which I've created with the following CREATE statement
CREATE TABLE ItemOrdered(OrderID varchar2(9) REFERENCES Ords(OrderID),
BeltID varchar2(9) REFERENCES BeltID(BeltID),
Quantity varchar(4) NOT NULL,
PRIMARY KEY(OrderID, BeltID))
As you can See I have the following foriegn keys Ords and BeltID.
Now when I try to run the following statement
INSERT INTO ItemOrdered VALUES(401565981,234489212,'2')
It gives me the following error
violated - parent key not found
02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"
I have provided my Ords CREATE statement if its needed
CREATE TABLE Ords(OrderID varchar2(9) PRIMARY KEY,
CustomerID varchar(9) REFERENCES Customers(CustomerID),
Expected_Delivery_Date date DEFAULT sysdate NOT NULL,
Actual_Delivery_Date date DEFAULT sysdate NOT NULL,
Payment_Due_Date date DEFAULT sysdate NOT NULL,
Order_Date date DEFAULT sysdate NOT NULL, Price Varchar(10),
Order_Placed varchar2(1) CONSTRAINT OrderPlaced
CHECK(Order_Placed IN('Y','N')) NOT NULL,
Order_Confirmed varchar2(1)
CONSTRAINT Order_Confirmed CHECK(Order_Confirmed IN('Y','N')) NOT NULL,
Order_Completed varchar2(1) CONSTRAINT Order_Completed
CHECK(Order_Completed IN('Y','N')) NOT NULL)
And I have also provided my BeltID CREATE statement
CREATE TABLE BeltID(BeltID varchar2(9) PRIMARY KEY,
BeltLengthID varchar2(9) REFERENCES BeltLength(BeltLengthID),
ColourID varchar2(9) REFERENCES Colour(ColourID),
DesignID varchar2(9) REFERENCES Design(DesignID),ComponentID varchar2(9) REFERENCES Component(ComponentID))
I don't seem to quite understand why I am getting this error. Is there an clear explanation why?
Here is the http link of what I am trying to do.
link text
Due to the foreign key constraints you specified when you created table ItemOrdered, when you perform this insert:
INSERT INTO ItemOrdered VALUES(401565981,234489212,'2')
... the values 401565981 and 234489212 must correspond to key values in the Ords and BelitId tables respectively - i.e. these 2 queries should return rows:
select *
from Ords
where OrderId = 401565981;
select *
from BeltId
where BeltId = 234489212;
The error message suggests this is not the case.
I have no Oracle installation available so I can't test, but does it matter if you enclose the OrderID and BeltID in single quotes (which you should do anyway since the columns are declared as varchars)? I haven't tested it, but one idea would be that Oracle barfs on the fact that you are inserting a numeric value into a varchar column.
insert
into ItemOrdered VALUES('401565981', '234489212','2')
Another option would be that the order of the columns in the table does not correspond to the order you gave them in the insert statement. To rule this out, try rewrite the statement as:
insert
into ItemOrdered(OrderID,BeltID,Quantity) values('401565981', '234489212','2')
Related
Trying to insert some values into a table and always showing me some errors, i think it has something to do with the date or smth but not quite sure about that, if anyone knows what's wrong, please let me know...
The error that currently shows me is:
ORA-01861: literal does not match format string ORA-06512: at "SYS.DBMS_SQL", line 1721
INSERT INTO Cats VALUES ('JACEK','M','CAKE','CATCHING','BALD','2008-12-01',67,NULL,2);
Here's the table:
CREATE TABLE Cats (
name VARCHAR2(15) CONSTRAINT cat_name_nn NOT NULL,
gender VARCHAR2(1) CONSTRAINT cat_gen_ch CHECK (gender IN('M', 'W')),
nickname VARCHAR2(15) CONSTRAINT cat_pk PRIMARY KEY,
function VARCHAR2(10),
chief VARCHAR2(15),
in_herd_since DATE DEFAULT SYSDATE CONSTRAINT cat_inherd_nn NOT NULL,
mice_ration NUMBER(3),
mice_extra NUMBER(3),
band_no NUMBER(2),
CONSTRAINT cat_banno_fk FOREIGN KEY (band_no) REFERENCES Bands(band_no),
CONSTRAINT cat_chief_fk FOREIGN KEY (chief) REFERENCES Cats(nickname),
CONSTRAINT cat_fun_fk FOREIGN KEY (function) REFERENCES Functions(function)
);
when entering character values for a column whose data type is Date you have to convert it into a DATE by using the TO_DATE function. In you case the column in_herd_since is of DATE type , you should try this
TO_DATE('2008-12-01', 'YYYY-MM-DD') -- untested
INSERT INTO Cats VALUES ('JACEK','M','CAKE','CATCHING','BALD',
TO_DATE('2008-12-01', 'YYYY-MM-DD'),67,NULL,2);
I currently have this assignment where we are supposed to create triggers to block the insert of a shipment depending on whether they exceed the total or not.
I have to sum all the shipped quantities and group them by order and product number.
Here are my tables to give an outlook:
My variables are in French, I'm sorry
CREATE TABLE LigneLivraison
(noLivraison NUMBER(19) NOT NULL,
noProduit NUMBER(19) NOT NULL,
noCommande NUMBER(19) NOT NULL,
quantiteLivree NUMBER(19) NOT NULL,
PRIMARY KEY (noLivraison),
FOREIGN KEY (noLivraison) REFERENCES Livraison,
FOREIGN KEY (noProduit) REFERENCES Produit,
FOREIGN KEY (noCommande) REFERENCES Commande
)
/
CREATE TABLE LigneCommande
(noCommande NUMBER(19) NOT NULL,
noProduit NUMBER(19) NOT NULL,
quantite NUMBER(19) NOT NULL,
CHECK (quantite > 0),
PRIMARY KEY (noCommande, noProduit),
FOREIGN KEY (noCommande) REFERENCES Commande,
FOREIGN KEY (noProduit) REFERENCES Produit
)
/
When I try to execute it, Oracle mentions that the SQL command does not end correctly
Here is what I have so far for the first trigger.
CREATE OR REPLACE TRIGGER bloquerInsertionCommande
BEFORE INSERT
ON LigneLivraison
REFERENCING
NEW AS NouvelleLivraison
FOR EACH ROW
BEGIN
SELECT LigneLivraison.noProduit, LigneLivraison.noCommande, LigneCommande.quantite, SUM(LigneLivraison.quantiteLivree) shipped, (LigneCommande.quantite - shipped) Total
FROM LigneLivraison, LigneCommande
WHERE LigneLivraison.noCommande = LigneCommande.noCommande
GROUP BY noCommande AND noProduit;
IF :NouvelleLivraison.quantiteLivree > Total THEN raise_application_error(-20100, 'La quantite a livrer est trop elevee');
END IF;
END;
/
Since shipping can be made seperately and an x number of times, I need to add all the same product of a certain order number to compare it with what the client originally ordered.
Your create table commands are not working as there are multiple issues as mentioned in the comments inline in following code:
CREATE TABLE LigneLivraison
(noLivraison NUMBER(19) NOT NULL,
noProduit NUMBER(19) NOT NULL,
noCommande NUMBER(19) NOT NULL,
quantiteLivree NUMBER(19) NOT NULL,
PRIMARY KEY (noLivraison),
FOREIGN KEY (noLivraison) REFERENCES Livraison(pk_column_name_of_livraison_table), -- you need to add column name of referencing table here
FOREIGN KEY (noProduit) REFERENCES Produit(pk_column_name_of_produit_table), -- you need to add column name of referencing table here
FOREIGN KEY (noCommande) REFERENCES Commande(pk_column_name_of_commande_table) -- you need to add column name of referencing table here
); -- ending statement with ;
-- / -- this is not needed in sql statements
CREATE TABLE LigneCommande
(noCommande NUMBER(19) NOT NULL,
noProduit NUMBER(19) NOT NULL,
quantite NUMBER(19) NOT NULL CHECK (quantite > 0), -- combined it in single column level constraint
PRIMARY KEY (noCommande, noProduit),
FOREIGN KEY (noCommande) REFERENCES Commande(pk_column_name_of_commande_table), -- you need to add column name of referencing table here
FOREIGN KEY (noProduit) REFERENCES Produit(pk_column_name_of_produit_table) -- you need to add column name of referencing table here
);
--/
Also, pk of referencing table is written in comment for clarity. You can use pk or unique key in references clause according to your requirement.
After your tables are created properly then only you will be able to identify proper error in the trigger.
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.
What I am trying to do ?
I am trying to create two tables and at the same time i am trying to link them together using foreign and primary keys. However I successfully create my parent table ( Student with primary key ) but failed to create child table ( Attendence with foreign key ).
What is the problem ?
I get the following error while creating Attendence table:
ERROR at line 5: ORA-01748: only simple column names allowed here
My code:
Student table:
create table Student (
ST_ROLLNO NUMBER(6) constraint s_pk primary key,
ST_NAME VARCHAR(30) not null,
ST_ADDRESS varchar(35) not null
);
Attendence table:
create table Attendence (
ST_ROLLNO NUMBER(6),
ST_DATE VARCHAR(30) not null,
ST_PRESENT_ABSENT varchar(1) not null,
constraint f_pk Attendence.ST_ROLLNO foreign key references Student(ST_ROLLNO)
);
Your foreign key constraint syntax is wrong; it should be:
constraint f_pk foreign key (ST_ROLLNO) references Student(ST_ROLLNO)
You are preceding the FK column name with the table name, which is wrong in itself, but also have it in the wrong place.
create table Student (
ST_ROLLNO NUMBER(6) constraint s_pk primary key,
ST_NAME VARCHAR(30) not null,
ST_ADDRESS varchar(35) not null
);
Table STUDENT created.
create table Attendence (
ST_ROLLNO NUMBER(6),
ST_DATE VARCHAR(30) not null,
ST_PRESENT_ABSENT varchar(1) not null,
constraint f_pk foreign key (ST_ROLLNO) references Student(ST_ROLLNO)
);
Table ATTENDENCE created.
According to oracle documentation,
ORA ERR
ORA-01748 only simple column names allowed here
The following is the cause of this error:
This SQL statement does not allow a qualified column name, such as
username.table.column or table.column.
Action you can take to resolve this issue: Remove the qualifications
from the column and retry the operation.
In your case, you are trying to refer to the table name while defining a constraint -
Attendence.ST_ROLLNO - WRONG.
It must contain a simple name without the table name or schema name.
I cannot find the solution on the web. I am wondering how I write the recursive relation in oracle. At the moment this is what I got:
create table medewerkers
(medewerker_ID varchar(15) primary key,
naam varchar(50) not null,
adres varchar(50) not null,
telefoon_nummer varchar(10) not null,
salaris number(4) not null,
functie varchar(50) not null,
manager varchar(15) constraint FK_Manager references medewerkers (medewerker_ID) on delete cascade,
werknemer_winkel_nummer number(15) constraint FK_W_winkel references winkel (winkel_nummer) on delete cascade,
constraint check_salaris check (salaris < 3000)
);
at the moment I created a manager column as FK for this recursive relation. I did not create an extra table because I am told that if they are 1-to-many with employee then I could place the FK within the table.
Now I am inserting a value like this one:
insert into MEDEWERKERS (medewerker_id, naam, adres, telefoon_nummer, salaris, functie, werknemer_winkel_nummer, manager)
values(11159112, 'Joost', 'Eindhoven Langloopstraat 1', 0678765478, 1500, 'baliemedewerker', 10, 'nee');
Oracle db gives an error back:
SQL Error: ORA-02291: integrity constraint (MAXIME.FK_MANAGER) violated - parent key not found
02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"
*Cause: A foreign key value has no matching primary key value.
*Action: Delete the foreign key or add a matching primary key.
How am I else supposed to get values into the manager column?
I hope my question is not too vague.
You need to make sure the manager is there before you add their underlings.
The CEO/Manager/Owner can be added with a NULL manager:
INSERT INTO MEDEWERKERS (medewerker_id, naam, adres, telefoon_nummer, salaris, functie, werknemer_winkel_nummer, manager)
VALUES ( 'nee', 'The Boss Man', 'Home Office', '0000000001', 9999, 'Owner', 10, NULL );
The employees can then be added with the correct foreign key references (as per the OP).
Also - if you are entering telephone numbers with a leading 0 then you probably want to wrap the data in quotes '' otherwise you may find that the conversion from number to varchar will lose it.
[As an aside: do you really want ON DELETE CASCADE on the foreign keys? If you delete a manager then all their employees will be deleted as well.]
Your question was perfectly formed.
Have you considered temporarily disabling the FK_Manager constraint so you can add the top-level of management? Then enable the constraint while you add the next level down of employees?
Just a thought.