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! ;-)

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

Comments

Comments loading...