Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> CREATE DATABASE empleados CHARACTER SET utf8mb4;
Query OK, 1 row affected (0.02 sec)
mysql>
mysql>
mysql>
mysql> CREATE TABLE empleado (id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, nif
VARCHAR(9) NOT NULL UNIQUE, nombre VARCHAR(100) NOT NULL, apellido1 VARCHAR(100)
NOT NULL, apellido2 VARCHAR(100), id_departamento INT UNSIGNED, FOREIGN KEY
(id_departamento) REFERENCES departamento(id));
Query OK, 0 rows affected (0.06 sec)
mysql>
mysql>
mysql>
mysql> INSERT INTO departamento VALUES(1, 'Desarrollo', 120000, 6000);
Query OK, 1 row affected (0.01 sec)
mysql>
mysql> INSERT INTO departamento VALUES(2, 'Sistemas', 150000, 21000);
Query OK, 1 row affected (0.01 sec)
mysql>
mysql> INSERT INTO departamento VALUES(3, 'Recursos Humanos', 280000, 25000);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> INSERT INTO departamento VALUES(4, 'Contabilidad', 110000, 3000);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> INSERT INTO departamento VALUES(5, 'I+D', 375000, 380000);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> INSERT INTO departamento VALUES(6, 'Proyectos', 0, 0);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> INSERT INTO departamento VALUES(7, 'Publicidad', 0, 1000);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql>
mysql>
mysql> INSERT INTO empleado VALUES(1, '32481596F', 'Aarón', 'Rivero', 'Gómez', 1);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> INSERT INTO empleado VALUES(2, 'Y5575632D', 'Adela', 'Salas', 'Díaz', 2);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> INSERT INTO empleado VALUES(3, 'R6970642B', 'Adolfo', 'Rubio', 'Flores', 3);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> INSERT INTO empleado VALUES(4, '77705545E', 'Adrián', 'Suárez', NULL, 4);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> INSERT INTO empleado VALUES(5, '17087203C', 'Marcos', 'Loyola', 'Méndez',
5);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> INSERT INTO empleado VALUES(6, '38382980M', 'María', 'Santana', 'Moreno',
1);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> INSERT INTO empleado VALUES(7, '80576669X', 'Pilar', 'Ruiz', NULL, 2);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> INSERT INTO empleado VALUES(8, '71651431Z', 'Pepe', 'Ruiz', 'Santana', 3);
Query OK, 1 row affected (0.01 sec)
mysql>
mysql> INSERT INTO empleado VALUES(9, '56399183D', 'Juan', 'Gómez', 'López', 2);
Query OK, 1 row affected (0.01 sec)
mysql>
mysql> INSERT INTO empleado VALUES(10, '46384486H', 'Diego','Flores', 'Salas', 5);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> INSERT INTO empleado VALUES(11, '67389283A', 'Marta','Herrera', 'Gil', 1);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> INSERT INTO empleado VALUES(12, '41234836R', 'Irene','Salas', 'Florez',
NULL);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> INSERT INTO empleado VALUES(13, '82635162B', 'Juan Antonio','Sáenz',
'Guerrero', NULL);
Query OK, 1 row affected (0.01 sec)
mysql> select nombre, aoellido1, apellido2, nif from empleado where id_depar
tamento = 3;
ERROR 1054 (42S22): Unknown column 'aoellido1' in 'field list'
mysql> select nombre, apellido1, apellido2, nif from empleado where id_depar
tamento = 3;
+--------+-----------+-----------+-----------+
| nombre | apellido1 | apellido2 | nif |
+--------+-----------+-----------+-----------+
| Adolfo | Rubio | Flores | R6970642B |
| Pepe | Ruiz | Santana | 71651431Z |
+--------+-----------+-----------+-----------+
2 rows in set (0.00 sec)
mysql> select nombre, apellido1, apellido2, nif from empleado where id_departamento
in (2, 4, 5);
+---------+-----------+-----------+-----------+
| nombre | apellido1 | apellido2 | nif |
+---------+-----------+-----------+-----------+
| Adela | Salas | Díaz | Y5575632D |
| Pilar | Ruiz | NULL | 80576669X |
| Juan | Gómez | López | 56399183D |
| Adrián | Suárez | NULL | 77705545E |
| Marcos | Loyola | Méndez | 17087203C |
| Diego | Flores | Salas | 46384486H |
+---------+-----------+-----------+-----------+
6 rows in set (0.00 sec)
mysql> select nombre, presupuesto from departamento order by presupuesto asc limit
3;
+--------------+-------------+
| nombre | presupuesto |
+--------------+-------------+
| Proyectos | 0 |
| Publicidad | 0 |
| Contabilidad | 110000 |
+--------------+-------------+
3 rows in set (0.00 sec)
mysql> select nombre, presupuesto from departamento where presupuesto >= 150000;
+------------------+-------------+
| nombre | presupuesto |
+------------------+-------------+
| Sistemas | 150000 |
| Recursos Humanos | 280000 |
| I+D | 375000 |
+------------------+-------------+
3 rows in set (0.00 sec)
mysql> select nombre, presupuesto from departamento where gastos < 5000;
+--------------+-------------+
| nombre | presupuesto |
+--------------+-------------+
| Contabilidad | 110000 |
| Proyectos | 0 |
| Publicidad | 0 |
+--------------+-------------+
3 rows in set (0.00 sec)
mysql> select from departamento where presupuesto not between 100000 and 200
000;
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 'from
departamento where presupuesto not between 100000 and 200000' at line 1
mysql> select nombre from departamento where presupuesto not between 100000
and 200000;
+------------------+
| nombre |
+------------------+
| Recursos Humanos |
| I+D |
| Proyectos |
| Publicidad |
+------------------+
4 rows in set (0.00 sec)
mysql> select nombre, gastos, presupuesto from departamento where gastos >
presupuesto;
+------------+--------+-------------+
| nombre | gastos | presupuesto |
+------------+--------+-------------+
| I+D | 380000 | 375000 |
| Publicidad | 1000 | 0 |
+------------+--------+-------------+
2 rows in set (0.00 sec)
mysql> select nombre, gastos, presupuesto from departamento where gastos < p
resupuesto;
+------------------+--------+-------------+
| nombre | gastos | presupuesto |
+------------------+--------+-------------+
| Desarrollo | 6000 | 120000 |
| Sistemas | 21000 | 150000 |
| Recursos Humanos | 25000 | 280000 |
| Contabilidad | 3000 | 110000 |
+------------------+--------+-------------+
4 rows in set (0.00 sec)
mysql> select d.id, d.nombre from departamento d inner join empleado e on d.id =
e.id_departamento group by d.id, d.nombre;
+----+------------------+
| id | nombre |
+----+------------------+
| 1 | Desarrollo |
| 2 | Sistemas |
| 3 | Recursos Humanos |
| 4 | Contabilidad |
| 5 | I+D |
+----+------------------+
5 rows in set (0.01 sec)
mysql> select d.* from departamento d left join empleado e on d.id = e.id_de
partamento where e.id is null;
+----+------------+-------------+--------+
| id | nombre | presupuesto | gastos |
+----+------------+-------------+--------+
| 6 | Proyectos | 0 | 0 |
| 7 | Publicidad | 0 | 1000 |
+----+------------+-------------+--------+
2 rows in set (0.00 sec)
mysql> select nombre , presupuesto from departamento order by presupuesto asc limit
1 ;
+-----------+-------------+
| nombre | presupuesto |
+-----------+-------------+
| Proyectos | 0 |
+-----------+-------------+
1 row in set (0.00 sec)
mysql>