Archive for the ‘MySQL’ Category

h1

MySQL Basics

August 24, 2008

I had a pretty hard time finding a good mysql tutorial that covers the basics of MySQL , Some syntaxes i found weren’t useful and i decided to make it easier for thoose who wanted to learn some basic MySQL syntaxes. MySQL is a markup language that helps you create, edit and post databases both roaming and locally.

In this tutorial i already count that you have a computer or server with MySQL installed and ready. If not you can download it from Sun’s own MySQL website.

If you run linux you can start mySQL directly from the terminal using the following command:

mysql -u root -p

But of course you have to setup a password by using the following syntax before accessing:

mysqladmin -u root password your_new_password

The first thing you’ll notice when firing up MySQL is that the bash prompt ” :~$ ” has been replaced with the
“mysql>” prompt.

Now there are some commands in MySQL that you will frequently encounter.
CREATE
ALTER
INSERT
SHOW

You should become familiar to these commands as you progress.
Now this is how you create a database in MySQL.

CREATE DATABASE name;

Now remember that every MySQL syntax should be typed with uppercase letters to signify that you are making a request to MySQL. Every “request” you make in MySQL should also end with a semi-colon ( ; )
to signify the end of a request. After making a database we have to access it in some kind of way and that’s were the CONNECT command comes in handy. To connect to a database in MySQL simply type:

CONNECT name;

The ‘name’ part is the name of the database you want MySQL to connect too. After connecting to a database we want to start of by making a new table, a table consist of multiple columns so when we define the table in the database we need to define the columns.

CREATE TABLE name(column_name varchar(10), column_name varchar(15));

As you can see it doesn’t get easier than this. Many people may find this syntax pretty self-explanitory but just for the heck of it i’ll explain it. we start of by creating a table with a name, the name is defined by the user himself (that is you) and the column_name is supposed to replace the real column names. varchar(10) is a variable that consist of 10 characters. If you’ve ever programmed in any programming langauge you’ve probably stumbled upon this variable. We can of course use a INTEGER variable to insert numeric values.

CREATE TABLE name(column_name INT(10));

Now lets say that we wanna insert values to a table, how do we do it.

INSERT TABLE name VALUES(‘name’, ‘lastname’, ‘250′);

Remember that characters / text values goes to varchar variables and numeric values goes to INT variables.

Try to insert some more values to the table, try to type

SELECT * FROM table_name;

This command shows you all the columns in a table, If you want to connect to a pre-existing database / table but you’re not sure what it’s named, try using this command;

SHOW DATABASES;
SHOW TABLES;

More to come later, this is only the basics but i hope you’ve enjoyed it and be sure to check back again for more tutorials about whatever :D .