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.
Related
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
I need to group two columns, one of them with an average, AVG() inside an LISTAGG().
I have the following code:
CREATE OR REPLACE VIEW countryTimes AS
SELECT
LISTAGG(claOL.odCode||'-'||(AVG(claOL.timeCla) GROUP BY(claOl.timeCla))) WITHIN GROUP (ORDER BY c.cCode) AS ProvaTempsMig,
c.cDescription AS País,
c.cCode AS CodiPaís
FROM countries c
JOIN athletes a ON c.cCode = a.country
JOIN classificationOL claOL ON a.idCode = claOL.idAth;
But this throws this error:
ORA-00907: missing right parenthiesis erecho 00907. 00000 - "missing right parenthesis" *Cause: *Action:
I'm using Oracle.
UPDATE:
What I need to do is create a view where appears cCode, cDescription and a last column with the AVG of all the times for a single country. So I need to create from multiple rows, a single row for each country.
Code:
CREATE TABLE Countries (
cCode VARCHAR(5) NOT NULL,
cdescription VARCHAR(100) NOT NULL,
CONSTRAINT couPK PRIMARY KEY (cCode)
);
CREATE TABLE athletes (
idCode NUMBER NOT NULL,
Name VARCHAR(200) NOT NULL,
Surname VARCHAR(200) NOT NULL,
country VARCHAR(5) NOT NULL,
CONSTRAINT athPK PRIMARY KEY (idCode),
CONSTRAINT countryFK FOREIGN KEY (country) REFERENCES Countries (cCode)
);
CREATE TABLE olympicDisciplines (
oCode VARCHAR(10) NOT NULL,
odName VARCHAR(200) NOT NULL,
discipline VARCHAR(200) NOT NULL,
CONSTRAINT olympicPK PRIMARY KEY (oCode)
);
CREATE TABLE classificationOL(
idAth NUMBER NOT NULL,
odCode VARCHAR(10) NOT NULL,
timeCla INTEGER,
CONSTRAINT classifPK PRIMARY KEY (idAth, odCode),
CONSTRAINT claAthFK FOREIGN KEY (idAth) REFERENCES athletes (idCode),
CONSTRAINT claDFK FOREIGN KEY (odCode) REFERENCES olympicDisciplines (oCode)
);
UPDATE 2:
Data:
INSERT INTO Countries VALUES ('UK', 'United Kingdom');
INSERT INTO Countries VALUES ('AND', 'Andorra');
INSERT INTO Countries VALUES ('FR', 'France');
INSERT INTO athletes VALUES (1, 'Jack', 'Johnson', 'UK');
INSERT INTO athletes VALUES (2, 'Pau', 'Márquez', 'AND');
INSERT INTO athletes VALUES (3, 'Pierre', 'Dubois', 'FR');
INSERT INTO athletes VALUES (4, 'Christophe', 'Dubois', 'FR');
INSERT INTO athletes VALUES (5, 'Adolphe', 'Moreau', 'FR');
INSERT INTO olympicDisciplines VALUES ('ATH', 'Athletics', 'Athletics');
INSERT INTO olympicDisciplines VALUES ('CYC', 'Cycling', 'Cycling');
INSERT INTO olympicDisciplines VALUES ('CCC', 'Cycling CC', 'Cross Country Cycling');
INSERT INTO classificationOL VALUES (1, 'ATH', 120);
INSERT INTO classificationOL VALUES (2, 'ATH', 119);
INSERT INTO classificationOL VALUES (3, 'CCC', 38);
INSERT INTO classificationOL VALUES (4, 'CCC', 37);
INSERT INTO classificationOL VALUES (5, 'ATH', 122);
Reading your first UPDATE, if you're allowed to, you can transform your tables to object to solve your necessity, instead of using LISTAGG(). I'll show you:
CREATE TYPE average AS OBJECT(
name VARCHAR(200),
avgerageTime NUMBER);
CREATE TYPE results AS TABLE OF average;
CREATE TYPE countriesResults AS OBJECT(
cName VARCHAR(100),
cCode VARCHAR(5),
classifications results
);
CREATE VIEW countriesAverages OF countriesResults
WITH OBJECT OID (coName)
AS
SELECT c.cdescription, c.ccode,
CAST (MULTISET (SELECT
olympicDisciplines.name, avg(classificationOL.timeCla)
FROM athletes a, countries, classificationOL, olympicDisciplines
WHERE countries.cCode = c.cCode
AND a.idCode = classificationOL.idAth
AND a.country = countries.cCode
AND olympicdisciplines.oCode = classificationOL.oCode
GROUP BY olympicdisciplines.odName) AS results )
FROM countries c;
I have been attempting to create a query for checking available rooms between two dates but have been thus far unsuccessful. Please find the sql code for creating the tables below. I really really need your help. this is done because the system wants me to add more details
CREATE TABLE rooms (
id NUMERIC(3) NOT NULL,
type VARCHAR2(11) NOT NULL,
CONSTRAINT pk_rooms PRIMARY KEY (id),
CONSTRAINT fk_type
);
INSERT INTO rooms VALUES (101, 'Excellent');
INSERT INTO rooms VALUES (102, 'Excellent');
INSERT INTO rooms VALUES (103, 'Excellent');
INSERT INTO rooms VALUES (104, 'Excellent');
INSERT INTO rooms VALUES (105, 'Excellent');
INSERT INTO rooms VALUES (106, 'Excellent');
INSERT INTO rooms VALUES (107, 'Excellent');
INSERT INTO rooms VALUES (108, 'Excellent');
INSERT INTO rooms VALUES (109, 'Excellent');
INSERT INTO rooms VALUES (110, 'Excellent');
INSERT INTO rooms VALUES (111, 'Excellent');
INSERT INTO rooms VALUES (112, 'Excellent');
CREATE TABLE bookings_roombookings (
book_id VARCHAR(20) NOT NULL,
room_id NUMBER NOT NULL,
fromdate DATE NOT NULL,
todate DATE NOT NULL,
guest1 VARCHAR(40) NOT NULL,
guest2 VARCHAR(40),
guest3 VARCHAR(40),
CONSTRAINT cpk PRIMARY KEY (book_id, room_id),
CONSTRAINT fk_rid FOREIGN KEY (room_id) REFERENCES rooms(id));
INSERT INTO bookings_roombookings VALUES (
'DTR5000000', 320,
'01-JUN-2020', '01-SEP-2020',
'D.Trump', 'H.Clinton', 'S.Daniels');
INSERT INTO bookings_roombookings VALUES (
'DRI0000002', 102,
'11-DEC-19', '01-SEP-20',
'D.Ridley', NULL, NULL);
my query is as follows:
select r.id from rooms r
where r.id not in
( select bkr.room_id from bookings_roombookings bkr
where (fromdate > '15-JUN-20') and (todate < '18-JUN-20') );
but this will return both rows of the bkr table. I would appreciate any help.
You can start from the room table, and use a not exists condition with a correlated subquery to eliminate rooms that have a reservation period which overlaps the target date range:
select r.*
from rooms r
where not exists (
select 1
from bookings_roombookings bkr
where
bkr.room_id = r.id
and bkr.fromdate <= date'2020-06-15'
and bkr.todate >= date'2020-06-18'
)
Note: do not rely on implicit conversions from strings to date (as in fromdate > '15-JUN-20'), that depends on the nls settings of your database and session; instead, you can use date litterals, that are part of the ANSI SQL standard and which Oracle supports, like date'yyyy-mm-dd'.
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
I have multiple possible data tables (created to support parallel processing) to search against to find a match, and another table containing entries to exclude. The tables are defined as follows, along with some example data:
CREATE TABLE HOUSEHOLD_1
(
PERSON_ID NUMBER NOT NULL ENABLE,
HOUSEHOLD_HEAD_ID NUMBER NOT NULL ENABLE,
RELATIONSHIP CHAR (1) NOT NULL ENABLE,
CONSTRAINT HOUSEHOLD_1_PK PRIMARY KEY (PERSON_ID, HOUSEHOLD_HEAD_ID, RELATIONSHIP) ENABLE
);
INSERT INTO HOUSEHOLD_1 VALUES (1, 1, 'H');
INSERT INTO HOUSEHOLD_1 VALUES (2, 1, 'S');
INSERT INTO HOUSEHOLD_1 VALUES (3, 1, 'D');
CREATE TABLE HOUSEHOLD_2
(
PERSON_ID NUMBER NOT NULL ENABLE,
HOUSEHOLD_HEAD_ID NUMBER NOT NULL ENABLE,
RELATIONSHIP CHAR (1) NOT NULL ENABLE,
CONSTRAINT HOUSEHOLD_2_PK PRIMARY KEY (PERSON_ID, HOUSEHOLD_HEAD_ID, RELATIONSHIP) ENABLE
);
INSERT INTO HOUSEHOLD_2 VALUES (4, 4, 'H');
INSERT INTO HOUSEHOLD_2 VALUES (5, 4, 'S');
INSERT INTO HOUSEHOLD_2 VALUES (6, 4, 'D');
CREATE TABLE HOUSEHOLD_3
(
PERSON_ID NUMBER NOT NULL ENABLE,
HOUSEHOLD_HEAD_ID NUMBER NOT NULL ENABLE,
RELATIONSHIP CHAR (1) NOT NULL ENABLE,
CONSTRAINT HOUSEHOLD_3_PK PRIMARY KEY (PERSON_ID, HOUSEHOLD_HEAD_ID, RELATIONSHIP) ENABLE
);
INSERT INTO HOUSEHOLD_3 VALUES (7, 7, 'H');
INSERT INTO HOUSEHOLD_3 VALUES (8, 7, 'S');
INSERT INTO HOUSEHOLD_3 VALUES (9, 7, 'D');
CREATE TABLE HOUSEHOLD_DELETIONS
(
PERSON_ID NUMBER NOT NULL ENABLE,
HOUSEHOLD_HEAD_ID NUMBER NOT NULL ENABLE,
RELATIONSHIP CHAR (1) NOT NULL ENABLE,
CONSTRAINT HOUSEHOLD_DELETIONS_PK PRIMARY KEY (PERSON_ID, HOUSEHOLD_HEAD_ID, RELATIONSHIP) ENABLE
);
INSERT INTO HOUSEHOLD_DELETIONS VALUES (9, 7, 'D');
CREATE TABLE CLOSED_ACCOUNTS
(
PERSON_ID NUMBER NOT NULL ENABLE,
CONSTRAINT CLOSED_ACCOUNTS_PK PRIMARY KEY (PERSON_ID) ENABLE
);
INSERT INTO CLOSED_ACCOUNTS VALUES (3);
INSERT INTO CLOSED_ACCOUNTS VALUES (6);
INSERT INTO CLOSED_ACCOUNTS VALUES (9);
INSERT INTO CLOSED_ACCOUNTS VALUES (10);
I need to find the PERSON_ID values in CLOSED_ACCOUNTS that have a matching PERSON_ID in either HOUSEHOLD_1, HOUSEHOLD_2, or HOUSEHOLD_3, but do not have a PERSON_ID in HOUSEHOLD_DELETIONS. With the data above, I should only find PERSON_ID values 3 and 6. I've tried the SQL tricks I know, but I have not been successful, so any assistance would be appreciated. Thanks in advance.
SET operations should work to you:
SQL> select person_id from CLOSED_ACCOUNTS
2 intersect (
3 select person_id from HOUSEHOLD_1
4 union
5 select person_id from HOUSEHOLD_2
6 union
7 select person_id from HOUSEHOLD_3
8 )
9 minus
10 select person_id from HOUSEHOLD_DELETIONS
11 /
PERSON_ID
----------
3
6