Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. MY SQL
  2. 2. INTRODUCTION <ul><li>MySQL is a relational database management system (RDBMS)[1] that runs as a server providing multi-user access to a number of databases. </li></ul><ul><li>Some of the features of MySQL Include: </li></ul><ul><li>Handles large databases. </li></ul><ul><li>No memory leaks </li></ul><ul><li>Tested with a commercial memory leakage detector (purify). </li></ul><ul><li>A privilege and password system which is very flexible and secure, and which allows host-based verification. </li></ul><ul><li>Passwords are secure since all password traffic when connecting to a server is encrypted. </li></ul>
  3. 3. GETTING STARTED <ul><li>At the prompt type in something along the lines of the following syntax to login </li></ul><ul><li>mysql [-h host_name] [-u user_name] [-p your_pass] </li></ul><ul><li>Web host is asked for a MySQL user name and password, </li></ul><ul><li>When we log in to mysql, the first thing we see is: </li></ul><ul><li>Welcome to the MySQL monitor. Commands end with ; or g. </li></ul><ul><li>Your MySQL connection id is XXXX to server version: 3.22.22 </li></ul><ul><li>Type 'help' for help. </li></ul>
  4. 4. DATATYPES <ul><li>The most commonly used data types are </li></ul><ul><li>Numeric Types </li></ul><ul><li>INT[(M)] - A normal-size integer. </li></ul><ul><li>Example: Age INT; </li></ul><ul><li>FLOAT[(M,D)] - A small (single-precision) floating-point number. Cannot be unsigned. </li></ul><ul><li>Floating point numbers are made to be very precise. If I specified Price FLOAT(6,2) then 6 numbers would be allowed to the left of the decimal point, and 2 to the right. </li></ul>
  5. 5. <ul><li>Date and Time </li></ul><ul><li>DATE - A date. </li></ul><ul><li>MySQL displays date values in 'YYYY-MM-DD' format, but allows you to assign values to DATE columns using either strings or numbers. </li></ul><ul><li>Example: Today DATE; </li></ul><ul><li>TIME - A time. T </li></ul><ul><li>MySQL displays TIME values in 'HH:MM:SS' format, but allows you to assign values to TIME columns using either strings or numbers. </li></ul><ul><li>Example: Now TIME; </li></ul>
  6. 6. <ul><li>String Types </li></ul><ul><li>CHAR(M) - CHAR's are fixed length strings that are right-padded with space to whatever you specify the length as in M. The maximum size is 255 characters. </li></ul><ul><li>Example: Name CHAR(60); </li></ul><ul><li>VARCHAR(M) - These strings are made of variable lengths (as the name kind of suggests). All of the trailing spaces will be removed when the value is stored in the database. </li></ul><ul><li>E xample: Name VARCHAR(60); </li></ul>
  7. 7. CREATING A DATABASE <ul><li>We have already logged into MySQL, the screen will be displayed as below </li></ul><ul><li>Welcome to the MySQL monitor. Commands end with ; or g. </li></ul><ul><li>Your MySQL connection id is 10663 to server version: 3.22.22 </li></ul><ul><li>Type 'help' for help. </li></ul><ul><li>mysql> </li></ul><ul><li>ALL MySQL commands end with a semicolon &quot;;&quot; </li></ul><ul><li>Example:- mysql> create database info; </li></ul><ul><li>we get as Query OK, 1 row affected (0.03 sec) </li></ul>
  8. 8. <ul><li>To start using the database, type in </li></ul><ul><li>mysql> use info; </li></ul><ul><li>You will get the result; </li></ul><ul><li>Database changed. </li></ul>
  9. 9. CREATING TABLE <ul><li>SYNTAX: </li></ul><ul><li>mysql>CREATE TABLE tablename (column_names and their datatypes); </li></ul><ul><li>Example: </li></ul><ul><li>CREATE TABLE users ( id INT NOT NULL AUTO_INCREMENT,name VARCHAR (50),email VARCHAR (50), PRIMARY KEY (id)); </li></ul><ul><li>Here Table will be called users, have 3 total columns. 1, the ID Number, 2, the Person's Name, and 3, the Persons E-Mail address. </li></ul>
  10. 10. ....continued <ul><li>The first column contains id, an integer, its value cannot be null, and it automatically increments the number from the prior entry. </li></ul><ul><li>The name column is a Variable Length String </li></ul><ul><li>The Primary Key is a type of index MySQL uses. This index can do such things as; </li></ul><ul><li>1. Quickly find the rows that match a WHERE clause. </li></ul><ul><li>2. Retrieve rows from other tables when performing joins. 3. Sort or group a table if the sorting or grouping is done on a leftmost prefix of a usable key </li></ul>
  11. 11. ADDING DATA TO TABLE <ul><li>SYNTAX: </li></ul><ul><li>mysql>INSERT INTO tablename VALUES (contents of the fields separated by commas); </li></ul><ul><li>Example: mysql> INSERT INTO users VALUES (&quot;NULL&quot;,&quot;BlairIreland&quot;,;); </li></ul><ul><li>If successful, it displays </li></ul><ul><li>Query Ok, 1 row affected (0.05 sec) </li></ul>
  12. 12. <ul><li>When you add data, and you do not have any values to go into a column, you must still have it listed in your entry. </li></ul><ul><li>For example, if there is no e-mail address, the syntax would look like: </li></ul><ul><li>mysql> INSERT INTO users VALUES (&quot;NULL&quot;,&quot;Blair Ireland&quot;,&quot;&quot;); </li></ul>
  13. 13. VIEWING DATA <ul><li>To view the datas inserted ,we use the SELECT command. </li></ul><ul><li>To view all data in the table, </li></ul><ul><li>mysql> SELECT * FROM users; </li></ul><ul><li>This will give you an output like this </li></ul><ul><li>+--+----------------+---------------------------------+ </li></ul><ul><li>| id | name | email | </li></ul><ul><li>--+------------------+---------------------------------+ </li></ul><ul><li>| 1 | Blair Ireland | | </li></ul><ul><li>| 2 | Mark Hardy | | </li></ul><ul><li>+--+-----------------+---------------------------------+ </li></ul>
  14. 14. <ul><li>It will be displayed as </li></ul><ul><li>2 rows in set (0.00 sec) </li></ul><ul><li>to select a particular row in this database </li></ul><ul><li>mysql> SELECT * FROM users WHERE (name=&quot;Blair Ireland&quot;); </li></ul><ul><li>+----+---------------+---------------------------------+ </li></ul><ul><li>| id | name | email | </li></ul><ul><li>+----+---------------+---------------------------------+ </li></ul><ul><li>| 1 | Blair Ireland | | </li></ul><ul><li>+----+---------------+---------------------------------+ </li></ul>
  15. 15. <ul><li>To select specific columns, like this; </li></ul><ul><li>mysql> select name from users; </li></ul><ul><li>+----------------+ </li></ul><ul><li>| name | </li></ul><ul><li>+----------------+ </li></ul><ul><li>| Blair Ireland | </li></ul><ul><li>| Mark Hardy | </li></ul><ul><li>+----------------+ </li></ul><ul><li>It will display </li></ul><ul><li>2 rows in set (0.06 sec) </li></ul>
  16. 16. MODIFYING TABLE <ul><li>It can be done by using the UPDATE command in mysql. </li></ul><ul><li>mysql> UPDATE users SET email= “'' WHERE email = &quot;;; </li></ul><ul><li>This would just change all rows with email set to and change them to </li></ul><ul><li>In this case though, only one entry has as its email, so only one entry would be changed. </li></ul>
  17. 17. DELETING THE DATA <ul><li>If you want to remove data in the database, use MySQL's DELETE command. </li></ul><ul><li>mysql> DELETE FROM users WHERE (name=&quot;Mark Hardy&quot;); </li></ul><ul><li>This would delete Mark Hardy's entry in the database, leaving only Blair Ireland's entry in it. </li></ul><ul><li>mysql> CREATE TABLE users ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR (50), email VARCHAR (50), PRIMARY KEY (id)); </li></ul>
  18. 18. SEARCHING THE DATA <ul><li>The % character in MySQL is the wildcard character. That is, it can represent anything, literally. </li></ul><ul><li>To do a general search, you would use the following syntax; </li></ul><ul><li>mysql> SELECT * FROM test WHERE (name LIKE &quot;%B%&quot;); </li></ul><ul><li>This will result in finding anything with the capital letter B in the column name. </li></ul><ul><li>Notice the two %'s used. This checks for anything before or after that letter. </li></ul><ul><li>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. </li></ul>
  19. 19. ...CONTINUED <ul><li>Another wildcard character is the _ character. It will match exactly one character. </li></ul><ul><li>To use a literal wildcard character in your searches, you Order By </li></ul><ul><li>mysql> SELECT * FROM users WHERE(name = &quot;Joe%&quot;) ORDER BY id DESC; </li></ul><ul><li>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. </li></ul>
  20. 20. LOGICAL OPERATORS <ul><li>NOT (or) ! </li></ul><ul><li>mysql> SELECT * FROM users WHERE(name != &quot;Blair Ireland&quot;); </li></ul><ul><li>(or) </li></ul><ul><li>mysql> SELECT * FROM users WHERE(name NOT = &quot;Blair Ireland&quot;); </li></ul><ul><li>This query would return all records without Blair Ireland present as the name. </li></ul>
  21. 21. <ul><li>AND (or) && </li></ul><ul><li>mysql> SELECT * FROM users WHERE (name = &quot;Blair Ireland&quot;) AND (email=&quot;;); </li></ul><ul><li>(or) </li></ul><ul><li>mysql> SELECT * FROM users WHERE(name = &quot;Blair Ireland&quot;) && (email = &quot;;); </li></ul><ul><li>This query would return all records with Blair Ireland present as the name, and as the email. </li></ul>
  22. 22. <ul><li>OR ( or ) || </li></ul><ul><li>mysql> SELECT * FROM test WHERE(name = &quot;Blair Ireland&quot;) OR(email = &quot;;); </li></ul><ul><li>(or) </li></ul><ul><li>mysql> SELECT * FROM test WHERE (name = &quot;Blair Ireland&quot;) ||(email = &quot;;); </li></ul><ul><li>This query would return all records with Blair Ireland present as the name, or records with as the email. </li></ul>
  23. 23. TABLE MANIPULATION <ul><li>Renaming a Table </li></ul><ul><li>mysql> ALTER TABLE users RENAME public; </li></ul><ul><li>Changing a columns datatype </li></ul><ul><li>mysql> ALTER TABLE public MODIFY name CHAR(150); </li></ul><ul><li>Renaming a Table and Changing its datatype at once </li></ul><ul><li>mysql> ALTER table users CHANGE email emailaddy CHAR (100); </li></ul>
  24. 24. <ul><li>Adding a Column </li></ul><ul><li>mysql> ALTER TABLE public ADD time TIMESTAMP; </li></ul><ul><li>Remove a Column </li></ul><ul><li>mysql> ALTER TABLE public DROP COLUMN time; </li></ul>
  25. 25. OPTIMIZING THE TABLE <ul><li>After you make these changes to the table, you may want to optimize the table afterwards (especially if we use VARCHAR's, TEXT's or BLOB's, as this will optimize its memory allocation. </li></ul><ul><li>You will also want to do it if you have deleted a large part of a table. </li></ul><ul><li>During a table optimization, the original table is available to clients, however, modifying and adding to the table is stalled until optimization is complete. </li></ul><ul><li>The syntax is: </li></ul><ul><li>OPTIMIZE TABLE table_name_goes_here </li></ul>
  26. 26. DELETING THE TABLE <ul><li>To delete (or drop) an entire table, you would use the following syntax; </li></ul><ul><li>mysql> DROP TABLE public; </li></ul><ul><li>If you would like to drop more tables at once </li></ul><ul><li>mysql> DROP TABLE public, tests; </li></ul>