Parcial 1

Parcial 1

1.

Manejo de fechas 1:

Análisis de cada punto:


  • Que artículos fueron adquiridos antes del 6 mes del 2012.

Tabla: pagos 

Campo: descripcion, fechaAdquisicion

Condición: where fechaAdquisicion < '2012-06-01';

Comandos: select, from, where

Sintaxis: select descripcion from pagos where fechaAdquisicion < '2012-06-01';              


  • Determinar el articulo que tiene fecha mas antigua de adquisición.

Tabla: pagos

Campo: descripcion, fechaAdquisicion

Condición: where fechaAdquisicion = (select min(fechaAdquisicion) from pagos);

Comandos: select, from, where, min

Sintaxis: select descripcion from pagos where fechaAdquisicion = (select min(fechaAdquisicion) from pagos);


  • Seleccionar los artículos que terminan en en (o) y (m).

Tabla: pagos

Campo: descripcion, fechaAdquisicion

Condición: where descripcion like '%o' or descripcion like '%m';

Comandos: select, from, where, like, or

Sintaxis: select descripcion from pagos where descripcion like '%o' or descripcion like '%m';


  • Listar los productos que tienen al final consonante.

Tabla: pagos

Campo: descripcion

Condición: where descripcion regexp '[bcdfghjklmnpqrstvwxyz]$';

Comandos: select, from, where, regexp

Sintaxis: select descripcion from pagos where descripcion regexp '[bcdfghjklmnpqrstvwxyz]$';


  • Si las fechas de adquisición es en el primer semestre del año, se darán 5 meses para el pago

Tabla: pagos

Campo: descripcion, fechaAdquisicion

Condición: where fechaAdquisicion between '2012-01-01' and '2012-06-30';

Comandos: select, from, date_add, interval, as, where, between, and

Sintaxis: select descripcion, fechaAdquisicion, date_add(fechaAdquisicion, Interval 5 month) as nueva_fecha from pagos where fechaAdquisicion between '2012-01-01' and '2012-06-30';


  • Si las fechas de adquisición es en el segundo semestre del año, se darán 8 meses de plazo

Tabla: pagos

Campo: descripcion, fechaAdquisicion

Condición: where fechaAdquisicion between '2012-07-01' and '2012-12-31';

Comandos: select, from, date_add, interval, as, where, between, and

Sintaxis: select descripcion, fechaAdquisicion, date_add(fechaAdquisicion, Interval 8 month) as nueva_fecha from pagos where fechaAdquisicion between '2012-07-01' and '2012-12-31';


---------------------------------------------------------------------------------------------------

Manejo de fechas 2:

Analisis de cada punto:


  • Total de estudiantes nacidos en los años 70.

Tabla: estudiante

Campo: nombre, fechaNacimiento

Condición: where fechaNacimiento between '1970-01-01' and '1979-12-31';

Comandos: select, from, where, between, and

Sintaxis: select nombre from estudiante where fechaNacimiento between '1970-01-01' and '1979-12-31';


  • Consultar a los estudiantes que tienen entre 16 y 20 años.

Tabla: estudiante

Campo: nombre, fechaMatricula, fechaNacimiento

Condición: where year(fechaMatricula) - year(fechaNacimiento) between '16' and '20';

Comandos: select, from, where, between, and

Sintaxis: select nombre from estudiante where year(fechaMatricula) - year(fechaNacimiento) between '16' and '20';


  • Consultar los nombres de los estudiantes que son mayores de 25 años.

Tabla: estudiante

Campo: nombre, fechaMatricula, fechaNacimiento

Condición: where year(fechaMatricula) - year(fechanacimiento) > 25;

Comandos: select, from, where

Sintaxis: select nombre from estudiante where year(fechaMatricula) - year(fechanacimiento) > 25;


  • Calcular la edad de cada estudiante.

Tabla: estudiante

Campo: nombre, fechaMatricula, fechaNacimiento

Condición: No hay

Comandos: select, where, from

Sintaxis: select nombre, where year(fechaMatricula) - year(fechaNacimiento) 'edad' from estudiante;


  • Consultar las materias que tienen precio mayor a 300000 y sacar un mensaje que diga si pasa de 300000 cara de lo contrario económica.

Tabla: materia

Campo: descripcion, valor

Condición: if(valor>300000, 'cara', 'economica')

Comandos: select, if, as, from

Sintaxis: select descripcion, valor, if(valor>300000, 'cara', 'economica') as clasificacion from materia;


  • Agregar el campo edad a la tabla estudiante, insertar las edades respectivas y si el estudiante pasa de 30 años sacar un mensaje que diga hombre mayor, de lo contrario joven en desarrollo.

Tabla: estudiante

Campo: edad, nombre

Condición: if(edad>30, 'hombre mayor', 'joven en desarrollo')

Comandos: update, set, select, if, as, from

Sintaxis: update estudiante set edad = (select year(fechaMatricula) - year(fechaNacimiento));

select nombre, edad, if(edad>30, 'hombre mayor', 'joven en desarrollo')as clasificacion from estudiante;


  • Consultar las materias que tienen precio mayor a 200000 y menor igual a 300000, y sacar un mensaje que diga materia economica si esta entre 200000 y 300000 de lo contrario cara.

Tabla: materia

Campo: descripcion, valor

Condición: if(valor>200000 and valor<=300000, 'economica', 'cara')

Comandos: select, if, and, as, from

Sintaxis: select descripcion, valor, if(valor>200000 and valor<=300000, 'economica', 'cara') as clasificacion from materia;

---------------------------------------------------------------------------------------------------

Manejo de fechas 3:

Analisis de cada punto:


  • cuantas mujeres tiene su nombre terminado en z

Tabla: repaso

Campo: nombre, sexo

Condición: where sexo='mujer'

Comandos: select, count, from, where, and, like

Sintaxis: select count(*) from repaso where sexo='mujer' and nombre like '%z';


  • total de personas nacidas en los años 70's se

Tabla: repaso

Campo: nombre, fechaNacimiento

Condición: where fechaNacimiento between '1970-01-01' and '1979-12-31'

Comandos: select, from, where, between, and

Sintaxis: select nombre from repaso where fechaNacimiento between '1970-01-01' and '1979-12-31';


  • listar las personas con edad entre 25 y 30 años

Tabla: repaso

Campo: nombre, fechaNacimiento

Condición: between '25' and '30';

Comandos: select, from, where, between, and, current_date()

Sintaxis: select nombre from repaso where year(current_date()) - year(fechaNacimiento) between '25' and '30';


  • cuantos hijos existen en el total de datos

Tabla: repaso

Campo: nroHijos

Condición: no hay

Comandos: select, sum, from

Sintaxis: select sum(nroHijos) 'numero total de hijos' from repaso;


  • agrupar por numero de hijos

Tabla: repaso

Campo: nroHijos

Condición: no hay

Comandos: select, count, from, group by

Sintaxis: select nroHijos, count(*) from repaso group by nroHijos;


  • cuantas personas tiene el nombre carlos

Tabla: repaso

Campo: nombre

Condición: where nombre like 'carlos%'

Comandos: select, count, from, where, like

Sintaxis: select count(*) from repaso where nombre like 'carlos%';


  • total de mujeres menores o = de 22 años

Tabla: repaso

Campo: sexo, fechaNacimiento

Condición: where sexo ='mujer' and year(current_date()) - year(fechaNacimiento) <= 22;

Comandos: select, count, from, where, and, current_date()

Sintaxis: select count(*) from repaso where sexo ='mujer' and year(current_date()) - year(fechaNacimiento) <= 22;


  • total de hombre mayores de 30 años que tengan mas de 1 hijo

Tabla: repaso

Campo: sexo, nroHijos, fechaNacimiento

Condición: where sexo = 'hombre' and year(current_date()) - year(fechaNacimiento) >

30 and nroHijos > 1;

Comandos: select, count, from, where, and, current_date()

Sintaxis: select count(*) from repaso where sexo = 'hombre' and year(current_date()) - year(fechaNacimiento) >

30 and nroHijos > 1;


cuantas mujeres mayores de 25 años edad tiene entre 1 y 3 hijos

Tabla: repaso

Campo: sexo, fechaNacimiento, nroHijos

Condición: where sexo = 'mujer' and year(current_date()) - year(fechaNacimiento) > 25 and nroHijos between 1 and 3;

Comandos: select, count, from, where, and, current_date()

Sintaxis: select count(*) from repaso where sexo = 'mujer' and year(current_date()) - year(fechaNacimiento) > 25 and nroHijos between 1 and 3;


  • mostrar la fecha del sistema

Tabla: repaso

Campo: no hay

Condición: no hay

Comandos: select, now()

Sintaxis: select now();


  • Visualizar la hora actual del sistema

Tabla: repaso

Campo: no hay

Condición: no hay

Comandos: select,date_format, now()

Sintaxis: select date_format(now(),'%h:%i:%s %p');

---------------------------------------------------------------------------------------------------

Tabla con las 3 Formas normales:

  • Estudiante (id, nombre, edad, email)
  • Curso (id, nombre, crédito)
  • Inscripción (id, id_estudiante, id_curso, fecha_inscripción)

  • Procedimiento almacenado:

    delimiter //

    create procedure InscribirEstudiante(in est_id int, in cur_id int)

    begin

    insert into Inscripción (id_estudiante, id_curso, fecha_inscripción)

    values (est_id, cur_id, curdate());

    end //

    delimiter ;


    Trigger:

    create table Registro

    (id int auto_increment primary key,

    mensaje text,

    fecha timestamp default current_timestamp);


    delimiter //

    create trigger inscripcion

    after insert on inscripcion

    for each row

    begin

    insert into Registro (mensaje)

    values (concat('El estudiante ', new.id_estudiante, ' se inscribió en el curso ', new.id_curso));

    end //

    delimiter ;

    ---------------------------------------------------------------------------------------------------

    2.

    Auditoria Triggers:


    create table auditoria (

    id int auto_increment primary key,

    usuario varchar(50) not null,

    accion varchar(20) not null,

    fecha timestamp default current_timestamp,

    descripcion text not null

    );


    Trigger insertar:

    delimiter //

    create trigger auditoria_pagos_insert

    after insert on pagos

    for each row

    begin

    insert into auditoria (usuario, accion, descripcion)

    values (user(), 'insert',

    concat('se insertó el pago ', new.identi, ' con descripción "', new.descripcion, '" en ', new.fechaadquisicion));

    end;

    //

    delimiter ;


    Trigger Actualizar:

    delimiter //

    create trigger auditoria_pagos_update

    after update on pagos

    for each row

    begin

    insert into auditoria (usuario, accion, descripcion)

    values (user(), 'update',

    concat('se actualizó el pago ', old.identi, ' de "', old.descripcion, '" a "', new.descripcion, '" en ', new.fechaadquisicion));

    end;

    //

    delimiter ;


    Trigger Eliminar:

    delimiter //

    create trigger auditoria_pagos_delete

    after delete on pagos

    for each row

    begin

    insert into auditoria (usuario, accion, descripcion)

    values (user(), 'delete',

    concat('se eliminó el pago ', old.identi, ' con descripción "', old.descripcion, '" en ', old.fechaadquisicion));

    end;

    //

    delimiter ;


    Txt y base de datos




    ¡Crea tu página web gratis! Esta página web fue creada con Webnode. Crea tu propia web gratis hoy mismo! Comenzar