Issuu on Google+

INSTITUTO POLITÉCNICO NACIONAL

“ESCUELA SUPERIOR DE COMPUTO” Bases de Datos.

PRÁCTICA ALUMNO: GAYTÁN ARCACIA HAZAEL

Grupo: 2CV1

Asesor: EULER HERNÁNDEZ CONTRERAS

20-Oct-10


1a Parte BD: HomeDepot 1. Mostrar el identificador del HomeDepot, el nombre y el teléfono de los empleados que Laboran en el DF. select homedepot.idhd,homedepot.estado,asociado.nombre,asociado.tel from homedepot,asociado where homedepot.estado="DF" and asociado.homedepot_idhd=homedepot.idhd; salida: +-------+----------------------------------+-------------------+---------------| idhd | nombre | tel | verificarEstado +-------+----------------------------------+-------------------+---------------| HD014 | AGUIRRE MARTINEZ ISRAEL | 01(55)51-22-33-00 | DF […] | HD020 | ZUNIGA SANCHEZ GERARDO | 01(55)51-22-33-00 | DF +-------+----------------------------------+-------------------+---------------105 rows in set (0.02 sec) 2. Mostrar el nombre y el email de aquellos socios que pertenezcan a los HOMEDEPOT’s existentes en el DF. select socio.nombre,socio.email,homedepot.estado from homedepot,hdsocio,socio where homedepot.idhd=hdsocio.homedepot_idhd and hdsocio.socio_idsocio=socio.idsocio and homedepot.estado="DF"; salida: +----------------------------------+--------------------+-----------------+ | nombre | email | VerificarEstado | +----------------------------------+--------------------+-----------------+ | GASTELUM SARO VICTOR MANUEL | escom@escom.ipn.mx | DF | ……… ….. … | LEON OROZCO DANIEL | escom@escom.ipn.mx | DF | +----------------------------------+--------------------+-----------------+ 15 rows in set (0.03 sec)


3. Agregar a la tabla Depto una columna donde se pueda dar de alta la extensi贸n telef贸nica de cada departamento alter table depto add column tel varchar(20); salida: +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | idDepto | varchar(20) | NO | PRI | NULL | | | nombre | varchar(50) | YES | | NULL | | | tel | varchar(20) | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) 4. Mostrar el nombre de aquellos socios que se apellidan HERNANDEZ y el nombre del HomeDepot donde ellos pertenecen select s.nombre, hd.nombre from homedepot hd, hdsocio hds, socio s where hd.idhd = hds.homedepot_idhd and s.idsocio = hds.socio_idsocio and s.nombre like 'HERNANDEZ%'; Salida: +----------------------------------+---------+ | nombre | nombre | +----------------------------------+---------+ | HERNANDEZ ESTRADA ALAN YESSAHIR | Coapa_2 | | HERNANDEZ ESTRADA ALAN YESSAHIR | Mixcoac | | HERNANDEZ ROSAS DANIEL ALEJANDRO | Mixcoac | +----------------------------------+---------+ 3 rows in set (0.03 sec) 5. Mostrar el nombre y el monto de cr茅dito de aquellos socios que tienen una tarjeta de PAGOS FIJOS select socio.nombre,socio.credito from tarjeta,socio where tarjeta.socio_idsocio=socio.idsocio and tarjeta.nombre="PAGOS FIJOS";


salida: +-----------------------------------------+---------+---------------------+ 6 | nombre | credito | verificacionTarjeta | +-----------------------------------------+---------+---------------------+ | ANGELES DE LA CRUZ ANDREE MICHEL IRVING | 8000 | PAGOS FIJOS | ……. …. … .. | LAGOS CAXNAJOY EDGAR JACOBO | 3000 | PAGOS FIJOS | +-----------------------------------------+---------+---------------------+ 19 rows in set (0.02 sec) 6. El Departamento de Recursos humanos solicita al área de sistemas dar de alta a los siguientes Asociados: Nombre: Tel Sex:

VILLA MARTÍNEZ LUIS MISAEL : 01(922)221-2222 Masculino

LOZADA RODRÍGUEZ AURORA 01(998)881-8888 Femenino

Home Asignado:

Tuxtla Gutierrez

Cancun

insert into asociado values ("110","( select idhd from homedepot where nombre = 'CANCUN')","LOZADA RODRIGUEZ AURORA","01(998)-881-8888"), ("302",( select idhd from homedepot where nombre = “TUXTLA GUTIERREZ”), "villa martinez luis misael","01(922)221-2222"); Query OK, 2 rows affected (0.04 sec) Records: 2 Duplicates: 0 Warnings: 0

7. Actualiza tu correo en la tabla socio. select nombre, email from socio where idsocio = "1010"; +---------------------------+-----------------------+ | nombre | email | +---------------------------+-----------------------+ | gaytan Arcacia hazael |aza_gaytan@itelcel.com | +---------------------------+-----------------------+ 1 row in set (0.00 sec) update socio set email = azaelgaytan@gmail.com' where idsocio = "1010"; Query OK, 1 row affected (0.08 sec) Rows matched: 1 Changed: 1 Warnings: 0


select nombre, email from socio where idsocio = "1010�; +---------------------------+---------------------+ | nombre | email | +---------------------------+---------------------+ | Gaytan Arcacia hazael | azaelgaytan@gmail.com | +---------------------------+---------------------+ 1 row in set (0.00 sec)

8. Eliminar la columna sexo de la tabla asociado. alter table asociado drop column sexo; Query OK, 17 rows affected (0.19 sec) Records: 17 Duplicates: 0 Warnings: 0 8 desc asociado; +----------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------------+-------------+------+-----+---------+-------+ | idAsociado | varchar(20) | NO | PRI | NULL | | | HomeDepot_idHD | varchar(20) | NO | MUL | NULL | | | nombre | varchar(50) | YES | | NULL | | | tel | varchar(30) | YES | | NULL | | +----------------+-------------+------+-----+---------+-------+ 4 rows in set (0.01 sec)


2a Parte BD: Publicaciones 1. Construye el siguiente esquema relacional usando DDL(Leguaje de Definici贸n de Datos)

mysql> create table autor( -> idAutor int(11) not null primary key, -> nombre varchar(45)); Query OK, 0 rows affected (0.15 sec) mysql> desc autor; +---------+-------------+------+-----+---------+-------+ | Field | Type

| Null | Key | Default | Extra |

+---------+-------------+------+-----+---------+-------+ 9 | idAutor | int(11)

| NO | PRI | NULL |

| nombre | varchar(45) | YES |

| NULL |

+---------+-------------+------+-----+---------+-------+ 2 rows in set (0.01 sec) mysql> create table editorial( -> ideditorial int(11) not null primary key, -> tel int(11), -> localidad varchar(45)); Query OK, 0 rows affected (0.08 sec)

| |


mysql> desc editorial; +-------------+-------------+------+-----+---------+-------+ | Field

| Type

| Null | Key | Default | Extra |

+-------------+-------------+------+-----+---------+-------+ | ideditorial | int(11) | tel

| int(11)

| NO | PRI | NULL |

| YES |

| NULL |

| localidad | varchar(45) | YES |

|

|

| NULL |

|

+-------------+-------------+------+-----+---------+-------+ 3 rows in set (0.01 sec) mysql> create table articulo( -> idarticulo int(11) not null primary key, -> nombre varchar(45), -> autor_idautor int(11) not null, -> foreign key (autor_idautor) references autor(idautor) on delete cascade on update cascade); Query OK, 0 rows affected (0.08 sec) 1 0 mysql> desc articulo; +---------------+-------------+------+-----+---------+-------+ | Field

| Type

| Null | Key | Default | Extra |

+---------------+-------------+------+-----+---------+-------+ | idarticulo | int(11) | nombre

| NO | PRI | NULL |

| varchar(45) | YES |

| autor_idautor | int(11)

| NULL |

| NO | MUL | NULL |

+---------------+-------------+------+-----+---------+-------+ 3 rows in set (0.01 sec)

| | |


mysql> create table publicacion( -> idpublicacion int(11) not null primary key, -> anio varchar(45), -> noejemplares int(11), -> editorial_ideditorial int(11) not null, -> articulo_idarticulo int(11) not null, -> foreign key(editorial_ideditorial) references editorial(ideditorial) on d elete cascade on update cascade, -> foreign key(articulo_idarticulo) references articulo(idarticulo) on delete cascade on update cascade); Query OK, 0 rows affected (0.16 sec)

mysql> desc publicacion; +-----------------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------------+-------------+------+-----+---------+-------+ | idpublicacion

| int(11)

| NO | PRI | NULL |

| 1 1

| anio | noejemplares

| varchar(45) | YES | | int(11)

| YES |

| NULL |

|

| NULL |

|

| editorial_ideditorial | int(11)

| NO | MUL | NULL |

|

| articulo_idarticulo | int(11)

| NO | MUL | NULL |

|

+-----------------------+-------------+------+-----+---------+-------+ 5 rows in set (0.01 sec) 2. Agregar un campo en la relación artículo para almacenar el “ABSTRACT”, NOTA: ABSTRACT, es un resumen de un artículo. mysql> alter table articulo add column abstract varchar(250); Query OK, 0 rows affected (0.28 sec) Records: 0 Duplicates: 0 Warnings: 0


mysql> desc articulo; +---------------+--------------+------+-----+---------+-------+ | Field

| Type

| Null | Key | Default | Extra |

+---------------+--------------+------+-----+---------+-------+ | idarticulo | int(11) | nombre

| NO | PRI | NULL |

| varchar(45) | YES |

| autor_idautor | int(11) | abstract

|

| NULL |

|

| NO | MUL | NULL |

| varchar(250) | YES |

| NULL |

| |

+---------------+--------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) 3. Agregar a la tabla autor y editorial un campo que permita almacenar el email. mysql> alter table autor add column email varchar(45); Query OK, 0 rows affected (0.17 sec) Records: 0 Duplicates: 0 Warnings: 0 2 mysql> desc autor; +---------+-------------+------+-----+---------+-------+ | Field | Type

| Null | Key | Default | Extra |

+---------+-------------+------+-----+---------+-------+ | idAutor | int(11)

| NO | PRI | NULL |

| nombre | varchar(45) | YES | | email | varchar(45) | YES |

| NULL | | NULL |

+---------+-------------+------+-----+---------+-------+ 3 rows in set (0.01 sec)

| | |


mysql> alter table editorial add column email varchar(45); Query OK, 0 rows affected (0.17 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc editorial; +-------------+-------------+------+-----+---------+-------+ | Field

| Type

| Null | Key | Default | Extra |

+-------------+-------------+------+-----+---------+-------+ | ideditorial | int(11) | tel

| int(11)

| NO | PRI | NULL |

| YES |

| NULL |

| localidad | varchar(45) | YES | | email

| varchar(45) | YES |

|

|

| NULL | | NULL |

| |

+-------------+-------------+------+-----+---------+-------+ 4 rows in set (0.01 sec) 4. Cambiar la definici贸n de la PK en AUTOR, quedando PK(idAutor, email).13 mysql> alter table articulo drop foreign key articulo_ibfk_1; Query OK, 0 rows affected (0.23 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table autor drop primary key; Query OK, 0 rows affected (0.23 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table autor -> modify column email varchar(45) not null, ->

add

key(idautor,email);

primary Query

OK, 0 rows affected (0.19 sec) Records: 0 Duplicates: 0 Warnings: 0


mysql> desc autor; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | idAutor | int(11) | NO | PRI | NULL | | | nombre | varchar(45) | YES | | NULL | | | email | varchar(45) | NO | PRI | NULL | | +---------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> alter table articulo add foreign key(autor_idautor) references autor(idau tor) on delete cascade on update cascade; Query OK, 0 rows affected (0.25 sec) Records: 0 Duplicates: 0 Warnings: 0

5. Quitar el campo Aテ前 en publicaciテウn y agregarlo en artテュculo. 1 4 mysql> alter table publicacion drop column anio; Query OK, 0 rows affected (0.17 sec) Records: 0 Duplicates: 0 Warnings: 0

mysql> desc publicacion; +-----------------------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------------+---------+------+-----+---------+-------+ | idpublicacion | int(11) | NO | PRI | NULL | | | noejemplares | int(11) | YES | | NULL | | | editorial_ideditorial | int(11) | NO | MUL | NULL | | articulo_idarticulo | int(11) | NO | MUL | NULL | +-----------------------+---------+------+-----+---------+-------+ 4 rows in set (0.01 sec)

| |


mysql> alter table articulo add column anio varchar(45); Query OK, 0 rows affected (0.19 sec) Records: 0 Duplicates: 0 Warnings: 0

mysql> desc articulo; +---------------+--------------+------+-----+---------+-------+ | Field

| Type

| Null | Key | Default | Extra |

+---------------+--------------+------+-----+---------+-------+ | idarticulo | int(11) | nombre

| NO | PRI | NULL |

| varchar(45) | YES |

| autor_idautor | int(11)

|

| NULL |

|

| NO | MUL | NULL |

| 5

| abstract | anio

| varchar(250) | YES | | varchar(45) | YES |

| NULL | | NULL |

| |

+---------------+--------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)


3ÂŞ Parte. BD: MixUp 1. Mediante DDL, construye el siguiente esquema relacional

2. Agregar un campo para almacenar la pĂĄgina web de cada mixup. mysql> create table socio( -> idsocio int(11) not null primary key, -> nombre varchar(45), -> direccion varchar(45), -> tel varchar(45), -> email varchar(45)); Query OK, 0 rows affected (0.09 sec)

mysql> desc socio; +-----------+-------------+------+-----+---------+-------+ | Field

| Type

| Null | Key | Default | Extra |

+-----------+-------------+------+-----+---------+-------+ | idsocio | int(11)

| NO | PRI | NULL |

|16

| nombre | varchar(45) | YES |

| NULL |

|

| direccion | varchar(45) | YES |

| NULL |

|

| tel | email

| varchar(45) | YES | | varchar(45) | YES |

| NULL |

|

| NULL |

+-----------+-------------+------+-----+---------+-------+ 5 rows in set (0.01 sec)

|


mysql> create table mixup( -> idmixup int(11) not null primary key, -> nombre varchar(45), -> direccion varchar(45), -> tel varchar(45), -> estado varchar(45)); Query OK, 0 rows affected (0.14 sec)

mysql> desc mixup; +-----------+-------------+------+-----+---------+-------+ | Field

| Type

| Null | Key | Default | Extra |

+-----------+-------------+------+-----+---------+-------+ | idmixup | int(11) | NO | PRI | NULL | | | nombre | varchar(45) | YES |

| NULL |

|

| direccion | varchar(45) | YES |

| NULL |

|

| tel

| varchar(45) | YES |

| NULL |

|

| estado | varchar(45) | YES | | NULL | | +-----------+-------------+------+-----+---------+-------+ 5 rows in set (0.01 sec)1 mysql> create table empleado( -> idempleado int(11) not null primary key, -> nombre varchar(45), -> tel varchar(45), mixup_idmixup int(11) not null, -> foreign key(mixup_idmixup) references mixup(idmixup) on delete cascade on update cascade); Query OK, 0 rows affected (0.08 sec)


mysql> desc empleado; +---------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+---------+-------+ | idempleado | int(11) | NO | PRI | NULL | | | nombre | tel

| varchar(45) | YES | | varchar(45) | YES |

| NULL |

| NULL |

| |

| mixup_idmixup | int(11) | NO | MUL | NULL | +---------------+-------------+------+-----+---------+-------+

|

4 rows in set (0.00 sec) mysql> create table mixupsocio( -> mixup_idmixup int(11) not null, -> socio_idsocio int(11) not null, -> primary key(mixup_idmixup,socio_idsocio), -> foreign key(mixup_idmixup) references mixup(idmixup) on delete cascade on update cascade, -> foreign key(socio_idsocio) references socio(idsocio) on delete cascade on update cascade); Query OK, 0 rows affected (0.14 sec) mysql> desc mixupsocio; +---------------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+---------+------+-----+---------+-------+ | mixup_idmixup | int(11) | NO | PRI | NULL | | | socio_idsocio | int(11) | NO | PRI | NULL | | +---------------+---------+------+-----+---------+-------+ 2 rows in set (0.01 sec) 3. Agregar un campo para almacenar la pรกgina web de cada mixup. mysql> alter table mixup add column web varchar(45); Query OK, 0 rows affected (0.18 sec) Records: 0 Duplicates: 0 Warnings: 0


mysql> desc mixup; +-----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+-------+ | idmixup | int(11) | NO | PRI | NULL | | | nombre | varchar(45) | YES |

| NULL |

|

| direccion | varchar(45) | YES |

| NULL |

|

| tel

| varchar(45) | YES |

| NULL |

| estado | varchar(45) | YES |

| NULL |

| web

| NULL |

| varchar(45) | YES |

|19 | |

+-----------+-------------+------+-----+---------+-------+ 6 rows in set (0.00 sec) 4. Eliminar el campo telĂŠfono de las entidades empleado y socio. mysql> alter table empleado drop column tel; Query OK, 0 rows affected (0.25 sec) Records: 0 Duplicates: 0 Warnings: 0

mysql> desc empleado; +---------------+-------------+------+-----+---------+-------+ | Field

| Type

| Null | Key | Default | Extra |

+---------------+-------------+------+-----+---------+-------+ | idempleado | int(11) | nombre

| NO | PRI | NULL |

| varchar(45) | YES |

| mixup_idmixup | int(11)

| NULL |

|

| NO | MUL | NULL |

+---------------+-------------+------+-----+---------+-------+ 3 rows in set (0.01 sec)

|

|


mysql> alter table socio drop column tel; Query OK, 0 rows affected (0.17 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc socio; +-----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra |0 +-----------+-------------+------+-----+---------+-------+ | idsocio | int(11) | NO | PRI | NULL | | | nombre | varchar(45) | YES |

| NULL |

|

| direccion | varchar(45) | YES |

| NULL |

|

| email | varchar(45) | YES | | NULL | | +-----------+-------------+------+-----+---------+-------+ 4 rows in set (0.01 sec)

5. Renombrar la tabla MixUpSocio quedando como MixS. mysql> alter table mixupsocio rename to mixs; Query OK, 0 rows affected (0.05 sec) mysql> show tables; +---------------------------+ | Tables_in_mixupBd | +---------------------------+ | empleado | | mixs | | mixup | | socio | +---------------------------+ 4 rows in set (0.00 sec) 6. Quitar el enlace entre la entidad Empleado y MixUp. mysql> alter table empleado drop foreign key empleado_ibfk_3; Query OK, 0 rows affected (0.16 sec) Records: 0 Duplicates: 0 Warnings: 0


2 mysql> alter table empleado drop key mixup_idmixup; Query OK, 0 rows affected (0.18 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc empleado; +---------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+---------+-------+ | idempleado | int(11) | NO | PRI | NULL | | | nombre | varchar(45) | YES | | NULL | | | mixup_idmixup | int(11) | NO | | NULL | | +---------------+-------------+------+-----+---------+-------+ 3 rows in set (0.01 sec) 7. Renombrar el campo nombre de socio, quedando como nombreSocio. mysql> alter table socio change column nombre nombresocio varchar(45); Query OK, 0 rows affected (0.16 sec) Records: 0 Duplicates: 0 Warnings: 0

mysql> desc socio; +-------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------+------+-----+---------+-------+ | idsocio | int(11) | NO | PRI | NULL | | | nombresocio | varchar(45) | YES | | NULL | | | direccion | varchar(45) | YES | | NULL | | | email | varchar(45) | YES | | NULL | |22 +-------------+-------------+------+-----+---------+-------+ 4 rows in set (0.01 sec)


Prueba-pdf