Вы находитесь на странице: 1из 10

MYSQL Tutorial or MARIASQL

This is just a short exposure to another flavour of Database Management System.


You are going to use this knowledge later when we have to connect to a database which
is stored on a server. These commands will be embedded in PHP code.

MYSQL is free to use and it needs a server to run. Because of the restrictions imposed
to the school’s computers we can’t run MYSQL Server directly on the computer labs. To
go around this we have to install everything you need to learn these concepts on a
portable flash memory.

You will also install Apache Server, PHP, Perl, etc. that could be used later. All these
could be installed separately, but it could create a lot of problems. We are going to
install an application that contains all of the above in one standalone portable file. It is
called XAMPP (one of many…).

Download the XAMPP application by clicking on the picture. The file


is nearly half a G. If you can’t do this at school do it home or ask
your teacher to give you a copy (he will be happy).

Once downloaded, decompress and put it the way it is on a flash


drive. You do not have to install anything.

Navigate to the folder on the flash drive. Go at the bottom of the folder and click and
open the “xampp-control.exe” file that will show the GUI control centre.

You have to start the MYSQL server.

Click on the button START on MYSQL row.

Once is started it should show details of the


connection including the port that is using.
If it doesn’t start click the check box with
the module.

1
Now, that the server started we can actually use MYSQL. To do so we have to navigate
to the MYSQL folder and from there to a special folder called BIN. This is where most
of the “binary” files of the programs are stored.

One of the most used ways to work


with MYSQL is from the command
prompt. You can also use simple GUI
programs like HeidiSQL (also installed
on the computers).

The command prompt has to be open


to the BIN folder.
(or you run the “mysql.exe” file that is
inside the bin folder. This will open the
command prompt with the MySQL
already started)

Go to START and navigate to Visual Studio 2008


Command Prompt. This is an artificial way to
access the command prompt.

DO NOT ABUSE THIS ACCESS.

At command prompt type


the name of the portable
drive where you have the
XAMPP followed by
colon.

Now you have to reach BIN. Use a combination of:


 “DIR” command (to show the folder structure)
 “CD name folder” command (to change to that
folder)
 “CD ..” command (to go up a level).

When you reached the folder you should be like in the picture:

2
To start the program you have to type “MYSQL” in the command prompt. This program
will allow you to interact with the MYSQL Server. We do not worry about user
privileges or admin account for the moment (more about this at the end of tutorial).

If the server was started before and you navigated properly you should see this:

If the server wasn’t


started you should
see the second
picture:

All the databases you create with all the tables are stored in a file called DATA. We are
going to use, at the beginning, a database that exists by default and is empty. It is
called TEST. This is for trying the MYSQL for the first time. Later we are going to
create users and grant them privileges to create databases.

All commands could be researched at the


main authority in the filed MYSQL.com.
Click the picture for access. Understanding
commands syntax could be complex but in
time you will familiarise.

The “mysql>_” is called the prompt and it is the place where the commands are shown
when you type. The underscore sign will always flash.

All commands will finish in semicolon. You have to press enter to execute them. If you
type something wrong and you want to cancel that command type “\c”.

3
Try firstly to find the version of the MYSQL used:

MYSQL could be used as simple calculator as well:

Let’s go back to Databases. First command is to


show the existing databases:

The result shows you 2 existing databases.


Again we use TEST. This is already ready for all
users.

Next we have to tell the program to use that database:

This database is empty. To check this write a command that


could show all the tables in the database:

Let’s create our first table. Copy and paste the entire code shown below (this could be
entered on one line or on multiple lines as long you do not enter the semicolon):
create table if not exists players
( name VARCHAR(100) PRIMARY KEY,
password VARCHAR(100),
amount_money INT(10),
account_name VARCHAR(50)
);

Remember that paste in command prompt is here:

4
Lets see what happened:

So, we have a table in our database.


Yuppie, it worked!

We can look at the design structure of the table (like a design view):

If you want to delete


the table you have to
“drop” it:
Put it back now…

Good we have a table but it is empty. We should put something in it. We have two
options to add records in the table. Line by line or to read from a file.

Line by line using “insert”.

The values inserted have to follow the structure of the table:


insert into players values (‘ignatius’, ‘4321’, ‘5000’, ‘check’);

Now we
should check
if the table
has the
values we just inserted. To do so we have to write our first QUERY. The quesry in
English is: “show everything from table players”:

5
Multiple entries using “load data” from a file.

Create a text file like this (add what ever you want as long you maintain the structure):

To move between the columns use tab.


Once the file is made take it in put in the same
place with the database we use.
(mysql/data/test)
I called the file “players.txt”.

To copy the data from the file and insert them to the table do this:

load data local infile '../data/test/players.txt' into table players lines terminated by ‘\r\n’;

Lets see what we have:

We can write another query, “show the names of all people with a credit account”:

6
The rules for queries is :

select WHAT
from TABLE
where CONDITION
Lets try another one, “show name and amount of money of people with less than 1000
money into their account”:

Good, one more, “show the names of all people with 10000 in their account and the
type of account is check” :

You can also sort, “show the names of all clients sorted alphabetically”:

All the statistical queries you learned in Access could be aplied here: count, group, etc.

7
Now we should delete some entries from the table:

The record where the


name was “ali” it
dissapeared.

Also you can do query in a query, “show everything for the client that has the biggest
amount in to account”:

You got the point….

So this is in brief MYSQL. You can also use multiple tables and query both of them
separatelly or you can join them. All about this later.

To finsih with MYSQL do this:

Do not forget to stop the MYSQL server from XAMPP command GUI.

8
If you want to create users (as admin) in order to create databases do the following:
(the pictures are from my local computer but it works for XAMPP anyway)

 Log in to MYSQL using admin privileges:

 Select the mysql database (yes a database called mysql).


 Describe the tables to see what you have in the database.
 Look after user table (this is where all users login and password are stored, these are the users that are using
the database).
 If you type select user, password from user; you will get just the root user (the admin by default).
 To add another user, for example user wwwuser1 with the password wwwuser1 accessing localhost:

create user 'wwwuser1'@'localhost' identified by 'wwwuser1';

 Now you have to give full privileges (in real life you don’t do such a thing, but for this project you are admin
and user)”

grant all privileges on *.* to 'wwwuser1'@'localhost' with grant option;

 You have to start MYSQL with this (it will prompt for password):

mysql -u wwwuser1 -p

9
 In the end you will have to create a database for the user you just created. Or you access the
MYSQL as admin or as user and then you use the following command to create the database:

create database wwwuser1;

 If you want to delete the database youuse:

drop database wwwuser1;

CREATING THE USER AND A DATABASES FOR THE USER HAVE TO BE DONE BEFORE YOU START
THE PROJECT. YOU DO THIS USING THE ABOVE COMMANDS OR A GUI OPTION CALLED
“MYPHPADMIN”:

10

Вам также может понравиться