• Like
Diva10
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Published

 

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
550
On SlideShare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
9
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. mysql
  • 2. Contents of mysql
    • Introduction
    • Commands
    • examples
  • 3. 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
  • 4. 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,
  • 5. Mysql commands
    • CREATE DATABASE
    • CREATE TABLE
    • INSERT
    • REPLACE
    • UPDATE
    • SELECT
    • DELETE
    • WHERE
    • IN
    • AND
    • OR
    • RLIKE
    • DISTINCT
    • VALUES
    • SET
    • MAX
  • 6. 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.
  • 7. 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.
  • 8. 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_address] ");
    • If successful, you should get something like the following for a response;
    • Query Ok, 1 row affected (0.05 sec)
  • 9. 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.
  • 10. 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 | bireland@thescripts.com |
    • +----+---------------+---------------------------------+
  • 11. To select specific column
    • select specific columns, like this; mysql> select name from users;
    • +----------------+
    • | name |
    • +----------------+
    • | Blair Ireland |
    • | Mark Hardy |
    • +----------------+
  • 12. 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 = 'webmaster@thescripts.com'
    • -> WHERE email = " [email_address] ";
    • This would just change all rows with email set to bireland@thescripts.com and change them to webmaster@thescripts.com. In this case though, only one entry has bireland@thescripts.com as its email, so only one entry would be changed.
  • 13. 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.
  • 14.
    • Advanced commands:
    • sql> CREATE TABLE users (
    • -> id INT NOT NULL AUTO_INCREMENT,
    • -> name VARCHAR (50),
    • -> email VARCHAR (50),
    • -> PRIMARY KEY (id));
    • Table created.
  • 15. 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.
  • 16. 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.
  • 17. Logical operators
    • mysql> CREATE TABLE users (
    • -> id INT NOT NULL AUTO_INCREMENT,
    • -> name VARCHAR (50),
    • -> email VARCHAR (50),
    • -> PRIMARY KEY (id));
  • 18. 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.
  • 19. And operator
    • AND (or) &&
    • mysql> SELECT * FROM users WHERE
    • mysql> (name = "Blair Ireland") AND mysql> (email = "bireland@domainname.com");
    • or
    • mysql> SELECT * FROM users WHERE
    • -> (name = "Blair Ireland") &&
    • -> (email = "bireland@domainname.com");
    • This query would return all records with Blair Ireland present as the name, and bireland@domainname.com as the email.
  • 20. Or operator
    • mysql> SELECT * FROM test WHERE
    • -> (name = "Blair Ireland") OR
    • -> (email = "bireland@domainname.com");
    • or
    • mysql> SELECT * FROM test WHERE
    • -> (name = "Blair Ireland") ||
    • -> (email = "bireland@domainname.com");
    • This query would return all records with Blair Ireland present as the name, or records with bireland@domainname.com as the email.
  • 21. 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;
  • 22. 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