jueves, 14 de junio de 2012

set serveroutput on
Create Table Cliente( --Creacion de la tabla cliente
 NumCliente varchar2(4) not null,
 Nombre varchar2(30) ,
 Apellido varchar2(30),
 Direccion varchar2(35),
 Telefono varchar2(15) ,
 TipoPref varchar2(25) ,
 MaxRent Float,
 Constraint PK_CLIENTE PRIMARY KEY (NumCliente)
 );

 --Ahora insertamos registros

 begin
 insert into cliente values('CR78' , 'Juan' , 'Kayser', 'Alameda Numero 23' , '9898765' , 'Dpto' , '100000');
 insert into cliente values('CR79' , 'Miguel' , 'Sepulveda', 'Mar de Drake N°69' , '69696969' , 'Casa' , '1000000');
 insert into cliente values('CR80' , 'Gerald Mauricio' , 'Caris', 'Bellavista N° 23' , '98765432' , 'Dpto' , '900000');
 insert into cliente values('CR81' , 'Enrique' , 'Mimó', 'San Ramon N°314' , '99999999' , 'Parcela' , '1000000');
 END;

 select *from Cliente; --Con este select se ve los ingresos a la Tabla Cliente

 ---Procedimiento Ver Cliente

 Create or Replace Procedure verCliente (pNumCliente varchar2)IS
   vNumCliente Cliente.numCliente%TYPE;
   vNombre Cliente.nombre%TYPE;
   vApellido Cliente.apellido%TYPE;
   BEGIN
     select numCliente, nombre, apellido
     into vNumCliente, vNombre, vApellido
     from Cliente
      where numCliente=pNumCliente;
      DBMS_OUTPUt.PUT_LINE('NumCliente : ' ||vNumCliente || ' Nombre : ' ||trim(vNombre) || ' Apellido : ' ||vApellido);
      Exception
         When no_data_found then
            DBMS_OUTPUT.PUT_LINE( ' NO SE ENCUENTRA EL CLIENTE ' || pNumCliente );
            END;
           
 ----EL PROCEDIMIENTO verCliente me compilo, pero me arroja un Warning....
 show errors;
 --Para invocar a un procedimiento:

 begin
    verCliente('CR79');
    END;
   
    show errors;

lunes, 4 de junio de 2012

Clase 02 de Junio Script Trigger Compra

create table Compra (
idCompra integer,
fecha date
);

alter table Compra
modify idCompra integer not null;

alter table Compra
add constraint PK_Compra primary key (idcompra);

Create table DetalleCompra(
idCompra integer,
idProducto integer,
cantidad integer
);

alter table DetalleCompra
add constraint fk_Detalle_Compra foreign key (idCompra)
referencing Compra (idcompra);

alter table DetalleCompra
add constraint fk_Detalle_Producto foreign key (idProducto)
referencing Producto (idProducto);


Create table Producto(
idProducto integer,
nombreProducto varchar2(30),
stockActual integer
);

alter table producto
add constraint pk_Producto primary key (idProducto);

create sequence sqCompra;

insert into compra values (sqCompra.nextval, sysdate);
insert into compra values (sqCompra.nextval, sysdate);
insert into compra values (sqCompra.nextval, sysdate);

select * from compra;

create sequence sqProducto
START WITH 10
INCREMENT BY 10;

insert into Producto values (sqProducto.nextval, 'Pantalla Led 60 "', 100);
insert into Producto values (sqProducto.nextval, 'HD 6TB', 1000);
insert into Producto values (sqProducto.nextval, 'Radio Sony xTr', 10);
insert into Producto values (sqProducto.nextval, 'Moto Kawasaky', 20);

select * from Producto
select * from Compra

insert into detalleCompra values (1, 10, 2);
insert into detalleCompra values (1, 40, 1);
insert into detalleCompra values (1, 50, 5);

Clase 02 de Junio Trigger

-- trigger 1er ejercicio diapo trigger

create or replace trigger tr_ActualizaStock
after insert on DetalleCompra
for each row
begin
  update Producto
  set stockActual = stockActual + :new.Cantidad
  where idProducto = :new.idProducto
end trActualizaStock;

jueves, 31 de mayo de 2012

Clase 30 de Mayo

create table Autor(
idAutor int,
nombre varchar(30),
constraint Pk_idAutor primary key (idAutor) 
);

begin
insert into Autor values(7);
insert into Autor VALUES(1);
insert into Autor VALUES(2);
insert into Autor VALUES(3);
insert into Autor VALUES(4);
insert into Autor VALUES(5);
end;

create table Libro(
IdLibro integer,
IdAutor integer,
constraint Pk_idLibro primary key (IdLibro),
constraint Fk_IdAutor foreign key (IdAutor) references Autor
);

begin
insert into Libro values(1, 1);
insert into Libro values(2, 1);
insert into Libro values(3, 2);
insert into Libro values(4, 3);
end;

create sequence seqAutor
start with 1 increment by 10
nocache
nocycle;

create or replace trigger MiPrimerTrigger
after delete on Autor
for each row
begin
  DELETE FROM Libro
  WHERE IdAutor = :old.IdAutor;
end;

begin
delete from Autor
where idAutor = 1;
end;

select * from Libro;

domingo, 27 de mayo de 2012

26 de Mayo Ejercicio 1 Solemne 2

-- ejercicio 1 solemne 2
set serveroutput on;
Declare
Cursor Ldepto is
select d.department_id, department_name, count(employee_id) as total
from employees e, departments d
where e.department_id = d.department_id
group by d.department_id, department_name
order by department_name;
msg char(1);
cnt integer := 0;
Begin
For reg in Ldepto Loop --el reg toma el department_id, department_name y el total, del cursor Ldepto
  if mod(reg.total,2) = 0 then
    msg := '*';
    cnt := cnt+1;
  else
    msg := '';
  End if;
  dbms_output.put_line(reg.department_id || ' ' || reg.department_name || ' ' || reg.total || ' ' || msg);
End Loop;
  dbms_output.put_line('El total de departamentos con cantidad de empleados PAR es: ' || cnt);
End;

sábado, 26 de mayo de 2012

Clase 26 de Mayo

-- El Cursor

set serveroutput on
declare
cursor crCuentaEmpleados is
  select department_id, department_name, count(*) as total
  from employees natural join departments
  group by department_id, department_name
  order by department_name;
msg char(1);
cnt integer := 0;
begin
  for recCur in crCuentaEmpleados loop
      if recCur.total mod 2 = 0 then
         msg := '*';
         cnt := cnt + 1;
      else
        msg := ' ';
      end if;
      dbms_output.put_line (recCur.department_id||' '||recCur.department_name||' '||recCur.total||' '||msg);
  end loop;
  dbms_output.put_line ('El Total de departamentos con cantidad de empleados par es: '||cnt);
exception
when others then
  dbms_output.put_line ('Error desconocido');
end;


-- El Procedimiento
Create or Replace PROCEDURE actualizaFono (pid Employees.employee_id%type, pfono Employees.phone_number%type) is

cnt integer;
EMPLEADO_NO_EXISTE EXCEPTION;

begin

   select count(*) into cnt
   from employees
   where employee_id = pid;
   if cnt = 0 then
      raise EMPLEADO_NO_EXISTE;
   end if;

   update employees
   set phone_number = pfono
   where employee_id = pid;
   dbms_output.put_line ('Empleado '||pid||' Actualizado');
  
exception

    when EMPLEADO_NO_EXISTE then
        dbms_output.put_line ('Empleado '||pid||' No existe');

    when others then
        dbms_output.put_line ('Error desconocido');   
       
end;

-- La funcion

CREATE OR REPLACE FUNCTION CUENTAEMPLEADOS
(
  pDepto IN employees.department_id%type 
) RETURN NUMBER AS

totalEmpleados integer;
BEGIN
  select count(*) into totalEmpleados
  from Employees
  where department_id = pDepto;
  RETURN totalEmpleados;
END CUENTAEMPLEADOS;

viernes, 18 de mayo de 2012

Clase 16 de Mayo

create or replace FUNCTION parImpar (pNumero integer) return varchar2 is
msg varchar2(5):='imPar';
begin
if mod(pnumero,2) = 0 then
   msg := 'Par';
end if;
return msg;
end;
drop procedure concatenar
create or replace FUNCTION concatenar (ptexto1 varchar2,ptexto2 varchar2 ) return varchar2 is
begin
return ptexto1 ||' '||ptexto2;
end;
select concatenar ('7',parimpar(7)) from dual
declare
var varchar2(10);
begin
 var := parimpar(7);
 dbms........
create or replace PROCEDURE Concatenar (ptexto1 varchar2, ptexto2 varchar2) is
begin
  dbms_Output.put_line(ptexto1||' ' ||ptexto2);
end Concatenar;
show err procedure Concatenar
set serveroutput on
Declare
 vtexto varchar2(50) := 'Hola a Todos';
Begin
  Concatenar('Buenos','Dias');
  Concatenar(ptextitoo2=>'Buenos',ptexto1=>'Dias');
End;