Updated February 07, 2009
Introduction
As an introduction, allow me to explain to you what SQL/mySQL is, and why it holds superior to other database management systems. SQL was introduced as a way to organize and manipulate large yet sometimes small amounts of data in the early 70's. I'm not going to give you all of the useless information because you are viewing this tutorial as a way to jump start your knowledge base, not keep you booked reading a history lesson. Too keep it brief, I'll allow you to know that SQL was not always as amazing as it is today. Up until the past 4 years, Oracle databasing was by far faster, but with the release of SQL 2005, SQL has taken over dramatically. So now that I've given you one reason to focus on SQL as your primary choice in database needs, allow me to give you another good reason to stick with it. It's easier to learn, has more functionality, and is easy to integrate into programs written in several languages, such as PHP, C, C++, C#, etc.
Setup
Ok, the first part to this tutorial is setting up your database. First, you need to either have a web server with MySQL installed, or have it installed on your computer. I'm not going to explain how to set up a web server considering that isn't the focus of this tutorial, but to make life easier, there are a few software choices of mine that install and preconfigure what you need very nicely, and those are AppServ and Xampp.
Now with one of those packages installed, or on your typical web server, you will have access to a very handy php based script call phpMyAdmin. Once you are in phpMyAdmin, simply enter in what you want where it says "Create New Database". The name you select for this is irrelevant for this tutorial, and will never be mentioned again.
Creating Tables
Now that you have your database set up, we are prepared for the fun stuff. What I'm going to be doing throughout this tutorial is displaying a code, and then breaking it down for you so that you understand it. Onto creating tables:
Code:
CREATE TABLE tutorial ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name TEXT, data TEXT );
To better explain the attributes, I'm going to explain the ones that I used in the tutorial table. INT is short for integer, and tells the database that the rows in this field will be occupied by integers, or numbers. TEXT, like INT, tells your database that field will be filled with TEXT. Integers may still be used, but it is not defined by ONLY integers. This will be your main input. NOT NULL shows that this field cannot be left blank. It will automatically fill it with a default value, which you can set, which I will explain on a later date in a more advanced tutorial. AUTO_INCREMENT signifies that the field will automatically add 1 to the value of the last insert. For example, if you have a row with an id of 1, and you insert another one and leave the id field blank, it will automatically fill it with the next available integer, being two. PRIMARY KEY indicates that field is the main identifier of your table. By default, the database will organize itself by whatever field is assigned as the primary key. Keep in mind, however, that more than one may be assigned, and the database will organize accordingly.
Altering Tables
As easy as it was to create the table, you can also alter the table just as easily, by adding or dropping columns or fields.
Code:
ALTER TABLE tutorial DROP data
Code:
ALTER TABLE tutorial ADD data2
Adding Data
To add information into your database, we are going to pretend that our table 'tutorials' has the fields id and name. You can add to your database like so:
Code:
INSERT INTO tutorials VALUES (NULL, test)
Code:
INSERT INTO tutorials (id, name) VALUES (NULL, test)
Code:
INSERT INTO tutorials (name) VALUES (test)
Updating Data
If you need to update existing data, first you need to know what you are looking for. Say your table looks like this:
id name
1 test
2 test2
3 test3
Say you need to change 'test3' to 'I love VDCore!'. Here is how you would go about doing that.
Code:
UPDATE tutorials SET name=I love VDCore! WHERE id=3
Deleting Data
To delete data from your table, just like updating it you need to define what you want to delete. Here is an example:
Code:
DELETE FROM tutorial WHERE id=1 LIMIT 1
Querying Data
Now it's getting even more fun. Now that you know how to manage all of that information, now it's time to learn how to display it. Allow me to introduce yet another command for you, SELECT. All it is doing for you is allowing you to view the information by what you selected it as. In this example, I'm going to use SELECT *. Just remember, the same as anything technology related, an asterisk is set as a wild card. So by saying SELECT *, I am saying to select everything from..whatever. On to the example.
Code:
SELECT * FROM tutorials
This is all for my basic MySQL introduction. If you have any questions, feel free to ask. I will hopefully be updating my PHP tutorial, as well as finally creating new ones, such as using your SQL databases in PHP, more advanced SELECT queries, etc.
