Mysql
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • 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
957
On Slideshare
957
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
13
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. MY SQL
  • 2. INTRODUCTION
    • MySQL is a relational database management system (RDBMS)[1] that runs as a server providing multi-user access to a number of databases.
    • Some of the features of MySQL Include:
    • Handles large databases.
    • No memory leaks
    • Tested with a commercial memory leakage detector (purify).
    • A privilege and password system which is very flexible and secure, and which allows host-based verification.
    • Passwords are secure since all password traffic when connecting to a server is encrypted.
  • 3. GETTING STARTED
    • At the prompt type in something along the lines of the following syntax to login
    • mysql [-h host_name] [-u user_name] [-p your_pass]
    • Web host is asked for a MySQL user name and password,
    • When we log in to mysql, the first thing we see is:
    • Welcome to the MySQL monitor. Commands end with ; or g.
    • Your MySQL connection id is XXXX to server version: 3.22.22
    • Type 'help' for help.
  • 4. DATATYPES
    • The most commonly used data types are
    • Numeric Types
    • INT[(M)] - A normal-size integer.
    • Example: Age INT;
    • FLOAT[(M,D)] - A small (single-precision) floating-point number. Cannot be unsigned.
    • 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.
  • 5.
    • Date and Time
    • DATE - A date.
    • MySQL displays date values in 'YYYY-MM-DD' format, but allows you to assign values to DATE columns using either strings or numbers.
    • Example: Today DATE;
    • TIME - A time. T
    • MySQL displays TIME values in 'HH:MM:SS' format, but allows you to assign values to TIME columns using either strings or numbers.
    • Example: Now TIME;
  • 6.
    • String Types
    • 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.
    • Example: Name CHAR(60);
    • 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.
    • E xample: Name VARCHAR(60);
  • 7. CREATING A DATABASE
    • We have already logged into MySQL, the screen will be displayed as below
    • Welcome to the MySQL monitor. Commands end with ; or g.
    • Your MySQL connection id is 10663 to server version: 3.22.22
    • Type 'help' for help.
    • mysql>
    • ALL MySQL commands end with a semicolon ";"
    • Example:- mysql> create database info;
    • we get as Query OK, 1 row affected (0.03 sec)
  • 8.
    • To start using the database, type in
    • mysql> use info;
    • You will get the result;
    • Database changed.
  • 9. CREATING TABLE
    • SYNTAX:
    • mysql>CREATE TABLE tablename (column_names and their datatypes);
    • Example:
    • CREATE TABLE users ( id INT NOT NULL AUTO_INCREMENT,name VARCHAR (50),email VARCHAR (50), PRIMARY KEY (id));
    • 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.
  • 10. ....continued
    • The first column contains id, an integer, its value cannot be null, and it automatically increments the number from the prior entry.
    • The name column is a Variable Length String
    • The Primary Key is a type of index MySQL uses. This index can do such things as;
    • 1. Quickly find the rows that match a WHERE clause.
    • 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
  • 11. ADDING DATA TO TABLE
    • SYNTAX:
    • mysql>INSERT INTO tablename VALUES (contents of the fields separated by commas);
    • Example: mysql> INSERT INTO users VALUES ("NULL","BlairIreland",ireland@thescripts.com");
    • If successful, it displays
    • Query Ok, 1 row affected (0.05 sec)
  • 12.
    • 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.
    • For example, if there is no e-mail address, the syntax would look like:
    • mysql> INSERT INTO users VALUES ("NULL","Blair Ireland","");
  • 13. VIEWING DATA
    • To view the datas inserted ,we use the SELECT command.
    • To view all data in the table,
    • mysql> SELECT * FROM users;
    • This will give you an output like this
    • +--+----------------+---------------------------------+
    • | id | name | email |
    • --+------------------+---------------------------------+
    • | 1 | Blair Ireland | bireland@thescripts.com |
    • | 2 | Mark Hardy | mhardy@thescripts.com |
    • +--+-----------------+---------------------------------+
  • 14.
    • It will be displayed as
    • 2 rows in set (0.00 sec)
    • to select a particular row in this database
    • mysql> SELECT * FROM users WHERE (name="Blair Ireland");
    • +----+---------------+---------------------------------+
    • | id | name | email |
    • +----+---------------+---------------------------------+
    • | 1 | Blair Ireland | bireland@thescripts.com |
    • +----+---------------+---------------------------------+
  • 15.
    • To select specific columns, like this;
    • mysql> select name from users;
    • +----------------+
    • | name |
    • +----------------+
    • | Blair Ireland |
    • | Mark Hardy |
    • +----------------+
    • It will display
    • 2 rows in set (0.06 sec)
  • 16. MODIFYING TABLE
    • It can be done by using the UPDATE command in mysql.
    • mysql> UPDATE users SET email= “webmaster@thescripts.com'' WHERE email = "bireland@thescripts.com";
    • 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.
  • 17. DELETING THE DATA
    • If you want to remove data in the database, use MySQL's DELETE command.
    • 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.
    • mysql> CREATE TABLE users ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR (50), email VARCHAR (50), PRIMARY KEY (id));
  • 18. SEARCHING THE DATA
    • The % character in MySQL is the wildcard character. That is, it can represent anything, literally.
    • 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.
    • 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.
  • 19. ...CONTINUED
    • Another wildcard character is the _ character. It will match exactly one character.
    • To use a literal wildcard character in your searches, you Order By
    • mysql> SELECT * FROM users WHERE(name = "Joe%") ORDER BY id DESC;
    • 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.
  • 20. LOGICAL OPERATORS
    • 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.
  • 21.
    • AND (or) &&
    • mysql> SELECT * FROM users WHERE (name = "Blair Ireland") AND (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.
  • 22.
    • OR ( or ) ||
    • 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.
  • 23. TABLE MANIPULATION
    • Renaming a Table
    • mysql> ALTER TABLE users RENAME public;
    • Changing a columns datatype
    • mysql> ALTER TABLE public MODIFY name CHAR(150);
    • Renaming a Table and Changing its datatype at once
    • mysql> ALTER table users CHANGE email emailaddy CHAR (100);
  • 24.
    • Adding a Column
    • mysql> ALTER TABLE public ADD time TIMESTAMP;
    • Remove a Column
    • mysql> ALTER TABLE public DROP COLUMN time;
  • 25. OPTIMIZING THE TABLE
    • 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.
    • 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
  • 26. DELETING THE 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
    • mysql> DROP TABLE public, tests;