Vous êtes sur la page 1sur 5

CREACIN DE TABLAS MUCHOS A MUCHOS CON REGLAS DE INTEGRIDAD ENTRE ALUMNOS Y CURSOS

CREATE TABLE alUmno (


id_alUmno NUMBER(10) NOT NULL,
nom_alUmno varchar(255) NOT NULL,
CONSTRAINT alUmno_pk PRIMARY KEY(id_alUmno)
);
CREATE TABLE cUrso (
id_cUrso NUMBER(10) NOT NULL,
nom_cUrso varchar(255) NOT NULL,
CONSTRAINT cUrso_pk PRIMARY KEY(id_cUrso)
);
CREATE TABLE alUcUr (
id_alUmno NUMBER(10) NOT NULL,
id_cUrso NUMBER(10) NOT NULL,
nota NUMBER(10),
CONSTRAINT alUcUr_pk PRIMARY KEY(id_alUmno,id_cUrso),
CONSTRAINT id_alUmno_fk FOREIGN KEY(id_alUmno)
REFERENCES alUmno(id_alUmno),
CONSTRAINT id_cUrso_fk FOREIGN KEY(id_cUrso)
REFERENCES cUrso(id_cUrso)
);
insert into alUmno values(1,'hUgo');
insert into alUmno values(2,'paco');
insert into alUmno values(3,'lUis');
insert into cUrso values(88,'fisi');
insert into cUrso values(99,'mate');
insert into alUcUr values(1,99,12);
insert into alUcUr values(1,88,15);
insert into alUcUr values(2,99,11);
insert into alUcUr values(2,88,18);
insert into alUcUr values(3,99,11);
insert into alUcUr values(3,88,18);
SELECT alUmno. nom_alUmno, alUcUr. nota
FROM alUmno, alUcUr
WHERE alUmno. id_alUmno = alUcUr. id_alUmno
ORDER BY alUmno. nom_alUmno;
EN PL SQL: BLOQUE QUE INGRESE UN NMERO POR TECLADO E IMPRIMA CUANTOS EMPLEADOS ESTN EN UN RANGO DEL NMERO
INGRESADO MS MENOS 500. TENER EN CUENTA EL MANEJO DE EXCEPCIONES:
A) SI NO HAY NINGUNO DENTRO DEL RANGO
B) SI HAY ALGUNO DENTRO DE RANGO
C) MANEJAR/INDICAR OTRO(S) ERROR(ES)
SET verify off
var g_mensaje varchar2(40);
accept g_salario prompt 'ingrese salario: ';
DECLARE
ex_ninguno exception;
ex_alguno exception;
v_num employees.employee_id%type;
BEGIN
select count(*)
into v_num
from employees
where salary between &g_salario - 500
and &g_salario + 500;
IF v_num >=1 then
raise ex_alguno;
ELSE
raise ex_ninguno;
END IF;

EXCEPTION
when ex_alguno then
:g_mensaje := 'empleados en el rango: '||to_char(v_num);
when ex_ninguno then
:g_mensaje := 'SIN empleados en el rango';
when others then
:g_mensaje:='error '||sqlcode||' '||sqlerrm;
END;
/
PRINT g_mensaje;
DEVOLVER LA SUMA DE SALARIOS DEL DEPARTAMENTO 60
DECLARE
v_sum_sal NUMBER(10,2);
v_dep_no NUMBER NOT NULL := 60;
BEGIN
SELECT SUM(salary)
INTO v_sum_sal FROM employees
WHERE department_id = v_dep_no;
DBMS_OUTPUT.PUT_LINE('la sUma es ' || v_sum_sal);
END;
/
USO DE PARMETROS DE ENTRADA Y DE SALIDA
CREATE OR REPLACE PROCEDURE query_emp
(p_id IN employees.employee_id%TYPE,
p_name OUT employees.last_name%TYPE,
p_salary OUT employees.salary%TYPE) IS
BEGIN
SELECT last_name, salary INTO p_name, p_salary
FROM employees
WHERE employee_id = p_id;
END query_emp;
/
DECLARE
v_emp_name employees.last_name%TYPE;
v_emp_sal employees.salary%TYPE;
BEGIN
query_emp(171,v_emp_name,v_emp_sal);
DBMS_OUTPUT.PUT_LINE(v_emp_name || ' gana ' ||
TO_CHAR(v_emp_sal,'$999,999.00') );
END;
/
USO DE PARAMETRO DE ENTRADA Y DE SALIDA (A LA VEZ)
CREATE OR REPLACE PROCEDURE fto_telef
(p_nro_telef IN OUT VARCHAR2) IS
BEGIN
p_nro_telef := '(' || SUBSTR(p_nro_telef,1,3) ||
')' || SUBSTR(p_nro_telef,4,3) ||
'-' || SUBSTR(p_nro_telef,7);
END fto_telef;
/
VARIABLE b_nro_telef VARCHAR2(15);
EXECUTE :b_nro_telef :='051999340530'
PRINT b_nro_telef;
BEGIN
fto_telef(:b_nro_telef);
END;
/

SALARIO PROMEDIO DEL PERSONAL EN UN PAIS (PUEDEN TRABAJAR EN DISTINTOS DEPARTAMENTOS


select avg(salary), country_id from
(select salary, department_id, location_id, country_id
from employees natural join departments natural join locations)
group by country_id;
IDENTIFICAR LOS EMPLEADOS QUE TRABAJAN EN LOS DEPARTAMENTOS UBICADOS EN REINO UNIDO.
select last_name from employees where department_id in
(select department_id from departments where location_id in
(select location_id from locations where country_id =
( select country_id from countries
where country_name = 'United Kingdom'
)
)
);
DEVOLVER EL NOMBRE DE CADA EMPLEADO JUNTO CON EL NOMBRE DEL JEFE DEL EMPLEADO.
SELECT e1.last_name||' trabaja para '||e2.last_name
from employees e1, employees e2
where e1.manager_id = e2.employee_id
and e1.last_name like 'R%'
order by e1.last_name;
TODOS LOS ID_DEPARTMENT Y EL APELLIDO DE EMPLEADO QUE EN TRABAJAN
SELECT d.department_id, e.last_name
from departments d left outer join employees e
on d.department_id = e.department_id
order by d.department_id, e.last_name;
SELECT d.department_id as d_id_dpto, e.department_id as e_id_dpto, e.last_name
from departments d full outer join employees e
on d.department_id = e.department_id
order by d.department_id, e.last_name;
LISTAR LOS EMPLEADOS QUE NO ESTAN EN LOS DEPARTAMENTOS DE LOCATION_ID = 1700
SELECT * from employees
where department_id not in
(select department_id from departments
where location_id = 1700)
order by last_name;
SUBCONSULTAS:
NOMBRE, SALARIO Y DEPARTAMENTO EN EL QUE TRABAJAN LOS EMPLEADOS CON EL MENOR SALARIO.
select first_name, salary, department_id
from employees
where salary = (select min(salary)
from employees);
TRAER DETALLES DE LOS JOBS DONDE EL SALARIO MINIMO ES > 10000
select * from jobs where min_salary > 10000;
RECUPERAR EL NOMBRE Y LA FECHA DE INGRESO DE LOS EMPLEADOS QUE INGRESARON ENTRE EL 2002 Y EL 2005
select first_name,hire_date from employees
where to_char(hire_date,'YYYY') between 2002 and 2005
order by hire_date;
TRAER JOB_TITLE, LA DIFERENCIA ENTRE LOS SALARIOS EXTREMOS PARA LOS TRABAJOS CON SALARIO MAXIMO EN EL RANGO DE
10000 A 20000
select job_title, max_salary - min_salary diferenciv
from jobs
where max_salary between 10000 and 20000;

LISTAR EL NOMBRE, SALARIO Y EL SALARIO REDONDEADO A MILES DE EMPLEADOS


select first_name,salary,round(salary,-3)
from employees;
VISUALIZAR EMPLEADOS CUYO NOMBRE O APELLIDO EMPIECE POR J
select first_name, last_name
from employees
where first_name like 'J%' or last_name like 'J%';
IMPRIMIR EL NOMBRE Y LA EXPERIENCIA DE LOS EMPLEADOS
select first_name, hire_date, floor((sysdate-hire_date)/365)
from employees;
TRAER LOS EMPLEADOS QUE HAN INGRESADO A TRABAJAR EN EL MES DE MAYO
select last_name,hire_date from employees
where to_char(hire_date,'MON')='MAY';
VISUALIZAR EL NMERO DE DIAS TRANSCURRIDOS ENTRE LA FECHA DEL SISTEMAS Y EL 1 DE ENERO DE 2011
select sysdate - to_date('01-jan-2011') from dual;
TRAER LOS JEFES Y LA CANTIDAD DE SUBORDINADOS
select manager_id, count(*) from employees
group by manager_id;
ID DE EMPLEADO, SALARIO Y ID DE DEPARTAMENTO DE LOS EMPLEADOS CON MAYOR AL PROMEDIO DE LOS EMPLEADOS DE SU
DEPARTAMENTO)
SELECT employee_id,salary,department_id
from employees E
where salary >
(select avg(salary)
from employees T
where E.department_id=T.department_id);
DETALLES HISTORICOS DE LOS EMPLEADOS PARA AQUELLOS CUYO SALARIO ACTUAL ESTA EN EL RANGO DE 1000 Y 2000; Y TRABAJA
EN DEPARTAMENTO EN EL RANGO 10 Y 20
SELECT E.first_name, H.job_id, E.salary,H.start_date
FROM job_history H, employees E
where (E.salary, H.department_id) IN
(select salary, department_id
from employees
where salary between 100 and 20000
and department_id between 10 and 20);
MOSTRAR LOS EMPLEADOS QUE NO TIENEN SUBORDINADOS
SELECT emp.last_name from employees emp
where emp.employee_id NOT IN
(select mgr.manager_id
from employees mgr
where mgr.manager_id is NOT NULL);
MOSTRAR LOS EMPLEADOS QUE TIENEN SUBORDINADOS
SELECT emp.last_name from employees emp
where emp.employee_id IN
(select mgr.manager_id
from employees mgr);
SELECCIONAR LA CANTIDAD DE EMPLEADOS POR DEPARTAMENTO.
select count(employee_id),department_name from hr.employees e join hr.departments d on e.department_id = d.department_id group by
(d.department_name)
CANTIDAD DE EMPLEADOS POR CIUDAD.
select count(employee_id),city from (hr.employees e join hr.departments d on e.department_id = d.department_id) join hr.locations l on d.location_id =
l.location_id group by (l.city)

CANTIDAD DE DEPARTAMENTOS POR PAS.


select count(department_id),country_name from (hr.departments d join hr.locations l on d.location_id = l.location_id) join hr.countries c on l.country_id =
c.country_id group by (c.country_name)
MUESTRA LA CANTIDAD DE EMPLEADOS POR REGIN
select count(employee_id),region_name from(((hr.employees e join hr.departments d on e.department_id = d.department_id)join hr.locations l on
d.location_id = l.location_id)joinhr.countries c on l.country_id = c.country_id) joinhr.regions r on c.region_id = r.region_id group by (r.region_name) Muestra
la cantidad de empleados por pas select count(employee_id),country_name from(((hr.employees e join hr.departments d on e.department_id =
d.department_id)join hr.locations l on d.location_id = l.location_id)joinhr.countries c on l.country_id = c.country_id) joinhr.regions r on c.region_id =
r.region_id group by (c.country_name)
CANTIDAD DE EMPLEADOS POR PAS Y POR REGIN
select region_name,country_name,count(employee_id)from
(((hr.employees e join hr.departments d on e.department_id = d.department_id)join
hr.locations l on d.location_id = l.location_id)join
hr.countries c on l.country_id = c.country_id) join
hr.regions r on c.region_id = r.region_id group by (c.country_name,r.region_name)