
Comandos mysql
Comandos mysql
Introduce un texto aquí...
Microsoft Windows [Versión 10.0.22631.3737]
(c) Microsoft Corporation. Todos los derechos reservados.
C:\Users\A01-1-0604-11>cd/xampp/mysql/bin
C:\xampp\mysql\bin>mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.4.21-MariaDB mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
USE se utiliza para seleccionar la base de datos que estaremos trabajando y en donde se encontrarán nuestras tablas.
MariaDB [(none)]> show databases;
+--------------------+
- Database |
+--------------------+
- information_schema |
- mysql |
- performance_schema |
- phpmyadmin |
- test |
+--------------------+
5 rows in set (0.014 sec)
MariaDB [(none)]>
____________________________//_________________________
1. xampp
apache
mysql
2.cms o cmd
C:\Users\A01-1-0604-11>cd/xampp/mysql/bin
C:\xampp\mysql\bin>mysql -uroot -p -> pendejo no olvide el espacio
3.comando:
show databases; -> mostrar
tee -> para guardar en: tee c:/xampp/prueba2.txt
Para crear una base de dato:
create database nombre_la_BD; = el nombre tiene que tener relación con lo que se va hacer
eje:
MariaDB [(none)]> create database bdbiblioteca;
Como abrir una base de dato:
use nombre_base_datos;
eje:
MariaDB [(none)]> use bdbiblioteca;
para eliminar:
drop database bdbiblioteca;
_____________________//________
visualizar y crear una tabla:
MariaDB [Bd_Biblioteca]> create table Autor
-> (Id_Autor varchar(5) not null primary key,
-> Nom_Aut varchar(60) not null,
-> Nacionalidad varchar(20) not null);
Query OK, 0 rows affected (0.016 sec)
MariaDB [Bd_Biblioteca]> describe libro
-> ;
+----------+-------------+------+-----+---------+-------+
- Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
- Id_libro | varchar(10) | NO | PRI | NULL | |
- Titulo | varchar(60) | NO | | NULL | |
- Genero | varchar(30) | NO | | NULL | |
- precio | float | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
________//__________//___________
como eliminar una clave primaria:
comando Drop
Sintaxis
alter table nombretabla drop primary key;
Ejemplo:
+--------------+-------------+------+-----+---------+-------+
- Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
- idlibro | varchar(20) | NO | PRI | NULL | |
- Titulo | varchar(60) | NO | | NULL | |
- Nrodepaginas | int(4) | NO | | NULL | |
- precio | float | NO | | NULL | |
+--------------+-------------+------+-----+---------+-------+
4 rows in set (0.012 sec)
MariaDB [prueba]> alter table libro drop primary key;<- eliminar clave primaria.
Query OK, 0 rows affected (1.054 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [prueba]> describe libro;
+--------------+-------------+------+-----+---------+-------+
- Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
- idlibro | varchar(20) | NO | | NULL | |
- Titulo | varchar(60) | NO | | NULL | |
- Nrodepaginas | int(4) | NO | | NULL | |
- precio | float | NO | | NULL | |
+--------------+-------------+------+-----+---------+-------+
Como definir nuevamente la clave primaria a una tabla comando modify:
Sintaxis:
alter table nombretabla modify nombre_de_campa tipo (tamaño)
ejemplo:
MariaDB [prueba]> alter table libro modify idlibro varchar(20) not null primary key;
Como agregar compos a una tabla al final comando add:
sintaxis.
alter table nombretabla add nombre_campo tipo(tamaño) not null;
ejemplo:
alter tabla libro add Cantidad int(3) not null;
MariaDB [prueba]> alter table libro add Cantidad int(3) not null;
Query OK, 0 rows affected (0.114 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [prueba]> describe libro;
+--------------+-------------+------+-----+---------+-------+
- Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
- idlibro | varchar(20) | NO | PRI | NULL | |
- Titulo | varchar(60) | NO | | NULL | |
- Nrodepaginas | int(4) | NO | | NULL | |
- precio | float | NO | | NULL | |
- Cantidad | int(3) | NO | | NULL | |
+--------------+-------------+------+-----+---------+-------+
________//__________//___________
COMO COLOCAR UN CAMPO DE OTRO SEGUN LA NECESIDAD:
alter table (nombre campo) tipo(tamaño) not null after(nombre del campo que va a quedar antes del nuevo campo);
Ejemplo:
alter table libro add area varchar(20) not null after Titulo;
MariaDB [prueba]> alter table libro add area varchar(20)not null after Titulo;
Query OK, 0 rows affected (0.167 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [prueba]> describe libro;
+--------------+-------------+------+-----+---------+-------+
- Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
- idlibro | varchar(20) | NO | PRI | NULL | |
- Titulo | varchar(60) | NO | | NULL | |
- area | varchar(20) | NO | | NULL | |
- Nrodepaginas | int(4) | NO | | NULL | |
- precio | float | NO | | NULL | |
- Cantidad | int(3) | NO | | NULL | |
+--------------+-------------+------+-----+---------+-------+
INSETAR UN CAMPO AL PRINCIPIO DE LA TABLA:
COMANDO FIRST:
Sintaxis:
alter table nombretabla add nombre_campo tipo(tamaño) not null first;
Ejemplo:
MariaDB [prueba]> alter table libro add serial varchar(15) not null first;
Query OK, 0 rows affected (0.185 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [prueba]> describe libro;
+--------------+-------------+------+-----+---------+-------+
- Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
- serial | varchar(15) | NO | | NULL | |
- idlibro | varchar(20) | NO | PRI | NULL | |
- Titulo | varchar(60) | NO | | NULL | |
- area | varchar(20) | NO | | NULL | |
- Nrodepaginas | int(4) | NO | | NULL | |
- precio | float | NO | | NULL | |
- Cantidad | int(3) | NO | | NULL | |
+--------------+-------------+------+-----+---------+-------+
ELIMINAR CAMPO DE UNA TABLE COMANDO DROP:
SINTAXIS:
alter table nombretabla drop nombre_campo ;
Ejemplo;
alter table libro drop serial;
MariaDB [prueba]> alter table libro drop serial;
Query OK, 0 rows affected (0.155 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [prueba]> describe libro;
+--------------+-------------+------+-----+---------+-------+
- Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
- idlibro | varchar(20) | NO | PRI | NULL | |
- Titulo | varchar(60) | NO | | NULL | |
- area | varchar(20) | NO | | NULL | |
- Nrodepaginas | int(4) | NO | | NULL | |
- precio | float | NO | | NULL | |
- Cantidad | int(3) | NO | | NULL | |
+--------------+-------------+------+-----+---------+-------+
COMO CAMBIAR EL NOMBRE DE UN CAMPO COMANDO CHANGE:
SINTAXIS:
alter table nombretabla change nombre_campo_actual nuevo_nombre tipo(tamaño) not null;
Ejemplo:
MariaDB [prueba]> alter table libro change titulo Nombre varchar(60) not null;
Query OK, 0 rows affected (0.123 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [prueba]> describe libro;
+--------------+-------------+------+-----+---------+-------+
- Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
- idlibro | varchar(20) | NO | PRI | NULL | |
- Nombre | varchar(60) | NO | | NULL | |
- area | varchar(20) | NO | | NULL | |
- Nrodepaginas | int(4) | NO | | NULL | |
- precio | float | NO | | NULL | |
- Cantidad | int(3) | NO | | NULL | |
+--------------+-------------+------+-----+---------+-------+
________//__________//___________
VISUALIZAR COMO ESTA CREADA UNA TABLA:
COMANDO SHOW:
Sintaxis:
show create table nombretabla;
Ejemplo:
MariaDB [prueba]> show create table libro;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- Table | Create Table |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- libro | CREATE TABLE `libro` (
`idlibro` varchar(20) NOT NULL,
`Nombre` varchar(60) NOT NULL,
`area` varchar(20) NOT NULL,
`Nrodepaginas` int(4) NOT NULL,
`precio` float NOT NULL,
`Cantidad` int(3) NOT NULL,
PRIMARY KEY (`idlibro`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
________//__________//___________
COMO CAMBIAR EL NOMBRE DE UNA TABLA:
Comando Rename:
Sintaxis
alter table nombretable rename to nuevonombretable;
Ejemplo:
MariaDB [prueba]> alter table libro rename to obra;
MariaDB [prueba]> show tables;<- ver la tabla
+------------------+
- Tables_in_prueba |
+------------------+
- obra |
+------------------+
________//__________//___________
ELIMINAR UNA TABLA:
Comando: drop
ejemplo:
drop table libro;
________//__________//_______
INSERTAR INFORMACIÓN DE UNA TABLA
Comando Insert
insert into nombretabla(campo1,campo2,campo3,…campon) values(dato1,dato2,dato3,…daton);
insert into libro(Idlibro,Titulo,Area,Cantidad,Nropaginas,Precio) values('001','A','Informatica',50,50,4000);
COMANDO: Insert
Sintaxis
________//__________//_______
VISUALIZAR INFORMACIÓN DE UNA TABLA:
Ejemplo:
MariaDB [prueba]> select * from libro;
________//__________//_______
Como restaurar una base de datos eliminada:
Ejemplo:
source c:/xampp/prueba.sql
Clase 3:
Consultas con Condicionales
Como montar un trabajo en cms:
MariaDB [(none)]> source C:\Users\USUARIO\Downloads\libreria2.sql
source C:\Users\USUARIO\nombre_trabajo
ver algo en concreto:
MariaDB [libreria]> select titulo,precio from libro;
+---------------------+--------+
- titulo | precio |
+---------------------+--------+
- Cálculo II | 55000 |
- Bd II | 65000 |
- Estructura de Datos | 85000 |
- Inglés | 105000 |
- Admon en una página | 7500 |
- Contabiloidad I | 27500 |
- Redes | 32500 |
+---------------------+--------+
FUNCIONES:
Para contar la cantidad en este caso libros :
MariaDB [libreria]> select count(*) from libro;
+----------+
- count(*) |
+----------+
- 7 |
+----------+
1 row in set (0.000 sec)
Para contar la cantidad con un mensajes:MariaDB [libreria]> select count(*)"Cantidad de libro" from libro;
+-------------------+
- Cantidad de libro |
+-------------------+
- 7 |
+-------------------+
1 row in set (0.000 sec)
________//__________//_______
Para sumar o hallar el total:
MariaDB [libreria]> select * from libro;
+----------+---------------------+-------+--------+-----------+
- id_libro | Titulo | Numpg | Precio | codigmat1 |
+----------+---------------------+-------+--------+-----------+
- L01 | Cálculo II | 120 | 55000 | M01 |
- L02 | Bd II | 150 | 65000 | M09 |
- L03 | Estructura de Datos | 180 | 85000 | M03 |
- L04 | Inglés | 280 | 105000 | M04 |
- L05 | Admon en una página | 70 | 7500 | M05 |
- L06 | Contabiloidad I | 170 | 27500 | M06 |
- L07 | Redes | 370 | 32500 | M07 |
+----------+---------------------+-------+--------+-----------+
7 rows in set (0.000 sec)
MariaDB [libreria]> select sum(precio)"Valor total libros bb" from libro;
+-----------------------+
- Valor total libros bb |
+-----------------------+
- 377500 |
+-----------------------+
________//__________//_______
Para el valor máximo:
MariaDB [libreria]> select sum(precio)"Valor total libros bb" from libro;
+-----------------------+
- Valor total libros bb |
+-----------------------+
- 377500 |
+-----------------------+
1 row in set (0.000 sec)
MariaDB [libreria]> select max(precio)"Valordel libro más costoso bb" from libro;
+-------------------------------+
- Valordel libro más costoso bb |
+-------------------------------+
- 105000 |
+-------------------------------+
1 row in set (0.000 sec)
________//__________//_______
Para el valor mínimo:
MariaDB [libreria]> select min(precio)"Valordel libro menos costoso bb" from libro;
+---------------------------------+
- Valordel libro menos costoso bb |
+---------------------------------+
- 7500 |
+---------------------------------+
1 row in set (0.000 sec)
MariaDB [libreria]> select min(numpg)"Valordel libro menos costoso bb" from libro;
+---------------------------------+
- Valor del libro con menos paginas bb |
+---------------------------------+
- 70 |
+---------------------------------+
1 row in set (0.000 sec)
Para encontrar el promedio:
MariaDB [libreria]> select avg(precio)"Valor promedio libro bb" from libro;
+-------------------------+
| Valor promedio libro bb |
+-------------------------+
| 53928.57142857143 |
+-------------------------+
1 row in set (0.000 sec)
+----------+------------------------+-------+----------+--------+---------+-----------+
| id_libro | Titulo | Numpg | cantidad | Precio | vtotal | codigmat1 |
+----------+------------------------+-------+----------+--------+---------+-----------+
| L01 | Calculo II | 120 | 5 | 55000 | 275000 | M01 |
| L02 | Bd II | 150 | 10 | 65000 | 650000 | M09 |
| L03 | Estructura de Datos | 180 | 15 | 85000 | 1275000 | M03 |
| L04 | Inglés | 280 | 20 | 105000 | 2100000 | M04 |
| L05 | Admon en una página | 70 | 25 | 7500 | 187500 | M05 |
| L06 | Contabilidad I | 170 | 30 | 27500 | 825000 | M06 |
| L07 | Redes y Comunicaciones | 370 | 35 | 32500 | 1137500 | M07 |
+----------+------------------------+-------+----------+--------+---------+-----------+
7 rows in set (0.000 sec)
MariaDB [libreria]> delete from libro where id_libro="l07";
Query OK, 1 row affected (0.002 sec)
MariaDB [libreria]> select * from libro;
+----------+---------------------+-------+----------+--------+---------+-----------+
| id_libro | Titulo | Numpg | cantidad | Precio | vtotal | codigmat1 |
+----------+---------------------+-------+----------+--------+---------+-----------+
| L01 | Calculo II | 120 | 5 | 55000 | 275000 | M01 |
| L02 | Bd II | 150 | 10 | 65000 | 650000 | M09 |
| L03 | Estructura de Datos | 180 | 15 | 85000 | 1275000 | M03 |
| L04 | Inglés | 280 | 20 | 105000 | 2100000 | M04 |
| L05 | Admon en una página | 70 | 25 | 7500 | 187500 | M05 |
| L06 | Contabilidad I | 170 | 30 | 27500 | 825000 | M06 |
+----------+---------------------+-------+----------+--------+---------+-----------+<
strong>+----------+------------------------+-------+----------+--------+---------+-----------+| id_libro | Titulo | Numpg | cantidad | Precio | vtotal | codigmat1 |
+----------+------------------------+-------+----------+--------+---------+-----------+
| L01 | Calculo II | 120 | 5 | 55000 | 275000 | M01 |
| L02 | Bd II | 150 | 10 | 65000 | 650000 | M09 |
| L03 | Estructura de Datos | 180 | 15 | 85000 | 1275000 | M03 |
| L04 | Inglés | 280 | 20 | 105000 | 2100000 | M04 |
| L05 | Admon en una página | 70 | 25 | 7500 | 187500 | M05 |
| L06 | Contabilidad I | 170 | 30 | 27500 | 825000 | M06 |
| L07 | Redes y Comunicaciones | 370 | 35 | 32500 | 1137500 | M07 |
+----------+------------------------+-------+----------+--------+---------+-----------+
7 rows in set (0.000 sec)
MariaDB [libreria]> delete from libro where id_libro="l07";
Query OK, 1 row affected (0.002 sec)
MariaDB [libreria]> select * from libro;
+----------+---------------------+-------+----------+--------+---------+-----------+
| id_libro | Titulo | Numpg | cantidad | Precio | vtotal | codigmat1 |
+----------+---------------------+-------+----------+--------+---------+-----------+
| L01 | Calculo II | 120 | 5 | 55000 | 275000 | M01 |
| L02 | Bd II | 150 | 10 | 65000 | 650000 | M09 |
| L03 | Estructura de Datos | 180 | 15 | 85000 | 1275000 | M03 |
| L04 | Inglés | 280 | 20 | 105000 | 2100000 | M04 |
| L05 | Admon en una página | 70 | 25 | 7500 | 187500 | M05 |
| L06 | Contabilidad I | 170 | 30 | 27500 | 825000 | M06 |
+----------+---------------------+-------+----------+--------+---------+-----------+Modificar o actualizar datos o registro:
Comando: Update
Sintaxis:
update nombretabla set nombre del campo a actualizar="valor" condición;
Modificar Redes por Redes y comunicaciones:
MariaDB [libreria]> update libro set Titulo="Redes y Comunicaciones" where id_libro="l07";
Query OK, 1 row affected (0.002 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [libreria]> select * from libro;
+----------+------------------------+-------+--------+-----------+
| id_libro | Titulo | Numpg | Precio | codigmat1 |
+----------+------------------------+-------+--------+-----------+
| L01 | Cálculo II | 120 | 55000 | M01 |
| L02 | Bd II | 150 | 65000 | M09 |
| L03 | Estructura de Datos | 180 | 85000 | M03 |
| L04 | Inglés | 280 | 105000 | M04 |
| L05 | Admon en una página | 70 | 7500 | M05 |
| L06 | Contabiloidad I | 170 | 27500 | M06 |
| L07 | Redes y Comunicaciones | 370 | 32500 | M07 |
+----------+------------------------+-------+--------+-----------+
7 rows in set (0.000 sec)
Otro Ejemplo:
MariaDB [libreria]> update libro set Titulo="Redes y Comunicaciones" where id_libro="l07";
Query OK, 1 row affected (0.002 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [libreria]> select * from libro;
+----------+------------------------+-------+--------+-----------+
| id_libro | Titulo | Numpg | Precio | codigmat1 |
+----------+------------------------+-------+--------+-----------+
| L01 | Cálculo II | 120 | 55000 | M01 |
| L02 | Bd II | 150 | 65000 | M09 |
| L03 | Estructura de Datos | 180 | 85000 | M03 |
| L04 | Inglés | 280 | 105000 | M04 |
| L05 | Admon en una página | 70 | 7500 | M05 |
| L06 | Contabiloidad I | 170 | 27500 | M06 |
| L07 | Redes y Comunicaciones | 370 | 32500 | M07 |
+----------+------------------------+-------+--------+-----------+
7 rows in set (0.000 sec)
________//__________//_______
crear nuevos espacios en la tabla:
original :
+----------+------------------------+-------+--------+-----------+
- id_libro | Titulo | Numpg | Precio | codigmat1 |
+----------+------------------------+-------+--------+-----------+
- L01 | Cálculo II | 120 | 55000 | M01 |
- L02 | Bd II | 150 | 65000 | M09 |
- L03 | Estructura de Datos | 180 | 85000 | M03 |
- L04 | Inglés | 280 | 105000 | M04 |
- L05 | Admon en una página | 70 | 7500 | M05 |
- L06 | Contabilidad I | 170 | 27500 | M06 |
- L07 | Redes y Comunicaciones | 370 | 32500 | M07 |
+----------+------------------------+-------+--------+-----------+
Agregando nueva espacio en la tabla:
MariaDB [libreria]> alter table libro add cantidad int not null after numpg;
Query OK, 0 rows affected (0.006 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [libreria]> select * from libro;
+----------+------------------------+-------+----------+--------+-----------+
- id_libro | Titulo | Numpg | cantidad | Precio | codigmat1 |
+----------+------------------------+-------+----------+--------+-----------+
- L01 | Cálculo II | 120 | 0 | 55000 | M01 |
- L02 | Bd II | 150 | 0 | 65000 | M09 |
- L03 | Estructura de Datos | 180 | 0 | 85000 | M03 |
- L04 | Inglés | 280 | 0 | 105000 | M04 |
- L05 | Admon en una página | 70 | 0 | 7500 | M05 |
- L06 | Contabilidad I | 170 | 0 | 27500 | M06 |
- L07 | Redes y Comunicaciones | 370 | 0 | 32500 | M07 |
+----------+------------------------+-------+----------+--------+-----------+
________//__________//_______
rellenar los espacios del nuevo lugar ósea Cantidad :
MariaDB [libreria]> update libro set Cantidad="5" where id_libro="l01";
Query OK, 1 row affected (0.002 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [libreria]> update libro set Cantidad="10" where id_libro="l02";
Query OK, 1 row affected (0.002 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [libreria]> update libro set Cantidad="15" where id_libro="l03";
Query OK, 1 row affected (0.002 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [libreria]> update libro set Cantidad="20" where id_libro="l04";
Query OK, 1 row affected (0.002 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [libreria]> update libro set Cantidad="25" where id_libro="l05";
Query OK, 1 row affected (0.002 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [libreria]> update libro set Cantidad="30" where id_libro="l06";
Query OK, 1 row affected (0.002 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [libreria]> update libro set Cantidad="35" where id_libro="l07";
Query OK, 1 row affected (0.002 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [libreria]> select * from libro;
+----------+------------------------+-------+----------+--------+--------+-----------+
| id_libro | Titulo | Numpg | cantidad | Precio | vtotal | codigmat1 |
+----------+------------------------+-------+----------+--------+--------+-----------+
| L01 | Calculo II | 120 | 5 | 55000 | 0 | M01 |
| L02 | Bd II | 150 | 10 | 65000 | 0 | M09 |
| L03 | Estructura de Datos | 180 | 15 | 85000 | 0 | M03 |
| L04 | Inglés | 280 | 20 | 105000 | 0 | M04 |
| L05 | Admon en una página | 70 | 25 | 7500 | 0 | M05 |
| L06 | Contabilidad I | 170 | 30 | 27500 | 0 | M06 |
| L07 | Redes y Comunicaciones | 370 | 35 | 32500 | 0 | M07 |
+----------+------------------------+-------+----------+--------+--------+-----------+
7 rows in set (0.000 sec)
________//__________//_______
Como actualizar el campo de valor total : el campo de valor esta en 0 en todos sus espacios. vamos a multiplicar cantidad y precio para sacar el contenido de vtotal:
MariaDB [libreria]> update libro set vtotal=cantidad*precio;
Query OK, 7 rows affected (0.002 sec)
Rows matched: 7 Changed: 7 Warnings: 0
MariaDB [libreria]> select * from libro;
+----------+------------------------+-------+----------+--------+---------+-----------+
- id_libro | Titulo | Numpg | cantidad | Precio | vtotal | codigmat1 |
+----------+------------------------+-------+----------+--------+---------+-----------+
- L01 | Calculo II | 120 | 5 | 55000 | 275000 | M01 |
- L02 | Bd II | 150 | 10 | 65000 | 650000 | M09 |
- L03 | Estructura de Datos | 180 | 15 | 85000 | 1275000 | M03 |
- L04 | Inglés | 280 | 20 | 105000 | 2100000 | M04 |
- L05 | Admon en una página | 70 | 25 | 7500 | 187500 | M05 |
- L06 | Contabilidad I | 170 | 30 | 27500 | 825000 | M06 |
- L07 | Redes y Comunicaciones | 370 | 35 | 32500 | 1137500 | M07 |
+----------+------------------------+-------+----------+--------+---------+-----------+
7 rows in set (0.000 sec)
________//__________//_______
Como eliminar datos o registros:
dos formas:
Delete from libro where id_libro="l07";
Delete from libro where Titulo="redes y comunicaciones"
+----------+------------------------+-------+----------+--------+---------+-----------+
| id_libro | Titulo | Numpg | cantidad | Precio | vtotal | codigmat1 |
+----------+------------------------+-------+----------+--------+---------+-----------+
| L01 | Calculo II | 120 | 5 | 55000 | 275000 | M01 |
| L02 | Bd II | 150 | 10 | 65000 | 650000 | M09 |
| L03 | Estructura de Datos | 180 | 15 | 85000 | 1275000 | M03 |
| L04 | Inglés | 280 | 20 | 105000 | 2100000 | M04 |
| L05 | Admon en una página | 70 | 25 | 7500 | 187500 | M05 |
| L06 | Contabilidad I | 170 | 30 | 27500 | 825000 | M06 |
| L07 | Redes y Comunicaciones | 370 | 35 | 32500 | 1137500 | M07 |
+----------+------------------------+-------+----------+--------+---------+-----------+
7 rows in set (0.000 sec)
MariaDB [libreria]> delete from libro where id_libro="l07";<- para eliminar redes y comunicaciones:
Query OK, 1 row affected (0.002 sec)
MariaDB [libreria]> select * from libro;
+----------+---------------------+-------+----------+--------+---------+-----------+
| id_libro | Titulo | Numpg | cantidad | Precio | vtotal | codigmat1 |
+----------+---------------------+-------+----------+--------+---------+-----------+
| L01 | Calculo II | 120 | 5 | 55000 | 275000 | M01 |
| L02 | Bd II | 150 | 10 | 65000 | 650000 | M09 |
| L03 | Estructura de Datos | 180 | 15 | 85000 | 1275000 | M03 |
| L04 | Inglés | 280 | 20 | 105000 | 2100000 | M04 |
| L05 | Admon en una página | 70 | 25 | 7500 | 187500 | M05 |
| L06 | Contabilidad I | 170 | 30 | 27500 | 825000 | M06 |
+----------+---------------------+-------+----------+--------+---------+-----------+
________//__________//_______
asc= ascendentemente:
MariaDB [libreria]> Select * from libro order by Titulo asc;
+----------+---------------------+-------+----------+--------+---------+-----------+
- id_libro | Titulo | Numpg | cantidad | Precio | vtotal | codigmat1 |
+----------+---------------------+-------+----------+--------+---------+-----------+
- L05 | Admon en una página | 70 | 25 | 7500 | 187500 | M05 |
- L02 | Bd II | 150 | 10 | 65000 | 650000 | M09 |
- L01 | Calculo II | 120 | 5 | 55000 | 275000 | M01 |
- L06 | Contabilidad I | 170 | 30 | 27500 | 825000 | M06 |
- L03 | Estructura de Datos | 180 | 15 | 85000 | 1275000 | M03 |
- L04 | Inglés | 280 | 20 | 105000 | 2100000 | M04 |
+----------+---------------------+-------+----------+--------+---------+-----------+
MariaDB [libreria]> Select * from libro order by numpg asc;
+----------+---------------------+-------+----------+--------+---------+-----------+
- id_libro | Titulo | Numpg | cantidad | Precio | vtotal | codigmat1 |
+----------+---------------------+-------+----------+--------+---------+-----------+
- L05 | Admon en una página | 70 | 25 | 7500 | 187500 | M05 |
- L01 | Calculo II | 120 | 5 | 55000 | 275000 | M01 |
- L02 | Bd II | 150 | 10 | 65000 | 650000 | M09 |
- L06 | Contabilidad I | 170 | 30 | 27500 | 825000 | M06 |
- L03 | Estructura de Datos | 180 | 15 | 85000 | 1275000 | M03 |
- L04 | Inglés | 280 | 20 | 105000 | 2100000 | M04 |
+----------+---------------------+-------+----------+--------+---------+-----------+
desc= decendete:
MariaDB [libreria]> Select * from libro order by Titulo desc;
+----------+---------------------+-------+----------+--------+---------+-----------+
| id_libro | Titulo | Numpg | cantidad | Precio | vtotal | codigmat1 |
+----------+---------------------+-------+----------+--------+---------+-----------+
| L04 | Inglés | 280 | 20 | 105000 | 2100000 | M04 |
| L03 | Estructura de Datos | 180 | 15 | 85000 | 1275000 | M03 |
| L06 | Contabilidad I | 170 | 30 | 27500 | 825000 | M06 |
| L01 | Calculo II | 120 | 5 | 55000 | 275000 | M01 |
| L02 | Bd II | 150 | 10 | 65000 | 650000 | M09 |
| L05 | Admon en una página | 70 | 25 | 7500 | 187500 | M05 |
+----------+---------------------+-------+----------+--------+---------+-----------+
MariaDB [libreria]> Select * from libro order by numpg desc;
+----------+---------------------+-------+----------+--------+---------+-----------+
- id_libro | Titulo | Numpg | cantidad | Precio | vtotal | codigmat1 |
+----------+---------------------+-------+----------+--------+---------+-----------+
- L04 | Inglés | 280 | 20 | 105000 | 2100000 | M04 |
- L03 | Estructura de Datos | 180 | 15 | 85000 | 1275000 | M03 |
- L06 | Contabilidad I | 170 | 30 | 27500 | 825000 | M06 |
- L02 | Bd II | 150 | 10 | 65000 | 650000 | M09 |
- L01 | Calculo II | 120 | 5 | 55000 | 275000 | M01 |
- L05 | Admon en una página | 70 | 25 | 7500 | 187500 | M05 |
+----------+---------------------+-------+----------+--------+---------+-----------+
6 rows in set (0.000 sec)
________//__________//_______
COMANDO JOIN
sirve para relacionar tablas:
select * from tabla1 inner join tabla1.id=tabala2.id inner join tabla3 on tabla2.id=tabla3.id2;
MariaDB [libreria]> select libro.id_libro,libro.Titulo,autor.codaut,autor.nombre from libro inner join liautedi on libro.id_libro=liautedi.id_libro1 inner join autor on liautedi.codaut1=autor.codaut;
+----------+----------------+--------+---------------------+
- id_libro | Titulo | codaut | nombre |
+----------+----------------+--------+---------------------+
- L02 | Bd II | A01 | Luis Joyanes |
- L02 | Bd II | A05 | Robert Lorber |
- L06 | Contabilidad I | A02 | Jorge Vaquez Posada |
- L04 | Inglés | A04 | Riaz Khadem |
- L04 | Inglés | A04 | Riaz Khadem |
- L04 | Inglés | A04 | Riaz Khadem |
+----------+----------------+--------+---------------------+
6 rows in set (0.001 sec)
________//__________//_______
Para que no aparezcan los repetidos:
comando: distinct
MariaDB [libreria]> select distinct libro.id_libro,libro.Titulo,autor.codaut,autor.nombre from libro inner join liautedi on libro.id_libro=liautedi.id_libro1 inner join autor on liautedi.codaut1=autor.codaut;
+----------+----------------+--------+---------------------+
- id_libro | Titulo | codaut | nombre |
+----------+----------------+--------+---------------------+
- L02 | Bd II | A01 | Luis Joyanes |
- L02 | Bd II | A05 | Robert Lorber |
- L06 | Contabilidad I | A02 | Jorge Vaquez Posada |
- L04 | Inglés | A04 | Riaz Khadem |
+----------+----------------+--------+---------------------+
4 rows in set (0.001 sec)
Otros comandos:
Como guardar una base de Datos:
Ejemplo:
MariaDB [inventario]> exit
Bye
C:\xampp\mysql\bin>mysqldump -B -uroot -p inventario>c:/xampp/inventario.sql
Enter password:
COMANDO PARA RESTABLECER UNA BD:
source c:/xampp/subconsultas.sql