MySQL Introductions

MySQL Introductions

Posted By: jd-inflames on Mar 07, 2005

Learn the basics of Mysql

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
);
Ok, now lets break this down. When you look at this code, lets start at the beginning and work our way down. CREATE TABLE is the proper SQL syntax to do...guess what...create a table. Now after the CREATE TABLE command, you tell the server what the table you want it to be named. In this example, we are naming our table, tutorial. Now in parenthesis, we will be telling the database what fields will be added to this table, and what attributes will be associated with those fields. So in the parenthesis, you are going to have the field name, and then the attributes in capitol letters. If there is more information following the attributes, follow it up with a comma, otherwise you will be ending it by closing the parenthesis, and then a semicolon to tell the server that you are done with that particular command.

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
What this code did was drop the field 'data'. ALTER TABLE is a command telling the database to change the table by dropping (DROP) the column data. You can also add fields the same way. Here's an example:

Code:
ALTER TABLE tutorial
ADD data2
See what I did there?

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)
or

Code:
INSERT INTO tutorials (id, name)
VALUES (NULL, test)
Both variations work the same, the difference is the second one can allow you to enter specific values and leave the remaining either blank or their null default values. For instance, the proper way to have put in the second one is as follows:

Code:
INSERT INTO tutorials (name)
VALUES (test)
What that would have done, was insert 'test' into the 'name' column of your 'tutorials' table, while leaving the 'id' column to rely on its null default, which in this case is based on an auto_increment, meaning it will continue to go up by 1, example being 1,2,3,4,etc.

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
What this did, was look in your tutorials table for the column 'id', and find all entries where the value is '3'. After the database finds this, it replaces the column 'name' with the values that you set.

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
Now what the database is doing from this, is deleting from the database every entry where the id value is 1 in the table 'tutorial'. I also added in a new command just to mix things up, LIMIT. By adding limit, it will only delete the first entry with the value of 1 in the id column. If you are trying to check anything on your database, you can always use the LIMIT command to only show that many entries.

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 alone will display EVERYTHING on that database. Of course, you can always be more specific. You can replace the asterisk for one of your column names and it will just show the information in that column. I'm not going to go into advanced viewing techniques just yet, but there is your bread and butter.

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.
  • Comment on this tutorial
  • View the Printable Version

Posted On: Mar 07, 2005

Hits: 7588 - Unique: 5844

Advertise Here!

Quick Tutorial Search

VDC - Visual Design Core on Facebook

Our Popular Tutorials

Signature Backgrounds

Abstract Wave Technique

Abstract Airbrushing

Cinema 4d Materials Tutorial

Rusty Metal Plate

Dark Art Guide

Trendy Grunge Background

Abstract Renders in Cinema 4d