You are on page 1of 6

mysql> create database Empleados;

Query OK, 1 row affected (0.02 sec)


mysql> use Empleados;
Database changed
mysql> show databases;
+--------------------+
| Database
|
+--------------------+
| information_schema |
| empleados
|
| los_almacenes
|
| mysql
|
| ordenes
|
| test
|
| tienda_informatica |
+--------------------+
7 rows in set (0.00 sec)
mysql> create table DEPARTAMENTOS(codigo int, nombre nvarchar(100), presupuesto
int, primary key (codigo))engine=innodb;
Query OK, 0 rows affected (0.08 sec)
mysql> create table EMPLEADOS(dni varchar(8), nombre nvarchar(100), apellidos nv
archar(255), departamento int, primary key(dni), foreign key(departamento) refer
ences DEPARTAMENTOS(codigo) on delete cascade on update cascade) engine=InnoDB;
Query OK, 0 rows affected (0.14 sec)
mysql> describe DEPARTAMENTOS;
+-------------+--------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| codigo
| int(11)
| NO | PRI | 0
|
|
| nombre
| varchar(100) | YES |
| NULL
|
|
| presupuesto | int(11)
| YES |
| NULL
|
|
+-------------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> describe EMPLEADOS;
+--------------+--------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| dni
| varchar(8) | NO | PRI | NULL
|
|
| nombre
| varchar(100) | YES |
| NULL
|
|
| apellidos
| varchar(255) | YES |
| NULL
|
|
| departamento | int(11)
| YES | MUL | NULL
|
|
+--------------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>
mysql> insert into DEPARTAMENTOS values("14" , "computacion" , "60000");
Query OK, 1 row affected (0.30 sec)
mysql> insert into DEPARTAMENTOS values("15" , "video" , "80000");
Query OK, 1 row affected (0.30 sec)
mysql> insert into DEPARTAMENTOS values("20" , "programacion" , "70000");
Query OK, 1 row affected (0.28 sec)
mysql> insert into DEPARTAMENTOS values("25" , "mecanica" , "40000");

Query OK, 1 row affected (0.28 sec)


mysql> insert into EMPLEADOS values("11046688", "mikaela", "nero", "14");
Query OK, 1 row affected (0.05 sec)
mysql> insert into EMPLEADOS values("11086667", "bryan", "banjaa", "15");
Query OK, 1 row affected (0.05 sec)
mysql> insert into EMPLEADOS values("11024867", "pablo", "civisaca", "20");
Query OK, 1 row affected (0.05 sec)
mysql> insert into EMPLEADOS values("11034831", "manuel", "maza", "25");
Query OK, 1 row affected (0.08 sec)
mysql> select * from DEPARTAMENTOS;
+--------+--------------+-------------+
| codigo | nombre
| presupuesto |
+--------+--------------+-------------+
|
14 | computacion |
60000 |
|
15 | video
|
80000 |
|
20 | programacion |
70000 |
|
25 | mecanica
|
40000 |
+--------+--------------+-------------+
4 rows in set (0.00 sec)
mysql> select * from EMPLEADOS;
+----------+---------+-----------+--------------+
| dni
| nombre | apellidos | departamento |
+----------+---------+-----------+--------------+
| 11024867 | pablo | civisaca |
20 |
| 11034831 | manuel | maza
|
25 |
| 11046688 | mikaela | nero
|
14 |
| 11086667 | bryan | banjaa |
15 |
+----------+---------+-----------+--------------+
4 rows in set (0.00 sec)
mysql> SELECT apellidos FROM EMPLEADOS;
+-----------+
| apellidos |
+-----------+
| civisaca |
| maza
|
| nero
|
| banjaa |
+-----------+
4 rows in set (0.00 sec)
mysql> SELECT DISTINCT apellidos FROM EMPLEADOS;
+-----------+
| apellidos |
+-----------+
| civisaca |
| maza
|
| nero
|
| banjaa |
+-----------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM EMPLEADOS WHERE apellidos = 'maza'
-> SELECT * FROM EMPLEADOS WHERE apellidos = 'maza';

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'SELEC
T * FROM EMPLEADOS WHERE apellidos = 'maza'' at line 2
mysql> SELECT * FROM EMPLEADOS WHERE apellidos = 'maza';
+----------+--------+-----------+--------------+
| dni
| nombre | apellidos | departamento |
+----------+--------+-----------+--------------+
| 11034831 | manuel | maza
|
25 |
+----------+--------+-----------+--------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM EMPLEADOS WHERE apellidos = 'maza' OR apellidos = 'nero';
+----------+---------+-----------+--------------+
| dni
| nombre | apellidos | departamento |
+----------+---------+-----------+--------------+
| 11034831 | manuel | maza
|
25 |
| 11046688 | mikaela | nero
|
14 |
+----------+---------+-----------+--------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM EMPLEADOS WHERE Departamento = 14;
+----------+---------+-----------+--------------+
| dni
| nombre | apellidos | departamento |
+----------+---------+-----------+--------------+
| 11046688 | mikaela | nero
|
14 |
+----------+---------+-----------+--------------+
1 row in set (0.08 sec)
mysql> SELECT * FROM EMPLEADOS WHERE Departamento = 20 OR Departamento = 25;
+----------+--------+-----------+--------------+
| dni
| nombre | apellidos | departamento |
+----------+--------+-----------+--------------+
| 11024867 | pablo | civisaca |
20 |
| 11034831 | manuel | maza
|
25 |
+----------+--------+-----------+--------------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM EMPLEADOS WHERE Apellidos LIKE 'n%';
+----------+---------+-----------+--------------+
| dni
| nombre | apellidos | departamento |
+----------+---------+-----------+--------------+
| 11046688 | mikaela | nero
|
14 |
+----------+---------+-----------+--------------+
1 row in set (0.00 sec)
mysql> SELECT SUM(Presupuesto) FROM DEPARTAMENTOS;
+------------------+
| SUM(Presupuesto) |
+------------------+
|
250000 |
+------------------+
1 row in set (0.00 sec)
mysql> SELECT Departamento, COUNT(*) FROM EMPLEADOS GROUP BY Departamento;
+--------------+----------+
| Departamento | COUNT(*) |
+--------------+----------+
|
14 |
1 |
|
15 |
1 |
|
20 |
1 |

|
25 |
1 |
+--------------+----------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM EMPLEADOS INNER JOIN DEPARTAMENTOS ON EMPLEADOS.Departament
o = DEPARTAMENTOS.Codigo;
+----------+---------+-----------+--------------+--------+--------------+------------+
| dni
| nombre | apellidos | departamento | codigo | nombre
| presup
uesto |
+----------+---------+-----------+--------------+--------+--------------+------------+
| 11046688 | mikaela | nero
|
14 |
14 | computacion |
60000 |
| 11086667 | bryan | banjaa |
15 |
15 | video
|
80
000 |
| 11024867 | pablo | civisaca |
20 |
20 | programacion |
70000 |
| 11034831 | manuel | maza
|
25 |
25 | mecanica
|
40000 |
+----------+---------+-----------+--------------+--------+--------------+------------+
4 rows in set (0.00 sec)
mysql> SELECT EMPLEADOS.Nombre, Apellidos, DEPARTAMENTOS.Nombre, Presupuesto FRO
M EMPLEADOS INNER JOIN DEPARTAMENTOS ON EMPLEADOS.Departamento = DEPARTAMENTOS.C
odigo;
+---------+-----------+--------------+-------------+
| Nombre | Apellidos | Nombre
| Presupuesto |
+---------+-----------+--------------+-------------+
| mikaela | nero
| computacion |
60000 |
| bryan | banjaa | video
|
80000 |
| pablo | civisaca | programacion |
70000 |
| manuel | maza
| mecanica
|
40000 |
+---------+-----------+--------------+-------------+
4 rows in set (0.02 sec)
mysql> SELECT EMPLEADOS.Nombre, Apellidos FROM EMPLEADOS INNER JOIN DEPARTAMENTO
S ON EMPLEADOS.Departamento = DEPARTAMENTOS.Codigo AND DEPARTAMENTOS.Presupuesto
> 60000;
+--------+-----------+
| Nombre | Apellidos |
+--------+-----------+
| bryan | banjaa |
| pablo | civisaca |
+--------+-----------+
2 rows in set (0.01 sec)
mysql> SELECT * FROM DEPARTAMENTOS WHERE Presupuesto > ( SELECT AVG(Presupuesto)
FROM DEPARTAMENTOS);
+--------+--------------+-------------+
| codigo | nombre
| presupuesto |
+--------+--------------+-------------+
|
15 | video
|
80000 |
|
20 | programacion |
70000 |
+--------+--------------+-------------+
2 rows in set (0.00 sec)
mysql> SELECT Nombre FROM DEPARTAMENTOS WHERE Codigo IN ( SELECT Departamento FR
OM EMPLEADOS GROUP BY Departamento HAVING COUNT(*) > 2);

Empty set (0.00 sec)


mysql> INSERT INTO DEPARTAMENTOS VALUES ( 11 , 'calidad' , 40000);
Query OK, 1 row affected (0.03 sec)
mysql> INSERT INTO EMPLEADOS VALUES ( '89267109' , 'esther' , 'vazquez' , 11);
Query OK, 1 row affected (0.01 sec)
mysql> select * from DEPARTAMENTOS;
+--------+--------------+-------------+
| codigo | nombre
| presupuesto |
+--------+--------------+-------------+
|
11 | calidad
|
40000 |
|
14 | computacion |
60000 |
|
15 | video
|
80000 |
|
20 | programacion |
70000 |
|
25 | mecanica
|
40000 |
+--------+--------------+-------------+
5 rows in set (0.00 sec)
mysql> select * from EMPLEADOS;
+----------+---------+-----------+--------------+
| dni
| nombre | apellidos | departamento |
+----------+---------+-----------+--------------+
| 11024867 | pablo | civisaca |
20 |
| 11034831 | manuel | maza
|
25 |
| 11046688 | mikaela | nero
|
14 |
| 11086667 | bryan | banjaa |
15 |
| 89267109 | esther | vazquez |
11 |
+----------+---------+-----------+--------------+
5 rows in set (0.00 sec)
mysql> UPDATE DEPARTAMENTOS SET Presupuesto = Presupuesto * 0.9;
Query OK, 5 rows affected (0.05 sec)
Rows matched: 5 Changed: 5 Warnings: 0
mysql> select * from DEPARTAMENTOS;
+--------+--------------+-------------+
| codigo | nombre
| presupuesto |
+--------+--------------+-------------+
|
11 | calidad
|
36000 |
|
14 | computacion |
54000 |
|
15 | video
|
72000 |
|
20 | programacion |
63000 |
|
25 | mecanica
|
36000 |
+--------+--------------+-------------+
5 rows in set (0.00 sec)
mysql> UPDATE EMPLEADOS SET Departamento = 14 WHERE Departamento = 15;
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from EMPLEADOS;
+----------+---------+-----------+--------------+
| dni
| nombre | apellidos | departamento |
+----------+---------+-----------+--------------+
| 11024867 | pablo | civisaca |
20 |
| 11034831 | manuel | maza
|
25 |
| 11046688 | mikaela | nero
|
14 |
| 11086667 | bryan | banjaa |
14 |

| 89267109 | esther | vazquez |


11 |
+----------+---------+-----------+--------------+
5 rows in set (0.00 sec)
mysql> DELETE FROM EMPLEADOS WHERE Departamento = 14;
Query OK, 2 rows affected (0.05 sec)
mysql> select * from EMPLEADOS;
+----------+--------+-----------+--------------+
| dni
| nombre | apellidos | departamento |
+----------+--------+-----------+--------------+
| 11024867 | pablo | civisaca |
20 |
| 11034831 | manuel | maza
|
25 |
| 89267109 | esther | vazquez |
11 |
+----------+--------+-----------+--------------+
3 rows in set (0.00 sec)
mysql> DELETE FROM EMPLEADOS WHERE Departamento IN ( SELECT Codigo FROM DEPARTAM
ENTO WHERE Presupuesto >= 60000);
ERROR 1146 (42S02): Table 'empleados.departamento' doesn't exist
mysql> DELETE FROM EMPLEADOS WHERE DEPARTAMENTOS IN ( SELECT Codigo FROM DEPARTA
MENTO WHERE Presupuesto >= 60000);
ERROR 1146 (42S02): Table 'empleados.departamento' doesn't exist
mysql> DELETE FROM EMPLEADOS WHERE departamentos IN ( SELECT Codigo FROM DEPARTA
MENTO WHERE Presupuesto >= 60000);
ERROR 1146 (42S02): Table 'empleados.departamento' doesn't exist
mysql> select * from DEPARTAMENTOS;
+--------+--------------+-------------+
| codigo | nombre
| presupuesto |
+--------+--------------+-------------+
|
11 | calidad
|
36000 |
|
14 | computacion |
54000 |
|
15 | video
|
72000 |
|
20 | programacion |
63000 |
|
25 | mecanica
|
36000 |
+--------+--------------+-------------+
5 rows in set (0.00 sec)
mysql> DELETE FROM EMPLEADOS;
Query OK, 3 rows affected (0.05 sec)
mysql> select * from EMPLEADOS;
Empty set (0.00 sec)
mysql> select * from DEPARTAMENTOS;
+--------+--------------+-------------+
| codigo | nombre
| presupuesto |
+--------+--------------+-------------+
|
11 | calidad
|
36000 |
|
14 | computacion |
54000 |
|
15 | video
|
72000 |
|
20 | programacion |
63000 |
|
25 | mecanica
|
36000 |
+--------+--------------+-------------+
5 rows in set (0.00 sec)
mysql> exit