SQL desde la terminal [esp] [eng]

By jesusalejos on 8/15/2023

# VERSIÓN EN ESPAÑOL

English Version

![terminal_sql.jpg](Image from post) [Source](https://pixabay.com/es/photos/desarrollador-de-software-6521720/)

En uno de mis últimos posts comentaba que como herramienta en la práctica de las consultas a una base de datos tenemos los RDBMS, los cuales podemos usar tanto de manera gráfica como desde la consola de nuestro terminal. Las dos bases de datos que he utilizado son sqlite3 y mysql, ambas desde el terminal, aunque con la primera algunas interacciones desde la interfaz gráfica.

¿Por qué desde el terminal? Tengo dos razones, la primera es que siento que me enfrento más al lenguaje en su pureza, sin capas por encima, lo que me ayuda a tener una mayor comprensión del mismo. La segunda es que al trabajar con consultas de base de datos siempre será una característica importante a destacar la rapidez de obtención de estos datos, pues bien, usar los comandos desde el terminal nos facilita esto.

Aunque cada base de datos tendrá sus propios comandos, en cuanto al lenguaje de consulta todo permanece igual, en lo que se distinguen es la manera de acceder a los archivos y mostrar las tablas de registros. En este caso particular, solo por motivos ilustrativos, les mostraré algunos comandos de mysql.

mysql -u root -p

Luego de que instalemos esta BBDD en nuestra pc (yo lo hice desde dependencias de la misma) y configurado nuestro usuario y clave de acceso, podemos ingresar este comando para entrar con todos los permisos al uso de la misma, para validar nos pedirá el password configurado.

SHOW database;

Nos muestra las bases de datos que hayamos creado desde aquí, así como las que de manera predeterminada existen para el buen funcionamiento del programa. Notemos el uso inicial de mayúsculas en la palabra que inicia la acción (en este caso SHOW), lo cual es algo común en la sintaxis SQL aunque - vale la pena acotar - no es obligatorio, solo que se recomienda para una mejor lectura.

USE database_name;

SHOW tables;

De esta manera seleccionamos la base de datos que queremos consultar, luego de lo cual podemos ingresar el segundo comando que nos muestra todas las tablas que existen en la misma.

DESC table_name;

Este es un comando muy útil porque nos da una descripción de los registro de cada tabla, es decir, qué campos tiene, de qué tipos son (integer, char, text, boolean, etc.) y toda otra característica que sea pertinente y le haya sido añadida (si posee llave primaria, campos por default, entre otras).

Dejaré este post hasta aquí, no sin antes destacar que así de sencillo es empezar a interactuar con cualquier bases de datos desde la terminal. Ya en un próximo post, vamos a indagar en algunos comandos esenciales de SQL, no sin antes mencionar que a veces se hace la distinción entre DDL (Data Definition Language) que tiene unos comandos específicos que se usan sobre todo al crear las bases de datos; y DML (Data manipulation Language) cuyos comandos si son más del día a día cuando se quiere hacer consultas o interactuar de determinada manera con una bases de datos.



![separador 4.png](Image from post)
# ENGLISH VERSION

Versión en español

![terminal_sql.jpg](Image from post) [Source](https://pixabay.com/es/photos/desarrollador-de-software-6521720/)

In one of my last posts I commented that as a tool in the practice of queries to a database we have the RDBMS, which we can use both graphically and from the console of our terminal. The two databases that I have used are sqlite3 and mysql, both from the terminal, although with the first one some interactions from the graphical interface.

Why from the terminal? I have two reasons, the first one is that I feel that I face more the language in its purity, without layers on top, which helps me to have a better understanding of it. The second is that when working with database queries will always be an important feature to highlight the speed of obtaining this data, well, using the commands from the terminal facilitates this.

Although each database will have its own commands, in terms of the query language everything remains the same, what distinguishes them is the way to access the files and display the tables of records. In this particular case, just for illustrative purposes, I will show you some mysql commands.

mysql -u root -p

After we install this DB in our PC (I did it from dependencies of the same one) and configured our user and password, we can enter this command to enter with all the permissions to the use of the same one, to validate it will ask us for the configured password.

SHOW database;

It shows us the databases that we have created from here, as well as those that by default exist for the proper functioning of the program. Note the initial use of capital letters in the word that initiates the action (in this case SHOW), which is common in SQL syntax although - it is worth noting - it is not mandatory, it is just recommended for better reading.

USE database_name;

SHOW tables;

In this way we select the database we want to query, after which we can enter the second command that shows us all the tables that exist in it.

DESC table_name;

This is a very useful command because it gives us a description of the record of each table, that is to say, what fields it has, what type they are (integer, char, text, boolean, etc.) and any other relevant characteristic that has been added (if it has a primary key, default fields, among others).

I will leave this post until here, but not before emphasizing that it is that easy to start interacting with any database from the terminal. Already in a next post, we are going to investigate in some essential commands of SQL, not without before mentioning that sometimes the distinction is made between DDL (Data Definition Language) that has some specific commands that are used mainly when creating databases; and DML (Data manipulation Language) whose commands are more of the day to day when you want to make queries or interact in a certain way with a database.

![separador 4.png](Image from post)
![footer personal.gif](Image from post)
*** ***
)
*** [![](Image from post)](https://hiveonboard.com?ref=jesusalejos)