Sie sind auf Seite 1von 8

CREACIN BASE DE DATOS

mysql> create database taller;


Query OK, 1 row affected (0.31 sec)
HACER USO DE LA BASE DE DATOS
mysql> use taller;
Database changed
CREACIN TABLAS
mysql> create table ciclista(
-> dorsal int(10),
-> nombre varchar(100),
-> edad int(3),
-> nomeq varchar(100)
-> );
Query OK, 0 rows affected (0.70 sec)
mysql> create table equipo(
-> nomeq varchar(100),
-> descripcion varchar(100)
-> );
Query OK, 0 rows affected (0.65 sec)
mysql> create table etapa(
-> netapa int(10),
-> km int(10),
-> salida varchar(100),
-> llegada varchar(100),
-> dorsal int(10)
-> );
Query OK, 0 rows affected (0.37 sec)
mysql> create table llevar(
-> dorsal int(10),
-> netapa int(10),
-> codigo varchar(30)
-> );
Query OK, 0 rows affected (0.40 sec)
mysql> create table maillot(
-> codigo varchar(100),
-> tipo varchar(100),
-> color varchar(100),
-> premio int(40)
-> );
Query OK, 0 rows affected (0.63 sec)
mysql>
->
->
->
->
->
->
->

create table puerto(


nompuerto varchar(100),
altura int(10),
categoria varchar(100),
pendiente int(10),
netapa int(10),
dorsal int(10)
);

mysql> alter table llevar add column id_llevar int (10);


Query OK, 0 rows affected (0.53 sec)
Records: 0 Duplicates: 0 Warnings: 0
CREACIN LLAVES PRIMARIAS

mysql> alter table ciclista add primary key(dorsal);


Query OK, 0 rows affected (1.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table equipo add primary key(nomeq);
Query OK, 0 rows affected (0.76 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table etapa add primary key(netapa);
Query OK, 0 rows affected (0.68 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table puerto add primary key(nompuerto);
Query OK, 0 rows affected (1.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table maillot add primary key(codigo);
Query OK, 0 rows affected (0.98 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table llevar add primary key(id_llevar);
Query OK, 0 rows affected (0.93 sec)
Records: 0 Duplicates: 0 Warnings: 0
CREACION LLAVES FORANEAS
mysql> alter table ciclista add foreign key(nomeq) references equipo(nomeq);
Query OK, 0 rows affected (0.82 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table etapa add foreign key(dorsal) references ciclista(dorsal);
Query OK, 0 rows affected (1.44 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table llevar add foreign key(dorsal) references ciclista(dorsal);
Query OK, 0 rows affected (0.94 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table llevar add foreign key(netapa) references etapa(netapa);
Query OK, 0 rows affected (1.25 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table llevar add foreign key(codigo) references maillot(codigo);
Query OK, 0 rows affected (1.37 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table puerto add foreign key(netapa) references etapa(netapa);
Query OK, 0 rows affected (0.91 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table puerto add foreign key(dorsal) references ciclista(dorsal);
Query OK, 0 rows affected (1.30 sec)
Records: 0 Duplicates: 0 Warnings: 0
DESCRIBE DE LAS TABLAS(ESTRUCTURA)
mysql> describe ciclista
-> ;
+--------+--------------+------+-----+---------+-------+
| Field | Type
| Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+

| dorsal | int(10)
| NO | PRI | 0
|
|
| nombre | varchar(100) | YES |
| NULL
|
|
| edad | int(3)
| YES |
| NULL
|
|
| nomeq | varchar(100) | YES | MUL | NULL
|
|
+--------+--------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
mysql> describe equipo;
+-------------+--------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| nomeq
| varchar(100) | NO | PRI |
|
|
| descripcion | varchar(100) | YES |
| NULL
|
|
+-------------+--------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
mysql> describe etapa;
+---------+--------------+------+-----+---------+-------+
| Field | Type
| Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| netapa | int(10)
| NO | PRI | 0
|
|
| km
| int(10)
| YES |
| NULL
|
|
| salida | varchar(100) | YES |
| NULL
|
|
| llegada | varchar(100) | YES |
| NULL
|
|
| dorsal | int(10)
| YES | MUL | NULL
|
|
+---------+--------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
mysql> describe llevar;
+-----------+-------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id_llevar | int(10)
| NO | PRI | 0
|
|
| dorsal
| int(10)
| YES | MUL | NULL
|
|
| netapa
| int(10)
| YES | MUL | NULL
|
|
| codigo
| varchar(30) | YES | MUL | NULL
|
|
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
mysql> describe maillot;
+--------+--------------+------+-----+---------+-------+
| Field | Type
| Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| codigo | varchar(100) | NO | PRI |
|
|
| tipo | varchar(100) | YES |
| NULL
|
|
| color | varchar(100) | YES |
| NULL
|
|
| premio | int(40)
| YES |
| NULL
|
|
+--------+--------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
mysql> describe puerto;
+-----------+--------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| nompuerto | varchar(100) | NO | PRI |
|
|
| altura
| int(10)
| YES |
| NULL
|
|
| categoria | varchar(100) | YES |
| NULL
|
|
| pendiente | int(10)
| YES |
| NULL
|
|
| netapa
| int(10)
| YES | MUL | NULL
|
|
| dorsal
| int(10)
| YES | MUL | NULL
|
|

+-----------+--------------+------+-----+---------+-------+
6 rows in set (0.06 sec)
CARGAR REGISTROS A LA BASE DE DATOS
mysql> load data infile "Equipo.txt"
-> into table equipo
-> fields terminated by ' '
-> lines terminated by '\r\n';
Query OK, 10 rows affected (0.07 sec)
Records: 10 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from equipo;
+-----------------+--------------------+
| nomeq
| descripcion
|
+-----------------+--------------------+
| Amore Vita
| Ricardo Padacci
|
| Banesto
| Miguel Echevaerria |
| Bresciali-Refin | Pietro Armani
|
| Carrera
| Luigi Petroni
|
| Gatorade
| Gian Luca Pacceli |
| Kelme
| Alvaro Pino
|
| Mapei-Class
| Juan Fernandez
|
| Navigare
| Lorenzo Sciacci
|
| Telecom
| Morgan Reikcard
|
| TVM
| Steevens Henk
|
+-----------------+--------------------+
10 rows in set (0.00 sec)
mysql> load data infile "Maillot.txt"
-> into table Maillot
-> fields terminated by ' '
-> lines terminated by '\r\n';
Query OK, 6 rows affected (0.07 sec)
Records: 6 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from maillot;
+--------+-----------------+-------------------+---------+
| codigo | tipo
| color
| premio |
+--------+-----------------+-------------------+---------+
| MGE
| General
| Amarillo
| 1000000 |
| MMO
| Montaa
| Blanco y rojo
| 500000 |
| MMS
| Ms sufrido
| Estrellitas rojas | 400000 |
| MMV
| Metas volantes | Rojo
| 400000 |
| MRE
| Regularidad
| Verde
| 300000 |
| MSE
| Sprint especial | Rosa
| 300000 |
+--------+-----------------+-------------------+---------+
6 rows in set (0.00 sec)
mysql> load data infile "Etapa.txt"
-> into table etapa
-> fields terminated by ' '
-> lines terminated by '\r\n';
Query OK, 5 rows affected (0.07 sec)
Records: 5 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from etapa;
+--------+------+------------+--------------+--------+
| netapa | km | salida
| llegada
| dorsal |
+--------+------+------------+--------------+--------+
|
1 | 35 | Valladolid | vila
|
1 |

|
2 | 70 | Salamanca | Zamora
|
2 |
|
3 | 150 | Zamora
| Almendralejo |
1 |
|
4 | 330 | Crdoba
| Granada
|
1 |
|
5 | 150 | Granada
| Almera
|
3 |
+--------+------+------------+--------------+--------+
5 rows in set (0.00 sec)
mysql> load data infile "Ciclista.txt"
-> into table ciclista
-> fields terminated by ' '
-> lines terminated by '\r\n';
Query OK, 24 rows affected (0.07 sec)
Records: 24 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from ciclista;
+--------+------------------------+------+-----------------+
| dorsal | nombre
| edad | nomeq
|
+--------+------------------------+------+-----------------+
|
1 | Miguel Indurain
| 21 | Banesto
|
|
2 | Pedro Delgado
| 29 | Banesto
|
|
3 | Alex Zulle
| 20 | Navigare
|
|
4 | Alessio Di Basco
| 30 | TVM
|
|
5 | Armand
| 17 | Amore Vita
|
|
8 | Jean Van Poppel
| 24 | Bresciali-Refin |
|
9 | Maximo Podel
| 17 | Telecom
|
|
10 | Mario Cipollini
| 31 | Carrera
|
|
11 | Eddy Seigneur
| 20 | Amore Vita
|
|
12 | Alessio Di Basco
| 34 | Bresciali-Refin |
|
13 | Gianni Bugno
| 24 | Gatorade
|
|
15 | Jesus Montoya
| 25 | Amore Vita
|
|
16 | Dimitri Konishev
| 27 | Amore Vita
|
|
17 | Bruno Lealli
| 30 | Amore Vita
|
|
20 | Alfonso Gutierrez
| 27 | Navigare
|
|
22 | Giorgio Furlan
| 22 | Kelme
|
|
26 | Mikel Zarrabeitia
| 30 | Carrera
|
|
27 | Laurent Jalabert
| 22 | Banesto
|
|
30 | Melchor Mauri
| 26 | Mapei-Clas
|
|
31 | Per Pedersen
| 33 | Banesto
|
|
32 | Tony Rominger
| 31 | Kelme
|
|
33 | Stefenao della Sveitia | 26 | Amore Vita
|
|
34 | Claudio Chiapucci
| 23 | Amore Vita
|
|
35 | Gian Mateo Faluca
| 34 | TVM
|
+--------+------------------------+------+-----------------+
24 rows in set (0.00 sec)
mysql> load data infile "Puerto.txt"
-> into table puerto
-> fields terminated by ' '
-> lines terminated by '\r\n';
Query OK, 8 rows affected, 4 warnings (0.07 sec)
Records: 8 Deleted: 0 Skipped: 0 Warnings: 4
+----------------+--------+-----------+-----------+--------+--------+
| nompuerto
| altura | categoria | pendiente | netapa | dorsal |
+----------------+--------+-----------+-----------+--------+--------+
| p1
| 2489 | 1
|
34 |
2 |
3 |
| p2
| 2789 | 1
|
44 |
4 |
3 |
| Puerto 1
| 2500 | E
|
23 |
1 |
2 |
| Puerto F
| 2500 | E
|
17 |
4 |
2 |
| Puerto fff
| 2500 | E
|
17 |
4 |
2 |

| Puerto nuevo 1 | 2500 | a


|
17 |
4 |
1 |
| Puerto otro
| 2500 | E
|
17 |
4 |
1 |
+----------------+--------+-----------+-----------+--------+--------+
7 rows in set (0.00 sec)
mysql> load data infile "Llevar.txt"
-> into table llevar
-> fields terminated by ' '
-> lines terminated by '\r\n';
Query OK, 9 rows affected, 19 warnings (0.60 sec)
Records: 9 Deleted: 0 Skipped: 0 Warnings: 19
mysql> alter table llevar add column id_llevar int(4) primary key auto_increment
first;
Query OK, 0 rows affected (1.10 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from llevar;
+--------+--------+--------+
| dorsal | netapa | codigo |
+--------+--------+--------+
|
1 |
3 | MGE
|
|
1 |
4 | MGE
|
|
2 |
2 | MGE
|
|
3 |
1 | MGE
|
|
3 |
1 | MMV
|
|
3 |
4 | MRE
|
|
4 |
1 | MMO
|
|
5 |
2 | MMO
|
|
|
|
|
+--------+--------+--------+
9 rows in set (0.00 sec)
B) REALIZAR LAS SIGUIENTES CONSULTAS Y ENTREGAR SU RESULTADO (ENTREGAR CDIGO DE C
OMO LAS HIZO Y RESULTADOS)
Obtener el cdigo, el tipo, el color y el premio de todos los maillots que hay.
mysql> select maillot.codigo,maillot.tipo,maillot.color,maillot.premio,llevar.d
orsal,ciclista.nombre
-> from maillot,llevar,ciclista
-> where (llevar.codigo=maillot.codigo) and (llevar.dorsal=ciclista.dorsal);
+--------+----------------+---------------+---------+--------+-----------------+
| codigo | tipo
| color
| premio | dorsal | nombre
|
+--------+----------------+---------------+---------+--------+-----------------+
| MGE
| General
| Amarillo
| 1000000 |
1 | Miguel Indurain
|
| MGE
| General
| Amarillo
| 1000000 |
1 | Miguel Indurain
|
| MGE
| General
| Amarillo
| 1000000 |
2 | Pedro Delgado
|
| MGE
| General
| Amarillo
| 1000000 |
3 | Alex Zulle
|
| MMO
| Montaa
| Blanco y rojo | 500000 |
4 | Alessio Di Basco |
| MMO
| Montaa
| Blanco y rojo | 500000 |
5 | Armand
|
| MMV
| Metas volantes | Rojo
| 400000 |
3 | Alex Zulle
|

| MRE
| Regularidad
| Verde
| 300000 |
3 | Alex Zulle
|
+--------+----------------+---------------+---------+--------+-----------------+
8 rows in set (0.00 sec)
Obtener el dorsal y el nombre de los ciclistas cuya edad sea menor o igual que 2
5 aos.
mysql> select dorsal,nombre,edad
-> from ciclista
-> where(edad<=25);
+--------+-------------------+------+
| dorsal | nombre
| edad |
+--------+-------------------+------+
|
1 | Miguel Indurain | 21 |
|
3 | Alex Zulle
| 20 |
|
5 | Armand
| 17 |
|
8 | Jean Van Poppel | 24 |
|
9 | Maximo Podel
| 17 |
|
11 | Eddy Seigneur
| 20 |
|
13 | Gianni Bugno
| 24 |
|
15 | Jesus Montoya
| 25 |
|
22 | Giorgio Furlan
| 22 |
|
27 | Laurent Jalabert | 22 |
|
34 | Claudio Chiapucci | 23 |
+--------+-------------------+------+
11 rows in set (0.00 sec)
Obtener el nombre y la altura de todos los puertos de categora
mysql> select nompuerto,altura,categoria
-> from puerto
-> where(categoria='E');
+-------------+--------+-----------+
| nompuerto | altura | categoria |
+-------------+--------+-----------+
| Puerto 1
| 2500 | E
|
| Puerto F
| 2500 | E
|
| Puerto fff | 2500 | E
|
| Puerto otro | 2500 | E
|
+-------------+--------+-----------+
4 rows in set (0.00 sec)

E (Especial).

Obtener el nombre de cada ciclista junto con el nombre del equipo al que pertene
ce
mysql> select ciclista.nombre,ciclista.nomeq
-> from ciclista;
+------------------------+-----------------+
| nombre
| nomeq
|
+------------------------+-----------------+
| Miguel Indurain
| Banesto
|
| Pedro Delgado
| Banesto
|
| Alex Zulle
| Navigare
|
| Alessio Di Basco
| TVM
|
| Armand
| Amore Vita
|
| Jean Van Poppel
| Bresciali-Refin |
| Maximo Podel
| Telecom
|
| Mario Cipollini
| Carrera
|
| Eddy Seigneur
| Amore Vita
|
| Alessio Di Basco
| Bresciali-Refin |
| Gianni Bugno
| Gatorade
|

| Jesus Montoya
| Amore Vita
|
| Dimitri Konishev
| Amore Vita
|
| Bruno Lealli
| Amore Vita
|
| Alfonso Gutierrez
| Navigare
|
| Giorgio Furlan
| Kelme
|
| Mikel Zarrabeitia
| Carrera
|
| Laurent Jalabert
| Banesto
|
| Melchor Mauri
| Mapei-Clas
|
| Per Pedersen
| Banesto
|
| Tony Rominger
| Kelme
|
| Stefenao della Sveitia | Amore Vita
|
| Claudio Chiapucci
| Amore Vita
|
| Gian Mateo Faluca
| TVM
|
+------------------------+-----------------+
24 rows in set (0.00 sec)
Obtener el nombre de los ciclistas que sean de Banesto.
mysql> select ciclista.nombre,equipo.nomeq
-> from ciclista,equipo
-> where(ciclista.nomeq=equipo.nomeq) and (equipo.nomeq='BANESTO');
+------------------+---------+
| nombre
| nomeq |
+------------------+---------+
| Miguel Indurain | Banesto |
| Pedro Delgado
| Banesto |
| Laurent Jalabert | Banesto |
| Per Pedersen
| Banesto |
+------------------+---------+
4 rows in set (0.04 sec)
Cuntos ciclistas pertenecen al equipo Amore Vita?
mysql> select nomeq,count(*)
-> from ciclista
-> where(nomeq='AMORE VITA');
+------------+----------+
| nomeq
| count(*) |
+------------+----------+
| Amore Vita |
7 |
+------------+----------+
1 row in set (0.33 sec)
Nombre de los ciclistas que pertenezcan al mismo equipo que Miguel Indurain
mysql> select nombre,nomeq
-> from ciclista
-> where nomeq=(select nomeq from ciclista where nombre='Miguel Indurain');
+------------------+---------+
| nombre
| nomeq |
+------------------+---------+
| Miguel Indurain | Banesto |
| Pedro Delgado
| Banesto |
| Laurent Jalabert | Banesto |
| Per Pedersen
| Banesto |
+------------------+---------+
4 rows in set (0.00 sec)