Download How to Use MySQL

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Oracle Database wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Open Database Connectivity wikipedia , lookup

IMDb wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Concurrency control wikipedia , lookup

Functional Database Model wikipedia , lookup

Ingres (database) wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Database wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Relational model wikipedia , lookup

Clusterpoint wikipedia , lookup

ContactPoint wikipedia , lookup

Database model wikipedia , lookup

Transcript
How to Use MySQL
1. To install MySQL server in your computer: go to www.mysql.com to download
binary file and get proper file for your operation system. Then follow the
instruction and install it.
2. By default, MySQL server will setup a user called root without any password.
Therefore, you can login as root without specify a password. For windows user,
you should go to C-prompt and then change director to bin director in MySQL.
(i.e. cd mysql/bin ) For Linux user, just simply open a new terminal without
specify any directory.
3. After set up proper prompt window. Now it is time to login MySQL server. The
following prompt is example of how to login:
mysql –h hostname –u username –p
i.e.
mysql –h localhost –u root –p
or
mysql –u root -p
hostname: the hostname of MySQL server, often is called localhost and it is
option to put hostname in this prompt.
Username: the name of a specific user.
-p: indicates that user need type in password;
4. After the previous prompt, MySQL server will ask you to type password:
(i.e: password: ) type the password as you know.
5. If everything that you typed is right, MySQL server will display a short message
to welcome you and then the initial part to prompt will changed to mysql> . This
indicates that you have logged in and you are ready to use it.
6. Now the first thing you want to know is how to give access to a new user. It is
done by following command:
GRANT (type of privileges) ON (name of database) TO username
IDENTIFIED BY ‘password’;
i.e. GRANT SELECT, INSERT ON bgpdatabase TO root IDENTIFIED BY
‘PASSWORD’;
Type of privileges: SELECT, INSERT, UPDATE, DELETE, INDEX, ALTER,
CREATE, DROP. ( ALL for everything)
7. After create new user, now you may want to access a specific database. To use a
database:
USE databasename;
i.e.
USE bgpdatabase;
if the databasename exists, MySQL server will display a message to tell you that
database has changed.
8. Before you go to the detail steps of how to use database, let’s look at how to
create new database first.
To see databases in MySQL: SHOW DATABASES;
After check the databases in MySQL, you can create new database without
duplicates.
To create new databases:
CREATE DATABASE databasename;
i.e. CREATE DATABASE testing;
9. The next thing is to create tables in databases. In order to have a table, you have
to give table name and column name along with other specifications.
i.e. CREATE TABLE MsgPath (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
MsgPathID INT UNSIGNED NOT NULL,
PathOrder TINYINT NOT NULL,
ASID MEDIUMINT UNSIGNED,
Alternative BIT NOT NULL,
PRIMARY KEY(id),
index(MsgPathID,PathOrder,ASID) )
TYPE = InnoDB;
Look at www.mysql.com for more detailed explanation of data type and keys.
10. After create table, it is time to insert data into tables.
To insert data in one table: INSERT INTO tablename VALUES ( value1,
value2, value3, value4 … );
i.e. INSERT INTO Message VALUES(1, 8888888, ‘A’, …..)
This is very simple step; however, you have to remember that if you don’t have
right number of values correspond to number of columns, the data in this table
will not be the one that you desire.
11. The next thing you want to do is to retrieve data from database. To do this, you
can use select statement to retrieve the data from database:
SELECT items FROM table [WHERE condition] [GROUP BY group_type]
[HAVING where_definition] [LIMIT limit_criteria]
i.e.
SELECT COUNT(*) FROM Message;
SELECT * FROM Message WHERE MsgType = ‘W’;
SELECT * FROM Message WHERE MsgID < 100 GROUP BY MsgType;
SELECT * FROM Message WHERE MsgType = ‘A’ LIMIT 1;
12. The next step is to learn how to retrieve the data from multiple table;
First type is using where statement to link two tables together.
SELECT * FROM Message, DataSet WHERE Message.DID = DataSet.DID
AND Message.MsgType = ‘W’ LIMIT 10;
Second type is using join statement to joint two or more tables together:
SELECT * FROM DataSet LEFT JOIN Message ON DataSet.DID =
Message.DID WHERE Message.MsgType = ‘W’ LIMIT 10;
The result of previous two query will be the same, however, the second type is
preferred because it is more efficient.
13. Once the data was stored into database, you might need to change some
information that is old. This will be the next step you are going to learn:
To update data in database:
UPDATE tablename SET column1 = newvalue1, column2 = newvalue2, …
[WHERE condition] [LIMIT number]
i.e. UPDATE Message SET MsgType = ‘A’ WHERE MsgID = 5;
14. Delete an existing data is the next thing you like to know:
DELETE FROM tablename [WHERE condition] [LIMIT number]
i.e. DELETE FROM Message WHERE MsgType = ‘S’;
15. Last thing you want to do is to drop table or whole database that is no longer
needed in your database:
DROP TABLE tablename;
DROP DATABASE databasename;
i.e. DROP TABLE Message;
DROP DATABASE bgpdatabase;