update a select query in oracle DB - sql

I have 5 tables in a DB like this (script below)::
DB SCRIPT
CREATE TABLE EQUIPE (
code_equipe char(3) primary key,
nom varchar(30),
directeur varchar(30));
CREATE TABLE PAYS (
code_pays varchar(3) primary key,
nom varchar(20));
CREATE TABLE COUREUR (
num_dossart number(3) primary key,
code_equipe char(3),
nom varchar(30),
code_pays varchar(3));
CREATE TABLE ETAPE (
num_etape number(1) primary key,
date_etape date,
kms number(3),
ville_depart varchar(20),
ville_arrivee varchar(20));
CREATE TABLE TEMPS (
num_dossart number(3),
num_etape number(1),
temps_realise number(6),
primary key(num_dossart,num_etape));
ALTER table COUREUR add CONSTRAINT FK_avoir_code_equipe FOREIGN KEY (code_equipe) REFERENCES EQUIPE(code_equipe);
ALTER table COUREUR add CONSTRAINT FK_avoir_code_pays FOREIGN KEY (code_pays) REFERENCES PAYS(code_pays);
ALTER table TEMPS add CONSTRAINT FK_avoir_num_dossart FOREIGN KEY (num_dossart) REFERENCES COUREUR(num_dossart);
ALTER table TEMPS add CONSTRAINT FK_avoir_num_etape FOREIGN KEY (num_etape) REFERENCES ETAPE(num_etape);
and my query
select num_etape,max(temps_realise) from TEMPS group by num_etape
gave this result
and i want to update it to give result like this
this is

If I read it correctly, a simple join does the job:
select t.num_etape,
c.nom,
max(n.temps_realise)
from temps t join coureur c on c.num_dossart = t.num.dossart
group by t.num_etape,
c.nom;

I have solved my question thanks guys
select num_dossart,nom,num_etape,dernier from COUREUR C,
(select num_etape ,max(temps_realise) as dernier from TEMPS GROUP BY num_etape) T
where num_dossart =
(select num_dossart from TEMPS where temps_realise = dernier )

Related

There are no primary or candidate keys in the referenced table 'tblgender' that match the referencing column list in the foreign key 'giFK'

create database my_data
create table tblperson(
pid int primary key,
pname varchar(15),
pgender varchar(15)
)
create table tblgender(
gid varchar(15) primary key,
gender varchar(15)
)
Alter table tblperson add constraint giFK
Foreign key (pgender) references tblgender(gid)

I have a problem with the referencing of this Script

There is an error with the referencing of table booking to person and driver.
ERROR at line 17:
ORA-02270: no matching unique or primary key for this column-list
I've tried almost everything please help me.
alter session set NLS_DATE_FORMAT='DD/MM/YYYY';
DROP TABLE CARSERVICE;
DROP TABLE DRIVERBEN;
DROP TABLE BENEFITS;
DROP TABLE FOLLOWUP;
DROP TABLE INCIDENT;
DROP TABLE DSESSION;
DROP TABLE TRAINING;
DROP TABLE DRIVERINS;
DROP TABLE BOOKING;
DROP TABLE DRIVER;
DROP TABLE PERSON;
CREATE TABLE PERSON
(
PID CHAR(6) PRIMARY KEY,
PName VARCHAR(20),
PDOB DATE,
Sex CHAR(1),
PMOBILE CHAR(12)
);
CREATE TABLE DRIVER
(
DID CHAR(6),
DGrade CHAR(1),
DCarId CHAR(6),
DLicense CHAR(8),
DStart DATE,
DIPlan CHAR(1),
DSession CHAR(6),
DNRIC CHAR(12),
PID CHAR(6),
PRIMARY KEY (DID, DCarId),
FOREIGN KEY (DID) references PERSON(PID)
);
CREATE TABLE BOOKING
(
BookingID CHAR(6),
PickLoc VARCHAR(250),
DropLoc VARCHAR(250),
TripRating NUMBER(1),
RideFare NUMBER(*,1),
TollOther NUMBER(*,1),
TDate DATE,
TTime VARCHAR(5),
BStatus CHAR(1),
Payment VARCHAR(12),
IncRep CHAR(1),
PID CHAR(6),
DID CHAR(6),
PRIMARY KEY(BookingID,PID,DID),
FOREIGN KEY (DID) references Driver(DID),
FOREIGN KEY (PID) references PERSON(PID)
);
CREATE TABLE DRIVERINS
(
DIID CHAR(6) PRIMARY KEY,
PAmount NUMBER(*,1),
PDt DATE,
FOREIGN KEY (DIID) references DRIVER (DID)
);
CREATE TABLE TRAINING
(
TID CHAR(6) PRIMARY KEY,
TrainingPrg VARCHAR(50),
PrgSession VARCHAR2(10)
);
CREATE TABLE DSESSION
(
SID CHAR(6) PRIMARY KEY,
SDate DATE,
TID CHAR(6),
FOREIGN KEY (SID) references DRIVER(DID),
FOREIGN KEY (TID) references TRAINING(TID)
);
CREATE TABLE INCIDENT
(
INCID CHAR(6) PRIMARY KEY,
RIncident VARCHAR(30),
IncDate DATE,
FOREIGN KEY (INCID) references BOOKING(BookingID)
);
CREATE TABLE FOLLOWUP
(
FollowID CHAR(6) PRIMARY KEY,
IncPIC VARCHAR(30),
FollowUpDt VARCHAR2(50),
IncStatus CHAR(1),
FollowUpDate DATE,
FOREIGN KEY (FollowID) references INCIDENT(INCID)
);
CREATE TABLE BENEFITS
(
BID CHAR(6) PRIMARY KEY,
CMedBenefit NUMBER(*,1),
OBetterCars NUMBER(*,1),
PrepRet NUMBER(*,1)
);
CREATE TABLE DRIVERBEN
(
DBID CHAR(6),
CMedBenefit CHAR(1),
OBetterCars CHAR(1),
PrepRet CHAR(1),
FOREIGN KEY (DBID) references DRIVER(DID)
);
CREATE TABLE CARSERVICE
(
CarID CHAR(6) PRIMARY KEY,
CarType VARCHAR(20),
ServRem VARCHAR(250),
CarServDate DATE,
NextServD DATE,
FOREIGN KEY (CarID) references DRIVER(DCarId)
);
You have these definitions:
CREATE TABLE DRIVER (
. . .
PRIMARY KEY (DID, DCarId),
. . .
);
CREATE TABLE BOOKING (
. .
FOREIGN KEY (DID) references Driver(DID),
. . .
);
These are inconsistent. The primary key for Driver has two component keys. The foreign key reference only uses one. You need to reference both.

SQL Server - Invalid Table

I am fairly new to SQL and I can't understand why I am receiving an error when establishing foreign keys as I receive an error saying that the destination table is invalid.
Below is the SQL code, any advice on how to fix would be brilliant! :)
The error appears regarding tblFilms and tblCinemaScreens.
CREATE TABLE tblCustomer (
CustomerID int,
CustomerSurname NVARCHAR(25),
CustomerForename NVARCHAR(20),
CustomerAge int,
CustomerPhoneNumber NVARCHAR(12),
CustomerEmailAddress NVARCHAR(100),
CONSTRAINT PK_tblCustomer PRIMARY KEY CLUSTERED (CustomerID)
)
GO
CREATE TABLE tblBookings (
BookingID int,
FilmShowings TIME,
PriceOfFilm MONEY,
DateOfBooking DATE,
FilmID int,
CinemaScreenID int,
CustomerID int,
CONSTRAINT PK_tblBookings PRIMARY KEY CLUSTERED (BookingID),
CONSTRAINT FK_FilmID FOREIGN KEY (FilmID) REFERENCES tblFilms(FilmID),
CONSTRAINT FK_CustomerID FOREIGN KEY (CustomerID) REFERENCES tblCustomer(CustomerID)
)
GO
CREATE TABLE tblFilms (
FilmID int,
FilmName VARCHAR(100),
FilmDuration int,
AgeRating VARCHAR(3),
CriticScore int,
FilmDescription NVARCHAR(300),
FilmGenre NVARCHAR(20),
FilmStartScreeningDate DATE,
FlimEndScreeningDate DATE,
CinemaScreenID int,
CONSTRAINT PK_tblFilms PRIMARY KEY CLUSTERED (FilmID),
CONSTRAINT FK_tblFilms FOREIGN KEY (CinemaScreenID) REFERENCES tblCinemaScreens(CinemaScreenID)
)
GO
CREATE TABLE tblCinemaScreens (
CinemaScreenID int,
CinemaScreenType NVARCHAR(10),
NumberOfSeats int,
FilmID int,
CONSTRAINT PK_tblCinemaScreens PRIMARY KEY CLUSTERED (CinemaScreenID),
CONSTRAINT FK_tblCinemaScreens FOREIGN KEY (FilmID) REFERENCES tblFilms(FilmID)
)
GO
Cinema screens table does not need a film id and you need to create the tables BEFORE referencing them with FK's
CREATE TABLE tblCinemaScreens (
CinemaScreenID int,
CinemaScreenType NVARCHAR(10),
NumberOfSeats int,
FilmID int,
CONSTRAINT PK_tblCinemaScreens PRIMARY KEY CLUSTERED (CinemaScreenID)
GO
CREATE TABLE tblFilms (
FilmID int,
FilmName VARCHAR(100),
FilmDuration int,
AgeRating VARCHAR(3),
CriticScore int,
FilmDescription NVARCHAR(300),
FilmGenre NVARCHAR(20),
FilmStartScreeningDate DATE,
FlimEndScreeningDate DATE,
CinemaScreenID int,
CONSTRAINT PK_tblFilms PRIMARY KEY CLUSTERED (FilmID),
CONSTRAINT FK_tblFilms FOREIGN KEY (CinemaScreenID) REFERENCES tblCinemaScreens(CinemaScreenID)
)
GO
CREATE TABLE tblCustomer (
CustomerID int,
CustomerSurname NVARCHAR(25),
CustomerForename NVARCHAR(20),
CustomerAge int,
CustomerPhoneNumber NVARCHAR(12),
CustomerEmailAddress NVARCHAR(100),
CONSTRAINT PK_tblCustomer PRIMARY KEY CLUSTERED (CustomerID)
)
GO
CREATE TABLE tblFilms (
FilmID int,
FilmName VARCHAR(100),
FilmDuration int,
AgeRating VARCHAR(3),
CriticScore int,
FilmDescription NVARCHAR(300),
FilmGenre NVARCHAR(20),
FilmStartScreeningDate DATE,
FlimEndScreeningDate DATE,
CinemaScreenID int,
CONSTRAINT PK_tblFilms PRIMARY KEY CLUSTERED (FilmID),
CONSTRAINT FK_tblFilms FOREIGN KEY (CinemaScreenID) REFERENCES tblCinemaScreens(CinemaScreenID)
)
GO
You are trying to create a foreign key on tables before they are made. Comment out the lines in the create table statements that are giving you an error and create the tables. Once the tables are made create the missing foreign keys you need like this:
alter table tblFilms
add CONSTRAINT FK_tblFilms FOREIGN KEY (CinemaScreenID) REFERENCES tblCinemaScreens(CinemaScreenID)
alter table tblBookings
add CONSTRAINT FK_FilmID FOREIGN KEY (FilmID) REFERENCES tblFilms(FilmID)
The answer is they should have not direct relationships
A film is shown on 0 or more screens
You need a join table
rblFilmCinema
filmID fk to tblFilms
screenID fk to tblCinemaScreens
composite PK on filmID, screenID
I would change up the schema some to give yourself more flexibility. I would assume that a film can be on more than one cinema screen so I would take CinemaScreenID off of tblFilms and remove the foreign key. You can also remove FilmID from tblBookings since you have the CinemaScreenID already which has the FilmID.. Another thing to consider might be having multiple films on the same CinemaScreen which would be another tabled called tblCinemaScreenFilms and you would put that CinemaScreenFilmID on the tblBookings instead
CREATE TABLE tblFilms (
FilmID int,
...
CONSTRAINT PK_tblFilms PRIMARY KEY CLUSTERED (FilmID),
)
GO
CREATE TABLE tblCustomer (
CustomerID int,
...
CONSTRAINT PK_tblCustomer PRIMARY KEY CLUSTERED (CustomerID)
)
GO
CREATE TABLE tblBookings (
BookingID int,
CinemaScreenID int,
CustomerID int,
...
CONSTRAINT PK_tblBookings PRIMARY KEY CLUSTERED (BookingID),
CONSTRAINT FK_CinemaScreenID FOREIGN KEY (CinemaScreenID ) REFERENCES tblCinemaScreens(CinemaScreenID),
CONSTRAINT FK_CustomerID FOREIGN KEY (CustomerID) REFERENCES tblCustomer(CustomerID)
)
GO
CREATE TABLE tblCinemaScreens (
CinemaScreenID int,
FilmID int,
...
CONSTRAINT PK_tblCinemaScreens PRIMARY KEY CLUSTERED (CinemaScreenID),
CONSTRAINT FK_tblCinemaScreens FOREIGN KEY (FilmID) REFERENCES tblFilms(FilmID)
)
GO
Option B (preferred)
CREATE TABLE tblCustomer (
CustomerID int,
...
CONSTRAINT PK_tblCustomer PRIMARY KEY CLUSTERED (CustomerID)
)
GO
CREATE TABLE tblFilms (
FilmID int,
...
CONSTRAINT PK_tblFilms PRIMARY KEY CLUSTERED (FilmID),
)
GO
CREATE TABLE tblCinemaScreens (
CinemaScreenID int,
...
CONSTRAINT PK_tblCinemaScreens PRIMARY KEY CLUSTERED (CinemaScreenID)
)
GO
CREATE TABLE tblCinemaScreenFilms (
CinemaScreenFilmID int,
CinemaScreenID int,
FilmID int
CONSTRAINT PK_tblCinemaScreenFilms PRIMARY KEY CLUSTERED (CinemaScreenFilmID),
CONSTRAINT FK_FilmID FOREIGN KEY (FilmID) REFERENCES tblFilms(FilmID),
CONSTRAINT FK_CinemaScreenID FOREIGN KEY (FilmID) REFERENCES tblCinemaScreens(CinemaScreenID)
)
GO
CREATE TABLE tblBookings (
BookingID int,
CustomerID int,
CinemaScreenFilmID int,
...
CONSTRAINT PK_tblBookings PRIMARY KEY CLUSTERED (BookingID),
CONSTRAINT FK_CustomerID FOREIGN KEY (CustomerID) REFERENCES tblCustomer(CustomerID),
CONSTRAINT FK_CinemaScreenFilmID FOREIGN KEY (CinemaScreenFilmID) REFERENCES tblCinemaScreenFilms(CinemaScreenFilmID)
)
GO
You're creating a table - tblFilms - and adding a foreign key to tblCinemaScreens before the second table has been created.
As a help I usually create all tables and then create any foreign key relationships and other constraints.

Sturctured Query Language

create table customer(
custno number(10) constraint foo primary key,
custname character(15),
city character(15),
phone number(10));
create table invoice(
invo number(10) constraint inv primary key,
invdate date(10),
constarint c references customer(custno));
I cannot able to create the second table and am having doubt of the data type date and If I neglected that attribute(invdate) still I cannot able to create the second table.Reason please.
create table customer
( custno int constraint foo primary key
,custname character(15)
,city character(15)
,phone int
)
go
create table invoice
( invo int constraint inv primary key
constraint ck references customer(custno)
,invdate date
)

SQL Server - Adding Primary and Foreign Keys

I have one query that creates several tables, listed below, and a second query that is supposed to add Primary and Foreign keys to each. However, when I try executing my query, I'm greeted with the following error messages:
Msg 1776, Level 16, State 0, Line 2
There are no primary or candidate keys in the referenced table 'DEPARTMENT' that match the referencing column list in the foreign key 'FK__EMPLOYEE__dno__25869641'.
Msg 1750, Level 16, State 0, Line 2
Could not create constraint. See previous errors.
I'm not sure what this means, as I have researched both primary and foreign keys. Can anyone lead me in the right direction as to how to solve this issue? Thanks! :)
CREATE TABLE EMPLOYEE
(
fname varchar(100),
minit char(1),
lname varchar(100),
ssn char(9),
bdate date,
addr varchar(100),
sex char(1),
salary int,
super_ssn char(9),
dno int
);
CREATE TABLE DEPARTMENT
(
dname varchar(100),
dnumber int,
mgr_ssn char(9),
mgr_start_date date
);
CREATE TABLE DEPENDENTS
(
essn char(9),
dependent_name varchar(100),
sex char(1),
bdate date,
relationship varchar(100)
);
CREATE TABLE DEPT_LOCATIONS
(
dnumber int,
dlocation varchar(100)
);
CREATE TABLE PROJECT
(
pname varchar(100),
pnumber int,
plocation varchar(100),
dnum int
);
CREATE TABLE WORKS_ON
(
essn char(9),
pno int,
hrs float
);
ALTER TABLE EMPLOYEE ADD PRIMARY KEY(ssn);
ALTER TABLE EMPLOYEE ADD FOREIGN KEY(super_ssn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE EMPLOYEE ADD FOREIGN KEY(dno) REFERENCES DEPARTMENT(dnumber);
ALTER TABLE DEPARTMENT ADD PRIMARY KEY(dnumber);
ALTER TABLE DEPARTMENT ADD FOREIGN KEY(mgr_ssn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE DEPT_LOCATIONS ADD PRIMARY KEY(dnumber, dlocation);
ALTER TABLE DEPT_LOCATIONS ADD FOREIGN KEY(dnumber) REFERENCES DEPARTMENT(dnumber);
ALTER TABLE PROJECT ADD PRIMARY KEY(pnumber);
ALTER TABLE PROJECT ADD FOREIGN KEY(dnum) REFERENCES DEPARTMENT(dnumber);
ALTER TABLE WORKS_ON ADD PRIMARY KEY(essn, pno);
ALTER TABLE WORKS_ON ADD FOREIGN KEY(essn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE WORKS_ON ADD FOREIGN KEY(pno) REFERENCES PROJECT(pnumber);
ALTER TABLE DEPENDENTS ADD PRIMARY KEY(essn, dependent_name);
ALTER TABLE DEPENDENTS ADD FOREIGN KEY(essn) REFERENCES EMPLOYEE(ssn);
Primary key column should be non null able. Table definition should have not null for all PKs.
CREATE TABLE EMPLOYEE (
fname varchar(100),
minit char(1),
lname varchar(100),
ssn char(9) not null,
bdate date,
addr varchar(100),
sex char(1),
salary int,
super_ssn char(9),
dno int
);
CREATE TABLE DEPARTMENT (
dname varchar(100),
dnumber int not null,
mgr_ssn char(9),
mgr_start_date date
);
CREATE TABLE DEPENDENTS (
essn char(9) not null,
dependent_name varchar(100) not null,
sex char(1),
bdate date,
relationship varchar(100)
);
CREATE TABLE DEPT_LOCATIONS (
dnumber int not null,
dlocation varchar(100) not null
);
CREATE TABLE PROJECT (
pname varchar(100),
pnumber int not null,
plocation varchar(100),
dnum int
);
CREATE TABLE WORKS_ON (
essn char(9) not null,
pno int not null,
hrs float
);
ALTER TABLE EMPLOYEE ADD PRIMARY KEY(ssn);
ALTER TABLE DEPARTMENT ADD PRIMARY KEY(dnumber);
ALTER TABLE EMPLOYEE ADD FOREIGN KEY(super_ssn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE EMPLOYEE ADD FOREIGN KEY(dno) REFERENCES DEPARTMENT(dnumber);
ALTER TABLE DEPARTMENT ADD FOREIGN KEY(mgr_ssn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE DEPT_LOCATIONS ADD PRIMARY KEY(dnumber, dlocation);
ALTER TABLE DEPT_LOCATIONS ADD FOREIGN KEY(dnumber) REFERENCES DEPARTMENT(dnumber);
ALTER TABLE PROJECT ADD PRIMARY KEY(pnumber);
ALTER TABLE PROJECT ADD FOREIGN KEY(dnum) REFERENCES DEPARTMENT(dnumber);
ALTER TABLE WORKS_ON ADD PRIMARY KEY(essn, pno);
ALTER TABLE WORKS_ON ADD FOREIGN KEY(essn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE WORKS_ON ADD FOREIGN KEY(pno) REFERENCES PROJECT(pnumber);
ALTER TABLE DEPENDENTS ADD PRIMARY KEY(essn, dependent_name);
ALTER TABLE DEPENDENTS ADD FOREIGN KEY(essn) REFERENCES EMPLOYEE(ssn);