You are on page 1of 28

MY SQL

Chapter 7
What is SQL
SQL ( Structured Query Language) is standard programming language
SQL is used to communication with a data base According to ANSI
SQL statement are used to perform tasks such as update data on a
database or retrieve data from data base
Sql programming language was first
Developed on the 1970s by IBM
Historical background
The first version of sql initially called SEQUEL , was developed at IBM by
Chamberlin and Boyce early 1970s
Later formally standardized by American National standard institute
(ANSI)in 1986
The first non-commercial non-sql RDBMS was developed in 1974at the
U.C.Berkely
Types of database
PL/SQL
ORACLE
MYSQL
POSTGRESQL
MongoDB
MARINA DB
DB2
Memcached…..,
History of mysql
MySQL was created by a Swedish company MySQL AB, founded by David
Axmark, Allan Larsson and Michael "Monty" Widenius.
Sun Microsystems  acquired MySQL AB in 2008
The first version of MYSQL appeared on 23 may 1995 Version 3.19
Still my sql 8.0 April 19, 2018
MYSQL architecture
Data types
Numeric data types
Int , float ,double , decimal
Date and time
Date , time ,timestamp ,date time
String
Char , varchar ,text ,
Large object data type (LOB)
BLOB,LONGTEXT,MEDIUMBLOB
spatial data types
Point, line string ,polygon ,geometry
My sql database
MySQL allows us to store and retrieve the data from the database in a
efficient way
We can create a database using the CREATE DATABASE statement
 The database already exits it throws an error

Syntax Of Data Base:


 CREATE DATABASE database_name; 

 SHOW DATABASES; 

 USE database_name; 

 DROP DATABASE database_name; 
Mysql commands
DDL (data definition language)
Table is a collection of data, organized in terms of rows and
columns.
MySQL stores all the data in tables. Regardless of the prefix,
each MySQL database table consists of rows and columns.
DDL operations
Create
Alter table
truncate
Drop
Commant
Rename
DDL
Operation
cont.. command

DROP(Is table statement removes DROP TABLE  emp; 


the complete data with structure)

TRUNCATE ( is used when you TRUNCATE TABLE  emp; 


want to delete the complete data
from a table without removing
the table structure.)

RENAME TABLE old_table name RENAME TABLE old_table_name


TO new_tablename TO new_table_name
DDL cont..
operation command
Create table(create a new table) CREATE TABLE cus_tbl(  
 cus_id INT NOT NULL AUTO_INCREMENT,  
 cus_firstname VARCHAR(100) NOT NULL,  
  cus_surname VARCHAR(100) NOT NULL,     PRI
MARY KEY ( cus_id )  
);

Alter table(used when you want to change the 1. ALTER TABLE emp  


name of your table or any table field, It is also used RENAME TO workers; 
to add or delete an existing column in a table) 2. ALTER TABLE emp  
CHANGE COLUMN regno empno   
Int(10) not null;
 3. ALTER TABLE emp 
DROP COLUMN age;  
DML(Data Manipulation Language)
 DML Commands are used for managing data within tables
DML Operation
• SELECT
• INSERT
• UPDATE
• DELETE
• MERGE
• CALL
• LOCK TABLE 
Operation
DML Con.. command

Select ( Fetch data from the one or more tables SELECT name, address  


in MySQL) FROM officers  
Insert (Is used to insert data in MySQL table INSERT INTO emp(id,name,salary) VALUES (7,
within the database)  'Sonoo', 40000);  
Update (update the records on table) UPDATE emp 
SET emp_name = 'Ambani'  
WHERE  emp id = 5;
Delete (delete records on the basis of DELETE FROM emp  
conditions) WHERE empid = 6; 
Merge ( The MERGE statement is used to CREATE TABLE Emptotal (
make changes in one table based on values Empid int(10),
matched from another) name varchar(30),
date date
) ENGINE=MERGE
UNION=(employee01,employee02) ;
DML con..
Operation commands

Call(Call the stored procedure to retrieve $records = ( ‘CALL get_customers()’ );


MySQL records)

Lock table  (locks tables for the current LOCK TABLES


thread. UNLOCK TABLES releases any table_name1 [READ | WRITE],
locks held by the current thread.)             table_name2 [READ | WRITE],
             ... ;
Or
UNLOCK TABLES;
DCL (Data control language)
Is used to control access to data stored in a database
GRANT  :
To allow specified users to perform specified tasks.
control over a specific database
EX:
GRANT ALL PRIVILEGES ON database_name.* TO
'username'@'localhost';
DCL con…
REVOKE :
The REVOKE statement enables system administrators to revoke
privileges from MySQL accounts
User accounts from which privileges are to be revoked must exist,
but the privileges to be revoked need not be currently granted to them.
EX:
REVOKE SELECT ON employee FROM user
TCL(Transaction Control Language)
TCL commands are used to manage transactions in the database.
DML commands are managed by using TCL commands
Commit
Rollback
Save point
TCL con…
Operation Command
Commit (The main use of Commit command is UPDATE STUDENT SET STUDENT_NAME =
to make the transaction permanent) ‘Maria’ WHERE STUDENT_NAME = ‘Meena’;

Rollback (The database can be restored to the UPDATE STUDENT SET STUDENT_NAME =
last committed state) ‘Manish’ WHERE STUDENT_NAME = ‘Meena’;
ROLLBACK;

Save Point(The main use of the Save point INSERT into CLASS VALUES (101, ‘Rahul);
command is to save a transaction temporarily. Commit;
This way users can rollback to the point UPDATE CLASS SET NAME= ‘Tyler’ where id=
whenever it is needed.) 101
SAVEPOINT A;
Select * from Class;
Mysql clauses
Operation command
Where (used with SELECT, INSERT, UPDATE and DELETE SELECT *  
clause to filter the results) FROM officers  
WHERE address = 'Lucknow' ;
Distinct (used to remove duplicate records from the table and SELECT DISTINCT officer_name, address  
fetch only the unique records.) FROM officers;  

From (used to select some records from a table) SELECT *  


FROM officers  
WHERE officer id <= 3;
Order by (used to sort the records in ascending or descending SELECT *  
order) FROM officers  
(ASC,DESC) WHERE address = 'Lucknow'  
ORDER BY officer_name;
Group by(used to collect data from multiple records and group SELECT address, COUNT(*)  
the result by one or more column) FROM   officers   
GROUP BY address;

Having(HAVING Clause is used with GROUP BY clause. It SELECT empname, SUM(working hours)  FROM employees  


always returns the rows where condition is TRUE) GROUP BY emp name  
HAVING SUM(working hours) > 5;  
Mysql conditions
Conditions Example
And (both condition true execute the quires) SELECT *  
FROM emp  
WHERE emp name = 'A'  
AND emp_id > 3;

Or ( two or more conditions then one of the SELECT *  


conditions must be fulfilled to get the records as FROM cus_tbl  
result) WHERE cus_firstname = 'Ajeet'  
OR cus_id > 100;

Like ( used to perform pattern matching to find SELECT emp  no


the correct result) FROM emp 
WHERE name LIKE ‘A%'; 

IN( used to reduce the use of multiple OR SELECT *  


conditions) FROM emp  
WHERE emp id IN (‘10,’11‘,’12’);  
Mysql condtions con…
Condition Example
Not (NOT condition is opposite of MySQL IN SELECT *  
condition. ) FROM officers  
WHERE officer_name NOT LIKE 'A%‘

Is null (used to check if there is a NULL value in SELECT *  


the expression) FROM player
WHERE player_valute IS NULL;

Between And (values from an expression within SELECT *  


a specific range.) FROM emp  
WHERE emp_id BETWEEN 1 AND 3;

IS Not Null(used to check the NOT NULL value SELECT CustomerName, ContactName,


in the expression) Address
FROM Customers
WHERE  Address  IS NULL;
Join
MySQL JOINS are used with SELECT statement.
you need to fetch records from two or more tables.
Types of joins
Inner join
Left join

Right join
Inner join
The MySQL INNER JOIN is used to return all rows from multiple tables 
where the join condition is satisfied.

SELECT columns  
FROM table1   
INNER JOIN table2  
ON table1.column = table2.column; 
Left join
The LEFT OUTER JOIN returns all rows from the left hand table specified
in the ON condition
 only those rows from the other table where the join condition is fulfilled.
EX:
SELECT columns  
FROM table1  
LEFT [OUTER] JOIN table2  
ON table1.column = table2.column; 
 Right Join
The Right Outer Join returns all rows from the RIGHT-hand table specified in the
ON condition
 only those rows from the other table where he join condition is fulfilled.

EX:
SELECT columns  
FROM table1  
RIGHT [OUTER] JOIN table2  
ON table1.column = table2.column; 
Aggregate Functions
Functions Example
Count ( It is used when you need to count some SELECT COUNT   
records of your table.) FROM officers;  
Sum ( used to return the total summed value of an SELECT SUM (sal) 
expression.) FROM employees  
Avg ( the average value of an expression) SELECT AVG(sal) AS "Avg sal "  
FROM employees  
WHERE working_hours > 5;  
Min(the minimum value from the table) SELECT MIN (sal) 
FROM employees;
Max (the maximum value from the table) SELECT MAX (sal)   
FROM employees;

First (use limit clause to select first record or more) SELECT column_name  


FROM table_name  
LIMIT 1;  
Last (use limit clause to select last record or more) SELECT column_name  
FROM table_name  
ORDER BY column_name DESC  
LIMIT 1;
Thanking you

You might also like