domingo, 10 de julio de 2011

►Pauta de la 3ra Prueba.-

1) Desarrolle los comandos necesarios para crear la vista vistaPromedioRentaPropiedad que muestre para cada empleado el promedio de renta de las propiedades que administra: (15 Puntos)


create or replace view vistaPromedioRentaPropiedad as
select numempleado, avg(renta) as "Renta Promedio"
from Propiedad
group by numempleado




2) Indique el comando necesario para eliminar la vista vistaPromedioRentaPropiedad creada en la pregunta anterior. (5 Puntos)


drop view vistaPromedioRentaPropiedad;



3) Indique la instrucción para eliminar la referencia a la tabla Oficina desde la tabla Empleado
(La foreign key FK_EMPLEADO_REFERENCE_OFICINA) (10 puntos)



alter table Empleado
drop constraint FK_EMPLEADO_REFERENCE_OFICINA



4) Escriba la sentencia para eliminar la tabla Empleado junto a sus llaves foráneas.(10 Puntos)


drop table empleado cascade constraint



5) Indique el comando para actualizar la fecha de nacimiento (fechNac) del empleado ‘SL21’ a '01/07/2011' (10 puntos)


update empleado
set fechNac = '01/07/2011'
where numempleado = ‘SL21’



6) Indique el comando necesario para listar todos los empleados nacidos entre el año 1970 y 1980, ambas fechas inclusive. (10 puntos)


select *
from Empleado
where to_char(FechNac, 'YYYY') between '1950' and '1960'

select *
from Empleado
where fechnac >= '01/01/1950' and fechnac <= '31/12/1960'

domingo, 3 de julio de 2011

►Consultas SQL.-

1) Escriba el comando DDL que defina la llave foránea de EMPLEADO referenciando OFICINA 

Alter table EMPLEADO
add constraint fk_empleado_ref_Oficina
foreign key(NUMOFICINA) references
OFICINA (NUMOFICINA);



2) INSERTAR 2 REGISTROS EN EMPLEADO

primero insertamos UN registro a OFICINA
insert into OFICINA values ('0001', 'Alonso Ovalle', 'Santiago','56');

...Ahora van los Empleados.
insert into EMPLEADO values('E007','James','Bond','Espia','M','01/01/1939',200000,'0001');



3) Desarrolle una consulta SQL que liste todos los empleados que tienen un salario inferior a 1500

select *
from Empleado
where salario < 100000;


4) desarrolle una consulta SQL que liste todos los empleadois Hombre que tienen un cargo de Supervisor

select *
from Empleado
where lower(cargo) = 'supervisor';


5) Indicar el comando DDL necesario para agregar el atributo TotPropiedades a la tabla Oficina

Alter table OFICINA
add TotPropiedades number


6) Emitir un listado con el numero de empleados que trabajan en la ciudad de 'SANTIAGO'

select count(NUMOFICINA)
from empleado
where numOficina in
(select numOficina
from Oficina
where ciudad = 'santiago');


7) Listar NUMPROPIEDAD de las propiedades que han sido visitadas, eliminando los repetidos


select distinCt NUMPROPIEDAD
from VISITA;


opcion 2
select NUMPROPIEDAD
from PROPIEDAD
where NUMPROPIEDAD in(select NUMPROPIEDAD from VISITA);


8)Listar NUMPROPIEDAD de las propiedades que han sido visitadas, pero los clientes no han emitido comentario

select distinct NUMPROPIEDAD
from VISITA
where comnetario is NULL


9)Listar todos los Empleados, ordenados por Sexo y descendente por fecha de nacimiento

select *
from Empleado
order by sexo, FECHANAC DESC;


11) Indique el comando necesario para eliminar la vista vistaPromedioRentaPropiedad creada en la pregunta anterior. (5 Puntos) */

drop view vistaPromedioRentaPropiedad;


12) Indique la instrucción para eliminar la referencia a la tabla Oficina desde la tabla Empleado
(La foreign key FK_EMPLEADO_REFERENCE_OFICINA) (10 puntos) */

alter table Empleado
drop constraint FK_EMPLEADO_REFERENCE_OFICINA


13) Escriba la sentencia para eliminar la tabla Empleado junto a sus llaves foráneas.(10 Puntos)*/

drop table empleado cascade constraint


14) Indique el comando para actualizar la fecha de nacimiento (fechNac) del empleado ‘SL21’ a '01/07/2011' (10 puntos) */

update empleado
set fechNac = '01/07/2011'
where numempleado = ‘SL21’ 


15) Indique el comando necesario para listar todos los empleados nacidos entre el año 1970 y 1980, ambas fechas inclusive. (10 puntos) */

select *
from Empleado
where to_char(FechNac, 'YYYY') between '1950' and '1960'

select *
from Empleado
where fechnac >= '01/01/1950' and fechnac <= '31/12/1960'

►Crear y Poblar tablas.-

/* BORRADO DE TABLAS */
/*==============================================================*/
drop table TotPropEmpleado cascade constraints;

drop table ARRIENDO cascade constraints;

drop table CLIENTE cascade constraints;

drop table EMPLEADO cascade constraints;

drop table OFICINA cascade constraints;

drop table PROPIEDAD cascade constraints;

drop table PROPIETARIO cascade constraints;

drop table VISITA cascade constraints;
/*==============================================================*/
/* Tabla: ARRIENDO */
/*==============================================================*/
create table ARRIENDO (
NUMARRIENDO INTEGER not null,
NUMPROPIEDAD CHAR(4),
NUMCLIENTE CHAR(4),
RENTA FLOAT,
FORMAPAGO CHAR(10),
DEPOSITO FLOAT,
PAGADO CHAR(1),
INICIORENTA DATE,
FINRENTA DATE,
constraint PK_ARRIENDO primary key (NUMARRIENDO)
);
/*==============================================================*/
/* Tabla: CLIENTE */
/*==============================================================*/
create table CLIENTE (
NUMCLIENTE CHAR(4) not null,
NOMBRE CHAR(30),
APELLIDO CHAR(30),
DIRECCION CHAR(35),
TELEFONO CHAR(10),
TIPOPREF CHAR(25),
MAXRENT FLOAT,
constraint PK_CLIENTE primary key (NUMCLIENTE)
);
/*==============================================================*/
/* Tabla: EMPLEADO */
/*==============================================================*/
create table EMPLEADO (
NUMEMPLEADO CHAR(4) not null,
NOMBRE CHAR(30),
APELLIDO CHAR(30),
CARGO CHAR(35),
SEXO CHAR(1),
FECHNAC DATE,
SALARIO FLOAT,
NUMOFICINA CHAR(4),
constraint PK_EMPLEADO primary key (NUMEMPLEADO)
);
/*==============================================================*/
/* Tabla: OFICINA */
/*==============================================================*/
create table OFICINA (
NUMOFICINA CHAR(4) not null,
CALLE CHAR(30),
CIUDAD CHAR(25),
CODIGOPOSTAL CHAR(10),
constraint PK_OFICINA primary key (NUMOFICINA)
);
 /*==============================================================*/
/* Tabla: PROPIEDAD */
/*==============================================================*/
create table PROPIEDAD (
NUMPROPIEDAD CHAR(4) not null,
CALLE CHAR(30),
CIUDAD CHAR(25),
CODIGOPOSTAL CHAR(10),
TIPO CHAR(25),
HAB INTEGER,
RENTA FLOAT,
NUMPROPIETARIO CHAR(4),
NUMEMPLEADO CHAR(4),
constraint PK_PROPIEDAD primary key (NUMPROPIEDAD)
);
/*==============================================================*/
/* Tabla: PROPIETARIO */
/*==============================================================*/
create table PROPIETARIO (
NUMPROPIETARIO CHAR(4) not null,
NOMBRE CHAR(30),
APELLIDO char(30),
DIRECCION CHAR(30),
TELEFONO CHAR(10),
constraint PK_PROPIETARIO primary key (NUMPROPIETARIO)
);
/*==============================================================*/
/* Tabla: VISITA */
/*==============================================================*/
create table VISITA (
NUMCLIENTE CHAR(4) not null,
NUMPROPIEDAD CHAR(4) not null,
FECHA DATE not null,
COMENTARIO VARCHAR2(30),
constraint PK_VISITA primary key (NUMCLIENTE, NUMPROPIEDAD, FECHA)
);
/*==============================================================*/
/* Tabla TotPropEmpleado */
/* Se utiliza para insertar desde otra tabla
/*==============================================================*/
create table TotPropEmpleado (
NUMEMPLEADO CHAR(4) not null,
totProp INTEGER,
constraint PK_TotPropEmpleado primary key (NUMEMPLEADO)
);

alter table TotPropEmpleado
add constraint FK_TotProp_REFERENCE_EMPLEADO foreign key (NUMEMPLEADO)
references EMPLEADO (NUMEMPLEADO);

alter table EMPLEADO
add constraint FK_EMPLEADO_REFERENCE_OFICINA foreign key (NUMOFICINA)
references OFICINA (NUMOFICINA);

alter table PROPIEDAD
add constraint FK_PROPIEDA_REFERENCE_EMPLEADO foreign key (NUMEMPLEADO)
references EMPLEADO (NUMEMPLEADO);

alter table VISITA
add constraint FK_VISITA_REFERENCE_CLIENTE foreign key (NUMCLIENTE)
references CLIENTE (NUMCLIENTE);

alter table VISITA
add constraint FK_VISITA_REFERENCE_PROPIEDA foreign key (NUMPROPIEDAD)
references PROPIEDAD (NUMPROPIEDAD);
/*==============================================================*/
/*= P o b l a m i e n t o d e t a b l a s =*/
/*==============================================================*/

/*==============================================================*/
/* datos: oficina */
/*==============================================================*/
insert into oficina values('B005','16 Holhead','Aberdeem','AB7 5SU');
insert into oficina values('B007','6 Argvill St.','London','NW2');
insert into oficina values('B003','164 Main street','Glasgow','G119Qx');
insert into oficina values('B004','2 Manor Rd','Glasgow','G114Qx');
insert into oficina values('B001','10 Dale Rd','bristol','G12');
insert into oficina values('B002','17 Holhead','Aberdeem','AB7 5SU');
insert into oficina values('B008','7 Argvill St.','London','NW21');
insert into oficina values('B006','163 Main street','Glasgow','G11');
insert into oficina values('B010','2 Manor Rd','Glasgow','G114x');
insert into oficina values('B011','14 Dale Rd','bristol','G2');
insert into oficina values('B017','6 Argvill St.','London','W2');
insert into oficina values('B013','166 Main street','Glasgow','9Qx');
insert into oficina values('B014','3 Manor Rd','Glasgow','Qx');
insert into oficina values('B012','11 Dale Rd','bristol','GH2');
insert into oficina values('B015','Costanera 25','Valdivia','0324');
insert into oficina values('B115','Picarte 124','Valdivia','0324');
insert into oficina values('B215','El Morro 110','Arica','10300');
insert into oficina values('B315','El Vergel 1500','Arica','123123');
insert into oficina values('B415','Av. Walker Martinez 1360','Santiago','W101');
insert into oficina values('B515','Av. Antonio Varas 929','Santiago','W101');
/*==============================================================*/
/* datos: cliente */
/*==============================================================*/
insert into cliente values('CR76','Jhon','Kay','56 High ST,Londonn,SW14EH','0207774563','Departamento',450);
insert into cliente values('CR56','Aline','Stewart','64 Fern Dr, Glasgow G42 OBL','0141324182','Departamento',350);
insert into cliente values('CR74','Mike','Ritchie','63 Well St, Glasgow,G42','0141943742','Casa',750);
insert into cliente values('CR62','Mary','Tregear','12 Park PI, Glasgow, G40QR','0141225742','Departamento',600);
insert into cliente values('CR78','Juan','Kayser','55 High ST,Londonn,SW14EH','0207774564','Departamento',450);
insert into cliente values('CR57','Alicia','Soto','63 Fern Dr,. GlasgowG42 OBL','0141324183','Departamento',350);
insert into cliente values('CR72','Miguel','Torres','62 Well St, Glasgow,G42','0141943740','Casa',750);
insert into cliente values('CR63','Maria','Perez','13 Park PI, Glasgow,G4 0QR','0141225741','Departamento',600);
/*==============================================================*/
/* datos: empleado */
/*==============================================================*/
insert into empleado values('SL21','Jhon','White','Gerente','M','01/10/45',300000,'B005');
insert into empleado values('SG37','Peter','Denver','Asistente','M','10/11/60',120000,'B006');
insert into empleado values('SG14','David','Ford','Supervisor','M','09/09/58',180000,'B003');
insert into empleado values('SA9','Mary','Lee','Asistente','F','17/09/59',90000,'B007');
insert into empleado values('SG5','Susan','Sarandon','Gerente','F','21/03/60',240000,'B003');
insert into empleado values('SL41','Julie','Roberts','Asistente','F','13/06/63',90000,'B005');
insert into empleado values('SL22','Juan','Blanco','Gerente','M','01/10/44',300000,'B005');
insert into empleado values('SG36','Luis','Jara','Asistente','M','10/11/61',120000,'B003');
insert into empleado values('SG13','David','Gates','Supervisor','M','09/09/58',180000,'B003');
insert into empleado values('SA8','Maria','Bombal','Asistente','F','17/09/59',90000,'B007');
insert into empleado values('SG4','Susana','Sarandons','Gerente','F','21/03/60',240000,'B003');
insert into empleado values('SL40','James','Bond','Asistente','F','13/06/63',90000,'B005');
insert into empleado values('SL50','Juan','Perez','Vendedor','M','13/06/63',151000,'B015');
insert into empleado values('SL60','Jaime','Soto','Vendedor','M','14/06/83',350000,'B115');
insert into empleado values('SL70','Julia','Berne','Vendedor','F','23/01/53',200000,'B215');
insert into empleado values('SL55','Jorge','Fernandez','Vendedor','M','13/06/63',151000,'B015');
insert into empleado values('SL65','Jose','Isla','Vendedor','M','14/06/83',350000,'B115');
/*==============================================================*/
/* datos: Propietario */
/*==============================================================*/
insert into propietario values('C046','Joe','Keogh','2 Fergus Dr, AberdeenAB 7SX','0122486121');
insert into propietario values('C087','Carol','Farrel','6 Achray St.Glasgow, G32 9DX','0141357741');
insert into propietario values('C040','Tina','Murphy','63 Well St, Glasgow, G42','0141943742');
insert into propietario values('C093','Tony','Shaw','12 Park PI, Glasgow, G40QR','0141225742');
insert into propietario values('C047','Jose','Casanova','El Volvan 123, Santiago AB 7SX','0122486125');
insert into propietario values('C088','Carolina','Fernandez','Macul 1800. Santiago, G32 9DX','0141357741');
insert into propietario values('C041','Cristina','Mora','Av. Matta 1800, Santiago, G42','0141943752');
insert into propietario values('C094','Tomas','Figueroa','Av. Macul 120, Santiago, G40QR','0141225542');
/*==============================================================*/
/* datos: PROPIEDAD */
/*==============================================================*/
insert into PROPIEDAD values('PA14','16 Holhead','Aberdeem','AB7 5SU','Casa','6','650','C046','SL21');
insert into PROPIEDAD values('PL94','6 Argvill St.','London','NW2','Departamento','4','400','C087','SL21');
insert into PROPIEDAD values('PG4' ,'6 Lawrence St','Glasgow','G119QX','Departamento','3','350','C040','SA9');
insert into PROPIEDAD values('PG36','2 Manor Rd','Glasgow','G114QX','Departamento','3','375','C093','SA9');
insert into PROPIEDAD values('PG21','AV. Matta 150','Santiago','G12','Casa','5','600','C087','SG5' );
insert into PROPIEDAD values('PR01','Macul 120 ','Santaigo','G129AX','Departamento','4','450','C093','SA8');
insert into PROPIEDAD values('PR02','Macul 220','Santiago','G129AX','Departamento','5','550','C093','SG13');
insert into PROPIEDAD values('PR03','Macul 420','Santiago','G129AX','Departamento','6','650','C093','SG14');
insert into PROPIEDAD values('PR04','Macul 620','Santiago','G129AX','Departamento','3','350','C093','SG36');
insert into PROPIEDAD values('PR05','Loa 100','Santiago','G129AX','Departamento','2','250','C093','SG4');
insert into PROPIEDAD values('PG16','Arturo Prats 250','Santiago','G129AX','Departamento','4','450','C047','SL22');
insert into PROPIEDAD values('PR07','Gorbea 200','Santiago','G129AX', 'Departamento','6','650','C047','SL40');
insert into PROPIEDAD values('PR08','Gomez 230','Santiago','G129AX', 'Departamento','2','250','C041','SL41');
insert into PROPIEDAD values('PR09','Garibaldi 1500','Santiago','G129AX', 'Departamento','6','650','C041','SL50');
insert into PROPIEDAD values('PR10','Las Urbinas 210','Santiago','G129AX', 'Departamento','6','650','C094','SL55');
insert into PROPIEDAD values('PR11','Lastarria 1400','Santiago','G129AX', 'Departamento','3','350','C094','SL60');
insert into PROPIEDAD values('PR12','Las Giraldas 200','Santiago','G129AX','Departamento','4','450','C093','SL70');
/*==============================================================*/
/* datos: VISITA */
/*==============================================================*/
insert into visita values('CR56','PA14','24-11-1999','muy pequeño');
insert into visita values('CR62','PA14','14-11-1999','no tiene salón');
insert into visita values('CR76','PG4','20-10-1999','muy lejos');
insert into visita values('CR72','PG16','24-06-2007','Bakan');
insert into visita values('CR72','PG36','24-06-2007','Super');
insert into visita values('CR62','PG16','25-06-2007','Cool');
insert into visita values('CR62','PG4','25-06-2007', NULL);
insert into visita values('CR62','PG36','25-06-2007','No salva');
insert into visita (numCliente, numPropiedad, fecha) values ('CR72','PG4','25-06-2007');
insert into visita (numCliente, numPropiedad, fecha) values('CR56','PG36','28-10-1999');
insert into visita (numCliente, numPropiedad, fecha) values('CR56','PG4','26-11-1999');
/*==============================================================*/
/* datos: ARRIENDO*/
/*==============================================================*/
insert into arriendo values('10024','PA14','CR62','650','Visa','1300','Y','01-06-2005','31-05-2006');
insert into arriendo values('10075','PL94','CR76','400','Contado','800','N','01-08-2005','31-01-2006');
insert into arriendo values('10012','PG21','CR74','600','Cheque','1200','Y','01-07-2005','30-06-2006');
/*==============================================================*/