PRÁCTICAS DE BASES DE DATOS I - Universidad de Jaén
PRÁCTICAS DE BASES DE DATOS I - Universidad de Jaén
PRÁCTICAS DE BASES DE DATOS I - Universidad de Jaén
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Programación <strong>de</strong> Bases <strong>de</strong> Datos Oracle SQL<br />
Obtener los coches que NO son reparados por 'PEPE':<br />
SQL> SELECT mat,marca,mo<strong>de</strong>lo<br />
FROM coches<br />
WHERE mat not in (SELECT mat<br />
FROM trabajos t,mecanicos m<br />
WHERE t.dni=m.dni AND<br />
m.nombre='PEPE');<br />
MAT MARCA MO<strong>DE</strong>LO<br />
-------- --------------- ---------------<br />
M3020KY TOYOTA CARINA<br />
J1234Z RENAULT MEGANE<br />
Obtener los mecánicos que no han reparado ningún coche RENAULT:<br />
SQL> SELECT nombre<br />
FROM mecanicos m<br />
WHERE not exists (<br />
(SELECT distinct mat<br />
FROM coches<br />
WHERE marca = 'RENAULT')<br />
INTERSECT<br />
(SELECT distinct mat<br />
FROM trabajos t<br />
WHERE t.dni = m.dni));<br />
NOMBRE<br />
---------------<br />
EMILIO<br />
LUISA<br />
Otras posibles formas <strong>de</strong> resolver esta consulta son:<br />
SQL> (SELECT distinct nombre<br />
FROM mecanicos m,trabajos t,coches c<br />
WHERE m.dni=t.dni AND t.mat=c.mat AND c.marca!='RENAULT')<br />
MINUS<br />
(SELECT distinct nombre<br />
FROM mecanicos m,trabajos t,coches c<br />
WHERE m.dni=t.dni AND t.mat=c.mat AND c.marca='RENAULT');<br />
SQL> SELECT nombre<br />
FROM mecanicos<br />
WHERE dni not in<br />
(SELECT dni<br />
FROM trabajos t,coches c<br />
WHERE t.mat=c.mat AND c.marca='RENAULT');<br />
8.6. Vistas<br />
Crear una vista que contenga la marca y el mo<strong>de</strong>lo <strong>de</strong> cada coche, el puesto en que fue reparado y<br />
el total <strong>de</strong> horas invertidas en ese puesto:<br />
SQL> CREATE OR REPLACE VIEW total_coche_puesto<br />
as SELECT marca,mo<strong>de</strong>lo,puesto,SUM(horas) "total horas"<br />
FROM mecanicos m, coches c,trabajos t<br />
WHERE m.dni=t.dni AND c.mat=t.mat<br />
GROUP by marca,mo<strong>de</strong>lo,puesto;<br />
<strong>Universidad</strong> <strong>de</strong> <strong>Jaén</strong> 44