You are on page 1of 17

Universidad Abierta Interamericana

Rectorado: Chacabuco 90 - 1 Piso, Capital Federal Telfono: 4342-7788


Campus Lomas: Av. Hiplito Yrigoyen 9963, Lomas de Zamora - Provincia de Bs. As. Telfono: 4243-4827 / 4244-5839
http://www.vaneduc.edu.ar/uai/

Video Club
Materia: Base de Datos

2010
Curso: 3 A Turno Noche
Docente: Nicols Bocalandro
Alumnos: Nicotra, Antonela

Universidad Abierta Interamericana


Rectorado: Chacabuco 90 - 1 Piso, Capital Federal Telfono: 4342-7788
Campus Lomas: Av. Hiplito Yrigoyen 9963, Lomas de Zamora - Provincia de Bs. As. Telfono: 4243-4827 / 4244-5839
http://www.vaneduc.edu.ar/uai/

ndice
Crear BD ....................................................................................................................... 3
Crear las Tablas ............................................................................................................ 3
Crear las Claves Forneas ............................................................................................. 5
Borrar todos los datos de las tablas ............................................................................. 5
Insertar todos los datos en las tablas ........................................................................... 6
Diagrama de Base de Datos:......................................................................................... 8
Consulta 1 .................................................................................................................... 9
Consulta 2 .................................................................................................................... 9
Consulta 3 .................................................................................................................... 9
Consulta 4 .................................................................................................................... 9
Consulta 5 .................................................................................................................. 10
Consulta 6 .................................................................................................................. 10
Consulta 7 .................................................................................................................. 10
Consulta 8 .................................................................................................................. 10
Consulta 9 .................................................................................................................. 11
Consulta 10................................................................................................................. 12
Consulta 11................................................................................................................. 12
Consulta 12................................................................................................................. 12
Consulta 13................................................................................................................. 13
Consulta 14................................................................................................................. 14
Consulta 15................................................................................................................. 14
Consulta 16................................................................................................................. 14
Consulta 17................................................................................................................. 15
Consulta 18................................................................................................................. 15
Consulta 19................................................................................................................. 15

2
Trabajo Practico: Video Club
Materia: Base de Datos

Universidad Abierta Interamericana


Rectorado: Chacabuco 90 - 1 Piso, Capital Federal Telfono: 4342-7788
Campus Lomas: Av. Hiplito Yrigoyen 9963, Lomas de Zamora - Provincia de Bs. As. Telfono: 4243-4827 / 4244-5839
http://www.vaneduc.edu.ar/uai/

Consultas para Crear la BD completa:


Crear BD
Create database [VideoClub]

Crear las Tablas


/*Tabla Actor*/
create table Actor
(
CodigoActor int not null primary key,
NombreApellido varchar(100)
)
/*Tabla Alquiler_Cab*/
create table Alquiler_Cab
(
CodigoAlquiler int not null primary key,
FechaAlquiler datetime,
NumeroSocio int,
FechaDevolucion datetime
)
/*Tabla Alquiler_Det*/
create table Alquiler_Det
(
CodigoAlquiler int not null,
CodigoPelicula int not null,
NumeroCopia int not null,
PrecioAlquiler money
)
Alter table Alquiler_Det
add constraint PK_Alquiler_Det
Primary key (CodigoAlquiler, CodigoPelicula, NumeroCopia)
/*Tabla Director*/
create table Director
(
CodigoDirector int not null primary key,
Nombre varchar(50),
Apellido varchar(50)
)
/*Tabla Estreno*/
create table Estreno
(
CodigoPelicula int not null,
FechaDesde datetime not null,
FechaHasta datetime
)
Alter table Estreno
add constraint PK_Estreno
primary key (CodigoPelicula, FechaDesde)
/*Tabla Genero*/
create table Genero
(
CodigoGenero int not null primary key,
Descripcion varchar(100)
)

3
Trabajo Practico: Video Club
Materia: Base de Datos

Universidad Abierta Interamericana


Rectorado: Chacabuco 90 - 1 Piso, Capital Federal Telfono: 4342-7788
Campus Lomas: Av. Hiplito Yrigoyen 9963, Lomas de Zamora - Provincia de Bs. As. Telfono: 4243-4827 / 4244-5839
http://www.vaneduc.edu.ar/uai/

/*Tabla Pelicula*/
create table Pelicula
(
CodigoPelicula int not null primary key,
Titulo varchar(100),
CodigoGenero int,
CodigoCalificacion int,
CodigoDirector int,
CodigoProductora int
)
/*Tabla Pelicula_Actor*/
create table Pelicula_Actor
(
CodigoPelicula int not null,
CodigoActor int not null
)
Alter table Pelicula_Actor
add constraint PK_Pelicula_Actor
primary key (CodigoPelicula, CodigoActor)
/*Tabla Productora*/
create table Productora
(
CodigoProductora int not null primary key,
NombreProductora varchar (100)
)
/*Tabla Socio*/
create table Socio
(
NumeroSocio int not null primary key,
ApellidoNombre varchar (50),
Direccion varchar (50)
)
/*Tabla Socio_Telefono*/
create table Socio_Telefono
(NumeroSocio int not null,
Telefono varchar(20) not null
)
Alter table Socio_Telefono
add constraint PK_Socio_Telefono
primary key (NumeroSocio, Telefono)
/*Tabla Stock_Pelicula*/
create table Stock_Pelicula
(CodigoPelicula int not null,
NumeroCopia int not null
)
Alter table Stock_Pelicula
add constraint PK_Stock_Pelicula
primary key(CodigoPelicula, NumeroCopia)
/*Tabla Calificacion*/
create table Calificacion
(
CodigoCalificacion int not null primary key,
Calificacion Varchar(100)
)

4
Trabajo Practico: Video Club
Materia: Base de Datos

Universidad Abierta Interamericana


Rectorado: Chacabuco 90 - 1 Piso, Capital Federal Telfono: 4342-7788
Campus Lomas: Av. Hiplito Yrigoyen 9963, Lomas de Zamora - Provincia de Bs. As. Telfono: 4243-4827 / 4244-5839
http://www.vaneduc.edu.ar/uai/

Crear las Claves Forneas


Alter Table Alquiler_Cab with check add constraint
FK_AlquilerCab_Socio foreign key(NumeroSocio)
references Socio(NumeroSocio)
Alter Table Alquiler_Det with check add constraint
FK_AlquilerDet_AlquilerCab foreign key(CodigoAlquiler)
references Alquiler_Cab(CodigoAlquiler)
Alter Table Alquiler_Det with check add constraint
FK_AlquilerDet_StockPelicula foreign key(CodigoPelicula, NumeroCopia)
references Stock_Pelicula(CodigoPelicula, NumeroCopia)
Alter Table Estreno with check add constraint
FK_Estreno_Pelicula foreign key(CodigoPelicula)
references Pelicula(CodigoPelicula)
Alter Table Pelicula with check add constraint
FK_Pelicula_Director foreign key(CodigoDirector)
references Director(CodigoDirector)
Alter Table Pelicula with check add constraint
FK_Pelicula_Genero foreign key(CodigoGenero)
references Genero(CodigoGenero)
Alter Table Pelicula with check add constraint
FK_Pelicula_Productora foreign key(CodigoProductora)
references Productora(CodigoProductora)
Alter Table Pelicula_Actor with check add constraint
FK_PeliculaActor_Pelicula foreign key(CodigoPelicula)
references Pelicula(CodigoPelicula)
Alter Table Socio_Telefono with check add constraint
FK_SocioTelefono_Socio foreign key(NumeroSocio)
references Socio(NumeroSocio)
Alter Table Stock_Pelicula with check add constraint
FK_StockPelicula_Pelicula foreign key(CodigoPelicula)
references Pelicula(CodigoPelicula)
Alter Table Pelicula_Actor with check add constraint
FK_PeliculaActor_Actor foreign key(CodigoActor)
references Actor(CodigoActor)
Alter Table Pelicula with check add constraint
FK_Pelicula_Calificacion foreign key(CodigoCalificacion)
references Calificacion(CodigoCalificacion)

Borrar todos los datos de las tablas


delete
delete
delete
delete
delete
delete
delete
Delete
delete
delete

Socio_Telefono
Alquiler_Det
Alquiler_Cab
Stock_Pelicula
Estreno
Pelicula_Actor
Pelicula
Actor
Calificacion
Socio

5
Trabajo Practico: Video Club
Materia: Base de Datos

Universidad Abierta Interamericana


Rectorado: Chacabuco 90 - 1 Piso, Capital Federal Telfono: 4342-7788
Campus Lomas: Av. Hiplito Yrigoyen 9963, Lomas de Zamora - Provincia de Bs. As. Telfono: 4243-4827 / 4244-5839
http://www.vaneduc.edu.ar/uai/

delete Productora
delete Director
delete Genero

Insertar todos los datos en las tablas


/*Genero*/
insert into
insert into
insert into
insert into
insert into

Genero
Genero
Genero
Genero
Genero

values
values
values
values
values

/*Calificacion*/
insert into Calificacion
insert into Calificacion
insert into Calificacion
insert into Calificacion
insert into Calificacion

(1,
(2,
(3,
(4,
(5,

'Terror')
'Accion')
'Suspenso')
'Infantil')
'Comedia')

values
values
values
values
values

(1,
(2,
(3,
(4,
(5,

'Adultos')
'Chicos')
'Todo Publico')
'Mayor de 13 aos')
'Mayor de 18 aos')

/*Socio*/
insert into Socio values (1, 'Gomez Luis', 'Lavalle 3561')
insert into Socio values (2, 'Perez Mario', 'Lavalle 78')
insert into Socio values (3, 'Gomez Marisa', 'Viamonte 741')
/*Socio_Telefono*/
insert into Socio_Telefono
insert into Socio_Telefono
insert into Socio_Telefono
insert into Socio_Telefono
/*Director*/
insert into Director
insert into Director
insert into Director
insert into Director
/*Actor*/
insert into
insert into
insert into
insert into
insert into

Actor
Actor
Actor
Actor
Actor

values
values
values
values

values
values
values
values
values

/*Productora*/
insert into Productora
insert into Productora
insert into Productora
insert into Productora
insert into Productora
/*Pelicula*/
insert into Pelicula
insert into Pelicula
insert into Pelicula
insert into Pelicula
insert into Pelicula
insert into Pelicula

values
values
values
values

(1,
(2,
(3,
(4,

(1,
(2,
(3,
(4,
(5,

'4897-2110')
'4897-2114')
'4897-2110')
'15-9874-2154')

'Mel', 'Gibson')
'Quentin', 'Tarantino')
'Woody', 'Allen')
'Francis Ford', 'Coppola')

'Hulk Hogan')
'Antony Hopking')
'Ornalla Mutti')
'Charles Bronson')
'Antony Queen')

values
values
values
values
values

values
values
values
values
values
values

(1,
(1,
(2,
(2,

(1,
(2,
(3,
(4,
(5,

(1,
(2,
(3,
(4,
(5,
(6,

'Metro Goldwun Maier')


'Televisa Mexico')
'Telefe')
'Fox')
'Univision')

'La Ultima Cena',1,1,1,4)


'Los 4 Fantasticos',2,2,3,1)
'El Zorro',1,4,1,4)
'Arma Mortal IV',2,1,3,1)
'Caperucita Roja',2,1,3,1)
'Rambo I',3,1,3,1)

/*Estreno*/
insert into Estreno values (1, '10/05/2010', '12/12/2011')
insert into Estreno values (2, '05/14/2010', '12/18/2011')

6
Trabajo Practico: Video Club
Materia: Base de Datos

Universidad Abierta Interamericana


Rectorado: Chacabuco 90 - 1 Piso, Capital Federal Telfono: 4342-7788
Campus Lomas: Av. Hiplito Yrigoyen 9963, Lomas de Zamora - Provincia de Bs. As. Telfono: 4243-4827 / 4244-5839
http://www.vaneduc.edu.ar/uai/

insert into Estreno values (3, '05/19/2010', '12/20/2012')


/*Pelicula_Actor*/
insert into Pelicula_Actor values (1, 1)
insert into Pelicula_Actor values (1, 3)
insert into Pelicula_Actor values (2, 1)
/*Stock_Pelicula*/
insert into Stock_Pelicula
insert into Stock_Pelicula
insert into Stock_Pelicula
insert into Stock_Pelicula
insert into Stock_Pelicula
insert into Stock_Pelicula
insert into Stock_Pelicula
insert into Stock_Pelicula
insert into Stock_Pelicula
insert into Stock_Pelicula
insert into Stock_Pelicula
insert into Stock_Pelicula

values
values
values
values
values
values
values
values
values
values
values
values

(1,
(1,
(1,
(1,
(1,
(2,
(2,
(2,
(2,
(3,
(3,
(3,

1)
2)
3)
4)
5)
1)
2)
3)
4)
1)
2)
3)

/*Alquiler_Cab*/
insert into Alquiler_Cab
insert into Alquiler_Cab
insert into Alquiler_Cab
insert into Alquiler_Cab

values
values
values
values

(1,
(2,
(3,
(4,

/*Alquiler_Det*/
insert into Alquiler_Det
insert into Alquiler_Det
insert into Alquiler_Det
insert into Alquiler_Det
insert into Alquiler_Det
insert into Alquiler_Det

values
values
values
values
values
values

(1,1,1,5)
(1,2,1,15)
(2,2,1,15)
(2,2,3,15)
(3,2,4,15)
(4,1,5,15)

'1/6/2010',
'1/6/2010',
'1/6/2010',
'2/6/2010',

1,
1,
1,
1,

NULL)
NULL)
NULL)
NULL)

7
Trabajo Practico: Video Club
Materia: Base de Datos

Universidad Abierta Interamericana


Rectorado: Chacabuco 90 - 1 Piso, Capital Federal Telfono: 4342-7788
Campus Lomas: Av. Hiplito Yrigoyen 9963, Lomas de Zamora - Provincia de Bs. As. Telfono: 4243-4827 / 4244-5839
http://www.vaneduc.edu.ar/uai/

Diagrama de Base de Datos:

8
Trabajo Practico: Video Club
Materia: Base de Datos

Universidad Abierta Interamericana


Rectorado: Chacabuco 90 - 1 Piso, Capital Federal Telfono: 4342-7788
Campus Lomas: Av. Hiplito Yrigoyen 9963, Lomas de Zamora - Provincia de Bs. As. Telfono: 4243-4827 / 4244-5839
http://www.vaneduc.edu.ar/uai/

Consultas sobre la BD:


Consulta 1
/*Mostrar todas las peliculas que posee el Video Club*/
select * from pelicula

Consulta 2
/*Mostrar el codigo y la Calificacion de la Pelicula 'Los 4 Fantasticos'*/
select
G.descripcion as Genero,
C.calificacion as Calificacion
from Pelicula P
inner join Genero G on P.CodigoGenero = G.CodigoGenero
inner join Calificacion C on P.CodigoCalificacion = c.CodigoCalificacion
where P.Titulo = 'Los 4 Fantasticos'

Consulta 3
/*Mostrar la Cantidad de Peliculas por Productora*/
select P.CodigoProductora, COUNT(*)
from Productora P
group by P.CodigoProductora

Consulta 4
/*Mostrar la Productora que comercializa mas de 2 peliculas*/
select P.CodigoProductora, COUNT(*)
from Productora P
group by P.CodigoProductora
having COUNT (*)>=2

9
Trabajo Practico: Video Club
Materia: Base de Datos

Universidad Abierta Interamericana


Rectorado: Chacabuco 90 - 1 Piso, Capital Federal Telfono: 4342-7788
Campus Lomas: Av. Hiplito Yrigoyen 9963, Lomas de Zamora - Provincia de Bs. As. Telfono: 4243-4827 / 4244-5839
http://www.vaneduc.edu.ar/uai/

Consulta 5
/*Mostrar la cantidad de Peliculas esrenadas para el ao 2010*/
select COUNT(*)
from estreno E
where YEAR (E.FechaDesde)=2010

Consulta 6
/*Mostrar la cantidad de Peliculas esrenadas para el ao 2009 y 2010*/
select COUNT(*)
from estreno E
where YEAR (E.FechaDesde) in (2009,2010)

Consulta 7
/*Mostrar la cantidad de alquileres realizados por mes*/
select month(a.FechaAlquiler) as MesAlquiler, COUNT(*) as TotAlquiler
from alquiler_cab A
group by month(a.FechaAlquiler)
/*Otra forma (uso de la funcion DatePart)*/
select DATEPART (month, A.FechaAlquiler) as MesAlquiler, COUNT(*) as
TotAlquiler
from Alquiler_Cab A
group by DATEPART(month, A.FechaAlquiler)

Consulta 8
/*Mostrar la cantidad de alquileres realizados por mes (mostrado en letras)*/
select
case
when DATEPART(MONTH,A.FechaAlquiler)=1 then 'Enero'
when DATEPART(MONTH,A.FechaAlquiler)=2 then 'Febrero'
when DATEPART(MONTH,A.FechaAlquiler)=3 then 'Marzo'
when DATEPART(MONTH,A.FechaAlquiler)=4 then 'Abril'
when DATEPART(MONTH,A.FechaAlquiler)=5 then 'Mayo'
when DATEPART(MONTH,A.FechaAlquiler)=6 then 'Junio'
when DATEPART(MONTH,A.FechaAlquiler)=7 then 'Julio'
when DATEPART(MONTH,A.FechaAlquiler)=8 then 'Agosto'
when DATEPART(MONTH,A.FechaAlquiler)=9 then 'Septiembre'
when DATEPART(MONTH,A.FechaAlquiler)=10 then 'Octubre'
when DATEPART(MONTH,A.FechaAlquiler)=11 then 'Noviembre'
when DATEPART(MONTH,A.FechaAlquiler)=12 then 'Diciembre'
end MesAlquiler,
COUNT (*) as TotAlquiler
from Alquiler_Cab A
group by datepart (MONTH, A.FechaAlquiler)

10
Trabajo Practico: Video Club
Materia: Base de Datos

Universidad Abierta Interamericana


Rectorado: Chacabuco 90 - 1 Piso, Capital Federal Telfono: 4342-7788
Campus Lomas: Av. Hiplito Yrigoyen 9963, Lomas de Zamora - Provincia de Bs. As. Telfono: 4243-4827 / 4244-5839
http://www.vaneduc.edu.ar/uai/

Consulta 9
/*Mostrar los meses (mostrado en letras) que menos alquileres tuvieron en
primer lugar */
select
case
when DATEPART(MONTH,A.FechaAlquiler)=1 then 'Enero'
when DATEPART(MONTH,A.FechaAlquiler)=2 then 'Febrero'
when DATEPART(MONTH,A.FechaAlquiler)=3 then 'Marzo'
when DATEPART(MONTH,A.FechaAlquiler)=4 then 'Abril'
when DATEPART(MONTH,A.FechaAlquiler)=5 then 'Mayo'
when DATEPART(MONTH,A.FechaAlquiler)=6 then 'Junio'
when DATEPART(MONTH,A.FechaAlquiler)=7 then 'Julio'
when DATEPART(MONTH,A.FechaAlquiler)=8 then 'Agosto'
when DATEPART(MONTH,A.FechaAlquiler)=9 then 'Septiembre'
when DATEPART(MONTH,A.FechaAlquiler)=10 then 'Octubre'
when DATEPART(MONTH,A.FechaAlquiler)=11 then 'Noviembre'
when DATEPART(MONTH,A.FechaAlquiler)=12 then 'Diciembre'
end MesAlquiler,
COUNT (*) as TotAlquiler
from Alquiler_Cab A
group by datepart (MONTH, A.FechaAlquiler)
order by 2 desc
/*Otra forma (usando el ordenamiento con un count(*))*/
select
case
when DATEPART(MONTH,A.FechaAlquiler)=1 then 'Enero'
when DATEPART(MONTH,A.FechaAlquiler)=2 then 'Febrero'
when DATEPART(MONTH,A.FechaAlquiler)=3 then 'Marzo'
when DATEPART(MONTH,A.FechaAlquiler)=4 then 'Abril'
when DATEPART(MONTH,A.FechaAlquiler)=5 then 'Mayo'
when DATEPART(MONTH,A.FechaAlquiler)=6 then 'Junio'
when DATEPART(MONTH,A.FechaAlquiler)=7 then 'Julio'
when DATEPART(MONTH,A.FechaAlquiler)=8 then 'Agosto'
when DATEPART(MONTH,A.FechaAlquiler)=9 then 'Septiembre'
when DATEPART(MONTH,A.FechaAlquiler)=10 then 'Octubre'
when DATEPART(MONTH,A.FechaAlquiler)=11 then 'Noviembre'
when DATEPART(MONTH,A.FechaAlquiler)=12 then 'Diciembre'
end MesAlquiler,
COUNT (*) as TotAlquiler
from Alquiler_Cab A
group by datepart (MONTH, A.FechaAlquiler)
order by count(*) desc

11
Trabajo Practico: Video Club
Materia: Base de Datos

Universidad Abierta Interamericana


Rectorado: Chacabuco 90 - 1 Piso, Capital Federal Telfono: 4342-7788
Campus Lomas: Av. Hiplito Yrigoyen 9963, Lomas de Zamora - Provincia de Bs. As. Telfono: 4243-4827 / 4244-5839
http://www.vaneduc.edu.ar/uai/

Consulta 10
/*Mostrar los telefonos de todos los socios */
select
S.NumeroSocio, S.ApellidoNombre, T.Telefono
from Socio S, Socio_Telefono T
where S.NumeroSocio = T.NumeroSocio
/*Otra forma (usando inner join)*/
select
S.NumeroSocio, S.ApellidoNombre, T.Telefono
from Socio S
inner join Socio_Telefono T on S.NumeroSocio = T.NumeroSocio

Consulta 11
/*Mostrar los socios, tengan o no telefono, y sus telefonos (en caso de que un
socio no tenga telefono mostrarlo igual) */
select
S.NumeroSocio, S.ApellidoNombre, T.Telefono
from Socio S
left join Socio_Telefono T on S.NumeroSocio = T.NumeroSocio

Consulta 12
/*Mostrar solo los socios sin telefono */
select
S.NumeroSocio, S.ApellidoNombre, T.Telefono
from Socio S
left join Socio_Telefono T on S.NumeroSocio = T.NumeroSocio
where T.NumeroSocio is null

/*Otra forma (usando subconsultas)*/


select
S.NumeroSocio, S.ApellidoNombre
from Socio S
where S.NumeroSocio not in (select numerosocio from socio_telefono)

12
Trabajo Practico: Video Club
Materia: Base de Datos

Universidad Abierta Interamericana


Rectorado: Chacabuco 90 - 1 Piso, Capital Federal Telfono: 4342-7788
Campus Lomas: Av. Hiplito Yrigoyen 9963, Lomas de Zamora - Provincia de Bs. As. Telfono: 4243-4827 / 4244-5839
http://www.vaneduc.edu.ar/uai/

/*Otra forma (agregandole una descripcion de sin telefono cuando el telefono


es nulo, usando la funcion isnull)*/
select
S.NumeroSocio, S.ApellidoNombre, isnull(T.Telefono, 'Sin Telfono')
from Socio S
left join Socio_Telefono T on S.NumeroSocio = T.NumeroSocio
where T.NumeroSocio is null

/*Otra forma (idem anterior, pero en lugar de la funcion isnull utilizando un


case)*/
select
S.NumeroSocio, S.ApellidoNombre,
case
when T.Telefono is null then 'Sin Telfono'
else T.Telefono
end Telefono
from Socio S
left join Socio_Telefono T on S.NumeroSocio = T.NumeroSocio
where T.NumeroSocio is null

/*Otra variante del anterior cambiando la validacion*/


select
S.NumeroSocio, S.ApellidoNombre,
case
when T.Telefono is not null then T.Telefono
else 'Sin Telfono'
end Telefono
from Socio S
left join Socio_Telefono T on S.NumeroSocio = T.NumeroSocio
where T.NumeroSocio is null

Consulta 13
/*Mostrar las Peliculas alquiladas, su fecha de alquiler, en que mes, y su
titulo ordenadas de la ultima a la mas reciente*/
select
A.Codigoalquiler,
convert (Varchar (20), A.FechaAlquiler, 103) as FechaAlquiler,
Mes = month(A.FechaAlquiler),
P.Titulo, D.CodigoPelicula, D.NumeroCopia
from Alquiler_Cab A, Alquiler_Det D, Pelicula P
where A.CodigoAlquiler = D.Codigoalquiler
and D.CodigoPelicula = P.CodigoPelicula
order by A.FechaAlquiler desc

13
Trabajo Practico: Video Club
Materia: Base de Datos

Universidad Abierta Interamericana


Rectorado: Chacabuco 90 - 1 Piso, Capital Federal Telfono: 4342-7788
Campus Lomas: Av. Hiplito Yrigoyen 9963, Lomas de Zamora - Provincia de Bs. As. Telfono: 4243-4827 / 4244-5839
http://www.vaneduc.edu.ar/uai/

Consulta 14
/*Mostrar la Pelicula mas alquiladas*/
select top 1
P.Titulo,
count (*) CantVecesAlquilada
from Alquiler_Cab A, Alquiler_Det D, Pelicula P
where A.CodigoAlquiler = D.Codigoalquiler
and D.CodigoPelicula = P.CodigoPelicula
group by P.Titulo
order by count(*) desc

Consulta 15
/*Mostrar las Peliculas por Calificacion*/
select C.Calificacion,
count (*) Cantidad
from Pelicula P, Calificacion C
where P.CodigoCalificacion = C.CodigoCalificacion
group by C.Calificacion

Consulta 16
/*Mostrar el Socio que mas alquilo por mes*/
select top 1
S.ApellidoNombre,
Mes = month(A.FechaAlquiler),
count (*) Alquileres
from Alquiler_Cab A, Socio S
where A.NumeroSocio = S.NumeroSocio
group by S.ApellidoNombre, month (A.FechaAlquiler)
order by 3 desc

14
Trabajo Practico: Video Club
Materia: Base de Datos

Universidad Abierta Interamericana


Rectorado: Chacabuco 90 - 1 Piso, Capital Federal Telfono: 4342-7788
Campus Lomas: Av. Hiplito Yrigoyen 9963, Lomas de Zamora - Provincia de Bs. As. Telfono: 4243-4827 / 4244-5839
http://www.vaneduc.edu.ar/uai/

Consultas sobre la BD (Insert / Delete / Update):


Consulta 17
/*Agregar el Acento a la Calificacion para que diga 'Pblico'*/
update Calificacion
set Calificacion = 'Todo Pblico'
where CodigoCalificacion =3
/*Verificar si fue cambiado el Registro*/
select *
from Calificacion
where CodigoCalificacion =3

Consulta 18
/*Agregar cuatro nuevos actores*/
insert into Actor values (6, 'Amber Benson')
insert into Actor values (7, 'Johnny Deep')
insert into Actor values (8, 'Tom Hanks')
insert into Actor values (9, 'Jackie Chan')
insert into Actor values (10, 'Gary Coleman')
/*Verificar si fue cambiado el Registro*/
select *
from Actor

Consulta 19
/*Actualizar datos*/
/*El director 1 cambiarlo a Michael Curtiz*/
update Director
set Nombre='Michael', Apellido='Curtiz'
where CodigoDirector=1
/*Los Actores 1 y 3 actualizarlos a Humphrey Bogart e Ingrid Bergman,
respectivamente*/
update Actor
set NombreApellido ='Humphrey Bogart'
where CodigoActor=1

15
Trabajo Practico: Video Club
Materia: Base de Datos

Universidad Abierta Interamericana


Rectorado: Chacabuco 90 - 1 Piso, Capital Federal Telfono: 4342-7788
Campus Lomas: Av. Hiplito Yrigoyen 9963, Lomas de Zamora - Provincia de Bs. As. Telfono: 4243-4827 / 4244-5839
http://www.vaneduc.edu.ar/uai/

update Actor
set NombreApellido ='Ingrid Bergman'
where CodigoActor=3
/*Agregar un actor en la tabla Actores*/
insert into Actor values (11, 'Paul Henreid')
/*Agregar la relacion de la pelicula 1 con este actor*/
insert Pelicula_Actor values (1,11)
/*Actualizar la productora 4 a Warner Bros*/
update Productora
set NombreProductora ='Warner Bros'
where CodigoProductora=4
/*Actualice la pelicula 2, cambiando su director a Tim Story*/
update Director
set Nombre='Tim', Apellido='Story'
where CodigoDirector=3
/*Actualizar la productora 4 a 20th Century Fox*/
update Productora
set NombreProductora ='20th Century Fox'
where CodigoProductora=1
/*Para el Actor 2, actualice su nombre y apellido*/
update Actor
set NombreApellido ='Loan Gruffudd'
where CodigoActor=2
/*Para el Actor 4, actualice su nombre y apellido*/
update Actor
set NombreApellido ='Jessica Alba'
where CodigoActor=4
/*Para el Actor 5, actualice su nombre y apellido*/
update Actor
set NombreApellido ='Michael Chiklis'
where CodigoActor=5
/*Agregar un actor en la tabla Actores*/
insert into Actor values (12, 'Chris Evans')
/*Agregar un actor en la tabla Actores*/
insert into Actor values (13, 'Julian McMahon')
/*Agregar la relacion en Pelicula_Actor llevandola de CodigoPelicula=2 y
CodigoActor=1 a CodigoPelicula=2 y CodigoActor=2*/
update Pelicula_Actor
set CodigoActor=2
where CodigoPelicula=2 and CodigoActor=1
/*Agregar las relaciones de la Pelicula 'Los 4 Fantasticos' con los actores
agregados*/
insert Pelicula_Actor values(2,4)
insert Pelicula_Actor values (2,5)
insert Pelicula_Actor values(2,12)
insert Pelicula_Actor values(2,13)
/*Agregar nuevos Generos*/
insert Genero values (6, 'Ciencia Ficcion')
insert Genero values (7, 'Drama')

16
Trabajo Practico: Video Club
Materia: Base de Datos

Universidad Abierta Interamericana


Rectorado: Chacabuco 90 - 1 Piso, Capital Federal Telfono: 4342-7788
Campus Lomas: Av. Hiplito Yrigoyen 9963, Lomas de Zamora - Provincia de Bs. As. Telfono: 4243-4827 / 4244-5839
http://www.vaneduc.edu.ar/uai/

insert Genero values (8, 'Blica')


insert Genero values (9, 'Cine Nacional')
insert Genero values (10, 'Clasico')

17
Trabajo Practico: Video Club
Materia: Base de Datos

You might also like