Top Banner
22
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Diva10
Page 2: Diva10

Contents of mysql

Introduction Commands examples

Page 3: Diva10

Introduction to mysql

MySQL is a powerful Relational Database Management System (RDBMS) which we will use to learn the basic principles of database and data manipulation using Structured Query Language (SQL) statements. SQL is a database language that is used to retrieve, insert, delete and update stored data. This is achieved by constructing conditional statements that conform to a specific syntax

How does mysql works?

MySQL is a database server program and as such is installed on one machine, but can 'serve' the database to a variety of locations

Page 4: Diva10

Mysql syntax

mysql syntax: The great thing about everything you do in MySQL

is that the "code" is very easy for humans to read, as opposed to harder programming languages like C or C++. Very few special characters and symbols are required to create a MySQL query,

Page 5: Diva10

Mysql commands

CREATE DATABASE

CREATE TABLE

INSERT

REPLACE

UPDATE

SELECT

DELETE

WHERE

IN

AND

OR

RLIKE

DISTINCT

VALUES

SET

MAX

Page 6: Diva10

Create table

CREATE DATABASE:

CREATE DATABASE database_name ;

Will create a MySQL database.

Example:

mysql> CREATE TABLE users (

-> id INT NOT NULL AUTO_INCREMENT,

-> name VARCHAR (50),

-> email VARCHAR (50),

-> PRIMARY KEY (id));

Table created.

Page 7: Diva10

Primary key

The Primary Key is a type of index MySQL uses. This index can do such things as;

Quickly find the rows that match a WHERE clause. Retrieve rows from other tables when performing

joins. Sort or group a table if the sorting or grouping is done on a leftmost prefix of a usable key

This can definitely help boost the speeds of your queries as well.

Page 8: Diva10

To add data

Adding Data to a Table Adding your Data to a table is not that hard of a

process at all. Basically you specify what table you are inserting the values into, then you go ahead and do so. The syntax is as follows;

mysql> INSERT INTO users VALUES ("NULL","BlairIreland","[email protected]");

If successful, you should get something like the following for a response;

Query Ok, 1 row affected (0.05 sec)

Page 9: Diva10

Viewing data

Viewing Data:

After you add data to your table, you probably want to check it out to make sure everything went as planned. To do so, you would utilize the SELECT command.

To view all data in the table, you would use something like this;

mysql> SELECT * FROM users;

This will give you an output like this

2 rows is set.

Page 10: Diva10

To select a particular row

To select a particular row in this database though, you would use this sort of command;

mysql> SELECT * FROM users WHERE (name="Blair Ireland");

This would give you

+----+---------------+---------------------------------+

| id | name | email |

+----+---------------+---------------------------------+

| 1 | Blair Ireland | [email protected] |

+----+---------------+---------------------------------+

Page 11: Diva10

To select specific column

select specific columns, like this; mysql> select name from users;

+----------------+ | name | +----------------+ | Blair Ireland | | Mark Hardy | +----------------+

Page 12: Diva10

Modifying database

Modifying Database Data

If you have data already in the database that needs some modifying, you would change it by utilizing the UPDATE command in mysql.

Its use is something like this;

mysql> UPDATE users SET email = '[email protected]'

-> WHERE email = "[email protected]";

This would just change all rows with email set to [email protected] and change them to [email protected]. In this case though, only one entry has [email protected] as its email, so only one entry would be changed.

Page 13: Diva10

Deleting data

Deleting Database Data If you want to remove data in the database, you

would use MySQL's DELETE command. Its use would be as follows

mysql> DELETE FROM users WHERE (name="Mark Hardy");

This would delete Mark Hardy's entry in the database, leaving only Blair Ireland's entry in it.

Page 14: Diva10

Advanced commands: sql> CREATE TABLE users ( -> id INT NOT NULL AUTO_INCREMENT, -> name VARCHAR (50), -> email VARCHAR (50), -> PRIMARY KEY (id)); Table created.

Page 15: Diva10

Search command

To do a general search, you would use the following syntax;

mysql> SELECT * FROM test WHERE

-> (name LIKE "%B%");

This will result in finding anything with the capital letter B in the column name. Notice the two %'s used. This checks for anything before or after that letter. You can use just one if you like though.

You can place that % sign anywhere within the query though, as the search is based upon the placement of this character.

Page 16: Diva10

Order by command

To use a literal wildcard character in your searches, you Order By

mysql> SELECT * FROM users WHERE

-> (name = "Joe%") ORDER BY id DESC;

This will return all the records containing someone with the first name of Joe, and will output it from the greatest ID Number, descend until the lowest ID number is reached.

The default for ORDER BY is ascending, so if you want it to go by the lowest ID number first, you would just type in ORDER BY id, or you could plug in the ASC keyword where DESC is currently. Both would give you the same result.

Page 17: Diva10

Logical operators

mysql> CREATE TABLE users ( -> id INT NOT NULL AUTO_INCREMENT, -> name VARCHAR (50), -> email VARCHAR (50), -> PRIMARY KEY (id));

Page 18: Diva10

Not operator

NOT (or) ! mysql> SELECT * FROM users WHERE -> (name != "Blair Ireland"); or mysql> SELECT * FROM users WHERE -> (name NOT = "Blair Ireland"); This query would return all records without Blair

Ireland present as the name.

Page 19: Diva10

And operator

AND (or) &&

mysql> SELECT * FROM users WHERE

mysql> (name = "Blair Ireland") AND mysql> (email = "[email protected]");

or

mysql> SELECT * FROM users WHERE

-> (name = "Blair Ireland") &&

-> (email = "[email protected]");

This query would return all records with Blair Ireland present as the name, and [email protected] as the email.

Page 20: Diva10

Or operator

mysql> SELECT * FROM test WHERE

-> (name = "Blair Ireland") OR

-> (email = "[email protected]");

or

mysql> SELECT * FROM test WHERE

-> (name = "Blair Ireland") ||

-> (email = "[email protected]");

This query would return all records with Blair Ireland present as the name, or records with [email protected] as the email.

Page 21: Diva10

Renaming and deleting an entire table

Renaming a Table: mysql> ALTER TABLE users RENAME public; Deleting an entire table: To delete (or drop) an entire table, you would use

the following syntax; mysql> DROP TABLE public; If you would like to drop more tables at once

though, you would do this; mysql> DROP TABLE public, tests;

Page 22: Diva10

Remove and optimize table

Remove a Column:

mysql> ALTER TABLE public DROP COLUMN time;

After you make these changes to the table, you may want to optimize the table afterwards (especially if you are using VARCHAR's, TEXT's or BLOB's, as this will optimize its memory allocation. You will also want to do it if you have deleted a large part of a table.

During a table optimization, the original table is available to clients, however, modifying and adding to the table is stalled until optimization is complete.

The syntax is:

OPTIMIZE TABLE table_name_goes_here