To insert data into a database table, you use an insert statement (I say "statement" here because you aren't asking the database anything, you're telling it to do something, in this case, to insert something into the database).

To insert a new book into the database, you use an insert statement that looks like this:

insert into books (Title, SeriesID, AuthorID)
values ("Lair of Bones", 2, 2);

You are doing three things here:

  1. you are telling the database that we want to put some data into the books table
  2. you tell the database what data you want to enter (Title, SeriesID, and AuthorID - you don't have to bother with with the BookID, since it is automatically generated for you if you don't specify it)
  3. you give the database the actual information you 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 you ask the database the select question from the previous page, you 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 you 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! ;-)

Previous: A MariaDB Primer 03 - Viewing
Next: A MariaDB Primer 05 - Modifying

Comments

Comments loading...