Beginners Guide to MySQL
12,632 ViewsMySQL Tutorials March 25th, 2007
MySQL can be quite confusing when you first start learning it, but the truth is, once you get the basics down, it can be quite easy. MySQL is used in conjunction with PHP quite often, and they work flawlessly together, that is what PHP is meant for, database driven sites. So if you use PHP, then you should also be using MySQL. Learn from the beginning, on how to use MySQL on your sites, and start creating dynamic sites now!
At the end of this tutorial, I hope you will have the basic understandings of how to, create a table to store data in, how to insert data into your tables, how to delete data from your tables and how to update data in your tables. These are three very principal tasks when using MySQL, such as, adding a new user, deleting a user, and edit an existing user (in theory).
For a user to execute SQL queries, you need a means to do so. One of the most popular ways of executing SQL queries is through PhpMyAdmin. This is a very popular MySQL database management script that most web hosts offer, if you are using MySQL directly installed off of your own computer, you will most probably be using a MySQL command prompt, both work in the exact same way.
When using MySQL there are few things you should learn, in the way that data is organized.
First there is the database, this is what stores everything, in a database there are tables. Tables are used to organize the type of data stored, such as a table named users will most likely store information about users on a particular site. Tables can be named anything your heart desires. In a table, there are rows and columns. Below is an image demonstration of how this is all put together, hopefully it will explain this better to you -

Alright, so from my explanation and picture, you should hopefully already have a website (or have MySQL installed on your own computer)
. So the next step, is to create a database.
If you have your own web host, usually in your control panel for your site there will be a MySQL database area, where you can create new databases and users for those databases. This is a bit hard to help you with, seeing as how there are so many hosts offer different control panels, the most popular of them being cPanel though.
Once you get your database created (which is not very hard). Now we can get onto the actual MySQL work, so our first step will be to create a table.
Let's say we want to create a table that stores links in a database. This is a pretty simple example, we will need to store the URL of the link, the link name and lets say for each link we will add a title for the link. Also, we will want each link to have a unique ID, so in our scripts they are easier to reference.
-
CREATE TABLE links (
-
id INT(11) AUTO_INCREMENT NOT NULL,
-
name VARCHAR(30) NOT NULL,
-
url VARCHAR(70) NOT NULL,
-
title VARCHAR(50) NOT NULL,
-
PRIMARY KEY(id)
-
);
This is a SQL query, this creates a table. As you can see MySQL is almost like reading english. It reads all pretty straight forward (like a caveman was reading it).
First off, I would just like to say you do NOT need to capitalize certain parts of your query like I have, I do this to make it easier to read.
So first you define that you are creating a table, this is pretty obvious and straight forward, you name it then open your statement with a (. Next is where we define our fields, each field has a name and a type. So as you can see with our name row, we define it with VARCHAR(30). VARCHAR means that you want this field to store short bits of text, the max length a VARCHAR field can store is 255 characters, but we set our max to 30 in my example. I doubt anyone will have a link name of more then 30 characters, try to keep your max to a minimum, when querying your table later on it will go faster.
I skipped the id field at first, because it needs a whole section to explain :). First off, since we want a unique id identifier for each link we insert, the name 'id' seems pretty fitting, now our field type is INT (integer), and we want it to have a max of 11 numbers in it. So as long as your id numbers are below 11 numbers long (ex : 12345678912) then we are good! Now, the AUTO_INCREMENT part, this says that for each new link inserted, the next id number will be inserted automatically, and it will be incremented from the last one inserted. This way, you don't have to insert the id number yourself, it does it automatically.
As you can see on the end of each field, we have set a NOT NULL, this means that when we insert a new link this field can NOT be left empty, so it always has to have a value entered when you insert a new link.
Ok, now that you have the basics of creating your table, we are going to want to insert some links into it!
-
INSERT INTO links (name, url, title) VALUES ('TutorialCode', 'http://www.tutorialcode.com', 'Tons of great coding tutorials!');
This is pretty simple, first we say which table we would like to insert a new record (link in our case) into. Then in our first set of brackets, we define the fields that we are inserting data into, the order of these actually does matter, they must match up to the data in your values set of brackets. When defining your fields, there are NO single quotes ( ' ), each field is separated by a comma. Next you write VALUES, this means we are starting to insert some actual data. Each piece of data is encased in single quotes, (only if it is a string, if it is a number you do not put the single quotes), and each piece of data is seperated by a comma again. So in our example as you can see 'TutorialCode' matches up with the name field of our query, and so on and so forth.
If you noticed, in both our create table query and our insert query, we end it with a ; - this is to say that your query is finished, and not to expect more.
Now, let's say we made a mistake and want to change something in a link that is already added. We want to use an UPDATE query.
-
UPDATE links SET name = 'Coding Tutorials' WHERE id = 1;
This is really easy, we want to update our links table, and set the name of our link to just Coding Tutorials, now we have to define which link we want to update, if we didn't it would set all our links names to Coding Tutorials (even though we do only have 1 still). So we use a WHERE clause, we want to only change our link name where the id of the link is equal to 1. Which is the first link we entered into our table.
Now, that you can put data into your database and edit it, what if you wanted to erase data? So from this example we will be erasing our link from our table, then finish by erasing our table from our database completely.
-
DELETE FROM links WHERE id = 1;
Deleting something from our database is equally as easy, just use our WHERE clause once again, and delete our only link from the database, oh no!
-
DROP TABLE links;
And this final query, will totally drop this table from your database.
MySQL is a bit confusing in the beginning for any user, but once you get the basics, it is very easy to understand.
If you have any questions just leave a comment.

(10 votes, average: 4.4 out of 5)










March 26th, 2007 at 9:44 am
nice tut man
April 19th, 2007 at 5:09 am
nice tutorial indeed but how can i see the data that i entered in the tables of my database , i can't find any comnd that does that
April 19th, 2007 at 6:06 am
To get data from a MySQL table, you need to use the SELECT command. You will see that you use this command quite a lot.
Check out some of the other PHP tutorials on this site, you will see that I use the SELECT command in my PHP.
I am also planning on writing a brand new MySQL tutorial, and SELECT will be covered as one of the topics.
April 23rd, 2007 at 3:48 pm
Excellent tutorial! Refreshed some of the things I already knew and some more.
Keep these up!
April 23rd, 2007 at 8:39 pm
@Charlie - Thanks for the kind words! I hope you keep reading our tutorials.
May 31st, 2007 at 11:37 pm
Thanks for the tut. Very clearly worded.
June 4th, 2007 at 10:34 am
im a web graphic designer(more on designs), i want to develop a site with database. i want to ask if MySQL is part of Visual Studio?????? i dont know what software to buy.... and some of my friends say that it is with the visual studio.. thanks
June 22nd, 2007 at 5:50 am
Hi, well explained tut, i think i now have a grab of database structure. thanks admin.
June 22nd, 2007 at 5:53 am
I've setup a web server using apache, install Mysql setup n running but i can't get connected using Dreamwaver with PHP?
I need an help
July 21st, 2007 at 8:39 pm
Thanks, this was a great tutorial and helped out on some of my questions. My next goal is to incorporate this knowledge into PHP. Great tutorial, recommended over W3schools' version!!
October 27th, 2007 at 6:57 am
[...] Click here to learn more. [?] Share This Bookmark It HereSubscribeBlinklistBloglinesBlogmarksDiggdel.icio.usFacebookFurlMa.gnoliaNewsVineRedditStumbleUponTechnoratiBookmarks: Posted in MySQL | Leave a Comment [...]
December 12th, 2007 at 11:58 pm
Hey Sean....
I am your big fan! (Did u know it? ;))
I've learned many new things in PHP Coding from you and know let's get onto MySQL.
Keep Your Fingers on the Keyboard Pressing Fast
J.Wilson
December 15th, 2007 at 1:19 am
[...] Begginers guide to MySQL [...]
January 4th, 2008 at 5:31 am
i really liked the way u presented the concepts..i do ve knowledge abt it though..
January 9th, 2008 at 11:20 am
I'am just starting MYSQL and i fine these easy to follow
Thank's
February 6th, 2008 at 1:47 am
hi...
thanks for your tutorial..
can u help me a little bit??
we have a company thats in the service industries,
it is a driving academy company. ppl atually come here and learn how to drive. ok the problems is.. we have the timetable to book for every person that intended to do their class. bnut it turn out theres always a misunderstanding and misread in the timetable. sometime theres even unauthories person booking it in.
i was thinking of starting mysefl a database system to actually save data and details regardsing the student.
can you pls give me the advise abt it? i used to learn mysql but that was like 5 years ago ... sigh~
April 1st, 2008 at 11:23 pm
I have recently started this website, not knowing that computer databases were more involved than a windows database. Mysql and PHP is very confusing, this is the best tutorial i've seen yet. Most list technical terms right off the bat and I am immediately lost. I am looking forward to reading more of your tutorials. Thanks
April 16th, 2008 at 10:09 am
I have one doubt :
can we insert text starting with chars (-,_,.) in varchar field?
eg: assuming Empname and Deptname are varchar fileds in Sql Server 2000 if we execute foll. query -
insert into Emp(Empname,salary,Deptname) values('-rahul',12312,'.HR'); Will it execute errorlessly?
September 28th, 2008 at 4:41 am
Wow thanks... I am trying to teach myself PHP and MySQL and this is a great way to expand my knowledge
Thanks.