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 />
M3020KY TOYOTA 1996 CARINA<br />
GR4321A RENAULT 1978 5<br />
J1234Z RENAULT 1997 MEGANE<br />
B4444AC PEUGEOT 1978 504<br />
CA0000AD PEUGEOT 1996 205<br />
GR1111AK PEUGEOT 1998 207<br />
• Obtener los coches or<strong>de</strong>nados por el número <strong>de</strong> caracteres que suman la marca y el<br />
mo<strong>de</strong>lo.<br />
SQL> SELECT marca,mo<strong>de</strong>lo,<br />
LENGTH(marca || mo<strong>de</strong>lo)"Numero caracteres"<br />
FROM coches<br />
OR<strong>DE</strong>R by 3;<br />
MARCA MO<strong>DE</strong>LO Numero caracteres<br />
--------------- --------------- -----------------<br />
RENAULT 5 8<br />
VW BEATTLE 9<br />
PEUGEOT 504 10<br />
PEUGEOT 205 10<br />
PEUGEOT 207 10<br />
TOYOTA CARINA 12<br />
RENAULT MEGANE 13<br />
• Para consultar información relacionada almacenada en varias tablas hay que implementar la<br />
operación JOIN. Se pue<strong>de</strong>n realizar consultas don<strong>de</strong> intervengan más <strong>de</strong> una tabla <strong>de</strong> la base <strong>de</strong><br />
datos, especificando las relaciones entre las distintas tablas en la cláusula WHERE.<br />
Menos para el caso en el que se quiera realizar un producto cartesiano, CUANDO SE<br />
PONEN VARIAS TABLAS EN LA CLAUSULA FROM <strong>DE</strong>L COMANDO SELECT, <strong>DE</strong>BEN<br />
APARECER EN LA CLAUSULA WHERE LAS CORRESPONDIENTES CONDICIONES <strong>DE</strong><br />
IGUALDAD QUE UNAN LAS DISTINTAS TABLAS (ESTO ES LO QUE REALIZA<br />
VERDA<strong>DE</strong>RAMENTE EL JOIN), a<strong>de</strong>más, pue<strong>de</strong>n aparecer las condiciones que se requieran<br />
para realizar una consulta concreta.<br />
Supongamos que las tablas COCHES y TRABAJOS tienen los siguientes datos:<br />
COCHES:<br />
MAT MARCA AN_FAB MO<strong>DE</strong>LO<br />
-------- --------------- --------- ---------------<br />
M3020KY TOYOTA 1996 CARINA<br />
GR4321A RENAULT 1978 5<br />
TRABAJOS:<br />
MAT DNI HORAS FECHA_REP<br />
-------- --------- --------- ---------<br />
M3020KY 1111 1 23-FEB-96<br />
M3020KY 2222 2.5 23-FEB-96<br />
GR4321A 3333 2.1 01-JAN-98<br />
M3020KY 5555 2 23-FEB-96<br />
Si sólo se ponen las dos tablas en la claúsula FROM, entonces lo que se está realizando es un<br />
producto cartesiano, no un JOIN. Es <strong>de</strong>cir, se generan nuevas tuplas combinando cada tupla <strong>de</strong><br />
coches con cada tupla <strong>de</strong> trabajos, pero sin existir relación alguna en cada combinación:<br />
SQL> SELECT c.mat,c.marca,c.mo<strong>de</strong>lo,t.mat,t.dni,t.horas<br />
<strong>Universidad</strong> <strong>de</strong> <strong>Jaén</strong> 22