Database Reference
In-Depth Information
All three lines use the SQLstatement INSERT to insert, or add data, to the books table.
Each line will be followed by a status message (or an error message if you mistype
something), but I didn't bother to include those messages here. Notice that numbers don't
need to be within quotes, buttext does. The syntax of SQL statements like this one is
pretty structured — hence the name Structured Query Language . You can be casual about
spacing between elements of the statements, but you must enter everything in the right or-
der and use theparentheses, commas, and semicolons as shown. Keeping SQL statements
structured makes queries predictable and the database faster.
The previous examples insert the values given in parentheses into the table. The values are
given in the same order and format as we told MySQL to expect when we created the
table: three fields, of which the first and third will be numbers, and the second will be any
kind of text. Let's ask MySQL to display the data we just gave it to see how it looks:
SELECT * FROM books;
+---------+------------------------+--------+
| book_id | title | status |
+---------+------------------------+--------+
| 100 | Heart of Darkness | 0 |
| 101 | The Catcher of the Rye | 1 |
| 102 | My Antonia | 0 |
+---------+------------------------+--------+
In this table, you can see more easily why they call records rows and fields columns . We
usedthe SELECT statement to select all columns — theasterisk ( * ) means “everything”
— from the table named. In this example, book_id functions as a record identification
number, while title and status contain the text and numbers we want to store. I pur-
posely gave status values of 0 or 1 to indicate status: 0 means inactive and 1 means
active. These are arbitrary designations and mean nothing to MySQL or MariaDB. Incid-
entally, the title of the second book is not correct, but we'll use it later as an example of
how to change data.
Let's play with these values and the SELECT statement to see how it works. Let'sadd a
WHERE clause to the SQL statement:
SELECT * FROM books WHERE status = 1;
+---------+------------------------+--------+
| book_id | title | status |
+---------+------------------------+--------+
| 101 | The Catcher of the Rye | 1 |
+---------+------------------------+--------+
Search WWH ::




Custom Search