Mysqlcourse 190815092242
Mysqlcourse 190815092242
Mysqlcourse 190815092242
COM
SQL Language
WWW.PAVANONLINETRAININGS.COM
SQL Commands
WWW.PAVANONLINETRAININGS.COM
Create Database/Schema
WWW.PAVANONLINETRAININGS.COM
Creating Table
▪ Example:
▪ USE mydb;
WWW.PAVANONLINETRAININGS.COM
Inserting data into table
▪ Example:
▪ USE mydb;
WWW.PAVANONLINETRAININGS.COM
Selecting Rows from a table
▪ USE hr;
WWW.PAVANONLINETRAININGS.COM
SQL Data types
▪ Numeric
▪ Text
▪ Date/time
WWW.PAVANONLINETRAININGS.COM
Numeric Data Types
WWW.PAVANONLINETRAININGS.COM
Text Data Types
WWW.PAVANONLINETRAININGS.COM
Date & Time
DATE YYYY-MM-DD
DATETIME YYYY-MM-DD HH:MM:SS
TIMESTAMP YYYYMMDDHHMMSS
TIME HH:MM:SS
WWW.PAVANONLINETRAININGS.COM
Where clause
▪ Used for selecting the rows based on condition.(Filtering the rows using where condition)
▪ USE hr;
WWW.PAVANONLINETRAININGS.COM
Logical Operators (AND, OR, NOT)
▪ USE hr;
▪ SET SQL_SAFE_UPDATES = 0;
WWW.PAVANONLINETRAININGS.COM
Between & IN Operators
▪ Between Used to display the rows which is following in the range of values.
▪ Not Between
▪ USE hr;
▪ SELECT * FROM EMPLOYEES WHERE SALARY NOT BETWEEN 10000 AND 12000;
▪ IN IN operator return the rows when the values are matching in the list
▪ Not In
WWW.PAVANONLINETRAININGS.COM
Pattern Matching operators ( whiled card characters)
WWW.PAVANONLINETRAININGS.COM
DDL Commands
WWW.PAVANONLINETRAININGS.COM
DDL Commands( Data Definition Language)
1) CREATE
2) ALTER
3) DROP
4) TRUNCATE
5) RENAME
WWW.PAVANONLINETRAININGS.COM
Create & Alter
▪ ALTER
• Adding a new column
• Dropping the existing column
• Modifying the existing column ( Increase/Decrease size of the column & change the data type
of the column)
• Renaming a column
WWW.PAVANONLINETRAININGS.COM
▪ use mydb;
▪ SELECT * FROM STUDENT;
▪ DESCRIBE STUDENT;
▪ Adding a new column
▪ ALTER TABLE STUDENT ADD(grade varchar(2));
▪ Dropping a column from table
▪ ALTER TABLE STUDENT DROP COLUMN grade;
▪ Modifying the existing column
– We can increase/decrease the size of the column.
– We can decrease the column size ONLY when existing column values can fit into new size..
– Column should be empty should be empty to change its data type.
WWW.PAVANONLINETRAININGS.COM
DROP, TRUNCATE & DELETE
▪ DROP:
▪ TRUNCATE
▪ DELETE
WWW.PAVANONLINETRAININGS.COM
Differences between Drop, Truncate & delete
▪ DELETE FROM STUDENT; -- Removes the all the rows temporarily. We can Roll back the rows.
• To roll back record we need to apply AUTO COMMIT Zero
• SET SQL_SAFE_UPDATES = 0;
• SET autocommit=0;
▪ RENAME TABLE
WWW.PAVANONLINETRAININGS.COM
MySQL Functions
WWW.PAVANONLINETRAININGS.COM
MySQL Functions
4) Aggregate functions - operate on all of the data types and produce summarized result sets.
WWW.PAVANONLINETRAININGS.COM
String Functions
▪ SELECT LENGTH('oracle’);
▪ SELECT INSTR('ORACLE','E');
WWW.PAVANONLINETRAININGS.COM
String Functions…
▪ SUBSTR() /SUBSTRING(): Returns the substring of the string.
▪ USE hr;
▪ SELECT CONCAT('ORACLE','TRAINING’);
▪ USE hr;
WWW.PAVANONLINETRAININGS.COM
Numeric Functions
▪ SELECT ABS(-40);
▪ SELECT ABS(40);
▪ select SQRT(25);
▪ select MOD(10,3);
▪ select power(2,5);
WWW.PAVANONLINETRAININGS.COM
Numeric Functions…
▪ GREATEST() & LEAST(): returns greatest, least values in the provided values.
▪ SELECT GREATEST(100,200,300,400,500);
▪ SELECT LEAST(100,200,300);
▪ Ref: https://dev.mysql.com/doc/refman/5.5/en/numeric-functions.html
WWW.PAVANONLINETRAININGS.COM
Date Functions
▪ MONTH() function returns the month part for a given date (a number from 1 to 12).
▪ SELECT MONTH("2019-05-19"); -- 5
▪ YEAR() function returns the year part for a given date (a number from 1000 to 9999).
▪ SELECT YEAR("2019-05-19"); -- 2019
▪ DAY() function returns the day of the month for a given date (a number from 1 to 31).
▪ SELECT DAY("2019-05-19");
▪ Ref https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
WWW.PAVANONLINETRAININGS.COM
Queries on Date Functions
WWW.PAVANONLINETRAININGS.COM
Aggregate Functions
▪ Aggregate Functions are all about performing calculations on multiple rows of a single column of
a table and returning a single value.
▪ USE hr;
WWW.PAVANONLINETRAININGS.COM
Group By clause
WWW.PAVANONLINETRAININGS.COM
Group By clause
▪ Group By clause:
▪ GROUP BY typically also involves aggregates: COUNT, MAX, SUM, AVG, etc.
▪ All the columns in the select list should include in group by clause.
WWW.PAVANONLINETRAININGS.COM
Having & Order by clause
▪ Having clause: Having clause is used to filter the output from the group by clause.
▪ Order By clause: Order by clause is used to arrange the rows in a table ( ascending or descending
order).
WWW.PAVANONLINETRAININGS.COM
Order Of Execution
WWW.PAVANONLINETRAININGS.COM
UNION Operator
WWW.PAVANONLINETRAININGS.COM
UNION & UNION ALL
▪ The UNION operator is used to combine the result-set of two or more SELECT statements.
▪ Each SELECT statement within UNION must have the same number of columns
▪ The columns must also have similar data types
▪ The columns in each SELECT statement must also be in the same order
WWW.PAVANONLINETRAININGS.COM
UNION & UNION ALL..
▪ COMMIT;
▪ Displays all the records from multiple tables including duplicates.
▪ INSERT INTO B VALUES(11,'A’); ▪ SELECT NUM FROM A UNION ALL SELECT NUM FROM B;
▪ INSERT INTO B VALUES(12,'B’);
▪ COMMIT
WWW.PAVANONLINETRAININGS.COM
SQL Joins
▪ The tables are mutually related using primary and foreign keys.
▪ Types of Joins
1. Equi Join/Inner Join/Simple Join
2. Right Join
3. Left Join
4. Full Join
5. Self Join
WWW.PAVANONLINETRAININGS.COM
CREATE TABLE TAB1(NUMID INT(3));
CREATE TABLE TAB2(NUMID INT(3));
WWW.PAVANONLINETRAININGS.COM
Equi Join/Inner Join
▪ Inner/Equi join (Returns Only matched records from Tab1 & Tab2)
– SELECT * FROM TAB1 INNER JOIN TAB2 ON TAB1.NUMID=TAB2.NUMID;
▪ Right outer join (Returns matched records+ unmatched from right table Tab1)
– SELECT * FROM TAB1 RIGHT JOIN TAB2 ON TAB1.NUMID=TAB2.NUMID;
▪ Left outer join (Returns matched records+ unmatched from right table Tab2)
– SELECT * FROM TAB1 LEFT JOIN TAB2 ON TAB1.NUMID=TAB2.NUMID;
▪ Full outer join (Returns matched records+ unmatched from both tables) -- not supported in mysql
– SELECT * FROM TAB1 FULL JOIN TAB2 t2 ON t1.NUMID=t2.NUMID;
WWW.PAVANONLINETRAININGS.COM
Inner Join/Self-Join
WWW.PAVANONLINETRAININGS.COM
Right Join
WWW.PAVANONLINETRAININGS.COM
Left Join
WWW.PAVANONLINETRAININGS.COM
Self-Join
WWW.PAVANONLINETRAININGS.COM
Sub Queries
WWW.PAVANONLINETRAININGS.COM
Single Row Sub Queries
WWW.PAVANONLINETRAININGS.COM
Single Row Sub Queries…
▪ Find the salary of employees whose salary is greater than the salary of employee whose
EMPLOYEE_ID 150.
▪ SELECT SALARY FROM EMPLOYEES WHERE SALARY>(SELECT SALARY FROM EMPLOYEES WHERE
EMPLOYEE_ID=150);
▪ Display the employees who all are earning the highest salary.
WWW.PAVANONLINETRAININGS.COM
Multi Row Sub Queries
▪ Display employees whose salary is equal to the salary of the at least one employee in department
id 30.
▪ SELECT * FROM EMPLOYEES WHERE SALARY IN (SELECT SALARY FROM EMPLOYEES WHERE
DEPARTMENT_ID=30);
▪ Display the employees whose salary is greater than the at least on employee in department id 30.
▪ Display the employees whose salary is less than the at least on employee in department id 30.
WWW.PAVANONLINETRAININGS.COM
Multi Row Sub Queries…
▪ List out the employees who are having salary less than the maximum salary and also having hire
date greater than the hire date of an employee who is having maximum salary.
WWW.PAVANONLINETRAININGS.COM
Integrity Constraints
WWW.PAVANONLINETRAININGS.COM
Integrity Constraints
▪ Constraints can be specified when the table is created with the CREATE TABLE statement, or after
the table is created with the ALTER TABLE statement.
▪ SQL Constraints
▪ PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table
WWW.PAVANONLINETRAININGS.COM
Not null
▪ NOT NULL: This is a constraint will not accept NULL values into the column.
WWW.PAVANONLINETRAININGS.COM
UNIQUE
▪ Column Level
▪ Table Level
WWW.PAVANONLINETRAININGS.COM
UNIQUE…
WWW.PAVANONLINETRAININGS.COM
Primary Key
▪ primary key column will not allow duplicate values and also null values.
▪ primary key constraint can create both column level & table level.
sname varchar(10),
marks int(3));
WWW.PAVANONLINETRAININGS.COM
Primary Key…
* we can create primary key on combination of two columns called as composite primary key.
WWW.PAVANONLINETRAININGS.COM
Foreign key constraint
▪ A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in
another table.
▪ The table containing the foreign key is called the child table, and the table containing the
candidate key is called the referenced or parent table.
WWW.PAVANONLINETRAININGS.COM
Foreign key constraint…
WWW.PAVANONLINETRAININGS.COM
ON DELETE CASCADE
▪ ON DELETE CASCADE:
▪ Normally, we cannot delete rows from Parent table unless we delete corresponding row from
child table.
▪ We can delete the rows from the parent table & corresponding child table row as well(at same
time) by using ONDELETECASCADE option.
WWW.PAVANONLINETRAININGS.COM
ON DELETE CASCADE…
WWW.PAVANONLINETRAININGS.COM
ON DELETE CASCADE…
* One row deleted from parent table and one from child table also deleted.
WWW.PAVANONLINETRAININGS.COM
Foreign key constraint at table level
WWW.PAVANONLINETRAININGS.COM
Check Constraint
▪ Check constraint is used for allowing to user to enter specific values into column.
create table student(
sno int(5),
sname varchar(15),
marks int(5) check(marks between 50 and 100));
▪ insert into student values(101,'amith',60); // valid
▪ insert into student values(101,'amith',45);// in valid
▪ insert into student values(101,'amith',105); //invalid
create table loc
( city varchar(15) check(city in('HYDERABD','CHENNAI','DELHI')),
country varchar(15),
pin int(8));
▪ insert into loc values('HYDERABD','INDIA',123456); // VALID
▪ insert into loc values('MUMBAI','INDIA',644566); // in valid
▪ insert into loc values('DELHI','INDIA',678445); //valid
▪ select * from loc;
WWW.PAVANONLINETRAININGS.COM
Default Constraint
▪ The default value will be added to all new records IF no other value is specified.
(ID int(5),
OrderNumber int(5) ,
WWW.PAVANONLINETRAININGS.COM
AUTO_INCREMENT
▪ Auto Increment is a function that operates on numeric data types. It automatically generates
sequential numeric values every time that a record is inserted into a table for a field defined as
auto increment.
WWW.PAVANONLINETRAININGS.COM
LIMIT
WWW.PAVANONLINETRAININGS.COM
Views
WWW.PAVANONLINETRAININGS.COM
Index
▪ Indexes are used to retrieve data from the database very fast.
▪ The users cannot see the indexes, they are just used to speed up searches/queries.
▪ Creating Index:
– CREATE INDEX idx_employees ON Employees (First_Name);
▪ Dropping Index:
– drop index idx_employees on Employees;
WWW.PAVANONLINETRAININGS.COM
TCL – Commit & Rollback
▪ SET autocommit = 1;
▪ drop table student;
▪ create table student(sid int(3),sname varchar(15));
▪ insert into student values(101,'abc’);
▪ insert into student values(102,'abc’);
▪ insert into student values(103,'abc’);
▪ Delete from student where sid=103; //Deletes record remporarly
▪ select * from student;
▪ Rollback; // Rollback record
▪ select * from student;
▪ Commit; //Commited delete
▪ Rollback;
▪ select * from student; // cannot get deleted record after commit
WWW.PAVANONLINETRAININGS.COM
Exists
▪ The EXISTS operator is used to test for the existence of any record in a subquery.
▪ The EXISTS operator returns true if the subquery returns one or more records.
WWW.PAVANONLINETRAININGS.COM
JDBC – Java Database
Connectivity
WWW.PAVANONLINETRAININGS.COM