ejercitario taller final

13
[Fecha] Base de Datos [Subtítulo del documento] Escuela Politécnica Nacional Escuela de formación de Tecnólogos VIng. Veintimilla Marina Casalliglla Jhonatan FECHA ENTREGA : 20/07 /2015

Upload: jonhna-casaliglla

Post on 07-Sep-2015

226 views

Category:

Documents


0 download

DESCRIPTION

Documento sumamente util para el aprendizaje de modelamiento

TRANSCRIPT

Base de Datos

create table Sector ( codSector int primary key identity (1,1), ubicacion varchar(50)NOT NULL, nombreSector varchar(50)NOT NULL)

create type Tipo_fono From char(12) NULL; create type Tipo_direc From char(30) NULL create table Sucursal ( codSucursal int primary key identity (1,1), nomSucursal varchar(30)NOT NULL, sector int NOT NULL, direccion tipo_direc, FkSector int foreign key references SECTOR (codSector))

create table Ejecutivos ( codEjecutivo int primary key identity (1,1), cedula char(10)UNIQUE, apellido varchar(30)NOT NULL, nombre varchar(30)NOT NULL, codSucursal int NULL, fono_Bco tipo_fono, fono_Personal tipo_fono, fkCodSucursal int foreign key references sucursal (codSucursal))

create table Cuentas( numCta int primary key identity (1,1), titulo varchar(50)NOT NULL, fecha_Apertura smalldatetime NOT NULL, sucursal int NOT NULL, tipo char(10)NOT NULL, saldo_Contable money NOT NULL, saldo_Efectivo money NOT NULL, cod_Ejecutivo int NOT NULL, FKSucursal int foreign Key references Sucursal (codSucursal), FKEjecutivo int foreign key references Ejecutivos (codEjecutivo) ) create table Detalle_Mov_Cta( num_Mov int primary key identity (1,1), numCta int NOT NULL, fecha_Mov smalldatetime NOT NULL, tipo_Mov char(1)NOT NULL, valor money NOT NULL, FkCuentas int foreign key references CUENTAS(numCta))

create table Clientes( codCliente int primary key identity (1,1), cedula char(10) UNIQUE NOT NULL, apellido varchar(30) NOT NULL, nombre varchar(30) NOT NULL, fechaNac smalldatetime NULL, direccion tipo_direc, sector int NOT NULL, fono_Ofic tipo_fono, fono_Dom tipo_fono, fono_Cel tipo_fono, codEjecutivo int NOT NULL, FkEjecutivo int foreign key references Ejecutivos (CodEjecutivo), FkSector int foreign Key references Sector(CodSector))

create table ClientexCta( codCliente int primary key identity (1,1), numCta int, activo bit NOT NULL, FkCuenta int foreign key references CUENTAS(numCta), FkClientes int foreign key references Clientes(codCliente))

create table Detalle_Prestamo( num_detalle_pres int primary key identity (1,1), fecha_pago smalldatetime NOT NULL, valor money NOT NULL, numPrestamo int NOT NULL, fkCliente int foreign key references CLIENTES (codCliente) ) create table Prestamos( numPrestamo int primary key identity (1,1), monto money NOT NULL, fecha_aproba smalldatetime NOT NULL, dividendos tinyint NOT NULL, saldo money NOT NULL, codSucursal int NOT NULL, codCliente int NOT NULL, fKDetalle_Clientes int foreign key references Detalle_Prestamo (num_detalle_pres) )

-- R E G I S T R O S --Sector: 3 registros

SELECT * FROM Sector

insert into Sector VALUES ('Centro','Lo nogales')insert into Sector VALUES ('Norte','Los nogales')insert into Sector VALUES ('Sur ',' La Marin')

--Sucursal: 8 registrosSELECT * FROM Sucursal

insert into Sucursal VALUES ('La Ronda',1,'Centro',1)insert into Sucursal VALUES ('El bosque',2,'Norte',1)insert into Sucursal VALUES ('Caldern ',3,'Norte',1)insert into Sucursal VALUES ('Cayambe',4,'Norte',3)insert into Sucursal VALUES ('Manta',1,'Sur',1)insert into Sucursal VALUES ('Cayambe',3,'Norte',2)insert into Sucursal VALUES ('Quinche',1,'Norte',3)insert into Sucursal VALUES ('Comite del Pueblo',1,'Norte',1) --Ejecutivos: 8 registrosSELECT * FROM Ejecutivos

insert into Ejecutivos VALUES ('1723205660','Enriquez','Elissa',1,'2411517','2788674',3 )insert into Ejecutivos VALUES ('1723235789','Gallardo','Esteban',2,'2416617','2413876',4)insert into Ejecutivos VALUES ('1723204583','Lopes','Lorena',2,'2413432','2413876',5)insert into Ejecutivos VALUES ('1723384759','Mero','Maria',2,'2417256','2413876',6)insert into Ejecutivos VALUES ('1729865389','Quinaluisa','Xavier',2,'2486347','2413876',7)insert into Ejecutivos VALUES ('1723224539','Lopes','Jhoana',2,'2419000','2413876',8)insert into Ejecutivos VALUES ('1723205839','Jimenez','Mariela',2,'2415555','2413876',4)insert into Ejecutivos VALUES ('1723289455','Ordoes','Sebastian',2,'2413333','2413876',3)

--Cuentas : 10 registros SELECT * FROM CuentasSELECT * FROM SucursalSELECT * FROM Ejecutivos

insert into Cuentas VALUES ('Pichincha','1990-07-11',001,'Corriente',5000,10000,50,3,2)insert into Cuentas VALUES ('Austro','1990-07-11',002,'Ahorros',2500,1000,56,4,3 )insert into Cuentas VALUES ('Pacifico','1994-07-11',003,'Prestamo',700,4000,50,6,5 )insert into Cuentas VALUES ('Pichincha','1990-07-11',006,'Corriente',1200,300,43,9,8 )insert into Cuentas VALUES ('Pacifico','1990-07-11',008,'Corriente',3500,700,33,3,2)insert into Cuentas VALUES ('Pacifico','1990-07-11',009,'Ahorros',18000,800,78,4,3 )insert into Cuentas VALUES ('Austro','1990-07-11',011,'Ahorros',2500,800,26,6,5 )insert into Cuentas VALUES ('Pacifico','1990-08-01',010,'Corriente',6000,100,78,4,3 )insert into Cuentas VALUES ('Austro','1993-09-11',004,'Corriente',3500,3500,56,6,5 )insert into Cuentas VALUES ('Pichincha','1991-04-04',006,'Corriente',1000,100,86,9,8)insert into Cuentas VALUES ('Pichincha','1993-05-10',007,'Ahorros',3000,100,78,5,4 )

--Detalle_Mov_Cta: 15 registros --(ingrese D para dbitos y C para crditos en tipo_mov.- Ingrese movimientos para al menos 8 cuentas)

SELECT * FROM Detalle_Mov_CtaSELECT * FROM Cuentas

insert into Detalle_Mov_Cta VALUES (172320569,'1990-12-08','D',5500,1)insert into Detalle_Mov_Cta VALUES (172320569,'1993-11-09','C',4500,2)insert into Detalle_Mov_Cta VALUES (144757839,'1994-08-07','C',1500,3)insert into Detalle_Mov_Cta VALUES (223465237,'1991-05-05','C',2000,3)insert into Detalle_Mov_Cta VALUES (258935634,'1989-03-06','C',3000,5)insert into Detalle_Mov_Cta VALUES (276537854,'1987-06-04','D',900,6)insert into Detalle_Mov_Cta VALUES (154785678,'1988-04-01','D',1500,7)insert into Detalle_Mov_Cta VALUES (315678955,'1990-06-07','D',3100,8)insert into Detalle_Mov_Cta VALUES (324564565,'1993-08-03','C',2100,9)insert into Detalle_Mov_Cta VALUES (174567876,'1990-11-04','D',800,5)insert into Detalle_Mov_Cta VALUES (382345567,'1989-07-07','D',900,1)insert into Detalle_Mov_Cta VALUES (146567788,'1991-07-05','C',800,3)insert into Detalle_Mov_Cta VALUES (153344778,'1987-03-02','C',750,6)insert into Detalle_Mov_Cta VALUES (148966543,'1994-01-08','D',830,8)insert into Detalle_Mov_Cta VALUES (148346543,'1990-02-06','D',980,7)

--ClientexCta: 15 registros--Clientes: 10 registrosSELECT * FROM ClientesSELECT * FROM EjecutivosSELECT * FROM Sector

insert into Clientes VALUES ('1723205668','Quijia','Antonia','1994-04-04','Joaquin Sumaita',01,'2411517','2417438','3264563',101,2,1)insert into Clientes VALUES ('1723837458','Ordoes','Anahi','1993-05-08','Los Guabos',02,'2411222','2417438','7777323',101,3,2)insert into Clientes VALUES ('1723229034','Mejia','Alberto','1990-01-05','Comite del pueblo',03,'2411222','2417777','3262323',101,4,3)--revisarinsert into Clientes VALUES ('1723205668','Camao','Pablo','1991-12-08','El Inca',08,'26615777','2417438','3262444',101,7,3)insert into Clientes VALUES ('1723205668','Giron','Enquique','1991-02-04','El condado',06,'2411888','2417438','2442323',101,7,3)insert into Clientes VALUES ('1723205668','Mena','Xavier','1991-06-09','El Bosque',07,'2411999','2417438','3266666',101,8,1)insert into Clientes VALUES ('1723205668','Rodriguez','Mary','1989-03-06','Los granados',08,'2411111','2417438','3264444',101,2,1)insert into Clientes VALUES ('1723205668','Pullupaxi','Estefana','1990-02-08','Eloy alfaro',09,'241152222','2177777','3262323',101,3,2)insert into Clientes VALUES ('1723205668','Abad','Gabriela','1993-09-07','El Camal',10,'2411517','2417666','3265555',101,3,4)insert into Clientes VALUES ('1723205668','Torres','Juan','1999-07-04','Los Madroos',04,'2411666','2417438','6666323',101,5,1)

-- (un cliente puede tener ms de una cuenta; al menos para tres clientes,-- asgneles dos cuentas a cada uno, para el resto, solo una cuenta)

SELECT * FROM ClientexCtaSELECT * FROM CuentasSELECT * FROM Clientes

insert into ClientexCta VALUES (172320569,800,1,2)insert into ClientexCta VALUES (172320569,2300,2,1)insert into ClientexCta VALUES (144757839,1500,3,3)insert into ClientexCta VALUES (223465237,1200,5,1)insert into ClientexCta VALUES (258935634,3400,7,3)insert into ClientexCta VALUES (276537854,2700,5,1)insert into ClientexCta VALUES (154785678,900,8,2)insert into ClientexCta VALUES (315678955,240,5,3)insert into ClientexCta VALUES (324564565,290,5,2)insert into ClientexCta VALUES (176789567,600,7,2)insert into ClientexCta VALUES (174567876,400,5,1)insert into ClientexCta VALUES (382345567,250,7,1)insert into ClientexCta VALUES (146567788,250,6,3)insert into ClientexCta VALUES (153344778,350,8,2)insert into ClientexCta VALUES (148966543,100,9,2)

--Detalle_Prestamo: ingrese movimiento para al menos 4 prstamos SELECT * FROM ClientesSELECT * FROM Detalle_Prestamo

insert into Detalle_Prestamo VALUES ('2015-12-03',500,2,2)insert into Detalle_Prestamo VALUES ('2017-12-02',100,3,1)insert into Detalle_Prestamo VALUES ('2018-12-03',700,1,3)insert into Detalle_Prestamo VALUES ('2019-12-03',500,2,1)

--Prestamos: 5 registros SELECT * FROM ClientesSELECT * FROM SucursalSELECT * FROM Detalle_Prestamo

insert into Prestamos VALUES (5000,'2014-02-01',100,2500,001,100,2)insert into Prestamos VALUES (700,'2014-05-08',150,300,002,200,2)insert into Prestamos VALUES (2000,'2014-07-09',98,600,003,300,3)insert into Prestamos VALUES (4500,'2014-09-05',130,510,004,400,4)insert into Prestamos VALUES (3100,'2014-06-08',120,640,005,500,2)SELECT * FROM Prestamos

*CONSULTAS SOBRE LA BASE DE DATOS BANCOS*//* Presente cdula, apellido, nombre (en la forma apellido, nombre) y nombre de la sucursal de todos los ejecutivos, ordenados por nombre de la sucursal y dentro de sta, en orden alfabtico.*/ select cedulaEj, apellidoEj,nombreEj, nomSucursal from Ejecutivos, Sucursal where codSucursal=codSucursalFK order by nomSucursal,apellidoEj asc /* Presente un listado de todas las cuentas: nmero de la cuenta, tipo de la cuenta, ttulo, fecha de apertura (de la forma 11 May 2008) y nombre, apellido del cliente dueo de la cuenta. Primero deben estar las cuentas de tipo Ahorro y luego las de tipo Corriente.*/ select numCta,tipo,titulo ,fecha_Apertura=convert(CHAR(12), fecha_Apertura, 106),nombreCl,apellidoCl from Cuentas,Clientes,ClientexCta where (codCliente=codClienteFK2 and numCta=numCtaFK2) order by tipo asc /* Presente un listado de los prstamos como: nmero del prstamo, monto, nombre y apellido del cliente, para aquellos prstamos superiores a $ 3.000*/ select numPrestamo, monto,nombreCl ,apellidoCl from Prestamos,Clientes where (codCliente=codClienteFK3 and monto>3000.00)

/* Presentar cdula, apellido, nombre direccin y nombre del sector en que viven, de todos los empleados con apellidos que empiezan con VA.*/ select cedulaEj , apellidoEj, nombreEj,direccion, nombreSector from Ejecutivos,Sector,Sucursal where (codSucursal=codSucursalFK and codSector=codSectorFK and apellidoEj like 'VA%')

/* Presentar un listado de los prstamos, nmero de prstamo, monto, dividendos, para aquellos cuyo nmero de dividendos est entre 12 y 24.*/ select numPrestamo, monto,dividendos from Prestamos where (dividendos between 12 and 24) /* Presente un listado de los clientes con toda la informacin, mostrando el nombre y apellido del ejecutivo que atiende al cliente y el nombre del sector en que vive el cliente, ordenado por nombre de sector y dentro de ste en orden alfabtico.*/ select codCliente,cedulaCl,nombreCl,apellidoCl,fechaNac,direccion,nombreSector ,fono_Ofic,fono_Dom,fono_Cel ,nombreEj, apellidoEj from Clientes,Sector,Ejecutivos where codSector=codSectorFK2 and codEjecutivoFK =codEjecutivo order by nombreSector asc

/*

Presente un listado de todas las cuentas corrientes que se hayan abierto desde el ao 2007 en adelante.*/ select numCta,titulo,Ao_Apertura=year(fecha_Apertura) from Cuentas where year(fecha_Apertura)>=2007

/* Presente un listado de las cuentas que tienen distinto valor entre saldo_contable y saldo_efectivo. */ select *from Cuentas where (saldo_Contable saldo_Efectivo )/* Presente un listado de las sucursales, con el nombre del sector al que pertenecen. Ordene alfabticamente por nombre de sector y dentro de ste por nombre de sucursal. */ select codSucursal,nomSucursal,direccion,nombreSector from Sucursal, Sector where codSector=codSectorFK order by nombreSector ,nomsucursal asc /* Muestre todo el detalle de pago a prstamos, indicando el nmero de prstamo al que pagan, para aquellos pagos menores a $300.*/ select num_detalle_pres,fecha_pago=convert(CHAR(12), fecha_pago, 103),valor,numPrestamo from Prestamos,Detalle_Prestamo where numPrestamo=numPrestamoFK and valor 100)

/ Obtenga el monto total de prstamos otorgados por cada sucursal, que superen los $ 4000*/ select Monto_Total=sum(monto)from Prestamos where monto>4000

/*USANDO VISTAS Y PROCEDIMIENTOS*/

/ Muestre el nmero de cuenta, el apellido y nombre del cliente y el saldo efectivo de las cuentas Corrientes de los clientes.*/ create view Cta_Clientes as select numCta,apellidoCl,nombreCl,saldo_Efectivo from Clientes,Cuentas,ClientexCta where codCliente=codClienteFK2 and numCta=numCtaFK2 and tipo='Corriente' select *from Cta_Clientes/* Muestre el apellido y nombre de los clientes con el apellido y nombre de su ejecutivo*/ create procedure Cliente_Ejecutivo as beginselect apellidoCl,nombreCl,apellidoEj, nombreEj from Clientes,Ejecutivoswhere codEjecutivo=codEjecutivoFK end exec Cliente_Ejecutivo/* Muestre el nmero de cuenta con el apellido y nombre del cliente y el nombre de la sucursal en que se abri la cuenta.*/ create view Cta_Sucursal asselect numCta,apellidoCl,nombreCl,nomSucursal from Cuentas,Clientes,Sucursal, ClientexCtawhere codCliente=codClienteFK2 and numCta=numCtaFK2 and codSucursalFK2=codSucursal select *from Cta_Sucursal /* Mostrar las cuentas que pertenecen a ms de un cliente (use una combinacin no igual que se combina con una autocombinacin para buscar todas las filas de la tabla ClienteXCta en la que dos o ms filas tengan la misma numCta pero distintos nmeros de CodCliente).*/ create procedure N_Ctas_Mayor as begin select count(numCtaFK2)as 'N_Ctas', apellidoCl from Clientes,ClientexCta where codCliente=codClienteFK2 group by apellidoCl having count(numCtaFK2) >=2 end exec N_Ctas_Mayor /* Muestre el listado del los clientes (apellido y nombre) que viven en el mismo sector que su ejecutivo de cuenta.*/ create view Clientes_Sector as select nombreCl,apellidoCl,nombreEj,apellidoEj,nombreSector from Clientes,Ejecutivos,Sector where codEjecutivo=codEjecutivoFK and codSector=codSectorFK2

select *from Clientes_Sector

/* Muestre un detalle de los pagos realizados a prstamos (valor, y fecha_pago), el nmero de prstamo, su monto, y el apellido y nombre del cliente que tiene el prstamo.*/ create view Prestamo_Cliente as select valor,Fecha_pago=convert(CHAR(12), fecha_pago, 103),numPrestamo,monto,nombreCl,apellidoCl from Prestamos,Detalle_Prestamo,Clientes where codCliente=codClienteFK3 and numPrestamo=numPrestamoFK select *from Prestamo_Cliente /* Muestre un detalle de cada sucursal y el nombre del sector al que pertenece*/ create procedure Sucursal_Sector as begin select codSucursal,nomSucursal,direccion,nombreSector from Sucursal,Sector where codSector=codSectorFK end exec Sucursal_Sector

/* Muestre los clientes (apellido, nombre) y el sector en que residen, muestre el nombre de sucursal si encuentra una sucursal en ese sector.*/ create view Sector_Cliente as select apellidoCl,nombreCl,nombreSector from Clientes,Sector where codSectorFK2=codSector select *from Sector_Cliente

/* Muestre las sucursales del banco en los sectores y si algn cliente reside all.*/ create procedure Sucursal_Sector_Cli as begin select distinct nomSucursal,nombreSector,apellidoCl,nombreCl from Clientes ,Sucursal,Cuentas,Sector where codSectorFK=codSectorFK2 and codSucursal=codSucursalFK2 and codEjecutivoFK=codEjecutivoFK2 and codSector=codSectorFK end exec Sucursal_Sector_Cli /* Muestre los clientes y las sucursales.*/ create procedure Clientes_Cucursal @Valor int AS begin if(@Valor=1)select *from Clientes if(@Valor=2)select *from Sucursal end

exec Clientes_Cucursal @Valor = 2