This is an old revision of the document!
Web Development Lesson 7 - Databases
Tables
Objective
In this activity you will use a new tool to get a good look at the structure of a database.
Setup
There is nothing to set up in this exercise.
There are many databases available that all meet our needs, but we'll be using MySQL, which is one of the most popular for use with PHP.
To simplify access, we'll begin using PHPMyAdmin to administer the database. This tool also allows us to look at the contents of our database and to update information.
Navigate to
phpmyadmin and login using the password stored on your computer.
You'll see a list of all the available databases on your left. Select the one belonging to your class.
Tables
You should now see a list of tables in the main pane of the site.
Click on the table called 'Marvel'.
Now you can see why it's called a table. It contains information that is presented in the form of a table, with column headings at the top and many rows of data below with each field containing data corresponding to its column.
Here you can edit, copy and delete data.
Above the table, you can see a menu of tabs. You're currently in the 'Browse' tab where you can see the data.
Below that is a yellow row showing the number of rows in the result and how long it took to process. At this stage you don't need to worry about the processing time, but that can be very important for optimising complex queries.
Below the yellow row is some text that says SELECT * FROM `marvel`
. This is the SQL command that extracts the data. We'll spend more time looking at queries in the next activity.
Fields
Click on the 'Structure' tab.
Here we see a different table. It shows how the table is structured.
Each row corresponds to a 'field', which is a piece of data. Each column in the 'Browse' view is a 'field'.
Two of the fields have type 'varchar' which means text (a variable number of characters). The number in brackets is the number of characters that are allowed.
'first_appeared' is of type integer with length 4 for storing the year the character first appeared in Marvel comics.
'hero_villain' is of type 'set' which means only specific values will be accepted. In this case, the values are 'Hero' and 'Villain'.
All columns have a 'No' value for 'Null' which means they must have a value. In some cases, you might be happy not to have any information for a certain field, in which case this would be marked 'Yes'.
Below this table, you'll see a section called 'Indexes'. An index is way of sorting the information (like the letters on library books), and every table must have at least one index, called the 'Primary' index that is unique. Imagine having two rows for 'Iron Man' and updating each of them with different information. You'd never know which one was correct.
In this table, the primary index is the 'character_name' field.
SELECT Queries
Click on 'Browse' to see the data again.
Now take another look at the 'query' (just below the yellow row at the top).
When you clicked on the 'heroes' table, PHPMyAdmin ran this query for you automatically.
It says 'show me everything in the heroes table. Let's take a closer look.
SELECT * FROM `heroes`
A query is a command to the database for information or to take an action.
The query you see here is the basic form of every SELECT query.
Queries beginning with SELECT
are asking for information and is immediately followed by the list of fields wanted.
*
means 'all fields'.
FROM
tells the database that the following items are the data sources. In this case, it's the table 'heroes'.
Restricting Data
This data set is currently very small, but imagine if it had every character from every Marvel film listed. It would get quite long, and we may not want to see the whole list.
Perhaps we're only interested in the characters with magic.
Click on the 'SQL' tab. Here we can write our own queries.
There's a query already written for you.
SELECT * FROM `heroes` WHERE 1