Live sql oracle integrity constraint error, primary key not found - sql

fairly new to sql not sure why this error is coming up, i'm asked to use alter to add columns and foreign key to the animal table referencing the species table species name. i can't figure out a way around this, any help is appreciated. thanks :)
here's my drop tables and create tables query:
DROP TABLE AnimalSale;
DROP TABLE Animal;
DROP TABLE Customer;
DROP TABLE Species;
CREATE TABLE Animal(
animalID NUMBER(4),
animalName VARCHAR2(50),
PRIMARY KEY (animalID)
);
CREATE TABLE Customer(
custID NUMBER(4),
custName VARCHAR2(50),
custEmail VARCHAR2(50),
PRIMARY KEY (custID)
);
CREATE TABLE AnimalSale(
animalID NUMBER(4),
custID NUMBER(4),
PRIMARY KEY (animalID, custID)
);
CREATE TABLE Species(
speciesCode NUMBER(6),
speciesName VARCHAR2(30) UNIQUE,
speciesPrice DECIMAL(5,2),
PRIMARY KEY (speciesCode)
);
My alter query, my lecturer told me the constraint part is wrong but i still couldn't figure out what's wrong and she's not giving me answer :(
ALTER TABLE Animal ADD speciesName VARCHAR2(30);
ALTER TABLE Animal ADD CONSTRAINT fk_species_name FOREIGN KEY (speciesName) REFERENCES Species(speciesName);
Insert table query:
INSERT INTO Animal (animalID, animalName, speciesName) VALUES( '1', 'Tiny', 'Dog');
INSERT INTO Animal (animalID, animalName, speciesName) VALUES( '2', 'Prince', 'Dog');
INSERT INTO Animal (animalID, animalName, speciesName) VALUES( '3', 'CJ', 'Cat');
INSERT INTO Animal (animalID, animalName, speciesName) VALUES( '4', 'Sid', 'Cat');
INSERT INTO Animal (animalID, animalName, speciesName) VALUES( '5', 'Sid', 'Snake');
INSERT INTO Animal (animalID, animalName, speciesName) VALUES( '6', 'Danger', 'Mouse');
INSERT INTO Animal (animalID, animalName, speciesName) VALUES( '7', 'Bonnie', 'Dog');
INSERT INTO Customer (custID, custName, custEmail) VALUES( '1', 'D.Smith', 'dsmith#yahoo.co.uk');
INSERT INTO Customer (custID, custName, custEmail) VALUES( '2', 'B.Bryne', 'bb#gmail.com');
INSERT INTO Customer (custID, custName, custEmail) VALUES( '3', 'X.Dobbs', 'xb#gmail.com');
INSERT INTO AnimalSale (animalID, custID) VALUES( '1', '1');
INSERT INTO AnimalSale (animalID, custID) VALUES( '2', '2');
INSERT INTO AnimalSale (animalID, custID) VALUES( '3', '2');
INSERT INTO AnimalSale (animalID, custID) VALUES( '4', '3');
INSERT INTO AnimalSale (animalID, custID) VALUES( '5', '2');
INSERT INTO Species (speciesCode, speciesName, speciesPrice) VALUES ('1', 'Dog', '9.99');
INSERT INTO Species (speciesCode, speciesName, speciesPrice) VALUES ('2', 'Cat', '10.20');
INSERT INTO Species (speciesCode, speciesName, speciesPrice) VALUES ('3', 'Snake', '20.00');
INSERT INTO Species (speciesCode, speciesName, speciesPrice) VALUES ('4', 'Mouse', '5.00');

i'm asked to use alter to add columns and foreign key to the animal table referencing the species table species name
The error I get when running your script is:
ORA-02291: integrity constraint (FIDDLE_LVXOXDENESSERWXORCJT.FK_SPECIES_NAME) violated - parent key not found
db<>fiddle
To solve it, I just swapped the order of the insert statements so that values were inserted in to the species table before you try to insert values into the animal table so that the foreign key reference can be satisfied.
db<>fiddle
Note: the INSERT statements were wrapped in an anonymous PL?SQL block just for simplicity and so they could all be executed in a single statement; you could equally run each individually.

Ideally, or might be your lecturer wants the animal table's foreign key to refer primary key of species table.
Do this:
ALTER TABLE Animal ADD species_FK NUMBER(6);
ALTER TABLE Animal ADD CONSTRAINT fk_species_id FOREIGN KEY (species_FK) REFERENCES Species(speciesCODE);
In insert, use the speciescode from species table into your animal table's newly added column.
Cheers!!

Related

PostgreSQL Databases

I have created a table 'DayPass':
CREATE TABLE DayPass (
memberNo INT PRIMARY KEY, FOREIGN KEY (memberNo) REFERENCES DayPass(memberNo),
startDate Date,
numberDays INT,
price VARCHAR(30),
check(numberDays > 0)
);
I am trying to insert these values:
INSERT INTO DayPass (memberNo, startDate, numberDays, price)
VALUES (3, '2022-01-01', '5', '£9.99');
INSERT INTO DayPass
VALUES (3, '2022-02-01', '5', '£9.99');
INSERT INTO DayPass
VALUES (3, '2022-03-01', '£5', '£9.99');
SELECT * FROM DayPass;
but Postgres gives me an error:
ERROR: duplicate key value violates unique constraint "daypass_pkey"
if am unsure where i am going wrong

Oracle SQL Unique Constraint Violated

I need to create tables and input values. All but two rows when inserted give me an error, Unique Constraint Violated. The last row in DEPT_LOCATIONS table and the second to the last row in DEPENDENT table. I'm not sure why these rows aren't being added because other rows in the same table are.
Here's my code so far:
/*Create Employee Table/*
create table EMPLOYEE(FNAME varchar(30), MINIT varchar(5), LNAME varchar(30), SSN varchar(10) PRIMARY KEY, BDATE varchar(30), ADDRESS varchar(30), SEX varchar(5), SALARY NUMERIC(10,0),
SUPERSSN varchar(30),DNO varchar(30));
/*Insert values into Employee Table/*
insert into EMPLOYEE values('John','B','Smith','123456789','1965-01-09','731 Fonden, Houston, TX','M', 30000,'333445555','5');
insert into EMPLOYEE values('Franklin','T','Wong','333445555','1955-12-08','638 Voss, Houston, TX','M', 40000,'888665555','5');
insert into EMPLOYEE values('Alice','J','Zelaya','999887777','1968-01-19','3321 Castle, Spring, TX','F', 25000,'987654321','4');
insert into EMPLOYEE values('Jennifer','S','Walace','987654321','1941-06-20','291 Berry, Bellaire, TX','F', 43000,'888665555','4');
insert into EMPLOYEE values('Ramesh','K','Narayan','666884444','1962-09-15','975 Fire Oak, Humble, TX','M', 38000,'333445555','5');
insert into EMPLOYEE values('Joyce','A','English','453453453','1972-07-31','5631 Rice, Houston, TX','F', 25000,'333445555','5');
insert into EMPLOYEE values('Ahmed','V','Jabbar','987987987','1969-03-29','980 Dallas, Houston, TX','M', 25000,'987654321','4');
insert into EMPLOYEE values('James','E','Bong','888665555','1937-11-10','450 Stone, Houston, TX','M', 55000,'null','1');
/*Create Department Table*/
create table DEPARTMENT(DNAME varchar(30), DNUMBER varchar(30) PRIMARY KEY, MGRSSN varchar(30), MGRSTARTDATE varchar(30));
/*Insert values into Department Table*/
insert into DEPARTMENT values('Research', '5', '333445555', '1988-05-22');
insert into DEPARTMENT values('Administration', '4', '987654321', '1995-01-01');
insert into DEPARTMENT values('Headquarters', '1', '888665555', '1981-06-19');
/*Create Department Location Table*/
create table DEPT_LOCATIONS(DNUMBER varchar(30) REFERENCES DEPARTMENT(DNUMBER), DLOCATION varchar(30) PRIMARY KEY);
/*Insert values into Department Location Table*/
insert into DEPT_LOCATIONS values('1', 'Houston');
insert into DEPT_LOCATIONS values('4', 'Stafford');
insert into DEPT_LOCATIONS values('5', 'Bellarire');
insert into DEPT_LOCATIONS values('5', 'Sugarland');
insert into DEPT_LOCATIONS values('5', 'Houston');
/*Create Project Table*/
create table PROJECT(PNAME varchar(30), PNUMBER varchar(30) PRIMARY KEY, PLOCATION varchar(30), DNUM varchar(30) REFERENCES DEPARTMENT(DNUMBER));
/*Insert values into Project Table*/
insert into PROJECT values('ProductX', '1', 'Bellaire', '5');
insert into PROJECT values('ProductY', '2', 'Sugarland', '5');
insert into PROJECT values('ProductZ', '3', 'Houston', '5');
insert into PROJECT values('Computerization', '10', 'Stafford', '4');
insert into PROJECT values('Reorganization', '20', 'Houston', '1');
insert into PROJECT values('Newbenefits', '30', 'Stafford', '4');
/*Create Works On table*/
create table WORKS_ON(ESSN varchar(30) REFERENCES EMPLOYEE(SSN), PNO varchar(30) REFERENCES PROJECT(PNUMBER), HOURS numeric(5, 1));
/*Insert values into Works on Table*/
insert into WORKS_ON values('123456789', '1', 32.5);
insert into WORKS_ON values('123456789', '2', 7.5);
insert into WORKS_ON values('666884444', '3', 40.0);
insert into WORKS_ON values('453453453', '1', 20.0);
insert into WORKS_ON values('453453453', '2', 20.0);
insert into WORKS_ON values('333445555', '2', 10.0);
insert into WORKS_ON values('333445555', '3', 10.0);
insert into WORKS_ON values('333445555', '10', 10.0);
insert into WORKS_ON values('333445555', '20', 10.0);
insert into WORKS_ON values('999887777', '30', 30.0);
insert into WORKS_ON values('999887777', '10', 10.0);
insert into WORKS_ON values('987987987', '10', 35.0);
insert into WORKS_ON values('987987987', '30', 5.0);
insert into WORKS_ON values('987654321', '30', 20.0);
insert into WORKS_ON values('987654321', '20', 15.0);
insert into WORKS_ON values('888665555', '20', null);
/*Create Dependent table*/
create table DEPENDENT(ESSN varchar(30) REFERENCES EMPLOYEE(SSN), DEPENDENT_NAME varchar(30) PRIMARY KEY, SEX varchar(30), BDATE varchar(30), RELATIONSHIP varchar(30));
/*Insert values into Dependent Table*/
insert into DEPENDENT values('333445555', 'Alice', 'F', '1986-04-05', 'Daughter');
insert into DEPENDENT values('333445555', 'Theodore', 'M', '1983-10-25', 'Son');
insert into DEPENDENT values('333445555', 'Joy', 'F', '1958-05-03', 'Spouse');
insert into DEPENDENT values('987654321', 'Abner', 'M', '1942-02-28', 'Spouse');
insert into DEPENDENT values('123456789', 'Michael', 'M', '1988-01-04', 'Son');
insert into DEPENDENT values('123456789', 'Alice', 'F', '1988-12-30', 'Daughter');
insert into DEPENDENT values('123456789', 'Elizabeth', 'F', '1967-05-05', 'Spouse');
The tables should look like this:
enter image description here
enter image description here
Could someone please help me understand what is going on?
Thank you
DLOCATION varchar(30) PRIMARY KEY
insert into DEPT_LOCATIONS values('1', 'Houston');
insert into DEPT_LOCATIONS values('5', 'Houston');
The DLOCATION column is a primary key, but you are trying to insert duplicate values 'Houston' into the table. Therefore it throws unique constraint violated error. To overcome this, you could create it as a composite primary key with DNUMBER and DLOCATION together as:
CONSTRAINT pk_num_loc PRIMARY KEY (DNUMBER, DLOCATION)
DDL & inserts:
/*Create Department Location Table*/
CREATE TABLE dept_locations (
dnumber VARCHAR(30)
REFERENCES department ( dnumber ),
dlocation VARCHAR(30),
CONSTRAINT pk_num_loc PRIMARY KEY ( dnumber, dlocation ));
insert into DEPT_LOCATIONS values('1', 'Houston');
insert into DEPT_LOCATIONS values('4', 'Stafford');
insert into DEPT_LOCATIONS values('5', 'Bellarire');
insert into DEPT_LOCATIONS values('5', 'Sugarland');
insert into DEPT_LOCATIONS values('5', 'Houston');
SELECT * FROM dept_locations;
Output:
DNUMBER DLOCATION
------- ------------------------------
1 Houston
4 Stafford
5 Bellarire
5 Houston
5 Sugarland
Similarly, you need to do the same for rest of the tables which are failing due to unique constraint error.
In both cases you violated the primary key of the table, which must be unique. "Houston" is repeated for DEPT_LOCATIONS, as is "Alice" for DEPENDENTS.

Using multiple foreign keys in a SQL select statement

I have four tables, one of them named matches and another one named teams. I need to display the teamCode in teams with the number of point they have that we can calculate in matches. Each win is 2 points each draw one point and each lost zero.
CREATE TABLE divisions
(
codediv CHAR(1) NOT NULL,
nomdiv VARCHAR2(40)
);
ALTER TABLE divisions
ADD CONSTRAINT divisions_pk PRIMARY KEY (codediv);
CREATE TABLE teams
(
teamCode CHAR(3) NOT NULL,
teamName VARCHAR2(50),
codediv CHAR(1) NOT NULL,
ville VARCHAR2(40),
nbcoupes NUMBER
);
ALTER TABLE teams ADD CHECK (nbcoupes >= 0);
ALTER TABLE teams ADD CONSTRAINT teams_pk PRIMARY KEY (teamCode);
CREATE TABLE joueurs
(
numjoueur NUMBER(3) NOT NULL,
nom VARCHAR2(30),
prenom VARCHAR2(30),
codeequipe CHAR(3)
);
ALTER TABLE joueurs ADD CONSTRAINT joueurs_pk PRIMARY KEY (numjoueur);
CREATE TABLE matchs
(
MatchNumber NUMBER(4) NOT NULL,
datematch DATE,
codeVisitingTeam CHAR(3) NOT NULL,
codeReceivingTeam CHAR(3) NOT NULL,
scoreVisitingTeam NUMBER(2),
scoreReceivingTeam NUMBER(2)
);
ALTER TABLE matchs ADD CONSTRAINT matchs_pk PRIMARY KEY (MatchNumber);
CREATE TABLE statistiques
(
nummatch NUMBER(4) NOT NULL,
numjoueur NUMBER(3) NOT NULL,
nbbuts NUMBER(3),
nbpasse NUMBER(3)
);
ALTER TABLE statistiques
ADD CONSTRAINT statistiques_pk PRIMARY KEY (numjoueur, nummatch);
ALTER TABLE matchs
ADD CONSTRAINT codeVisitingTeam FOREIGN KEY ( codeVisitingTeam)
REFERENCES teams ( teamCode );
ALTER TABLE teams
ADD CONSTRAINT teams_divisions_fk FOREIGN KEY ( codediv )
REFERENCES divisions ( codediv );
ALTER TABLE joueurs
ADD CONSTRAINT joueurs_equipes_fk FOREIGN KEY ( teamCode )
REFERENCES equipes ( teamCode );
ALTER TABLE matchs
ADD CONSTRAINT matchs_equipes_fk FOREIGN KEY ( CodeReceivingTeam )
REFERENCES equipes ( teamCode );
ALTER TABLE statistiques
ADD CONSTRAINT statistiques_joueurs_fk FOREIGN KEY ( numjoueur )
REFERENCES joueurs ( numjoueur );
ALTER TABLE statistiques
ADD CONSTRAINT statistiques_matchs_fk FOREIGN KEY ( nummatch )
REFERENCES matchs ( nummatch );
insert into divisions values('O', 'OUEST');
insert into divisions values('E', 'EST');
insert into equipes values('MTL', 'LES CANADIENS DE MONTRÉAl', 'E', 'MONTRÉAl', 24);
insert into equipes values('TOR', 'LES MAPLE LEAFS', 'E', 'TORONTO', 22);
insert into equipes values('OTT', 'LES SÉNATEURS', 'E', 'OTTAWA', 4);
insert into equipes values('AVL', 'LES AVALANCHES', 'O', 'COLORADO', 2);
insert into equipes values('VAN', 'LES CANUKS', 'O', 'VANCOUVER', 1);
insert into equipes values('BRU', 'LES BRUNS DE BOSTON', 'E', 'BOSTON', 13);
insert into Joueurs values(1, 'PRICE', 'CAREY', 'MTL');
insert into Joueurs values(2, 'MARKOV', 'ANDRÉ', 'MTL');
insert into Joueurs values(3, 'SUBBAN', 'KARL', 'MTL');
insert into Joueurs values(4, 'PATIORETTY', 'MAX', 'MTL');
insert into Joueurs values(10, 'HAMMOND', 'ANDREW', 'OTT');
insert into Joueurs values(6, 'STONE', 'MARC', 'OTT');
insert into Joueurs values(9, 'TURIS', 'KYLE', 'OTT');
insert into Joueurs values(7, 'GALLAGHER', 'BRANDON', 'MTL');
insert into Joueurs values(8, 'TANGUAY', 'ALEX', 'AVL');
insert into Joueurs values(11, 'THOMAS', 'BIL', 'AVL');
insert into Joueurs values(5, 'PATOCHE', 'ALAIN', NULL);
insert into Joueurs values(12, 'POIRIER', 'JUTEUX', NULL);
insert into Matchs values(100, TO_DATE('18-10-30', 'YY/MM/DD'), 'MTL', 'TOR', 3 , 4);
insert into Matchs values(101, TO_DATE('18-11-10', 'YY/MM/DD'), 'TOR', 'MTL', 3 , 3);
insert into Matchs values(102, TO_DATE('18-10-12', 'YY/MM/DD'), 'MTL', 'OTT', 2 , 0);
insert into Matchs values(103, TO_DATE('18-10-20', 'YY/MM/DD'), 'OTT', 'MTL', 0 , 1);
insert into Matchs values(104, TO_DATE('18-11-30', 'YY/MM/DD'), 'MTL', 'AVL', 3 , 4);
insert into Matchs values(105, TO_DATE('18-11-10', 'YY/MM/DD'), 'AVL', 'MTL', 0 , 0);
insert into Matchs values(106, TO_DATE('18-12-12', 'YY/MM/DD'), 'MTL', 'VAN', 2 , 0);
insert into Matchs values(107, TO_DATE('18-03-17', 'YY/MM/DD'), 'VAN', 'MTL', 3 , 1);
insert into Matchs values(108, TO_DATE('18-11-30', 'YY/MM/DD'), 'OTT', 'VAN', 0 , 0);
insert into Matchs values(109, TO_DATE('18-11-10', 'YY/MM/DD'), 'OTT', 'TOR', 0 , 4);
insert into Matchs values(114, TO_DATE('18-10-30', 'YY/MM/DD'), 'BRU', 'TOR', 3 , 4);
insert into Matchs values(115, TO_DATE('19-02-15', 'YY/MM/DD'), 'AVL', 'TOR', null , null);
insert into Matchs values(120, TO_DATE('18-02-26', 'YY/MM/DD'), 'MTL', 'AVL', null , null);
insert into Matchs values(121, TO_DATE('19-02-20', 'YY/MM/DD'), 'MTL', 'OTT', null , null);
Insert into statistiques values(100,3,2,2);
Insert into statistiques values(100,7,1,1);
Insert into statistiques values(101,3,1,0);
Insert into statistiques values(101,7,0,1);
Insert into statistiques values(101,4,1,2);
Insert into statistiques values(101,2,1,2);
Insert into statistiques values(100,4,0,2);
Insert into statistiques values(102,3,1,1);
Insert into statistiques values(102,7,1,2);
Insert into statistiques values(102,9,0,1);
Insert into statistiques values(106,4,1,1);
Insert into statistiques values(106,3,0,2);
Insert into statistiques values(106,2,1,0);
Insert into statistiques values(100,1,null,null);
Insert into statistiques values(101,1,null,null);
Insert into statistiques values(103,1,null,null);
Insert into statistiques values(102,1,null,null);
I've started with this to get the winning teams
select *
from
(select v.teamcode, count(m.codeVisitingTeam) as WonMatches
from matchs m
join teams v on v.teamCode = m.codeVisitingTeam
where m.scoreVisitingTeam > m.scoreReceivingTeam
group by v.teamCode
union all
select r.codeequipe, count(m.codeequiper) as WonMatches
from matchs m
join teams r on r.teamCode = m.codeReceivingTeam
where m.scoreReceivingTeam > m.scoreVisitingTeam
group by r.teamCode));
The only problem with the code above is that it returns twice the team 'MTL' as so :
VAN 1
MTL 2
AVL 1
TOR 3
MTL 1
So I was having a bit of fun with your query and did a bit more than you asked. I wrote the following query to calculated the full complement of wins, losses, and ties. I then ordered the results by points (3 points for a win and 1 for a draw). I hope this helps you a bit:
SELECT *
FROM (SELECT TeamCode,
CASE
WHEN (team = 'VISTING' AND scoreVisitingTeam > SCORERECEIVINGTEAM) OR (team = 'RECEIVING' AND SCORERECEIVINGTEAM > SCOREVISITINGTEAM) THEN 'WIN'
WHEN (team = 'VISTING' AND scoreVisitingTeam < SCORERECEIVINGTEAM) OR (team = 'RECEIVING' AND SCORERECEIVINGTEAM < SCOREVISITINGTEAM) THEN 'LOSS'
ELSE 'DRAW'
END AS RESULT
FROM matchs
UNPIVOT (TeamCode FOR Team IN (CodeVisitingTeam AS 'VISTING', CodeReceivingTeam AS 'RECEIVING')))
PIVOT(COUNT(*) FOR RESULT IN ('WIN' AS WINS, 'LOSS' AS LOSSES, 'DRAW' AS DRAWS))
ORDER BY (wins*3)+draws DESC;
If you have questions, please let me know.
I also created a SQLFiddle. (Link) Note: I removed your constraints from the SQLFiddle. They were throwing errors and I didn't feel like debugging it.
You can do it in a single query with conditional aggregation:
select e.teamCode,
sum(
case
when (e.teamCode = m.codeReceivingTeam and m.scoreReceivingTeam > m.scoreVisitingTeam)
or (e.teamCode = m.codeVisitingTeam and m.scoreReceivingTeam < m.scoreVisitingTeam) then 2
when m.scoreReceivingTeam = m.scoreVisitingTeam then 1
else 0
end
) total_points
from equipes e inner join matchs m
on e.teamCode in (m.codeReceivingTeam, m.codeVisitingTeam)
group by e.teamCode

SQL Developer - Integrity Constraint , parent key not found ( while inserting values )

I know that in order to insert values in a table which relies on foreign keys you need to have data in that primary key of that table .
These are my constraints :
ALTER TABLE DIDACT
MODIFY (CONSTRAINT id_prof_fk FOREIGN KEY(id_prof) REFERENCES profs (id_prof));
ALTER TABLE DIDACT
MODIFY (CONSTRAINT id_course_fk FOREIGN KEY(id_course) REFERENCES courses (id_course));
Next I insert values in both profs and courses tables :
INSERT INTO courses VALUES ('21', 'Logic', 1, 1, 5);
INSERT INTO courses VALUES ('22', 'Math', 1, 1, 4);
INSERT INTO courses VALUES ('23', 'OOP', 1, 2, 5);
INSERT INTO courses VALUES ('24', 'DB', 2, 1, 8);
INSERT INTO courses VALUES ('25', 'Java', 2, 2, 5);
INSERT INTO profs VALUES ('p1', 'Mary', 'Banks', 'Prof');
INSERT INTO profs VALUES ('p2', 'Francis', 'Steven', 'Conf');
INSERT INTO profs VALUES ('p3', 'John', 'Jobs', 'Prof');
INSERT INTO profs VALUES ('p4', 'Alex', 'Brown', 'Prof');
INSERT INTO profs VALUES ('p5', 'Dan', 'Lovelace', 'Lect');
INSERT INTO profs VALUES ('p6', 'Roxanne', 'Smith', 'Conf');
Then I'm trying to populate the DIDACT table:
INSERT INTO didact VALUES ('p1','21');
INSERT INTO didact VALUES ('p3','21');
INSERT INTO didact VALUES ('p5','22');
But this occurs :
INSERT INTO didact VALUES ('p1','21') Error report - SQL Error:
ORA-02291: integrity constraint (user.ID_COURSE_FK) 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.
These are my tables , in case it will help :
CREATE TABLE courses(
id_course CHAR(2),
course_name VARCHAR2(15),
year NUMBER(1),
semester NUMBER(1),
credits NUMBER(2)
)
CREATE TABLE profs(
id_prof CHAR(4),
name CHAR(10),
surname CHAR(10),
grade VARCHAR2(5)
)
CREATE TABLE didact(
id_prof CHAR(4),
id_course CHAR(4)
)
I'm struggling with this for about an hour and I still haven't managed to find my mistake.
Thank you.
You seem to have different formats for id_course in your tables. In didact it's id_course CHAR(4) and in courses it's id_course CHAR(2).
And since you use a fixed-length type the value in didact will differ from the one in courses by two added blanks.

referencing keys oracle, could be foreign key issue

I keep having an issue where I'm trying to load all my tables into a database.
Whenever I try load a table which references "paper_code" I keep getting problem errors, which then don't create the table.
I've been trying to nail out the errors and have got some papers loading, but it seems only the tables that reference papers(paper_code) are having issues.
code:
DROP TABLE contacts;
DROP TABLE gender;
DROP TABLE lecture_location;
DROP TABLE enrols;
DROP TABLE teaches;
DROP TABLE staff;
DROP TABLE student;
DROP TABLE papers;
DROP TABLE departments;
CREATE TABLE departments
(dept_id INT PRIMARY KEY,
dept_location VARCHAR2(25) NOT NULL,
dept_name VARCHAR2(25) NOT NULL);
INSERT INTO departments VALUES
(01, 'ALBANY STREET', 'COMPUTER SCIENCE');
INSERT INTO departments VALUES
(02, 'UNION STREET', 'MUSIC');
INSERT INTO departments VALUES
(03, 'HOWE STREET', 'ANATOMY');
INSERT INTO departments VALUES
(04, 'ANZAC AVENUE', 'THEATRE');
CREATE TABLE papers
(paper_code INT,
EFTS INT NOT NULL,
dept_id INT REFERENCES departments(dept_id),
PRIMARY KEY(paper_code, dept_id));
INSERT INTO papers VALUES
(160, 0.18, 01);
INSERT INTO papers VALUES
(241, 0.18, 02);
INSERT INTO papers VALUES
(344, 0.18, 03);
INSERT INTO papers VALUES
(444, 0.18, 04);
CREATE TABLE student
(student_id INT PRIMARY KEY,
fname CHAR(11) NOT NULL,
lname CHAR(11) NOT NULL,
degree CHAR(15) NOT NULL);
INSERT INTO student VALUES
(172384, 'Michael', 'McDonald', 'BSc');
INSERT INTO student VALUES
(849294, 'Matthew', 'Brockie', 'BA');
INSERT INTO student VALUES
(384583, 'Daniel', 'Anderson', 'BSc');
CREATE TABLE staff
(staff_id INT PRIMARY KEY,
dept_id INT REFERENCES departments(dept_id),
fname CHAR(15) NOT NULL,
lname CHAR(15) NOT NULL);
INSERT INTO staff VALUES
(31, 01, 'Tony', 'Michaels');
INSERT INTO staff VALUES
(32, 01, 'Steph', 'Cardy');
INSERT INTO staff VALUES
(33, 02, 'Alex', 'Freeland');
INSERT INTO staff VALUES
(34, 02, 'Sam', 'Stewart');
INSERT INTO staff VALUES
(35, 03, 'Monique', 'Cardy');
INSERT INTO staff VALUES
(36, 03, 'Bayan', 'Zach');
CREATE TABLE teaches
(paper_code INT,
staff_id INT REFERENCES staff(staff_id),
dept_id INT,
PRIMARY KEY(paper_code, staff_id, dept_id),
FOREIGN KEY (paper_code, dept_id) REFERENCES papers(paper_code, dept_id));
INSERT INTO teaches VALUES
(160, 32, 01);
INSERT INTO teaches VALUES
(241, 31, 01);
INSERT INTO teaches VALUES
(344, 33, 02);
INSERT INTO teaches VALUES
(241, 34, 03);
INSERT INTO teaches VALUES
(444, 35, 03);
INSERT INTO teaches VALUES
(444, 36, 04);
CREATE TABLE enrols
(paper_code INT REFERENCES papers(paper_code),
student_id INT REFERENCES student(student_id),
date_enrolled DATE,
PRIMARY KEY(paper_code, student_id));
INSERT INTO enrols VALUES
(160, 172384, TO_DATE('22-Mar-1994', 'dd-mon-yyyy'));
INSERT INTO enrols VALUES
(444, 849294, TO_DATE('14-Jul-1992', 'dd-mon-yyyy'));
INSERT INTO enrols VALUES
(444, 172384, TO_DATE('23-Mar-1992', 'dd-mon-yyyy'));
INSERT INTO enrols VALUES
(160, 384583, TO_DATE('07-Aug-1992', 'dd-mon-yyyy'));
INSERT INTO enrols VALUES --fix
(160, 172384, TO_DATE('30-Jul-1994', 'dd-mon-yyyy'));
INSERT INTO enrols VALUES
(241, 849294, TO_DATE('08-Sep-1995', 'dd-mon-yyyy'));
INSERT INTO enrols VALUES
(241, 384583, TO_DATE('25-Dec-1996', 'dd-mon-yyyy'));
CREATE TABLE lecture_location
(paper_code INT REFERENCES papers(paper_code),
dept_id INT REFERENCES departments(dept_id),
lecture_loc VARCHAR2(15),
PRIMARY KEY(paper_code, dept_id, lecture_loc));
INSERT INTO lecture_location VALUES
(160, 'ARCHWAY');
INSERT INTO lecture_location VALUES
(241, 'CASTLE');
CREATE TABLE gender
(student_id INT PRIMARY KEY REFERENCES student(student_id),
gender CHAR(9) NOT NULL);
INSERT INTO gender VALUES
(172384, 'Female');
INSERT INTO gender VALUES
(384583, 'Male');
CREATE TABLE contacts
(contact_details INT,
staff_id INT REFERENCES staff(staff_id),
PRIMARY KEY(contact_details, staff_id));
INSERT INTO contacts VALUES
(022017456, 31);
INSERT INTO contacts VALUES
(034737447, 31);
INSERT INTO contacts VALUES
(02285756, 32);
INSERT INTO contacts VALUES
(034735858, 32);
INSERT INTO contacts VALUES
(034552097, 33);
INSERT INTO contacts VALUES
(022867385, 33);
INSERT INTO contacts VALUES
(021495939, 34);
INSERT INTO contacts VALUES
(034993872, 35);
INSERT INTO contacts VALUES
(027459278, 36);
COMMIT;
All the tables except the ones refering paper_code are working.
Errors are:
INSERT INTO teaches VALUES
*
ERROR at line 1:
ORA-02291: integrity constraint (DAANDERSON.SYS_C00623423) violated - parent
key not found
For enrols:
(paper_code INT REFERENCES papers(paper_code),
*
ERROR at line 2:
ORA-02270: no matching unique or primary key for this column-list
For lecture_location
(paper_code INT REFERENCES papers(paper_code),
*
ERROR at line 2:
ORA-02270: no matching unique or primary key for this column-list
Been trying for a few hours and stil can't get to work. Any information appreciated!
When creating your table teaches, you have:
FOREIGN KEY (paper_code, dept_id) REFERENCES papers(paper_code, dept_id));
So every (paper_code, dept_id) must exist as (paper_code, dept_id) in papers.
Your 2nd entry (241, 01) does not exist, nor does your fourth value (241, 03).
(others may be missing, I didn't check them all)
You're also not inserting dept_id values into lecture_location
You have a primary key value duplicated in enrols:
(160, 172384)
This part of enrols:
CREATE TABLE enrols
(paper_code INT REFERENCES papers(paper_code),
is invalid, since you must reference a unique foreign key - and the only unique key on papers is its primary key of (paper_code, dept_id)
Ditto for this part of lecture_location:
(paper_code INT REFERENCES papers(paper_code),
On the bright side, after doing the fixes above the schema actually builds: http://sqlfiddle.com/#!4/5ed2c2