Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
FA C U LTA D D E I N G E N I E R I A Y A R Q U I T E C T U R A Ciclo
Objetivos
Que el estudiante sea capaz de:
Realizar las operaciones bsicas de consulta en SQL sobre Oracle utilizando una sola tabla.
Comprender los conceptos fundamentales referentes a consultas en SQL.
Utilizar correctamente la clusula where para restringir las filas a mostrar, con expresiones
simples y complejas.
Hacer ajustes en las consultas que utilizan valores nulos.
Introduccin
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
1
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
Desarrollo.
Esquemas a utilizar en la gua
Definicin
El conjunto de objetos que tiene una cuenta de usuario se denomina esquema del usuario,
por lo tanto el nombre del esquema ser tambin el nombre del usuario.Cuando creamos la base
de datos de Oracle, por defecto crea dos esquemas de ejemplo, para poder realizar nuestras
pruebas.
Esquemas
Estos esquemas son los siguientes:
Esquema HR
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
2
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
Esquema SCOTT
Conexiones
Realice las conexiones para los esquemas Scott y HR con los parmetros que se muestran:
HR
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
3
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
SCOTT
Si ya los haba hecho, haga caso omiso de este apartado. Si tiene instalada la versin express de
Oracle, consulte el Anexo.
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
4
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
b) Consultar el catlogo de HR
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
5
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
Como ejemplo ilustrativo consultemos la estructura de la tabla EMP del esquema SCOTT:
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
6
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
Fundamentos de SQL
Definiciones
Data Manipulation Language (DML)
Usado para acceder, crear, modificar, o eliminar data en una estructura de base de datos
existente.
Usado para crear, modificar, o eliminar objetos de base de datos y sus privilegios.
Transaction Control
Session Control
System Control
Number NUMBER
Date and time DATE, TIMESTAMP, TIMESTAMP WITH TIME ZONE, TIME STAMP WITH LOCAL TIME
ZONE, INTERVAL YEAR TO MONTH, INTERVAL DAY TO SECOND
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
7
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
Operadores y Literales
Operadores Aritmticos
Operador de Concatenacin
Ejemplo Resultado
Operadores de Conjuntos
Operador Propsito
UNION Retorna todas las filas de cada consulta; no las filas duplicadas.
Retorna todas las filas de cada consulta, incluyendo las filas duplicadas.
UNION ALL
no las filas duplicadas
MINUS Retorna las filas distintas que son retornadas por la primera consulta pero
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
8
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
Precedencia de Operadores
2 */ Multiplicacin, divisin
Literales
Son valores que representan un valor fijo. Estos pueden ser de cuatro tipos diferentes:
CEPS-UNI
28-JUL-2006
45
Entero
-345
25
Nmero -456.78
15E-15
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
9
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
Consultas Simples
Nota: Las siguientes consultas se realizaran sobre el esquema HR a no ser que se indique lo
contrario
Seleccionando Columnas
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
10
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
11
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
La Tabla DUAL
Igualdad ( = )
Script: select first_name || ' ' || last_name, department_i d from employees where department_i d = 90;
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
12
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
Script : select first_name || ' ' || last_name, commission_pct from employees where commission_pct
<> .35;
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
13
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
Script : select first_name || ' ' || last_name, commission_pct from employees where commission_pct <
.15;
Script : select first_name || ' ' || last_name, commission_pct from employees where commission_pct >
.35;
Script : select first_name || ' ' || last_name, commission_pct from employees where commission_pct
<= .15;
Script : select first_name || ' ' || last_name, commission_pct from employees where commission_pct
>= .35;
ANY SOME
Script: select first_name || ' ' || last_name, department_i d from employees where department_i d <=
ANY (10,15,20,25);
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
14
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
ALL
Script: select first_name || ' ' || last_name, department_i d from employees where department_i d >=
ALL (80,90,100);
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
15
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
NOT
Script select first_name, department_id from employees where not (department_i d >= 30);
AND
j) Mostrar nombre y salario de los empleados que se apellidan Smith y ganan mas
de 75000.
Script: select first_name, salary from employees where last_name = 'Smith' and salary > 7500;
OR
Script: select first_name, last_name from employees where first_name = 'Kelly' or last_name =
'Smith';
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
16
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
IN y NOT IN
Script: select first_name, last_name, department_i d from employees where department_id in (10, 20,
90);
Script: select first_name, last_name, department_id from employees where department_i d not in (10,
30, 40, 50, 60, 80,90, 100,110);
BETWEEN
n) Mostrar nombre, apellido y salario de los empleados que ganan entre 5000 y
6000.
Script: select first_name, last_name, salary from employees where salary between 5000 and 6000;
EXISTS
Script: select first_name, last_name, department_i d from employees e where exists (select 1 from
departments d where d.department_i d = e.department_i d and d.department_name =
'Administration');
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
17
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
LIKE
Script: select first_name, last_name from employees where first_name like 'Su% ' and last_name not
like 'S% ';
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
18
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
Script: select first_name, last_name from employees where department_i d = 90 order by first_name;
Script: select first_name || ' ' || last_name "Employee Name" from employees where department_i d =
90 order by last_name;
Script: select first_name, hire_date, salary, manager_id mid from employees where department_i d in
(110,100) order by mid asc, salary desc, hire_date;
Script: select distinct 'Region ' || region_id from countries order by 'Region ' || region_id;
Script: select first_name, hire_date, salary, manager_id mid from employees where department_i d in
(110,100) order by 4, 2, 3;
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
19
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
Script: select last_name, commission_pct from employees where last_name like 'A% ' order by
commission_pct asc;
Script: select last_name, commission_pct from employees where last_name like 'A% 'order by
commission_pct asc nulls first;
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
20
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
La Expresin CASE
Caso 1
Formato
CASE <expresin>
.. .
END
Script: select country_name, region_id, case region_id when 1 then 'Europa' when 2 then 'America'
when 3 then 'Asia' else 'Otro' end as continente from countries where country_name like 'I% ';
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
21
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
Caso 2
Formato
CASE
.. .
END
end as Categora
from employees
order by first_name;
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
22
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
23
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
Anexo.
Este anexo ha sido hecho para que se pueda desarrollar las guas de Oracle sobre los esquemas HR
y SCOTT para la base de datos xe (Express Edition)
1. Re activar el usuario hr
Conctese al usuario administrador, y dentro de este digite los comandos que se le muestran
Cierre la conexin al administrador y haga una conexin nueva a Scott con los parmetros
siguientes:
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
24
Universidad de El Salvador BAD-115 Gua de Laboratorio #3
Ing. Elmer Arturo Carballo Ruiz MSc. / Ing. Cesar Augusto Gonzlez Rodrguez MAF
Ciclo II-2012
25