Lo primermos que debemos de hacer es ubicarnos en el panel de control y buscar el "xampp" dar clic y abrir el programa para activar los motores de bases de datos
Iniciamos en el inicio del sistema el cmd y cambiamos la direccion asi: cd/xampp/mysql/bin
espacio entre los datos por que nos muestra un error como muestra la imagen.
Ahora creamos las tablas que contendrá la base de datos para crear una tabla usamos el comando "create", la sintaxis:
verificamos que la tabla si fue creata con la linea show tables; y ademas la descripcion completa de la tabla con la lnea describe libro;
Ahora vamos a crear una nueva tabla relacional entre las dos anteriores, para esto vamos a usar claves foraneas. Para nuestra base de datos ejemplo, ingresaremos el siguiente código:
-> foreign key(cod_aut) references autor(cod_aut) on delete cascade on update cascade);
El comando "on update cascade", hace casi la misma función, ya que este solo actualiza la información.
select nom_libro, precio_libro, max(precio_lib) ´libro mas caro que tiene la blioteca es´ from libro;
select *(campo deseados) from tabla condicion order by campo a ordenar tipo de ordenado;
ordenar la tabla libro de forma ascendente por los campos editorial y nombre de libro, visualizando solo los siguientes datos, codigo del libro, nombre del libro y la editorial.
select libro.cod_lib,libro.nom_lib,lib_aut.cod_lib,lib_aut.cod_aut,autor.nombre_aut from libro,lib_aut,autor where libro.cod_lib= lib_aut.cod_lib and lib_aut.cod_aut=autor.cod_aut;
borramos todos los registros con el comando truncate para eliminar con los codigos
ojo en el comando group by no funciona el comando where entonces lo cambiamos por having
right join para visualizar datos que tienen relaciones directas con los demas datos (tablas)
select * from libro inner join lib_aut on libro.cod_lib =lib_aut.cod_lib inner join autor on lib_aut.cod_aut=autor.cod_auto;
para visualizar las coincidencias de los codigos entre la tabla libro y la tabla lib_aut
lo primero que hacemos es llenar las tablas de la base de datos;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| bdcole |
| cdcol |
| empresa |
| facturacion |
| fechas |
| matricula2 |
| mysql |
| performance_schema |
| phpmyadmin |
| prueba |
| sifavidb |
| subconsultas |
| test |
| webauth |
+--------------------+
15 rows in set (0.00 sec)
mysql> use facturacion;
Database changed
mysql> show tables;
+-----------------------+
| Tables_in_facturacion |
+-----------------------+
| cliente |
| detallefactura |
| factura |
| producto |
+-----------------------+
4 rows in set (0.00 sec)
mysql> select * from cliente;
Empty set (0.00 sec)
mysql> insert into cliente (cedula,nombre,direccion,fechainicio) values (100,'al
exandra lopez','aranjuez','2003-12-03')
-> ;
Query OK, 1 row affected (0.06 sec)
y asi con todos quedanto la tablas asi:
mysql> select * from cliente;
+--------+--------------------+-----------+-------------+
| cedula | nombre | direccion | fechainicio |
+--------+--------------------+-----------+-------------+
| 100 | alexandra lopez | aranjuez | 2003-12-03 |
| 200 | sebastian perez | sabaneta | 2005-07-17 |
| 300 | sara herrera | floresta | 2007-09-20 |
| 400 | lusa roldan | floresta | 2009-10-15 |
| 500 | mario henao | bello | 2004-02-28 |
| 600 | luisa cifuentes | florencia | 2003-09-01 |
| 700 | edith garcia | envigado | 2006-03-03 |
| 800 | claudia lainez | itag | 2005-04-13 |
| 900 | viviana piedrahita | centro | 2006-01-19 |
+--------+--------------------+-----------+-------------+
9 rows in set (0.00 sec)
mysql>
mysql> insert into producto (codigo,articulo,valorunitario,cantidad,valorventa,e
xistencia) values('10','tv',1250000,22,0,0);
Query OK, 1 row affected (0.07 sec)
mysql> insert into producto (codigo,articulo,valorunitario,cantidad,valorventa,e
xistencia) values('20','auriculares',75000,27,0,0);
Query OK, 1 row affected (0.09 sec)
mysql> insert into producto (codigo,articulo,valorunitario,cantidad,valorventa,e
xistencia) values('30','mp3',150000,24,0,0);
Query OK, 1 row affected (0.10 sec)
mysql> insert into producto (codigo,articulo,valorunitario,cantidad,valorventa,e
xistencia) values('40','mouse',25000,33,0,0);
Query OK, 1 row affected (0.11 sec)
mysql> insert into producto (codigo,articulo,valorunitario,cantidad,valorventa,e
xistencia) values('50','teclado',130000,45,0,0);
Query OK, 1 row affected (0.06 sec)
mysql> insert into producto (codigo,articulo,valorunitario,cantidad,valorventa,e
xistencia) values('60','disco duro',203000,17,0,0);
Query OK, 1 row affected (0.06 sec)
mysql> insert into producto (codigo,articulo,valorunitario,cantidad,valorventa,e
xistencia) values('70','unidad de dvd',250000,19,0,0);
Query OK, 1 row affected (0.03 sec)
mysql> insert into producto (codigo,articulo,valorunitario,cantidad,valorventa,e
xistencia) values('80','usb',80000,28,0,0);
Query OK, 1 row affected (0.06 sec)
mysql> insert into producto (codigo,articulo,valorunitario,cantidad,valorventa,e
xistencia) values('90','lapiz optico',134000,17,0,0);
Query OK, 1 row affected (0.06 sec)
mysql> select * from producto;
+--------+---------------+---------------+----------+------------+------------+
| codigo | articulo | valorunitario | cantidad | valorventa | existencia |
+--------+---------------+---------------+----------+------------+------------+
| 10 | tv | 1250000 | 22 | 0 | 0 |
| 20 | auriculares | 75000 | 27 | 0 | 0 |
| 30 | mp3 | 150000 | 24 | 0 | 0 |
| 40 | mouse | 25000 | 33 | 0 | 0 |
| 50 | teclado | 130000 | 45 | 0 | 0 |
| 60 | disco duro | 203000 | 17 | 0 | 0 |
| 70 | unidad de dvd | 250000 | 19 | 0 | 0 |
| 80 | usb | 80000 | 28 | 0 | 0 |
| 90 | lapiz optico | 134000 | 17 | 0 | 0 |
+--------+---------------+---------------+----------+------------+------------+
9 rows in set (0.00 sec)
mysql>
mysql> describe factura;
+------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+-------+
| nrofactura | int(4) | NO | PRI | NULL | |
| cedula | int(10) | NO | MUL | NULL | |
| subtotal | int(20) | NO | | NULL | |
| iva | int(20) | NO | | NULL | |
| retencion | int(20) | NO | | NULL | |
| total | int(20) | NO | | NULL | |
+------------+---------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql> insert into factura (nrofactura,cedula,subtotal,iva,retencion,total) valu
es(1001,100,0,0,0,0);
Query OK, 1 row affected (0.03 sec)
mysql> select * from factura;
+------------+--------+----------+-----+-----------+-------+
| nrofactura | cedula | subtotal | iva | retencion | total |
+------------+--------+----------+-----+-----------+-------+
| 1001 | 100 | 0 | 0 | 0 | 0 |
+------------+--------+----------+-----+-----------+-------+
1 row in set (0.00 sec)
mysql> insert into factura (nrofactura,cedula,subtotal,iva,retencion,total) valu
es(2002,300,0,0,0,0);
Query OK, 1 row affected (0.07 sec)
mysql> insert into factura (nrofactura,cedula,subtotal,iva,retencion,total) valu
es(3003,200,0,0,0,0);
Query OK, 1 row affected (0.06 sec)
mysql> insert into factura (nrofactura,cedula,subtotal,iva,retencion,total) valu
es(4004,400,0,0,0,0);
Query OK, 1 row affected (0.05 sec)
mysql> insert into factura (nrofactura,cedula,subtotal,iva,retencion,total) valu
es(5005,200,0,0,0,0);
Query OK, 1 row affected (0.12 sec)
mysql> insert into factura (nrofactura,cedula,subtotal,iva,retencion,total) valu
es(6006,500,0,0,0,0);
Query OK, 1 row affected (0.05 sec)
mysql> insert into factura (nrofactura,cedula,subtotal,iva,retencion,total) valu
es(7007,100,0,0,0,0);
Query OK, 1 row affected (0.06 sec)
mysql> insert into factura (nrofactura,cedula,subtotal,iva,retencion,total) valu
es(8008,400,0,0,0,0);
Query OK, 1 row affected (0.06 sec)
mysql> insert into factura (nrofactura,cedula,subtotal,iva,retencion,total) valu
es(8008,400,0,0,0,0);
ERROR 1062 (23000): Duplicate entry '8008' for key 'PRIMARY'
mysql> insert into factura (nrofactura,cedula,subtotal,iva,retencion,total) valu
es(9009,400,0,0,0,0);
Query OK, 1 row affected (0.02 sec)
mysql> insert into factura (nrofactura,cedula,subtotal,iva,retencion,total) valu
es(1101,600,0,0,0,0);
Query OK, 1 row affected (0.06 sec)
mysql> insert into factura (nrofactura,cedula,subtotal,iva,retencion,total) valu
es(2202,700,0,0,0,0);
Query OK, 1 row affected (0.09 sec)
mysql> insert into factura (nrofactura,cedula,subtotal,iva,retencion,total) valu
es(3303,600,0,0,0,0);
Query OK, 1 row affected (0.05 sec)
mysql> insert into factura (nrofactura,cedula,subtotal,iva,retencion,total) valu
es(4404,100,0,0,0,0);
Query OK, 1 row affected (0.03 sec)
mysql> insert into factura (nrofactura,cedula,subtotal,iva,retencion,total) valu
es(5505,700,0,0,0,0);
Query OK, 1 row affected (0.08 sec)
mysql> insert into factura (nrofactura,cedula,subtotal,iva,retencion,total) valu
es(6606,800,0,0,0,0);
Query OK, 1 row affected (0.10 sec)
mysql> insert into factura (nrofactura,cedula,subtotal,iva,retencion,total) valu
es(7707,900,0,0,0,0);
Query OK, 1 row affected (0.07 sec)
mysql> insert into factura (nrofactura,cedula,subtotal,iva,retencion,total) valu
es(8808,800,0,0,0,0);
Query OK, 1 row affected (0.08 sec)
mysql> insert into factura (nrofactura,cedula,subtotal,iva,retencion,total) valu
es(9909,900,0,0,0,0);
Query OK, 1 row affected (0.06 sec)
mysql> describe producto;
+---------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------+------+-----+---------+-------+
| codigo | char(10) | NO | PRI | NULL | |
| articulo | char(20) | NO | | NULL | |
| valorunitario | int(20) | NO | | NULL | |
| cantidad | int(4) | NO | | NULL | |
| valorventa | int(20) | NO | | NULL | |
| existencia | int(4) | NO | | NULL | |
+---------------+----------+------+-----+---------+-------+
6 rows in set (0.01 sec)
mysql> select * from producto;
+--------+---------------+---------------+----------+------------+------------+
| codigo | articulo | valorunitario | cantidad | valorventa | existencia |
+--------+---------------+---------------+----------+------------+------------+
| 10 | tv | 1250000 | 22 | 0 | 0 |
| 20 | auriculares | 75000 | 27 | 0 | 0 |
| 30 | mp3 | 150000 | 24 | 0 | 0 |
| 40 | mouse | 25000 | 33 | 0 | 0 |
| 50 | teclado | 130000 | 45 | 0 | 0 |
| 60 | disco duro | 203000 | 17 | 0 | 0 |
| 70 | unidad de dvd | 250000 | 19 | 0 | 0 |
| 80 | usb | 80000 | 28 | 0 | 0 |
| 90 | lapiz optico | 134000 | 17 | 0 | 0 |
+--------+---------------+---------------+----------+------------+------------+
9 rows in set (0.00 sec)
mysql> select * from cliente;
+--------+--------------------+-----------+-------------+
| cedula | nombre | direccion | fechainicio |
+--------+--------------------+-----------+-------------+
| 100 | alexandra lopez | aranjuez | 2003-12-03 |
| 200 | sebastian perez | sabaneta | 2005-07-17 |
| 300 | sara herrera | floresta | 2007-09-20 |
| 400 | lusa roldan | floresta | 2009-10-15 |
| 500 | mario henao | bello | 2004-02-28 |
| 600 | luisa cifuentes | florencia | 2003-09-01 |
| 700 | edith garcia | envigado | 2006-03-03 |
| 800 | claudia lainez | itag | 2005-04-13 |
| 900 | viviana piedrahita | centro | 2006-01-19 |
+--------+--------------------+-----------+-------------+
9 rows in set (0.00 sec)
mysql> select * from factura;
+------------+--------+----------+-----+-----------+-------+
| nrofactura | cedula | subtotal | iva | retencion | total |
+------------+--------+----------+-----+-----------+-------+
| 1001 | 100 | 0 | 0 | 0 | 0 |
| 1101 | 600 | 0 | 0 | 0 | 0 |
| 2002 | 300 | 0 | 0 | 0 | 0 |
| 2202 | 700 | 0 | 0 | 0 | 0 |
| 3003 | 200 | 0 | 0 | 0 | 0 |
| 3303 | 600 | 0 | 0 | 0 | 0 |
| 4004 | 400 | 0 | 0 | 0 | 0 |
| 4404 | 100 | 0 | 0 | 0 | 0 |
| 5005 | 200 | 0 | 0 | 0 | 0 |
| 5505 | 700 | 0 | 0 | 0 | 0 |
| 6006 | 500 | 0 | 0 | 0 | 0 |
| 6606 | 800 | 0 | 0 | 0 | 0 |
| 7007 | 100 | 0 | 0 | 0 | 0 |
| 7707 | 900 | 0 | 0 | 0 | 0 |
| 8008 | 400 | 0 | 0 | 0 | 0 |
| 8808 | 800 | 0 | 0 | 0 | 0 |
| 9009 | 400 | 0 | 0 | 0 | 0 |
| 9909 | 900 | 0 | 0 | 0 | 0 |
+------------+--------+----------+-----+-----------+-------+
18 rows in set (0.00 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (1,1001,'10',1,0,0);
Query OK, 1 row affected (0.08 sec)
mysql> select * from detallefactura;
+------+------------+--------+----------+-------+-------+
| id | nrofactura | codigo | cantidad | valor | total |
+------+------------+--------+----------+-------+-------+
| 0001 | 1001 | 10 | 1 | 0 | 0 |
+------+------------+--------+----------+-------+-------+
1 row in set (0.05 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (2,1001,'40',2,0,0);
Query OK, 1 row affected (0.08 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (3,1001,'70',1,0,0);
Query OK, 1 row affected (0.09 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (4,2002,'60',2,0,0);
Query OK, 1 row affected (0.06 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (5,3003,'20',4,0,0);
Query OK, 1 row affected (0.09 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (6,3003,'80',1,0,0);
Query OK, 1 row affected (0.11 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (7,4004,'10',2,0,0);
Query OK, 1 row affected (0.06 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (8,4004,'20',1,0,0);
Query OK, 1 row affected (0.06 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (9,4004,'30',1,0,0);
Query OK, 1 row affected (0.09 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (10,4004,'60',1,0,0);
Query OK, 1 row affected (0.08 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (11,4004,'70',1,0,0);
Query OK, 1 row affected (0.06 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (12,4004,'80',2,0,0);
Query OK, 1 row affected (0.06 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (13,5005,'10',3,0,0);
Query OK, 1 row affected (0.06 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (14,6006,'20',1,0,0);
Query OK, 1 row affected (0.09 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (15,6006,'80',2,0,0);
Query OK, 1 row affected (0.09 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (16,6006,'30',2,0,0);
Query OK, 1 row affected (0.09 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (17,6006,'90',2,0,0);
Query OK, 1 row affected (0.09 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (18,6006,'50',1,0,0);
Query OK, 1 row affected (0.08 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (19,6006,'40',2,0,0);
Query OK, 1 row affected (0.09 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (20,7007,'10',2,0,0);
Query OK, 1 row affected (0.08 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (21,7007,'90',2,0,0);
Query OK, 1 row affected (0.08 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (22,8008,'60',2,0,0);
Query OK, 1 row affected (0.05 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (23,8008,'40',2,0,0);
Query OK, 1 row affected (0.05 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (24,8008,'50',1,0,0);
Query OK, 1 row affected (0.05 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (25,9009,'50',2,0,0);
Query OK, 1 row affected (0.08 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (26,9009,'80',1,0,0);
Query OK, 1 row affected (0.13 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (27,1101,'30',1,0,0);
Query OK, 1 row affected (0.83 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (26,9009,'80',1,0,0);
ERROR 1062 (23000): Duplicate entry '0026' for key 'PRIMARY'
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (28,2202,'30',2,0,0);
Query OK, 1 row affected (0.08 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (28,2202,'30',2,0,0);
ERROR 1062 (23000): Duplicate entry '0028' for key 'PRIMARY'
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (29,1101,'60',1,0,0);
Query OK, 1 row affected (0.08 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (30,3303,'60',2,0,0);
Query OK, 1 row affected (0.09 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (31,4404,'90',3,0,0);
Query OK, 1 row affected (0.08 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (32,5505,'80',4,0,0);
Query OK, 1 row affected (0.84 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (33,6606,'70',2,0,0);
Query OK, 1 row affected (0.09 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (34,7707,'10',1,0,0);
Query OK, 1 row affected (0.08 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (35,8808,'40',2,0,0);
Query OK, 1 row affected (0.08 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (36,9909,'20',3,0,0);
Query OK, 1 row affected (0.08 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (37,9909,'30',4,0,0);
Query OK, 1 row affected (0.73 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (38,6606,'50',5,0,0);
Query OK, 1 row affected (0.11 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (39,7707,'40',1,0,0);
Query OK, 1 row affected (0.11 sec)
mysql> insert into detallefactura (id,nrofactura,codigo,cantidad,valor,total) va
lues (40,1101,'30',1,0,0);
Query OK, 1 row affected (0.06 sec)
mysql> select * from ciente;
ERROR 1146 (42S02): Table 'facturacion.ciente' doesn't exist
mysql> select * from cliente;
+--------+--------------------+-----------+-------------+
| cedula | nombre | direccion | fechainicio |
+--------+--------------------+-----------+-------------+
| 100 | alexandra lopez | aranjuez | 2003-12-03 |
| 200 | sebastian perez | sabaneta | 2005-07-17 |
| 300 | sara herrera | floresta | 2007-09-20 |
| 400 | lusa roldan | floresta | 2009-10-15 |
| 500 | mario henao | bello | 2004-02-28 |
| 600 | luisa cifuentes | florencia | 2003-09-01 |
| 700 | edith garcia | envigado | 2006-03-03 |
| 800 | claudia lainez | itag | 2005-04-13 |
| 900 | viviana piedrahita | centro | 2006-01-19 |
+--------+--------------------+-----------+-------------+
9 rows in set (0.00 sec)
mysql> select * from producto;
+--------+---------------+---------------+----------+------------+------------+
| codigo | articulo | valorunitario | cantidad | valorventa | existencia |
+--------+---------------+---------------+----------+------------+------------+
| 10 | tv | 1250000 | 22 | 0 | 0 |
| 20 | auriculares | 75000 | 27 | 0 | 0 |
| 30 | mp3 | 150000 | 24 | 0 | 0 |
| 40 | mouse | 25000 | 33 | 0 | 0 |
| 50 | teclado | 130000 | 45 | 0 | 0 |
| 60 | disco duro | 203000 | 17 | 0 | 0 |
| 70 | unidad de dvd | 250000 | 19 | 0 | 0 |
| 80 | usb | 80000 | 28 | 0 | 0 |
| 90 | lapiz optico | 134000 | 17 | 0 | 0 |
+--------+---------------+---------------+----------+------------+------------+
9 rows in set (0.00 sec)
mysql> select * from factura;
+------------+--------+----------+-----+-----------+-------+
| nrofactura | cedula | subtotal | iva | retencion | total |
+------------+--------+----------+-----+-----------+-------+
| 1001 | 100 | 0 | 0 | 0 | 0 |
| 1101 | 600 | 0 | 0 | 0 | 0 |
| 2002 | 300 | 0 | 0 | 0 | 0 |
| 2202 | 700 | 0 | 0 | 0 | 0 |
| 3003 | 200 | 0 | 0 | 0 | 0 |
| 3303 | 600 | 0 | 0 | 0 | 0 |
| 4004 | 400 | 0 | 0 | 0 | 0 |
| 4404 | 100 | 0 | 0 | 0 | 0 |
| 5005 | 200 | 0 | 0 | 0 | 0 |
| 5505 | 700 | 0 | 0 | 0 | 0 |
| 6006 | 500 | 0 | 0 | 0 | 0 |
| 6606 | 800 | 0 | 0 | 0 | 0 |
| 7007 | 100 | 0 | 0 | 0 | 0 |
| 7707 | 900 | 0 | 0 | 0 | 0 |
| 8008 | 400 | 0 | 0 | 0 | 0 |
| 8808 | 800 | 0 | 0 | 0 | 0 |
| 9009 | 400 | 0 | 0 | 0 | 0 |
| 9909 | 900 | 0 | 0 | 0 | 0 |
+------------+--------+----------+-----+-----------+-------+
18 rows in set (0.00 sec)
mysql> select * from detallefactura;
+------+------------+--------+----------+-------+-------+
| id | nrofactura | codigo | cantidad | valor | total |
+------+------------+--------+----------+-------+-------+
| 0001 | 1001 | 10 | 1 | 0 | 0 |
| 0002 | 1001 | 40 | 2 | 0 | 0 |
| 0003 | 1001 | 70 | 1 | 0 | 0 |
| 0004 | 2002 | 60 | 2 | 0 | 0 |
| 0005 | 3003 | 20 | 4 | 0 | 0 |
| 0006 | 3003 | 80 | 1 | 0 | 0 |
| 0007 | 4004 | 10 | 2 | 0 | 0 |
| 0008 | 4004 | 20 | 1 | 0 | 0 |
| 0009 | 4004 | 30 | 1 | 0 | 0 |
| 0010 | 4004 | 60 | 1 | 0 | 0 |
| 0011 | 4004 | 70 | 1 | 0 | 0 |
| 0012 | 4004 | 80 | 2 | 0 | 0 |
| 0013 | 5005 | 10 | 3 | 0 | 0 |
| 0014 | 6006 | 20 | 1 | 0 | 0 |
| 0015 | 6006 | 80 | 2 | 0 | 0 |
| 0016 | 6006 | 30 | 2 | 0 | 0 |
| 0017 | 6006 | 90 | 2 | 0 | 0 |
| 0018 | 6006 | 50 | 1 | 0 | 0 |
| 0019 | 6006 | 40 | 2 | 0 | 0 |
| 0020 | 7007 | 10 | 2 | 0 | 0 |
| 0021 | 7007 | 90 | 2 | 0 | 0 |
| 0022 | 8008 | 60 | 2 | 0 | 0 |
| 0023 | 8008 | 40 | 2 | 0 | 0 |
| 0024 | 8008 | 50 | 1 | 0 | 0 |
| 0025 | 9009 | 50 | 2 | 0 | 0 |
| 0026 | 9009 | 80 | 1 | 0 | 0 |
| 0027 | 1101 | 30 | 1 | 0 | 0 |
| 0028 | 2202 | 30 | 2 | 0 | 0 |
| 0029 | 1101 | 60 | 1 | 0 | 0 |
| 0030 | 3303 | 60 | 2 | 0 | 0 |
| 0031 | 4404 | 90 | 3 | 0 | 0 |
| 0032 | 5505 | 80 | 4 | 0 | 0 |
| 0033 | 6606 | 70 | 2 | 0 | 0 |
| 0034 | 7707 | 10 | 1 | 0 | 0 |
| 0035 | 8808 | 40 | 2 | 0 | 0 |
| 0036 | 9909 | 20 | 3 | 0 | 0 |
| 0037 | 9909 | 30 | 4 | 0 | 0 |
| 0038 | 6606 | 50 | 5 | 0 | 0 |
| 0039 | 7707 | 40 | 1 | 0 | 0 |
| 0040 | 1101 | 30 | 1 | 0 | 0 |
+------+------------+--------+----------+-------+-------+
40 rows in set (0.00 sec)
- calcular valorventa aumentando un 17% del valorunitario
1 actualizamos el campo valorventa de la tabla producto
2 tablas afectadas solo una producto
3 condiciones en particula o hay
4 comando updtae
sintaxis
update producto set valorventa=valorunitario*1.17;
mysql> update producto set valorventa=valorunitario*1.17;
Query OK, 9 rows affected (0.13 sec)
Rows matched: 9 Changed: 9 Warnings: 0
mysql> select * from producto;
+--------+---------------+---------------+----------+------------+------------+
| codigo | articulo | valorunitario | cantidad | valorventa | existencia |
+--------+---------------+---------------+----------+------------+------------+
| 10 | tv | 1250000 | 22 | 1462500 | 0 |
| 20 | auriculares | 75000 | 27 | 87750 | 0 |
| 30 | mp3 | 150000 | 24 | 175500 | 0 |
| 40 | mouse | 25000 | 33 | 29250 | 0 |
| 50 | teclado | 130000 | 45 | 152100 | 0 |
| 60 | disco duro | 203000 | 17 | 237510 | 0 |
| 70 | unidad de dvd | 250000 | 19 | 292500 | 0 |
| 80 | usb | 80000 | 28 | 93600 | 0 |
| 90 | lapiz optico | 134000 | 17 | 156780 | 0 |
+--------+---------------+---------------+----------+------------+------------+
9 rows in set (0.00 sec)
mysql>
- llevar el valorventa a valor de detallefactura segun articulo comprado
1 actualizamos el campo valor de la tabla detallefactura con el valorventa de la
tabla producto segun el articulo comprado
2 tablas afectadas una producto y detallefactura
3 condiciones que cuando producto.codigo=detallefactura.codigo
4 comando updtae
mysql> update factura set iva=subtotal*.16;
Query OK, 18 rows affected (0.08 sec)
Rows matched: 18 Changed: 18 Warnings: 0
mysql> update factura set retencion=(subtotal-iva)*0.035;
Query OK, 18 rows affected (0.86 sec)
Rows matched: 18 Changed: 18 Warnings: 0
mysql> select * from factura;
+------------+--------+----------+--------+-----------+-------+
| nrofactura | cedula | subtotal | iva | retencion | total |
+------------+--------+----------+--------+-----------+-------+
| 1001 | 100 | 1813500 | 290160 | 53317 | 0 |
| 1101 | 600 | 588510 | 94162 | 17302 | 0 |
| 2002 | 300 | 475020 | 76003 | 13966 | 0 |
| 2202 | 700 | 351000 | 56160 | 10319 | 0 |
| 3003 | 200 | 444600 | 71136 | 13071 | 0 |
| 3303 | 600 | 475020 | 76003 | 13966 | 0 |
| 4004 | 400 | 3905460 | 624874 | 114821 | 0 |
| 4404 | 100 | 470340 | 75254 | 13828 | 0 |
| 5005 | 200 | 4387500 | 702000 | 128993 | 0 |
| 5505 | 700 | 374400 | 59904 | 11007 | 0 |
| 6006 | 500 | 1150110 | 184018 | 33813 | 0 |
| 6606 | 800 | 1345500 | 215280 | 39558 | 0 |
| 7007 | 100 | 3238560 | 518170 | 95214 | 0 |
| 7707 | 900 | 1491750 | 238680 | 43857 | 0 |
| 8008 | 400 | 685620 | 109699 | 20157 | 0 |
| 8808 | 800 | 58500 | 9360 | 1720 | 0 |
| 9009 | 400 | 397800 | 63648 | 11695 | 0 |
| 9909 | 900 | 965250 | 154440 | 28378 | 0 |
+------------+--------+----------+--------+-----------+-------+
18 rows in set (0.00 sec)
sintaxis
mysql> update detallefactura set valor=(select valorventa from producto where pr
oducto.codigo=detallefactura.codigo);
Query OK, 40 rows affected (0.11 sec)
Rows matched: 40 Changed: 40 Warnings: 0
mysql> select * from detallefactura;
+------+------------+--------+----------+---------+-------+
| id | nrofactura | codigo | cantidad | valor | total |
+------+------------+--------+----------+---------+-------+
| 0001 | 1001 | 10 | 1 | 1462500 | 0 |
| 0002 | 1001 | 40 | 2 | 29250 | 0 |
| 0003 | 1001 | 70 | 1 | 292500 | 0 |
| 0004 | 2002 | 60 | 2 | 237510 | 0 |
| 0005 | 3003 | 20 | 4 | 87750 | 0 |
| 0006 | 3003 | 80 | 1 | 93600 | 0 |
| 0007 | 4004 | 10 | 2 | 1462500 | 0 |
| 0008 | 4004 | 20 | 1 | 87750 | 0 |
| 0009 | 4004 | 30 | 1 | 175500 | 0 |
| 0010 | 4004 | 60 | 1 | 237510 | 0 |
| 0011 | 4004 | 70 | 1 | 292500 | 0 |
| 0012 | 4004 | 80 | 2 | 93600 | 0 |
| 0013 | 5005 | 10 | 3 | 1462500 | 0 |
| 0014 | 6006 | 20 | 1 | 87750 | 0 |
| 0015 | 6006 | 80 | 2 | 93600 | 0 |
| 0016 | 6006 | 30 | 2 | 175500 | 0 |
| 0017 | 6006 | 90 | 2 | 156780 | 0 |
| 0018 | 6006 | 50 | 1 | 152100 | 0 |
| 0019 | 6006 | 40 | 2 | 29250 | 0 |
| 0020 | 7007 | 10 | 2 | 1462500 | 0 |
| 0021 | 7007 | 90 | 2 | 156780 | 0 |
| 0022 | 8008 | 60 | 2 | 237510 | 0 |
| 0023 | 8008 | 40 | 2 | 29250 | 0 |
| 0024 | 8008 | 50 | 1 | 152100 | 0 |
| 0025 | 9009 | 50 | 2 | 152100 | 0 |
| 0026 | 9009 | 80 | 1 | 93600 | 0 |
| 0027 | 1101 | 30 | 1 | 175500 | 0 |
| 0028 | 2202 | 30 | 2 | 175500 | 0 |
| 0029 | 1101 | 60 | 1 | 237510 | 0 |
| 0030 | 3303 | 60 | 2 | 237510 | 0 |
| 0031 | 4404 | 90 | 3 | 156780 | 0 |
| 0032 | 5505 | 80 | 4 | 93600 | 0 |
| 0033 | 6606 | 70 | 2 | 292500 | 0 |
| 0034 | 7707 | 10 | 1 | 1462500 | 0 |
| 0035 | 8808 | 40 | 2 | 29250 | 0 |
| 0036 | 9909 | 20 | 3 | 87750 | 0 |
| 0037 | 9909 | 30 | 4 | 175500 | 0 |
| 0038 | 6606 | 50 | 5 | 152100 | 0 |
| 0039 | 7707 | 40 | 1 | 29250 | 0 |
| 0040 | 1101 | 30 | 1 | 175500 | 0 |
+------+------------+--------+----------+---------+-------+
40 rows in set (0.00 sec)
mysql>
- calcular el total en la tabla detallefactura (cantidad x valor)
1 actualizamos el campo totalde la tabla detallefactura con los datos anteriores
2 tablas afectadas solo una detallefactura
3 condiciones en particula o hay
4 comando updtae
mysql> update detallefactura set total=cantidad*valor;
Query OK, 40 rows affected (0.05 sec)
Rows matched: 40 Changed: 40 Warnings: 0
mysql> select * from detallefactura;
+------+------------+--------+----------+---------+---------+
| id | nrofactura | codigo | cantidad | valor | total |
+------+------------+--------+----------+---------+---------+
| 0001 | 1001 | 10 | 1 | 1462500 | 1462500 |
| 0002 | 1001 | 40 | 2 | 29250 | 58500 |
| 0003 | 1001 | 70 | 1 | 292500 | 292500 |
| 0004 | 2002 | 60 | 2 | 237510 | 475020 |
| 0005 | 3003 | 20 | 4 | 87750 | 351000 |
| 0006 | 3003 | 80 | 1 | 93600 | 93600 |
| 0007 | 4004 | 10 | 2 | 1462500 | 2925000 |
| 0008 | 4004 | 20 | 1 | 87750 | 87750 |
| 0009 | 4004 | 30 | 1 | 175500 | 175500 |
| 0010 | 4004 | 60 | 1 | 237510 | 237510 |
| 0011 | 4004 | 70 | 1 | 292500 | 292500 |
| 0012 | 4004 | 80 | 2 | 93600 | 187200 |
| 0013 | 5005 | 10 | 3 | 1462500 | 4387500 |
| 0014 | 6006 | 20 | 1 | 87750 | 87750 |
| 0015 | 6006 | 80 | 2 | 93600 | 187200 |
| 0016 | 6006 | 30 | 2 | 175500 | 351000 |
| 0017 | 6006 | 90 | 2 | 156780 | 313560 |
| 0018 | 6006 | 50 | 1 | 152100 | 152100 |
| 0019 | 6006 | 40 | 2 | 29250 | 58500 |
| 0020 | 7007 | 10 | 2 | 1462500 | 2925000 |
| 0021 | 7007 | 90 | 2 | 156780 | 313560 |
| 0022 | 8008 | 60 | 2 | 237510 | 475020 |
| 0023 | 8008 | 40 | 2 | 29250 | 58500 |
| 0024 | 8008 | 50 | 1 | 152100 | 152100 |
| 0025 | 9009 | 50 | 2 | 152100 | 304200 |
| 0026 | 9009 | 80 | 1 | 93600 | 93600 |
| 0027 | 1101 | 30 | 1 | 175500 | 175500 |
| 0028 | 2202 | 30 | 2 | 175500 | 351000 |
| 0029 | 1101 | 60 | 1 | 237510 | 237510 |
| 0030 | 3303 | 60 | 2 | 237510 | 475020 |
| 0031 | 4404 | 90 | 3 | 156780 | 470340 |
| 0032 | 5505 | 80 | 4 | 93600 | 374400 |
| 0033 | 6606 | 70 | 2 | 292500 | 585000 |
| 0034 | 7707 | 10 | 1 | 1462500 | 1462500 |
| 0035 | 8808 | 40 | 2 | 29250 | 58500 |
| 0036 | 9909 | 20 | 3 | 87750 | 263250 |
| 0037 | 9909 | 30 | 4 | 175500 | 702000 |
| 0038 | 6606 | 50 | 5 | 152100 | 760500 |
| 0039 | 7707 | 40 | 1 | 29250 | 29250 |
| 0040 | 1101 | 30 | 1 | 175500 | 175500 |
+------+------------+--------+----------+---------+---------+
40 rows in set (0.00 sec)
mysql>
- calcular cual es el subtotal de cada factura y guardarlo en el campo respectivo
1 actualizamos el campo subtotalde la tabla factura con los datos anteriores
2 tablas afectadas detallefactura y factura
3 condiciones que nrofactura.factura=nrofactura.detallefactura
4 comando updtae y sum
mysql> update factura set subtotal=(select total from detallefactura where fact
ra.nrofactura=detallefactura.nrofactura);
ERROR 1242 (21000): Subquery returns more than 1 row
mysql> update factura set subtotal=(select sum(total) from detallefactura where
factura.nrofactura=detallefactura.nrofactura);
Query OK, 18 rows affected (0.13 sec)
Rows matched: 18 Changed: 18 Warnings: 0
mysql> select * from factura;
+------------+--------+----------+-----+-----------+-------+
| nrofactura | cedula | subtotal | iva | retencion | total |
+------------+--------+----------+-----+-----------+-------+
| 1001 | 100 | 1813500 | 0 | 0 | 0 |
| 1101 | 600 | 588510 | 0 | 0 | 0 |
| 2002 | 300 | 475020 | 0 | 0 | 0 |
| 2202 | 700 | 351000 | 0 | 0 | 0 |
| 3003 | 200 | 444600 | 0 | 0 | 0 |
| 3303 | 600 | 475020 | 0 | 0 | 0 |
| 4004 | 400 | 3905460 | 0 | 0 | 0 |
| 4404 | 100 | 470340 | 0 | 0 | 0 |
| 5005 | 200 | 4387500 | 0 | 0 | 0 |
| 5505 | 700 | 374400 | 0 | 0 | 0 |
| 6006 | 500 | 1150110 | 0 | 0 | 0 |
| 6606 | 800 | 1345500 | 0 | 0 | 0 |
| 7007 | 100 | 3238560 | 0 | 0 | 0 |
| 7707 | 900 | 1491750 | 0 | 0 | 0 |
| 8008 | 400 | 685620 | 0 | 0 | 0 |
| 8808 | 800 | 58500 | 0 | 0 | 0 |
| 9009 | 400 | 397800 | 0 | 0 | 0 |
| 9909 | 900 | 965250 | 0 | 0 | 0 |
+------------+--------+----------+-----+-----------+-------+
18 rows in set (0.00 sec)
mysql>
- calcular el iva (16% del subtotal), la retencion(3.5% del subtotal) y el total
(subtotal + iva - retencion)
1 actualizamos el campo iva y retencion tabla factura con los datos anteriores
2 tablas afectadas detallefactura y factura
3 condiciones no hay
4 comando updtae
mysql> update factura set iva=subtotal*.16;
Query OK, 18 rows affected (0.08 sec)
Rows matched: 18 Changed: 18 Warnings: 0
mysql> update factura set retencion=(subtotal-iva)*0.035;
Query OK, 18 rows affected (0.86 sec)
Rows matched: 18 Changed: 18 Warnings: 0
mysql> select * from factura;
+------------+--------+----------+--------+-----------+-------+
| nrofactura | cedula | subtotal | iva | retencion | total |
+------------+--------+----------+--------+-----------+-------+
| 1001 | 100 | 1813500 | 290160 | 53317 | 0 |
| 1101 | 600 | 588510 | 94162 | 17302 | 0 |
| 2002 | 300 | 475020 | 76003 | 13966 | 0 |
| 2202 | 700 | 351000 | 56160 | 10319 | 0 |
| 3003 | 200 | 444600 | 71136 | 13071 | 0 |
| 3303 | 600 | 475020 | 76003 | 13966 | 0 |
| 4004 | 400 | 3905460 | 624874 | 114821 | 0 |
| 4404 | 100 | 470340 | 75254 | 13828 | 0 |
| 5005 | 200 | 4387500 | 702000 | 128993 | 0 |
| 5505 | 700 | 374400 | 59904 | 11007 | 0 |
| 6006 | 500 | 1150110 | 184018 | 33813 | 0 |
| 6606 | 800 | 1345500 | 215280 | 39558 | 0 |
| 7007 | 100 | 3238560 | 518170 | 95214 | 0 |
| 7707 | 900 | 1491750 | 238680 | 43857 | 0 |
| 8008 | 400 | 685620 | 109699 | 20157 | 0 |
| 8808 | 800 | 58500 | 9360 | 1720 | 0 |
| 9009 | 400 | 397800 | 63648 | 11695 | 0 |
| 9909 | 900 | 965250 | 154440 | 28378 | 0 |
+------------+--------+----------+--------+-----------+-------+
18 rows in set (0.00 sec)
mysql> update factura set total=subtotal+iva-retencion;
Query OK, 18 rows affected (0.06 sec)
Rows matched: 18 Changed: 18 Warnings: 0
mysql> select * from factura;
+------------+--------+----------+--------+-----------+---------+
| nrofactura | cedula | subtotal | iva | retencion | total |
+------------+--------+----------+--------+-----------+---------+
| 1001 | 100 | 1813500 | 290160 | 53317 | 2050343 |
| 1101 | 600 | 588510 | 94162 | 17302 | 665370 |
| 2002 | 300 | 475020 | 76003 | 13966 | 537057 |
| 2202 | 700 | 351000 | 56160 | 10319 | 396841 |
| 3003 | 200 | 444600 | 71136 | 13071 | 502665 |
| 3303 | 600 | 475020 | 76003 | 13966 | 537057 |
| 4004 | 400 | 3905460 | 624874 | 114821 | 4415513 |
| 4404 | 100 | 470340 | 75254 | 13828 | 531766 |
| 5005 | 200 | 4387500 | 702000 | 128993 | 4960507 |
| 5505 | 700 | 374400 | 59904 | 11007 | 423297 |
| 6006 | 500 | 1150110 | 184018 | 33813 | 1300315 |
| 6606 | 800 | 1345500 | 215280 | 39558 | 1521222 |
| 7007 | 100 | 3238560 | 518170 | 95214 | 3661516 |
| 7707 | 900 | 1491750 | 238680 | 43857 | 1686573 |
| 8008 | 400 | 685620 | 109699 | 20157 | 775162 |
| 8808 | 800 | 58500 | 9360 | 1720 | 66140 |
| 9009 | 400 | 397800 | 63648 | 11695 | 449753 |
| 9909 | 900 | 965250 | 154440 | 28378 | 1091312 |
+------------+--------+----------+--------+-----------+---------+
18 rows in set (0.00 sec)
- calcular cuales son las existencias de cada articulo
1 actualizamos el campo exitencia de la tabla producto con salida en las facturas
2 tablas afectadas detallefactura y producto
3 condiciones no hay la relacion entre detallefactura.cantidad y producto.cantidad
4 comando updtae
mysql> select * from producto;
+--------+---------------+---------------+----------+------------+------------+
| codigo | articulo | valorunitario | cantidad | valorventa | existencia |
+--------+---------------+---------------+----------+------------+------------+
| 10 | tv | 1250000 | 22 | 1462500 | 0 |
| 20 | auriculares | 75000 | 27 | 87750 | 0 |
| 30 | mp3 | 150000 | 24 | 175500 | 0 |
| 40 | mouse | 25000 | 33 | 29250 | 0 |
| 50 | teclado | 130000 | 45 | 152100 | 0 |
| 60 | disco duro | 203000 | 17 | 237510 | 0 |
| 70 | unidad de dvd | 250000 | 19 | 292500 | 0 |
| 80 | usb | 80000 | 28 | 93600 | 0 |
| 90 | lapiz optico | 134000 | 17 | 156780 | 0 |
+--------+---------------+---------------+----------+------------+------------+
9 rows in set (0.00 sec)
mysql> update producto set existencia=cantidad-(select sum(cantidad) from detall
efactura where detallefactura.codigo=producto.codigo);
Query OK, 9 rows affected (0.19 sec)
Rows matched: 9 Changed: 9 Warnings: 0
mysql> select * from producto;
+--------+---------------+---------------+----------+------------+------------+
| codigo | articulo | valorunitario | cantidad | valorventa | existencia |
+--------+---------------+---------------+----------+------------+------------+
| 10 | tv | 1250000 | 22 | 1462500 | 13 |
| 20 | auriculares | 75000 | 27 | 87750 | 18 |
| 30 | mp3 | 150000 | 24 | 175500 | 13 |
| 40 | mouse | 25000 | 33 | 29250 | 24 |
| 50 | teclado | 130000 | 45 | 152100 | 36 |
| 60 | disco duro | 203000 | 17 | 237510 | 9 |
| 70 | unidad de dvd | 250000 | 19 | 292500 | 15 |
| 80 | usb | 80000 | 28 | 93600 | 18 |
| 90 | lapiz optico | 134000 | 17 | 156780 | 10 |
+--------+---------------+---------------+----------+------------+------------+
9 rows in set (0.00 sec)
mysql>
- cuantas facturas tiene cada cliente
mysql> select nombre, count(nombre) as facturas from factura,cliente where factu
ra.cedula=cliente.cedula group by nombre;
+--------------------+----------+
| nombre | facturas |
+--------------------+----------+
| alexandra lopez | 3 |
| claudia lainez | 2 |
| edith garcia | 2 |
| luisa cifuentes | 2 |
| lusa roldan | 3 |
| mario henao | 1 |
| sara herrera | 1 |
| sebastian perez | 2 |
| viviana piedrahita | 2 |
+--------------------+----------+
9 rows in set (0.09 sec)
mysql>
- listar el total(suma) de ventas de cada producto
mysql> select articulo, sum(total) as 'ventas por producto' from detallefactura,
producto where detallefactura.codigo=producto.codigo group by articulo
-> ;
+---------------+---------------------+
| articulo | ventas por producto |
+---------------+---------------------+
| auriculares | 789750 |
| disco duro | 1900080 |
| lapiz optico | 1097460 |
| mouse | 263250 |
| mp3 | 1930500 |
| teclado | 1368900 |
| tv | 13162500 |
| unidad de dvd | 1170000 |
| usb | 936000 |
+---------------+---------------------+
9 rows in set (0.00 sec)
mysql>
- listar las facturas y el valor de cada una segun las compra de alexandra lopez
mysql> select cedula,nrofactura,total from factura where factura.cedula=(select
cedula from cliente where nombre='alexandra lopez');
+--------+------------+---------+
| cedula | nrofactura | total |
+--------+------------+---------+
| 100 | 1001 | 2050343 |
| 100 | 4404 | 531766 |
| 100 | 7007 | 3661516 |
+--------+------------+---------+
3 rows in set (0.05 sec)
mysql>
-crear una tabla detallado dentro de la bd subconsultas con los campos
id auto incremento
cedula
nrofactura
totalarticulos
valortotal
mysql> create table detallado (id int(6) primary key not null auto_increment, ce
dula char(13) not null, nrofactura int(8) not null, totalarticulos int(6) not nu
ll, valortotal int(15) not null);
Query OK, 0 rows affected (0.36 sec)
mysql> describe detallado;
+----------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+----------+------+-----+---------+----------------+
| id | int(6) | NO | PRI | NULL | auto_increment |
| cedula | char(13) | NO | | NULL | |
| nrofactura | int(8) | NO | | NULL | |
| totalarticulos | int(6) | NO | | NULL | |
| valortotal | int(15) | NO | | NULL | |
+----------------+----------+------+-----+---------+----------------+
5 rows in set (0.02 sec)
mysql>
crear una subconsulta para ingresar estos datos a la tabla detallado
cedula del cliente
numero de facturas del cliente
suma de los articulos de cada factura
total pagado de cada factura
mysql> insert into detallado select null,cedula,nrofactura,subtotal,total from f
actura;
Query OK, 18 rows affected (0.10 sec)
Records: 18 Duplicates: 0 Warnings: 0
mysql> select * from detallado;
+----+--------+------------+----------------+------------+
| id | cedula | nrofactura | totalarticulos | valortotal |
+----+--------+------------+----------------+------------+
| 1 | 100 | 1001 | 1813500 | 2050343 |
| 2 | 600 | 1101 | 588510 | 665370 |
| 3 | 300 | 2002 | 475020 | 537057 |
| 4 | 700 | 2202 | 351000 | 396841 |
| 5 | 200 | 3003 | 444600 | 502665 |
| 6 | 600 | 3303 | 475020 | 537057 |
| 7 | 400 | 4004 | 3905460 | 4415513 |
| 8 | 100 | 4404 | 470340 | 531766 |
| 9 | 200 | 5005 | 4387500 | 4960507 |
| 10 | 700 | 5505 | 374400 | 423297 |
| 11 | 500 | 6006 | 1150110 | 1300315 |
| 12 | 800 | 6606 | 1345500 | 1521222 |
| 13 | 100 | 7007 | 3238560 | 3661516 |
| 14 | 900 | 7707 | 1491750 | 1686573 |
| 15 | 400 | 8008 | 685620 | 775162 |
| 16 | 800 | 8808 | 58500 | 66140 |
| 17 | 400 | 9009 | 397800 | 449753 |
| 18 | 900 | 9909 | 965250 | 1091312 |
+----+--------+------------+----------------+------------+
18 rows in set (0.00 sec)
mysql>
- eliminar las facturas de los clientes que hayan iniciado vinculo con la empresa
antes de 2004
mysql> delete from detallado where cedula=(select cedula from cliente where year
(fechainicio)<2004 and detallado.cedula=cliente.cedula);
Query OK, 5 rows affected (0.07 sec)
mysql> select * from detallado;
+----+--------+------------+----------------+------------+
| id | cedula | nrofactura | totalarticulos | valortotal |
+----+--------+------------+----------------+------------+
| 3 | 300 | 2002 | 475020 | 537057 |
| 4 | 700 | 2202 | 351000 | 396841 |
| 5 | 200 | 3003 | 444600 | 502665 |
| 7 | 400 | 4004 | 3905460 | 4415513 |
| 9 | 200 | 5005 | 4387500 | 4960507 |
| 10 | 700 | 5505 | 374400 | 423297 |
| 11 | 500 | 6006 | 1150110 | 1300315 |
| 12 | 800 | 6606 | 1345500 | 1521222 |
| 14 | 900 | 7707 | 1491750 | 1686573 |
| 15 | 400 | 8008 | 685620 | 775162 |
| 16 | 800 | 8808 | 58500 | 66140 |
| 17 | 400 | 9009 | 397800 | 449753 |
| 18 | 900 | 9909 | 965250 | 1091312 |
+----+--------+------------+----------------+------------+
13 rows in set (0.00 sec)
mysql>