1
Guys, I’m a bit of a beginner in SQL Database and I’m having a problem doing a query. I need a light, help because I’m not yet understanding how a query with a exist (has to be exists and exists only!) within another.
I have the tables Work in which is stored the Work (books), Author, Authorship(Relates Author to Work), Publisher(Publisher of books) and Copy(Which are the books themselves).
Database entities required for resolution (did select * from them):
Work:
Author:
Authorship:
Publishing house:
Exemplary:
Creation of tables with their keys:
Work:
create table Obra
(CodObr number (2) not null,
NomObr char (35) not null,
TipObr number (1) not null,
constraint PK_Obra primary key (CodObr),
constraint FK_Obra_TipoObra foreign key (TipObr) references TipoObra);
Author:
create table Autor
(CodAut number (2) not null,
NomAut char (35) not null,
CodPai number (2) not null,
constraint PK_Autor primary key (CodAut),
constraint FK_Autor_Pais foreign key (CodPai) references Pais);
Authorship:
Create table Autoria
(CodObr number(2) not null,
CodAut number(2) not null,
constraint PK_Autoria primary key (CodObr, CodAut),
constraint FK_Autoria_Autor foreign key (CodAut) references Autor,
constraint FK_Autor_Obra foreign key (CodObr) references Obra on delete cascade);
Publishing house:
create table Editora
(CodEdi number(2) not null,
NomEdi char(35) not null,
constraint PK_Editora primary key (CodEdi));
Exemplary:
create table Exemplar
(NumExe number(2) not null,
CodObr number(2) not null,
AnoEdi number(4) not null,
CodEdi number(2) not null,
ValExe number(6,2) not null,
constraint PK_Exemplar primary key (NumExe),
constraint FK_Exemplar_Editora foreign key (CodEdi) references Editora,
constraint FK_Exemplar_Obra foreign key (CodObr) references Obra);
I have to get the code and the name of the publishers of José Lins do Rego’s Works.
OBS: Remembering that I think I can only use exists and not exists!
I’ve tried to do that:
select codEdi, nomEdi from Editora where exists (select codEdi from Exemplar where exists (select codObr from Autoria where exists (select codAut from Autor where nomAut = 'José Lins do Rego' and codAut = Autoria.codAut)));
and this:
select codEdi, nomEdi from Editora where exists (select codObr from Exemplar where exists (select codAut from Autoria where exists (select codAut from Autor where nomAut = 'José Lins do Rego' and codAut = Autoria.codAut) where codObr = Exemplar.codObr) where codEdi = Editora.codEdi);
But either error or return me all Publishers of the bank, something I do not want.
The correct result would be Editora FTC and Editora LTC.
You could tell me how one exists inside the other?
And ah! I think I can only use exists and not exists, I don’t know if I can use in and not in, but if I can’t, I can only do it with exists?
– Pedro Egg