MYSQL Session Notes
MYSQL Session Notes
-------
Session-01
----------
File Management System
-----------------------
Information / data stored in file.
MS Excel
What is a Database ?
C - Create
R- Read
U - Update
D - Delete
Example : The college Database organizes the data about the admin, staff, students
and faculty etc.
Using the database, you can easily retrieve, insert, and delete the information.
Types Of Database
-----------------
1) DBMS ( Database management System) Dbase, Foxpro
2) RDBMS (Relational Database Management System) - MySQL, SQL Serever, DB2, Oracle,
MS-Access..
SQL (Structure Query Language)- used to communicate with the database to perform
different kinds of operations.
Components of Database
----------------------
1) Server - actual data is stored
2) Client - used to perform operations on databse server
Types of client
---------------
Graphical user Interface client
Command line client
MySQL Clients
-------------
MySQL Workbench (GUI Client)
MySQL Shell (CLI Client)
Toad
Squirell
Install MySQL
-------------
Session-02
--------------
1) Working on MySQL Workbench & Command prompt client
2) SQL Commands
Session-03
----------
How to select Data from the table?
Select
Error Code: 1075. Incorrect table definition; there can be only one auto column and
it must be defined as a key
SQL Datatypes
--------------
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
Numeric
Text
Date/Time
->WHERE CLAUSE
-------------
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Session-04
-----------
SELECT
select_list
FROM
table_name
ORDER BY
column1 [ASC|DESC],
column2 [ASC|DESC],
...;
MySQL DISTINCT clause to remove duplicates from the result given by the SELECT
clause.
SELECT DISTINCT
select_list
FROM
table_name
WHERE
search_condition
ORDER BY
sort_expression;
Session-05
OPERATORS
---------
AND operator
It is used in the WHERE clause of the SELECT, UPDATE, DELETE statements to form a
condition.
The AND operator is used to filter records based on more than one condition
Ex1.
SELECT
customername,
country
FROM
customers
WHERE country = 'USA' OR
country = 'France';
Ex2.
SELECT
customername,
country,
creditLimit
FROM
customers
WHERE(country = 'USA'
OR country = 'France')
AND creditlimit > 100000;
Not Operator
The NOT operator is used in combination with other operators to give the opposite
result
NOT LIKE
NOT BETWEEN
NOT IN
NOT GREATER THAN (>)
NOT LESS THAN (<)
IN Opertator
The IN operator allows you to specify multiple values in a WHERE clause.
Session- 06
-----------
DDL Commands
Primary Key
A primary key is a column or a set of columns that uniquely identifies each row in
the table.
1) A primary key must contain unique values. If the primary key consists of
multiple columns, the combination of values in these columns must be unique.
2 )A primary key column cannot have NULL values. Any attempt to insert or update
NULL to primary key columns will result in an error.
MySQL implicitly adds a NOT NULL constraint to primary key columns.
2) Alter Table - Used for modifying and renaming elements of an existing database
table. Add new column, Drop column, Modify existing column, Rename column
3) Truncate Table- Used to remove all the records from a database table.
TRUNCATE TABLE table_name;
Session-07
----------
1) String Functions
2) Mathematical/Numeric functions
3) Date/Time Functions
4) Aggregate Function
Session-08
-----------
1) Mathematical/Numeric functions
2) Date/Time Functions
3) Aggregate Function
Session-09
-----------
Group By Clause
In MySQL GROUP BY clause is used to collect data from multiple records and group
the result by one or more column.
Group by clause is often used with aggregate functions such as SUM, AVG, MAX, MIN,
and COUNT.
Select Column-names
From Table-name
where condition
Group By Column-names
Having condition
Order By Column-names
-------------------------SQL Queries----------------------------------------
Session -10
------------
Set Operators
1. Union - UNION operator combine two or more result sets from multiple SELECT
statements into a single result set.
2. Union all - The UNION ALL command combines the result set of two or more SELECT
statements (allowsduplicate values).
3. Intersect (INTERSECT was added in MySQL 8.0.31.) - compares the result sets of
two queries and returns the common rows.
Intersect ALL (allow duplicates)
4. Except (Minus) (EXCEPT was added in MySQL 8.0.31.)- allows you to retrieve rows
from one query that do not appear in another query. EXCEPT operator find the set
difference between two sets of data.
Rules
=====
The number of columns in all the SELECT statements must be the same.
The order of columns in all the SELECT statements must be the same.
The data types of the corresponding columns in both SELECT statements must be the
same or similar or convertible/compatible.
CREATE TABLE A (
col1 INT ,col2 INT
);
CREATE TABLE B (
col1 INT ,col2 INT
);
CREATE TABLE C (
col1 INT ,col2 INT
);
CREATE TABLE t1 (
id INT
);
CREATE TABLE t2 (
id INT
);
select * from t1
union
select * from t2;
select * from t1
union all
select * from t2;
select * from t1
intersect
select * from t2;
Session -11
-----------
JOINS
-----
JOINS are used with SELECT statement. It is used to retrieve data from multiple
tables.
The tables are mutually related using primary and foreign keys.
TYPES OF JOINS
--------------
SELECT
select_list
FROM t1
INNER JOIN t2 ON join_condition1
INNER JOIN t3 ON join_condition2
...;
2) LEFT OUTER JOIN / LEFT JOIN - Matched records from both the tables + un matched
records from left table.
SELECT
select_list
FROM
t1
LEFT JOIN t2 ON
join_condition;
3) RIGHT OUTER JOIN / RIGHT JOIN - Matched records from both the tables + un
matched records from right table.
SELECT
select_list
FROM t1
RIGHT JOIN t2 ON
join_condition;
CREATE TABLE t2 (
num INT
);
use project_hr;
select * from employees inner join departments on employees.department_id =
departments.department_id;
SELECT
productCode,
productName,
textDescription
FROM
products t1
INNER JOIN productlines t2
ON t1.productline = t2.productline;
SELECT
productCode,
productName,
textDescription
FROM
products
INNER JOIN productlines
using (productline);
use classicmodels;
SELECT
customers.customerNumber,
customerName,
orderNumber,
status
FROM
customers
LEFT JOIN orders
ON customers.customerNumber = orders.customerNumber
WHERE
orderNumber IS NULL;
use demodb;
SELECT * FROM T1 cross join T2;
Session -12
===========
Sub Queries
A MySQL subquery is a query nested within another query such as SELECT, INSERT,
UPDATE or DELETE.
Also, a subquery can be nested within another subquery.
A MySQL subquery is called an inner query while the query that contains the
subquery is called an outer query.
-- the following query returns the customer who has the highest payment.
SELECT MAX(amount) FROM payments;
SELECT
customerNumber,
checkNumber,
amount
FROM
payments
WHERE
amount = (SELECT MAX(amount) FROM payments);
select max(salary) from employees where salary < (select max(salary) from
employees);
SELECT
customerName
FROM
customers
WHERE
customerNumber NOT IN (SELECT DISTINCT
customerNumber
FROM
orders);