So maybe you're unconvinced: why do we need to know how to interact with databases? Aren't they the staple of corporation programming and more geared toward holding tons of information, like every single myspace profile? Well, even if you roll your own website on your own server, you'll probably need to use a database if you want to do anything dynamic with it (static websites aren't fun at all). And if you plan on making an application that will need an appreciable amount of persistent data (ie data that won't crash and burn or simply vanish when the program exits.), databases can be quite useful. Not only are they good data storage containers, they also do some nifty data structuring and arrangement for you. They're actually quite useful for non-trivial things, and learning how to use them now can be a plus when you have to work for the borg in order to have something to eat.
Today, we'll be installing the mysql database server1. Yes, it's a server; that's how the information gets into and out of the database. Duh! So, we'll take a tryst over this way: mysql, and download the free version of mysql. The last time I checked, the only thing the holders of mysql sold was support, and it should still be that way. Now, go through the install, making sure to install it as a service and writing down your root password somewhere. Most of the other options shouldn't greatly impact this tutorial, so you can keep them as is.
Now, a little about what you just put on your machine. Mysql, as the name implies, is a server that accepts queries formatted in sql, or s____ query language, that works on a set of tables. Harkening back to html, a table looks like this:
p>
Name | Weight | IQ | CQ2 | Birthday |
Timmy | 110 | 1000 | 20 | 3/10/2005 |
Boris | 260 | 5 | 30 | 10/2/1930 |
If this were an sql table, you would be able to insert into it, change the values of fields3 already there, and select certain fields from all or some of the records. Looking at the two-row table, all of those operations are easy to do. Doing them on 100 records would be tedious: on a million, near impossible (except insert). Thus, sql takes care of that for you. It's somewhat like programming, but a little more restricted. The commands usually come in the form:
p>operator arguments (table);
It's very simple (to an extent).
Now that you have a database, you want an actual database to play with, or maybe a few tables. So, let's go link into mysql:
p>Start->Mysql->Mysql (version)->Mysql Command Line Client
And now you have a black box of death! Fine, a command line. If you didn't allow annonymous access to mysql, now enter your root password. Let's see what databases we have:
p>show databases;
Now, let's make a new database:
p>create database banannas;
And now, let's use it:
p>use banannas;
As the show demonstrated, we have a few databases in our server. So yes, a mysql is actually a collection of databases. Go figure.
Now, this is the part where we have to figure out what we want in our database. Using our previous example with timmy and boris, we'll now make a table to hold our sql records:
p>create table grapes with (name varchar(100), weight int, IQ int, CQ int, Birthday varchar(10));
So this makes our table! You can check it by doing:
p>show tables;
And:
p>select * from grapes;4
which should turn up just the headers. So, an explanation of our table. The command "create table grapes" should be straight forward. The trailing
Now, let's insert Timmy:
p>insert into grapes ("Timmy", 110, 1000, 20, "3/10/2005");
And do the select again:
p>select * from grapes;
And Timmy should show up! Now, you can do the same for Boris.
Select does more than just display the entire table. Try this with Boris in the table:
p>select name, weight from grapes;
We only see two fields this time, which could be useful if you wrote a program that found the probablity of a certain outcome in a wrestling match between Timmy and Boris. Their IQ's, CQ's, and birthdates don't matter: Boris is just so much heavier than Timmy that those factors are quite irrelevant. Now, let's say you locked up this data, and left it alone for 3 months. You meet Timmy at the local supermarket, and you've quite forgotten his IQ. Coming home, you only want to know Timmy's IQ, not Boris'. So, you run the following through the database:
p>select IQ from grapes where name="Timmy";
And the output should be: 1000. Timmy's pretty smart. If you had left off the "where name='Timmy'" bit at the end, the IQ of both Timmy and Boris would have been shown. Appending that clause, though, filters through the records for only those that have name fields that equal Timmy. It's really psuedo-english, and it almost reads like a command you would give to a subordinate (we said almost). There are all sorts of crazy things you can do with select statements (like do a select on the results of another select), but just knowing that will get you places.
Now that you're done playing with your database, you've realized what an utter waste of time this has been, and want to wipe out your records of Timmy and Boris. One way to do so is to delete each record in turn, like so:
p>remove from grapes where name = "Timmy";
And so on. If you decide the table is tainted, and needs to go too, you can do:
p>remove grapes;
If the entire database is saturated with evil, then you can finally do:
p>remove banannas;
If you truly hate mysql, you can uninstall it. But first, if you want to take a look at an example of perl interfacing with mysql...
So,