You are on page 1of 6

Gua de MariaDB para principiantes 01 Introduccin.

Gua de MariaDB para principiantes


01 - Introduccin.
Esta gua debera ayudarte a utilizar la Lnea de comando cliente mysql de MariaDB en unos 10
minutos. Antes de comenzar, deberas saber:
1.
2.
3.
4.

...cmo introducir comandos en la lnea de comandos


...tu contrasea y nombre de usuario de MariaDB
...el nombre de la base de datos MariaDB a la que quieres acceder
...la direccin ip o el hostmane del servidor de la base de datos (si no est en tu local host)

Esta gua tiene 5 secciones:


1.
2.
3.
4.
5.

Introduccin - Es lo que ests leyendo ahora mismo.


Logging - Cmo iniciar la herramienta de lnea de comando mysql
Visualizacin - Cmo crear instrucciones SELECT
Insertando - Aadiendo datos a la base de datos
Modificando - Alterando registros existentes en la base de datos

Esta gua est diseada para ensearte las bases de la obtencin de informacin dentro y fuera de una
base de datos MariaDB existente, utilizando la Lnea de comando cliente mysql.
Esta gua no es una referencia completa, y no tratar ni de lejos ningn tipo de tpicos avanzados. Es
simplemente una toma de contacto rpida para la utilizacin de la lnea de comando mysql de
MariaDB.

02 - Logging
Regstrate en el servidor que tiene acceso a la base de datos MariaDB que quieres usar. A menudo sta
es la misma mquina que est hospedando la base de datos, pero otras veces no es as.
Para conectarte a la base de datos, teclea:
mysql -u username -p -h host databasename

Sustituye username por tu nombre de usuari@ de la base de datos, host por el URI (Identificador
Uniforme de Recursos) de la base de datos (si la base de datos est en tu local host basta con que

escribas 'localhost'), y databasename por el nombre de la base de datos a la que quieres acceder.
Cuando te lo solicite, introduce tu contrasea.
Si la copia de MariaDB a la que quieres acceder est en la misma mquina que t, puedes ignorar el
argumento "-h host". El programa mysql asumir que ests logndote en una base de datos local.
Si tu nombre de usuari@ de mysql es el mismo que el que utilizas en tu ordenador, puedes dejar fuera
el argumento "-u username".
Si te has registrado correctamente, deberas ver algo parecido a esto:
daniel@mycomputer:~$ mysql -u daniel -p -h localhost test
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.2.0-MariaDB-beta Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [test]>

Lo que realmente nos interesa es la lnea 'MariaDB [test]>'. Es aqu donde introducirs todas
tus rdenes SQL. Ms informacin sobre las mismas en la prxima pgina de esta gua. Por ahora,
djame explicarte las distintas partes de esta lnea:
1. La parte de "MariaDB" te indica que ests concectad@ a una base de datos MariaDB.
2. La palabra entre los corchetes ('[' y ']') es el nombre de la base de datos a la que ests
conectad@. En el ejemplo de arriba estamos conectados a la base de datos 'test'.
3. El signo '>' es la ltima parte de esta lnea. Existe para indicarte que el programa est listo y
esperando a que t introduzcas tus rdenes.

03 - Viewing
To get data out of a MariaDB, or any other, database, you need to ask it a question. These questions are
called SQL statements. SQL stands for Structured Query Language, which is just a fancy way of saying
that the database is picky about how you ask your questions. It doesn't care what you ask, so long as
you ask properly.
The first thing we need to know is what sort of information this database has. Databases store
information in groups, called tables. They're a lot like spreadsheets, only more powerful. You may
already know what tables your database has. If you don't, type:
show tables;

The semi-colon (';') is very important. SQL lets you create very complex questions using multiple
lines. It won't actually ask your question until you type a semi-colon and hit enter.
After typing 'show tables;' and hitting enter, you will see a list of the tables in the database, in our
sample database we see:
MariaDB [test]> show tables;
+----------------+
| Tables_in_test |

+----------------+
| Authors
|
| Books
|
| Series
|
+----------------+
3 rows in set (0.00 sec)
MariaDB [test]>

It looks like the test database has three tables, and we now know their names. Using this information,
we can ask it to tell us more about one of the tables:
describe Books;

This question asks the database to tell us more about the Books table, not the actual data in the table,
but how the table is set up. Executing the above statement gives us this:
MariaDB [test]> describe Books;
+----------+--------------+------+-----+---------+----------------+
| Field
| Type
| Null | Key | Default | Extra
|
+----------+--------------+------+-----+---------+----------------+
| BookID
| int(11)
| NO
| PRI | NULL
| auto_increment |
| Title
| varchar(100) | NO
|
| NULL
|
|
| SeriesID | int(11)
| YES |
| NULL
|
|
| AuthorID | int(11)
| YES |
| NULL
|
|
+----------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
MariaDB [test]>

The main bit of info we are interested in right now is the Field column. The other columns give us all
sorts of useful information about the structure and type of data in the database but the Field column
gives us names, and knowing the names of the individual columns of a database is very useful (as we
will see when we enter information into the database).
Knowing how a given table is organized is all well and good, but what is actually in this table? Well, to
find that out we use a select question:
select * from Books;

This question basically asks the database to show us everything in the Books table (the actual data).
The wildcard ('*') character is very useful for getting everything of something. Executing the above
statement on our test database gives us this:
MariaDB [test]> select * from Books;
+--------+----------------------------+----------+----------+
| BookID | Title
| SeriesID | AuthorID |
+--------+----------------------------+----------+----------+
|
1 | The Fellowship of the Ring |
1 |
1 |
|
2 | The Two Towers
|
1 |
1 |
|
3 | The Return of the King
|
1 |
1 |
|
4 | The Sum of All Men
|
2 |
2 |
|
5 | Brotherhood of the Wolf
|
2 |
2 |
|
6 | Wizardborn
|
2 |
2 |
|
7 | The Hobbbit
|
0 |
1 |
+--------+----------------------------+----------+----------+
7 rows in set (0.00 sec)

MariaDB [test]>

Not surprisingly, we find that the data in the Books table is a list of ...books. Of course, you already
knew that because of the output of the describe question above :-).
But wait! You just found out that David Farland's next book after Wizardborn in the Runelords series is
called Lair of Bones. We'd better add that to the database, don't you think?
To learn about inserting data into a database, go to the next page in the primer.

04 - Inserting
To insert data into a database table, we use an insert statement (I say "statement" here because we aren't
asking the database anything, we're telling it to do something, in this case, to insert something into the
database).
To insert our new book into the database, we use an insert statement that looks like this:
insert into Books (Title, SeriesID, AuthorID)
values ("Lair of Bones", 2, 2);

We are doing three things here:


1. we are telling the database that we want to put some data into the Books table.
2. we tell the database what data we want to enter (Title, SeriesID, and AuthorID - we won't bother
with the BookID, since it is automatically generated for us if we don't specify it).
3. we give the database the actual information we want to enter.
Executing this statement gives us:
MariaDB [test]> insert into Books (Title, SeriesID, AuthorID)
-> values ("Lair of Bones", 2, 2);
Query OK, 1 row affected (0.00 sec)
MariaDB [test]>

Since this statement is longer than the ones used previously in this tutorial, I hit the Enter key after
typing (Title, SeriesID, AuthorID). This added in the '->' line. You can use as many lines as you like
when entering statements (or questions), nothing will happen until you put a semicolon (';') at the end
of a line.
Now, if we ask the database the select question from the previous page, we will get:
MariaDB [test]> select * from Books;
+--------+----------------------------+----------+----------+
| BookID | Title
| SeriesID | AuthorID |
+--------+----------------------------+----------+----------+
|
1 | The Fellowship of the Ring |
1 |
1 |
|
2 | The Two Towers
|
1 |
1 |
|
3 | The Return of the King
|
1 |
1 |
|
4 | The Sum of All Men
|
2 |
2 |
|
5 | Brotherhood of the Wolf
|
2 |
2 |
|
6 | Wizardborn
|
2 |
2 |

|
7 | The Hobbbit
|
0 |
1 |
|
8 | Lair of Bones
|
2 |
2 |
+--------+----------------------------+----------+----------+
8 rows in set (0.00 sec)
MariaDB [test]>

As expected, the data we just entered shows up on the last row.


Uh-Oh, there seems to be a problem with the second-to-the-last entry in the database. I don't believe
"Hobbit" is spelled "Hobbbit". We'd better fix that. Go to the next page in the primer to find out
how! ;-)

05 - Modifying
On the previous page, we found an error in one of the entries in the Books table. Specifically, the word
"Hobbit" was spelled "Hobbbit". Here is what the select statement told us:
MariaDB [test]> select * from Books;
+--------+----------------------------+----------+----------+
| BookID | Title
| SeriesID | AuthorID |
+--------+----------------------------+----------+----------+
|
1 | The Fellowship of the Ring |
1 |
1 |
|
2 | The Two Towers
|
1 |
1 |
|
3 | The Return of the King
|
1 |
1 |
|
4 | The Sum of All Men
|
2 |
2 |
|
5 | Brotherhood of the Wolf
|
2 |
2 |
|
6 | Wizardborn
|
2 |
2 |
|
7 | The Hobbbit
|
0 |
1 |
|
8 | Lair of Bones
|
2 |
2 |
+--------+----------------------------+----------+----------+
8 rows in set (0.00 sec)
MariaDB [test]>

The easiest way to fix this is by executing an update statement:


update Books set Title = "The Hobbit" where BookID = 7;

What this statement does is tell the database to update the Books table by setting the Title field to "The
Hobbit" if the Book_ID equals 7. Executing this statement will give us something that looks like this:
MariaDB [test]> update Books set Title = "The Hobbit" where BookID = 7;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [test]>

Notice the feedback that mysql gives you after successfully executing the update statement.
1. it lets you know that one row in the database matched your statement (there's only one row with
a BookID of 7)
2. that it changed one row of the database (the one it matched)

3. that there were no warnings or problems that it could see with what you just did.
We can now check to see that our change has been made by executing another select statement:
MariaDB [test]> select * from Books;
+--------+----------------------------+----------+----------+
| BookID | Title
| SeriesID | AuthorID |
+--------+----------------------------+----------+----------+
|
1 | The Fellowship of the Ring |
1 |
1 |
|
2 | The Two Towers
|
1 |
1 |
|
3 | The Return of the King
|
1 |
1 |
|
4 | The Sum of All Men
|
2 |
2 |
|
5 | Brotherhood of the Wolf
|
2 |
2 |
|
6 | Wizardborn
|
2 |
2 |
|
7 | The Hobbit
|
0 |
1 |
|
8 | Lair of Bones
|
2 |
2 |
+--------+----------------------------+----------+----------+
8 rows in set (0.00 sec)
MariaDB [test]>

Sure enough, "Hobbit" is now spelled correctly.


That's about it for this 10 minute primer on using the MariaDB mysql command-line client.

https://mariadb.com/kb/es/mariadb-spanish/primeros-pasos/a-mariadb-primer-01-intro/

You might also like