PostgreSQL Databases - sql

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

Related

AVG() inside LISTAGG()

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;

SQL query available rooms for booking

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'.

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.

SQL Query with Multiple Possible Tables and an Exclusion Table

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

Operand type clash: int is incompatible with date + The INSERT statement conflicted with the FOREIGN KEY constraint

create table pilot (
emp_num int,
pl_license varchar (3),
pl_ratings varchar (30),
pl_med_type int,
pl_med_date date,
pl_pt135_date date,
constraint PK_pilot primary key (emp_num)
)
insert into pilot(emp_num,pl_license,pl_ratings,pl_med_type,pl_med_date,pl_pt135_date)
values (101,'ATP','SEL/MEL/instr/CFII',1,12-4-2005,15-6-2005)
insert into pilot(emp_num,pl_license,pl_ratings,pl_med_type,pl_med_date,pl_pt135_date)
values (104,'ATP','SEL/MEL/instr',1,10-5-2005,23-3-2006)
insert into pilot(emp_num,pl_license,pl_ratings,pl_med_type,pl_med_date,pl_pt135_date)
values (105,'COM','SEL/MEL/instr/CFI',2,25-2-2006,12-2-2005)
insert into pilot(emp_num,pl_license,pl_ratings,pl_med_type,pl_med_date,pl_pt135_date)
values (106,'COM','SEL/MEL/instr',2,02-4-2006,24-12-2005)
insert into pilot(emp_num,pl_license,pl_ratings,pl_med_type,pl_med_date,pl_pt135_date)
values (109,'COM','SEL/MEL/instr/CFII',1,14-4-2006,21-4-2006)
My question is there is an error in every insert
Operand type clash: int is incompatible with date
How to fix this?
Also here ...
create table employee (
emp_num int,
constraint PK_employee primary key (emp_num),
foreign key(emp_num) references pilot(emp_num),
emp_title varchar (4),
emp_lname varchar (20),
emp_fname varchar (30),
emp_initial varchar (2),
emp_dob date,
emp_hire_date date,
)
insert into employee(emp_num,emp_title,emp_lname,emp_fname,emp_initial,emp_dob,emp_hire_date)
values (100,'Mr.','Kolmycz','George','D',15-5-1942,15-3-1987)
insert into employee(emp_num,emp_title,emp_lname,emp_fname,emp_initial,emp_dob,emp_hire_date)
values (101,'Ms.','Lewis','Rhonda','G',19-3-1965,25-4-1988)
insert into employee(emp_num,emp_title,emp_lname,emp_fname,emp_initial,emp_dob,emp_hire_date)
values (102,'Mr.','Vandam','Rhett',' ',14-11-1958,20-12-1992)
insert into employee(emp_num,emp_title,emp_lname,emp_fname,emp_initial,emp_dob,emp_hire_date)
values (103,'Ms.','Jones','Anne','M',16-10-1974,28-8-2005)
insert into employee(emp_num,emp_title,emp_lname,emp_fname,emp_initial,emp_dob,emp_hire_date)
values (104,'Mr.','Lange','John','P',08-11-1971,20-10-1996)
insert into employee(emp_num,emp_title,emp_lname,emp_fname,emp_initial,emp_dob,emp_hire_date)
values (105,'Mr.','williams','Robert','D',14-3-1975,08-1-2006)
insert into employee(emp_num,emp_title,emp_lname,emp_fname,emp_initial,emp_dob,emp_hire_date)
values (106,'Mrs.','Duzak','Jeanine','K',12-2-1968,05-1-1991)
insert into employee(emp_num,emp_title,emp_lname,emp_fname,emp_initial,emp_dob,emp_hire_date)
values (107,'Mr.','Diante','Jorge','D',21-8-1974,05-1-1991)
insert into employee(emp_num,emp_title,emp_lname,emp_fname,emp_initial,emp_dob,emp_hire_date)
values (108,'Mr.','Wlesenbach','Paul','R',14-2-1966,18-11-1994)
insert into employee(emp_num,emp_title,emp_lname,emp_fname,emp_initial,emp_dob,emp_hire_date)
values (109,'Ms.','Travis','Elizabeth','K',18-6-1961,14-4-1991)
insert into employee(emp_num,emp_title,emp_lname,emp_fname,emp_initial,emp_dob,emp_hire_date)
values (110,'Mrs.','Genkazi','Leighla','W',19-5-1970,01-12-1992)
Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__crew__emp_num__0F975522". The conflict occurred in database "melisa", table "dbo.employee", column 'emp_num'. error on
this table
create table earndrating(
emp_num int,
constraint PK_earndarating primary key(emp_num, rtg_code),
rtg_code varchar(6),
foreign key (emp_num) references pilot(emp_num),
foreign key(rtg_code) references rating(rtg_code),
earningth_date varchar(20),
)
insert into earndrating(emp_num,rtg_code,earningth_date)
values(101,'CFI','18-Feb-98' )
insert into earndrating(emp_num,rtg_code,earningth_date)
values(101,'CFII','14-Dec-05' )
insert into earndrating(emp_num,rtg_code,earningth_date)
values(101,'INSTR','08-Nov-93' )
insert into earndrating(emp_num,rtg_code,earningth_date)
values(101,'MEL','23-Jun-94' )
insert into earndrating(emp_num,rtg_code,earningth_date)
values(101,'SEL','21-Apr-93' )
insert into earndrating(emp_num,rtg_code,earningth_date)
values(104,'INSTR','14-Jul-96' )
insert into earndrating(emp_num,rtg_code,earningth_date)
values(104,'MEL','29-Jan-97' )
insert into earndrating(emp_num,rtg_code,earningth_date)
values(104,'SEL','12-Mar-95' )
insert into earndrating(emp_num,rtg_code,earningth_date)
values(105,'CFI','18-Nov-97' )
insert into earndrating(emp_num,rtg_code,earningth_date)
values(105,'INSTR','17-Apr-95' )
insert into earndrating(emp_num,rtg_code,earningth_date)
values(105,'MEL','12-Aug-95' )
insert into earndrating(emp_num,rtg_code,earningth_date)
values(105,'SEL','23-Sep-94' )
insert into earndrating(emp_num,rtg_code,earningth_date)
values(106,'INSTR','20-Dec-95' )
insert into earndrating(emp_num,rtg_code,earningth_date)
values(106,'MEL','02-Apr-95' )
insert into earndrating(emp_num,rtg_code,earningth_date)
values(105,'SEL','10-Mar-94' )
insert into earndrating(emp_num,rtg_code,earningth_date)
values(109,'CFI','05-Nov-98' )
insert into earndrating(emp_num,rtg_code,earningth_date)
values(109,'CFII','21-Jun-03' )
insert into earndrating(emp_num,rtg_code,earningth_date)
values(109,'INSTR','23-Jul-96' )
insert into earndrating(emp_num,rtg_code,earningth_date)
values(109,'MEL','15-Marc-97' )
insert into earndrating(emp_num,rtg_code,earningth_date)
values(109,'SEL','05-Feb-96' )
insert into earndrating(emp_num,rtg_code,earningth_date)
values(109,'SES','12-May-96' )
This expression 12-4-2005 is a calculated int and the value is -1997. You should do like this instead '2005-04-12' with the ' before and after.
Try wrapping your dates in single quotes, like this:
'15-6-2005'
It should be able to parse the date this way.
FYI this turned out to be an issue for me where I had two tables in a statement like the following:
SELECT * FROM table1
UNION ALL
SELECT * FROM table2
It worked, but then somewhere along the line the order of columns in one of the table definitions got changed. Changing the * to SELECT column1, column2 fixed the issue. No idea how that happened, but lesson learned!
I had the same problem. I tried 'yyyy-mm-dd' format i.e. '2013-26-11' and got rid of this problem...
INSERT INTO EMPLOYEINFO VALUES
(1, 'Sanjay', 'Mehra', 'HR', 'P1', 'Hyderabad(HYD)', '01/12/1976', 'M'),
(2, 'Ananya', 'Mishra', 'Admin', 'P2', 'Delhi(DEL)', '02/05/1968', 'F'),
(3, 'Rohan', 'Diwan', 'Account', 'P3', 'Mumbai(BOM)', '01/01/1980', 'M'),
(4, 'Sonia', 'Kulkarni', 'HR', 'P1', 'Hyderabad(Hyd)', '02/05/1992', 'F'),
(5, 'Ankit', 'Kapoor', 'Admin', 'P2', 'Delhi(DEL)', '03/07/1994', 'M');
if you are getting this [Operand type clash: int is incompatible with date]
error then just add your dates in single qoutes ''.
the format is ('MM-DD-YYYY') for the date