MySQL Primer 
by Daniel Bartholomew

Intro | Logging In | Viewing | Inserting | Modifying | Links

<-- Previous

Inserting Data Into MySQL

Next -->

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 add).

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: First, we are telling mysql that we want to insert some data into the Books table. Second, we tell the database what data we want to enter (Title, SeriesID, and AuthorID - we won't bother with the Book_ID, since it is automatically incremented). Third, we give the database the actual information we want to enter.

Executing this statement gives us:

mysql> insert into Books (Title, SeriesID, AuthorID)
    -> values ("Lair of Bones", 2, 2);
Query OK, 1 row affected (0.26 sec)

mysql>

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:

mysql> select * from Books;
+---------+----------------------------+----------+----------+
| Book_ID | 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.23 sec)

mysql>

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 to find out how! ;-)

<-- Previous

Next -->

Creative Commons License
This work is licensed under a Creative Commons License.