Está en la página 1de 20

EXAMEN ORDINARIO

Descripción

INSTRUCCIONES: En el siguiente ejercicio escribe los comandos para llevar a


cabo cada una de las consultas solicitadas, ejecuta cada uno de ellos en el
SGBD de MySQL y escribe la sintaxis correspondiente.

NOTA: En un documento de WORD (no PDF o JPG u otro), escribe el enunciado


correspondiente, seguido de la sintaxis del comando SQL y el resultado
correspondiente de la salida. no realices una captura de pantalla de dicha
salida, sino que copia el resultado (y si quieres la sintaxis que aplicaste) y
pégala.

1.- Crea una base de datos llamada tienda.

mysql> create database tienda;

Query OK, 1 row affected (0.30 sec)

mysql>

2.- Muestra la base de datos almacenada.

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| clientes_ugrm |

| information_schema |

| mysql |

| performance_schema |

| sakila |

| sys |

| tienda |
| uaem |

| world |

+--------------------+

9 rows in set (0.01 sec)

mysql>

3. Habilita la Base de datos TIENDA.

mysql> use tienda;

Database changed

mysql> select database();

+------------+

| database() |

+------------+

| tienda |

+------------+

1 row in set (0.00 sec)

mysql>

4.- Genera las siguientes tablas:


 

mysql> create table fabricante (Clave_fabricante INT AUTO_INCREMENT


PRIMARY KEY, Nombre CHAR(30) not nu ll);

Query OK, 0 rows affected (2.27 sec)

mysql> create table articulo

-> (Clave_articulo INT(10) primary key,

-> Nombre CHAR(100) not null,

-> Precio FLOAT(10,2),

-> Clave_fabricante INT,

-> foreign key (Clave_fabricante) references fabricante(Clave_fabricante));

Query OK, 0 rows affected, 2 warnings (1.14 sec)

5.- Muestra las tablas de la base de datos TIENDA.

mysql> show tables;


+------------------+

| Tables_in_tienda |

+------------------+

| articulo |

| fabricante |

+------------------+

2 rows in set (0.00 sec)

6.- Muestra los atributos de la tabla ARTÍCULO.

mysql> explain articulo;

+------------------+------------+------+-----+---------+-------+

| Field | Type |N ull | Key | Default | Extra |

+------------------+------------+------+-----+---------+-------+

| Clave_articulo | int(11) | NO | PRI | NULL | |

| Nombre | char(1) | NO | | NULL | |

| Precio | float(4,2) | YES | | NULL | |

| Clave_fabricante | int(11) | YES | MUL | NULL | |

+------------------+------------+------+-----+---------+-------+

4 rows in set (0.07 sec)

mysql> explain fabricante;

+------------------+----------+------+-----+---------+----------------+

| Field | Type | Null | Key | Default | Extra |

+------------------+----------+------+-----+---------+----------------+

| Clave_fabricante | int(11) | NO | PRI | NULL | auto_increment |

| Nombre | char(30) | NO | | NULL | |

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

7.- Introduce los siguientes datos.


mysql> insert into fabricante VALUES('1', 'Kingston');

Query OK, 1 row affected (0.20 sec)

mysql> insert into fabricante VALUES('2', 'Adata');

Query OK, 1 row affected (0.21 sec)

mysql> insert into fabricante VALUES('3', 'Logitech');

Query OK, 1 row affected (0.18 sec)

mysql> insert into fabricante VALUES('4', 'Lexar');

Query OK, 1 row affected (0.13 sec)

mysql> insert into fabricante VALUES('5', 'Seagate');

Query OK, 1 row affected (0.13 sec)

mysql> insert into articulo VALUES('1', 'Teclado', '100.00', '3');

Query OK, 1 row affected (0.23 sec)

mysql> insert into articulo VALUES('2', 'Disco Duro 300 GB', '500.00', '5');

Query OK, 1 row affected (0.20 sec)

mysql> insert into articulo VALUES('3', 'Mouse', '80.00', '3');

Query OK, 1 row affected (0.16 sec)

mysql> insert into articulo VALUES('4', 'Memoria USB', '140.00', '4');

Query OK, 1 row affected (0.18 sec)

mysql> insert into articulo VALUES('5', 'Memoria RAM', '290.00', '1');

Query OK, 1 row affected (0.11 sec)

mysql> insert into articulo VALUES('6', 'Disco Duro Extraible 250 Gb',
'650.00', '5');

Query OK, 1 row affected (0.13 sec)

mysql> insert into articulo VALUES('7', 'Memoria USB', '279.00', '1');


Query OK, 1 row affected (0.12 sec)

mysql> insert into articulo VALUES('8', 'CD Rom', '200.00', '2');

Query OK, 1 row affected (0.12 sec)

mysql> insert into articulo VALUES('9', 'DVD Rom', '450.00', '2');

Query OK, 1 row affected (0.06 sec)

mysql> insert into articulo VALUES('10', 'Tarjeta de Red', '180.00', '3');

Query OK, 1 row affected (0.10 sec)

8.- Escribe la Sintaxis correspondiente para generar las siguientes consultas: Escribe
también en una hoja la sintaxis correspondiente en orden.

a)         Obtener todos los datos de los productos TIENDA.

mysql> select * from articulo;

+----------------+-----------------------------+--------+------------------+

| Clave_articulo | Nombre | Precio | Clave_fabricante |

+----------------+-----------------------------+--------+------------------+

| 1 | Teclado | 100.00 | 3|

| 2 | Disco Duro 300 GB | 500.00 | 5|

| 3 | Mouse | 80.00 | 3|

| 4 | Memoria USB | 140.00 | 4|

| 5 | Memoria RAM | 290.00 | 1|

| 6 | Disco Duro Extraible 250 Gb | 650.00 | 5|

| 7 | Memoria USB | 279.00 | 1|

| 8 | CD Rom | 200.00 | 2|

| 9 | DVD Rom | 450.00 | 2|


| 10 | Tarjeta de Red | 180.00 | 3|

+----------------+-----------------------------+--------+------------------+

10 rows in set (0.00 sec)

b)        Obtener los nombres de los productos de la tienda.

mysql> select Nombre from articulo;

+-----------------------------+

| Nombre |

+-----------------------------+

| Teclado |

| Disco Duro 300 GB |

| Mouse |

| Memoria USB |

| Memoria RAM |

| Disco Duro Extraible 250 Gb |

| Memoria USB |

| CD Rom |

| DVD Rom |

| Tarjeta de Red |

+-----------------------------+

10 rows in set (0.00 sec)

mysql>
c)         Obtener los nombres y precio de los productos de la tienda.

mysql> select Nombre, Precio from articulo;

+-----------------------------+--------+

| Nombre | Precio |

+-----------------------------+--------+

| Teclado | 100.00 |

| Disco Duro 300 GB | 500.00 |

| Mouse | 80.00 |

| Memoria USB | 140.00 |

| Memoria RAM | 290.00 |

| Disco Duro Extraible 250 Gb | 650.00 |

| Memoria USB | 279.00 |

| CD Rom | 200.00 |

| DVD Rom | 450.00 |

| Tarjeta de Red | 180.00 |

+-----------------------------+--------+

10 rows in set (0.00 sec)

d)        Obtener los nombres de los artículos sin repeticiones.

mysql> select distinct Nombre from articulo;

+-----------------------------+

| Nombre |

+-----------------------------+

| Teclado |

| Disco Duro 300 GB |


| Mouse |

| Memoria USB |

| Memoria RAM |

| Disco Duro Extraible 250 Gb |

| CD Rom |

| DVD Rom |

| Tarjeta de Red |

+-----------------------------+

9 rows in set (0.01 sec)

mysql>

e)        Obtener todos los datos del artículo cuya clave de producto es 5.

mysql> select * from articulo where Clave_articulo =5;

+----------------+-------------+--------+------------------+

| Clave_articulo | Nombre | Precio | Clave_fabricante |

+----------------+-------------+--------+------------------+

| 5 | Memoria RAM | 290.00 | 1|

+----------------+-------------+--------+------------------+

1 row in set (0.00 sec)

mysql>
f)         Obtener todos los datos del artículo cuyo nombre del producto es Teclado.

mysql> select * from articulo where Nombre ='Teclado';

+----------------+---------+--------+------------------+

| Clave_articulo | Nombre | Precio | Clave_fabricante |

+----------------+---------+--------+------------------+

| 1 | Teclado | 100.00 | 3|

+----------------+---------+--------+------------------+

1 row in set (0.00 sec)

mysql>

g)         Obtener todos los datos de la Memoria RAM y Memoria USB.

mysql> select Clave_articulo, Nombre, Precio, Clave_fabricante from articulo


where Nombre LIKE "%Memoria%";

+----------------+-------------+--------+------------------+

| Clave_articulo | Nombre | Precio | Clave_fabricante |

+----------------+-------------+--------+------------------+

| 4 | Memoria USB | 140.00 | 4|

| 5 | Memoria RAM | 290.00 | 1|

| 7 | Memoria USB | 279.00 | 1|

+----------------+-------------+--------+------------------+

3 rows in set (0.00 sec)

h)        Obtener los datos de los artículos que empiezan con M.

mysql> select * from articulo where Nombre regexp "^m|M."


-> ;

+----------------+-------------+--------+------------------+

| Clave_articulo | Nombre | Precio | Clave_fabricante |

+----------------+-------------+--------+------------------+

| 3 | Mouse | 80.00 | 3|

| 4 | Memoria USB | 140.00 | 4|

| 5 | Memoria RAM | 290.00 | 1|

| 7 | Memoria USB | 279.00 | 1|

+----------------+-------------+--------+------------------+

4 rows in set (0.12 sec)

mysql>

i)          Obtener el nombre de los productos donde el precio sea $ 100.00.

mysql> select * from articulo where Precio ='100.00';

+----------------+---------+--------+------------------+

| Clave_articulo | Nombre | Precio | Clave_fabricante |

+----------------+---------+--------+------------------+

| 1 | Teclado | 100.00 | 3|

+----------------+---------+--------+------------------+

1 row in set (0.00 sec)

mysql>
j)          Obtener el nombre de los productos donde el precio sea mayor a $ 200.00.

mysql> select * from articulo where Precio >='200.00';

+----------------+-----------------------------+--------+------------------+

| Clave_articulo | Nombre | Precio | Clave_fabricante |

+----------------+-----------------------------+--------+------------------+

| 2 | Disco Duro 300 GB | 500.00 | 5|

| 5 | Memoria RAM | 290.00 | 1|

| 6 | Disco Duro Extraible 250 Gb | 650.00 | 5|

| 7 | Memoria USB | 279.00 | 1|

| 8 | CD Rom | 200.00 | 2|

| 9 | DVD Rom | 450.00 | 2|

+----------------+-----------------------------+--------+------------------+

6 rows in set (0.00 sec)

k)         Obtener todos los datos de los artículos cuyo precio esté entre $ 100.00 y $
350.00.

mysql> select * from articulo where Precio between 100 and 350;

+----------------+----------------+--------+------------------+

| Clave_articulo | Nombre | Precio | Clave_fabricante |

+----------------+----------------+--------+------------------+

| 1 | Teclado | 100.00 | 3|

| 4 | Memoria USB | 140.00 | 4|

| 5 | Memoria RAM | 290.00 | 1|

| 7 | Memoria USB | 279.00 | 1|

| 8 | CD Rom | 200.00 | 2|
| 10 | Tarjeta de Red | 180.00 | 3|

+----------------+----------------+--------+------------------+

6 rows in set (0.00 sec)

l)          Obtener el precio medio de todos los productos.

mysql> select AVG(Precio) from articulo;

+-------------+

| AVG(Precio) |

+-------------+

| 286.900000 |

+-------------+

1 row in set (0.06 sec)

mysql>

m)       Obtener el precio medio de los artículos cuyo código de fabricante sea 2.

mysql> select AVG(Precio) from articulo where Clave_fabricante ='2';

+-------------+

| AVG(Precio) |

+-------------+

| 325.000000 |

+-------------+

1 row in set (0.07 sec)

mysql>
n)        Obtener el nombre y precio de los artículos ordenados por Nombre.

mysql> select Nombre, Precio from articulo order by Nombre;

+-----------------------------+--------+

| Nombre | Precio |

+-----------------------------+--------+

| CD Rom | 200.00 |

| Disco Duro 300 GB | 500.00 |

| Disco Duro Extraible 250 Gb | 650.00 |

| DVD Rom | 450.00 |

| Memoria RAM | 290.00 |

| Memoria USB | 140.00 |

| Memoria USB | 279.00 |

| Mouse | 80.00 |

| Tarjeta de Red | 180.00 |

| Teclado | 100.00 |

+-----------------------------+--------+

10 rows in set (0.00 sec)

mysql>

o)        Obtener todos los productos ordenados descendentemente por Precio.

mysql> select * from articulo order by Precio DESC;

+----------------+-----------------------------+--------+------------------+

| Clave_articulo | Nombre | Precio | Clave_fabricante |

+----------------+-----------------------------+--------+------------------+
| 6 | Disco Duro Extraible 250 Gb | 650.00 | 5|

| 2 | Disco Duro 300 GB | 500.00 | 5|

| 9 | DVD Rom | 450.00 | 2|

| 5 | Memoria RAM | 290.00 | 1|

| 7 | Memoria USB | 279.00 | 1|

| 8 | CD Rom | 200.00 | 2|

| 10 | Tarjeta de Red | 180.00 | 3|

| 4 | Memoria USB | 140.00 | 4|

| 1 | Teclado | 100.00 | 3|

| 3 | Mouse | 80.00 | 3|

+----------------+-----------------------------+--------+------------------+

10 rows in set (0.00 sec)

mysql>

p)        Obtener el nombre y precio de los artículos cuyo precio sea mayor a $ 250.00 y
ordenarlos descendentemente por precio luego por nombre.

mysql> select Nombre, Precio from articulo where Precio >= 250 order by
Precio desc;

+-----------------------------+--------+

| Nombre | Precio |

+-----------------------------+--------+

| Disco Duro Extraible 250 Gb | 650.00 |

| Disco Duro 300 GB | 500.00 |

| DVD Rom | 450.00 |

| Memoria RAM | 290.00 |

| Memoria USB | 279.00 |


+-----------------------------+--------+

5 rows in set (0.00 sec)

mysql> select Nombre, Precio from articulo where Precio >= 250 order by
Nombre desc;

+-----------------------------+--------+

| Nombre | Precio |

+-----------------------------+--------+

| Memoria USB | 279.00 |

| Memoria RAM | 290.00 |

| DVD Rom | 450.00 |

| Disco Duro Extraible 250 Gb | 650.00 |

| Disco Duro 300 GB | 500.00 |

+-----------------------------+--------+

5 rows in set (0.00 sec)

mysql>

q)        Obtener un listado completo de los productos, incluyendo por cada artículo los
datos del artículo y del fabricante.

mysql> select * from articulo, fabricante where articulo.Clave_fabricante1 =


fabricante.Clave_fabricante order by Clave_articulo asc;

+----------------+-----------------------------+--------+-------------------
+------------------+------------+

| Clave_articulo | Nombre | Precio | Clave_fabricante1 |


Clave_fabricante | Nombre_fab |

+----------------+-----------------------------+--------+-------------------
+------------------+------------+

| 1 | Teclado | 100.00 | 3| 3 | Logitech


|
| 2 | Disco Duro 300 GB | 500.00 | 5| 5|
Seagate |

| 3 | Mouse | 80.00 | 3| 3 | Logitech


|

| 4 | Memoria USB | 140.00 | 4| 4|


Lexar |

| 5 | Memoria RAM | 290.00 | 1| 1|


Kingston |

| 6 | Disco Duro Extraible 250 Gb | 650.00 | 5| 5|


Seagate |

| 7 | Memoria USB | 279.00 | 1| 1|


Kingston |

| 8 | CD Rom | 200.00 | 2| 2 | Adata


|

| 9 | DVD Rom | 450.00 | 2| 2 | Adata


|

| 10 | Tarjeta de Red | 180.00 | 3| 3|


Logitech |

+----------------+-----------------------------+--------+-------------------
+------------------+------------+

10 rows in set (0.00 sec)

mysql>

r)         Obtener la clave de producto, nombre de producto y nombre del fabricante de


todos los productos en venta

mysql> select Clave_articulo, Nombre, Nombre_fab from articulo, fabricante


where articulo.Clave_fabricante1 = fabricante.Clave_fabricante order by
Clave_articulo asc;

+----------------+-----------------------------+------------+

| Clave_articulo | Nombre | Nombre_fab |

+----------------+-----------------------------+------------+

| 1 | Teclado | Logitech |
| 2 | Disco Duro 300 GB | Seagate |

| 3 | Mouse | Logitech |

| 4 | Memoria USB | Lexar |

| 5 | Memoria RAM | Kingston |

| 6 | Disco Duro Extraible 250 Gb | Seagate |

| 7 | Memoria USB | Kingston |

| 8 | CD Rom | Adata |

| 9 | DVD Rom | Adata |

| 10 | Tarjeta de Red | Logitech |

+----------------+-----------------------------+------------+

10 rows in set (0.00 sec)

mysql>

s)         Obtener el nombre y precio de los artículos donde el fabricante sea Logitech
ordenarlos alfabéticamente por nombre del producto.

mysql> select Nombre, Precio from articulo, fabricante where


articulo.Clave_fabricante1='3' = fabricante.Clave_fabricante order by Nombre
asc;

+----------------+--------+

| Nombre | Precio |

+----------------+--------+

| Mouse | 80.00 |

| Tarjeta de Red | 180.00 |

| Teclado | 100.00 |

+----------------+--------+

3 rows in set (0.00 sec)


mysql>

t)         Obtener el nombre, precio y nombre de fabricante de los productos que son
marca Lexar o Kingston ordena-dos descendentemente pro precio.

select * from articulo inner join fabricante on articulo.Clave_fabricante1 =


fabricante.Clave_fabricante;

u)        Añade un nuevo producto: Clave producto: 11, Altavoces de $ 120.00 de


marca Adata.

v)         Cambia el nombre del producto 6 a Impresora Laser.

w)       Aplicar un descuento del 10% a todos los productos.

x)         Aplicar un descuento de $ 10.00 a todos los productos cuyo precio sea mayor
a $ 300.00.

y)         Borra el producto número 6.

Cal. máxima
100

También podría gustarte