Está en la página 1de 6

--1.

-
Create Table Modalidad_estudio
(
modalidadID int PRIMARY KEY,
descripción_modalidad character varying(100) not null
);
Create Table Tipo_grado
(
tipoID int PRIMARY KEY,
descripción_tipo character varying(100) not null
);
Create Table Grado_docente
(
key_grado int PRIMARY KEY,
deocenteID int not null references Docente,
modalidadID int not null references Modalidad_estudio,
tipoID int not null references Tipo_grado,
universidadID int not null references Universidad,
mencion_grado character varying(200) not null,
situacion character(1) not null,
año_ingreso int not null,
año_egreso int null
);

Alter table Grado_docente add descripcion_maestria character varying(200) not null;

--Alter table Grado_docente add CHECK(situacion in ('0','1'))

--Alter table Grado_docente drop constraint grado_docente_situacion_check

Alter table Grado_docente add CHECK(situacion in ('0','1','2'))

Alter table Grado_docente add CHECK(año_egreso>año_ingreso)

Select * from Grado_docente

--2.-
--ALTER TABLE Grado_docente RENAME COLUMN deocenteid TO docenteid

Select * from docente

Select * from Modalidad_estudio


insert into Modalidad_estudio values(10,'Presencial');
insert into Modalidad_estudio values(20,'Semi Presencial');
insert into Modalidad_estudio values(30,'Virtual');

Select * from Tipo_grado


insert into Tipo_grado values(1,'Bachiller');
insert into Tipo_grado values(2,'Maestro');
insert into Tipo_grado values(3,'Doctor');

select * from universidad

Alter table Grado_docente add año_grado int not null

Insert into
Grado_docente(key_grado,docenteid,modalidadid,universidadid,mencion_grado,situacion
,año_ingreso,
año_egreso,año_grado,descripcion_maestria,tipoID)
values(1,1,10,1,'Bachiller en Ciencias de Ingeniería de
sistemas','2',1993,1999,2000,'Ingenieria de sistemas',1)

--ALTER TABLE Grado_docente ALTER COLUMN año_grado DROP NOT NULL

Insert into
Grado_docente(key_grado,docenteid,modalidadid,universidadid,mencion_grado,situacion
,año_ingreso,
año_egreso,año_grado,descripcion_maestria,tipoID)
values(2,1,10,2,'Maestria en Educacion','1',2017,2019,Null,'Informatica Educativa y
TIC',2)

--3.-
Select * from grupo where semestreid='2019-2' and cursoid=29 and denominacion='A'
--10007
Update Grupo set docente_id=19 where grupoid=10007

Select * from horario where grupo_id=10007


--21, Jueves de 7 a 10 en el 105(11) --> Lunes de 18 a 21 en el 105
--22, Sabado de 7 a 9 en el 418(2) --> Viernes de 19 a 21 en el 418(2)

Select * from ambiente

update Horario
set dia='Lunes', h_inicio='18:00', h_fin='21:00'
where horarioid=21

update Horario
set dia='Viernes', h_inicio='19:00', h_fin='21:00', ambienteid=2
where horarioid=22

--4.-
Select * from Matricula

Alter table Matricula add UNIQUE(alumnoid,semestreid)


/*
Select alumnoid,semestreid,count(*)
from matricula
group by alumnoid,semestreid
having count(*)>1
*/
Alter table Horario add CHECK(dia in
('Lunes','Martes','Miércoles','Jueves','Sábado','Viernes','Domingo'))
/*
Select distinct dia
from horario
order by 1
*/

Select * from matricula

Alter table Matricula add h_registro time null

ALTER TABLE Matricula ALTER COLUMN f_registro SET DEFAULT current_date

ALTER TABLE Matricula ALTER COLUMN h_registro SET DEFAULT current_time

--26-09-2019
--FUNCIONES EN POSTGRESQL

--Procedimiento: No retiorna valor


Create or replace function n_funcion(parm1, param2, ..., paramN) returns VOID as
$$
Declare
var1 tipo1; var2 int=10;
Begin
--Intrucciones
End;
$$ language 'plpgsql'

--Funcion: Si retorna valor


Create or replace function n_funcion(parm1, param2, ..., paramN) returns tipo_dato
as
$$
Declare
var1 tipo1; var2 int=10;
Begin
--Intrucciones
return variable;
End;
$$ language 'plpgsql'

Select * from Modalidad_Estudio

--Insert
--V1
Create or replace function fn_insert_modalidad_estudio(codigo int,descr character
varying)
returns VOID as
$$
Declare

Begin
insert into Modalidad_estudio(modalidadid,descripción_modalidad)
values(codigo,descr);
End;
$$ language 'plpgsql'

--V2
Create or replace function fn_insert_modalidad_estudio(descr character varying)
returns VOID as
$$
Declare
cod int; num_filas int;
Begin
select count(*) into num_filas from Modalidad_estudio;
--Generar el valor para el modalidadID
if num_filas=0 then
cod=10;
else
Select max(modalidadid)+10 into cod from Modalidad_estudio;
end if;
insert into Modalidad_estudio(modalidadid,descripción_modalidad)
values(cod,descr);
End;
$$ language 'plpgsql'
-----------------------------------------------------------------------------------
-------
try{
--Instrucciones
}catch(Exception e){
--Manejo del error
}
--Eliminando una funcion
drop function fn_insert_modalidad_estudio(character varying)

--V3
Create or replace function fn_insert_modalidad_estudio(descr character varying)
returns boolean as
$$
Declare
cod int; num_filas int;
Begin
select count(*) into num_filas from Modalidad_estudio;
--Generar el valor para el modalidadID
if num_filas=0 then
cod=10;
else
Select max(modalidadid)+10 into cod from Modalidad_estudio;
end if;
insert into Modalidad_estudio(modalidadid,descripción_modalidad)
values(cod,descr);
return true;
Exception
when others then return false;
End;
8$$ language 'plpgsql'

select fn_insert_modalidad_estudio('Modalidad 3')

select * from Modalidad_estudio

--Funcion para eliminar


Create or replace function fn_delete_modalidad_estudio(cod int)
returns boolean as
$$
Declare
Begin
delete from Modalidad_estudio where modalidadid=cod;
return true;
Exception
when others then return false;
End;
$$ language 'plpgsql'

select fn_delete_modalidad_estudio(20)

--Funcion para modificar


Create or replace function fn_update_modalidad_estudio(cod int, nom character
varying)
returns boolean as
$$
Declare
Begin
update Modalidad_estudio set descripción_modalidad=nom where modalidadid=cod;
return true;
Exception
when others then return false;
End;
$$ language 'plpgsql'

select fn_update_modalidad_estudio(10,'Modalidad Tele presencial')

select * from Modalidad_estudio

delete from Modalidad_estudio where modalidadid=30;


--delete from grado_docente
--delete from modalidad_estudio
--Update

--Eliminar

select max(modalidadid)+10 from Modalidad_estudio


select count(*) from Modalidad_estudio

--ESTRUCTURAS DE CONTROL EN POSTGRESQL


--Estructura condicional
if <condicion> then
instrucciones
else
instrucciones
end if;

--Estructura selectiva: Lo que en Java es el switch()


case variable
when valor_1 then instrucciones;
when valor_2 then instrucciones;
...
else instrucciones;
end case;
--drop function fn_nombre_mes(int)

Create or replace function fn_nombre_mes(num_mes int) returns character varying as


$$
Declare
nom_mes character varying;
Begin
case num_mes
when 1 then nom_mes='Enero';
when 2 then nom_mes='Febrero';
when 3 then nom_mes='Marzo';
when 4 then nom_mes='Abril';
when 5 then nom_mes='Mayo';
when 6 then nom_mes='Junio';
when 7 then nom_mes='Julio';
when 8 then nom_mes='Agosto';
when 9 then nom_mes='Setiembre';
when 10 then nom_mes='Octubre';
when 11 then nom_mes='Noviembre';
when 12 then nom_mes='Diciembre';
else nom_mes='Numero de mes invalido';
end case;
return nom_mes;
end;
$$ language 'plpgsql';

select fn_nombre_mes(30)

--ESTRUCTURAS REPETITIVAS
loop, while, for

--Loop
LOOP
Instrucciones;
EXIT when <Condicion>;
END LOOP;

LOOP
EXIT when <Condicion>;
Instrucciones;
END LOOP;

--Sumar los N primeros numeros


fn_e01(6)

Create or replace FUNCTION fn_ej01(numero int) returns int as


$$
Declare
contador int=1; suma int=0;
Begin
LOOP
EXIT when contador>numero;
suma=suma+contador;
contador=contador+1;
END LOOP;
return suma;
end;
$$ language 'plpgsql';

select fn_ej01(2)

También podría gustarte