Creación de Tablas Muchos a Muchos Con Reglas de Integridad Entre Alumnos y Cursos

download Creación de Tablas Muchos a Muchos Con Reglas de Integridad Entre Alumnos y Cursos

of 5

Transcript of Creación de Tablas Muchos a Muchos Con Reglas de Integridad Entre Alumnos y Cursos

  • 8/18/2019 Creación de Tablas Muchos a Muchos Con Reglas de Integridad Entre Alumnos y Cursos

    1/5

    CREACIÓN 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));

    nsert into alUmno values(1,'hUgo');nsert into alUmno values(2,'paco');

    nsert into alUmno values(3,'lUis');nsert into cUrso values(88,'fisi');nsert into cUrso values(99,'mate');nsert into alUcUr values(1,99,12);nsert into alUcUr values(1,88,15);nsert into alUcUr values(2,99,11);nsert into alUcUr values(2,88,18);nsert into alUcUr values(3,99,11);nsert into alUcUr values(3,88,18);

    SELECT alUmno. nom_alUmno, alUcUr. notaFROM alUmno, alUcUrWHERE alUmno. id_alUmno = alUcUr. id_alUmno

    ORDER BY alUmno. nom_alUmno;

    EN PL SQL: BLOQUE QUE INGRESE UN NÚMERO POR TECLADO E IMPRIMA CUANTOS EMPLEADOS ESTÁN EN UN RANGO DEL NÚMENGRESADO MÁS MENOS 500. TENER EN CUENTA EL MANEJO DE EXCEPCIONES:

    A) SI NO HAY NINGUNO DENTRO DEL RANGOB) SI HAY ALGUNO DENTRO DE RANGOC) MANEJAR/INDICAR OTRO(S) ERROR(ES)

    SET verify offvar g_mensaje varchar2(40);accept g_salario prompt 'ingrese salario: ';

    DECLAREex_ninguno exception;ex_alguno exception;v_num employees.employee_id%type;

    BEGINselect count(*)into v_numfrom employeeswhere salary between &g_salario - 500and &g_salario + 500;

    F v_num >=1 thenraise ex_alguno;

    ELSEraise ex_ninguno;

    END IF;

  • 8/18/2019 Creación de Tablas Muchos a Muchos Con Reglas de Integridad Entre Alumnos y Cursos

    2/5

    EXCEPTIONwhen 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

    DECLAREv_sum_sal NUMBER(10,2);v_dep_no NUMBER NOT NULL := 60;

    BEGINSELECT SUM(salary)INTO v_sum_sal FROM employeesWHERE department_id = v_dep_no;DBMS_OUTPUT.PUT_LINE('la sUma es ' || v_sum_sal);

    END;/

    USO DE PARÁMETROS 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

    BEGINSELECT last_name, salary INTO p_name, p_salaryFROM employeesWHERE 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

    BEGINp_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;/

  • 8/18/2019 Creación de Tablas Muchos a Muchos Con Reglas de Integridad Entre Alumnos y Cursos

    3/5

    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_idfrom employees natural join departments natural join locations)group by country_id;

    DENTIFICAR 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_namefrom employees e1, employees e2where 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 TRABAJANSELECT d.department_id, e.last_namefrom departments d left outer join employees e

    on d.department_id = e.department_idorder by d.department_id, e.last_name;

    SELECT d.department_id as d_id_dpto, e.department_id as e_id_dpto, e.last_namefrom departments d full outer join employees e

    on d.department_id = e.department_idorder by d.department_id, e.last_name;

    LISTAR LOS EMPLEADOS QUE NO ESTAN EN LOS DEPARTAMENTOS DE LOCATION_ID = 1700

    SELECT * from employeeswhere department_id not in(select department_id from departmentswhere 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_idfrom employeeswhere salary = (select min(salary)

    from employees);

    TRAER DETALLES DE LOS JOBS DONDE EL SALARIO MINIMO ES > 10000select * from jobs where min_salary > 10000;

    RECUPERAR EL NOMBRE Y LA FECHA DE INGRESO DE LOS EMPLEADOS QUE INGRESARON ENTRE EL 2002 Y EL 2005select first_name,hire_date from employeeswhere to_char(hire_date,'YYYY') between 2002 and 2005order by hire_date;

    TRAER JOB_TITLE, LA DIFERENCIA ENTRE LOS SALARIOS EXTREMOS PARA LOS TRABAJOS CON SALARIO MAXIMO EN EL RANGO DE10000 A 20000select job_title, max_salary - min_salary diferencivfrom jobswhere max_salary between 10000 and 20000;

  • 8/18/2019 Creación de Tablas Muchos a Muchos Con Reglas de Integridad Entre Alumnos y Cursos

    4/5

    LISTAR EL NOMBRE, SALARIO Y EL SALARIO REDONDEADO A MILES DE EMPLEADOSselect first_name,salary,round(salary,-3)from employees;

    VISUALIZAR EMPLEADOS CUYO NOMBRE O APELLIDO EMPIECE POR Jselect first_name, last_namefrom employeeswhere first_name like 'J%' or last_name like 'J%';

    MPRIMIR EL NOMBRE Y LA EXPERIENCIA DE LOS EMPLEADOSselect first_name, hire_date, floor((sysdate-hire_date)/365)from employees;

    TRAER LOS EMPLEADOS QUE HAN INGRESADO A TRABAJAR EN EL MES DE MAYOselect last_name,hire_date from employeeswhere to_char(hire_date,'MON')='MAY';

    VISUALIZAR EL NÚMERO DE DIAS TRANSCURRIDOS ENTRE LA FECHA DEL SISTEMAS Y EL 1 DE ENERO DE 2011select sysdate - to_date('01-jan-2011') from dual;

    TRAER LOS JEFES Y LA CANTIDAD DE SUBORDINADOSselect manager_id, count(*) from employeesgroup by manager_id;

    D 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_idfrom employees Ewhere salary >

    (select avg(salary)from employees Twhere 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 TRABAJAEN DEPARTAMENTO EN EL RANGO 10 Y 20SELECT E.first_name, H.job_id, E.salary,H.start_dateFROM job_history H, employees Ewhere (E.salary, H.department_id) IN

    (select salary, department_idfrom employeeswhere salary between 100 and 20000and department_id between 10 and 20);

    MOSTRAR LOS EMPLEADOS QUE NO TIENEN SUBORDINADOSSELECT emp.last_name from employees empwhere emp.employee_id NOT IN

    (select mgr.manager_idfrom employees mgr

    where mgr.manager_id is NOT NULL);

    MOSTRAR LOS EMPLEADOS QUE TIENEN SUBORDINADOSSELECT emp.last_name from employees empwhere emp.employee_id IN

    (select mgr.manager_idfrom 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 =.location_id group by (l.city)

  • 8/18/2019 Creación de Tablas Muchos a Muchos Con Reglas de Integridad Entre Alumnos y Cursos

    5/5

    CANTIDAD DE DEPARTAMENTOS POR PAÍS.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 REGIÓNselect count(employee_id),region_name from(((hr.employees e join hr.departments d on e.department_id = d.department_id)join hr.locations l ond.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) Muesta cantidad de empleados por país 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 PAÍS Y POR REGIÓNselect region_name,country_name,count(employee_id)from(((hr.employees e join hr.departments d on e.department_id = d.department_id)joinhr.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,r.region_name)