MySQL Tutorial
MySQL Tutorial
Introduction _________________________________________________ 1
Operators ____________________________________________________ 41
Keys __________________________________________________________ 51
Clauses__________________________________________________ ____ 63
Joins ________________________________________________________ 76
Partitioning __________________________________________________ 84
CTE ____________________________________________________________104
Indexes _______________________________________________________109
Subquery _____________________________________________________119
Store Function and Prodedure ______________________________ 122
Normalization_________________________________________________ 163
MUJAHID RAZA 1
INTRODUCTION
What is Data?
Data is a collection of a distinct small unit of information. It can be used in a variety of
forms like text, numbers, media, bytes, etc. it can be stored in pieces of paper or electronic
memory, etc.
Word 'Data' is originated from the word 'datum' that means 'single piece of information.'
It is plural of the word datum.
In computing, Data is information that can be translated into a form for efficient
movement and processing. Data is interchangeable.
What is Database?
A database is an organized collection of data, so that it can be easily accessed and
managed.
You can organize data into tables, rows, columns, and index it to make it easier to find
relevant information.
The main purpose of the database is to operate a large amount of information by storing,
retrieving, and managing data.
There are many databases available like MySQL, Sybase, Oracle, MongoDB, Informix, PostgreSQL,
SQL Server, etc.
Evolution of Databases
The database has completed more than 50 years of journey of its evolution from flat-file
system to relational and objects relational systems. It has gone through several
generations.
➢ 1968 was the year when File-Based database were introduced. In file-based databases,
data was maintained in a flat file.
➢ 1968-1980 was the era of the Hierarchical Database. Prominent hierarchical database
model was IBM's first DBMS. It was called IMS (Information Management System).
➢ Charles Bachman developed the first DBMS at Honeywell called Integrated Data Store
(IDS). It was developed in the early 1960s, but it was standardized in 1971 by the CODASYL
group (Conference on Data Systems Languages).
MUJAHID RAZA 2
➢ 1970 - Present: It is the era of Relational Database and Database Management. In 1970,
the relational model was proposed by E.F. Codd.
o Data Definition: It is used for creation, modification, and removal of definition that
defines the organization of data in the database.
o Data Updation: It is used for the insertion, modification, and deletion of the actual
data in the database.
o Data Retrieval: It is used to retrieve the data from the database which can be used
by applications for various purposes.
o User Administration: It is used for registering and monitoring users, maintain data
integrity, enforcing data security, dealing with concurrency control, monitoring
performance and recovering information corrupted by unexpected failure.
Characteristics of DBMS
o It uses a digital repository established on a server to store and manage the
information.
o It can provide a clear and logical view of the process that manipulates data.
o DBMS contains automatic backup and recovery procedures.
o It contains ACID properties which maintain data in a healthy state in case of failure.
o It can reduce the complex relationship between data.
o It is used to support manipulation and processing of data.
o It is used to provide security of data.
o It can view the database from different viewpoints according to the requirements
of the user.
MUJAHID RAZA 3
Advantages of DBMS
o Controls database redundancy: It can control data redundancy because it stores all the
data in one single database file and that recorded data is placed in the database.
o Data sharing: In DBMS, the authorized users of an organization can share the data among
multiple users.
o Easily Maintenance: It can be easily maintainable due to the centralized nature of the
database system.
o Reduce time: It reduces development time and maintenance need.
o Backup: It provides backup and recovery subsystems which create automatic backup of
data from hardware and software failures and restores the data if required.
o multiple user interface: It provides different types of user interfaces like graphical user
interfaces, application program interfaces
Disadvantages of DBMS
o Cost of Hardware and Software: It requires a high speed of data processor and
large memory size to run DBMS software.
o Size: It occupies a large space of disks and large memory to run them efficiently.
o Complexity: Database system creates additional complexity and requirements.
o Higher impact of failure: Failure is highly impacted the database because in most
of the organization, all the data stored in a single database and if the database is
damaged due to electric failure or database corruption then the data may be lost
forever.
Types of Databases
There are various types of databases used for storing different varieties of data:
1) Centralized Database
It is the type of database that stores data at a centralized database system. It comforts the
users to access the stored data from different locations through several applications. These
applications contain the authentication process to let users access data securely. An
example of a Centralized database can be Central Library that carries a central database
of each library in a college/university
MUJAHID RAZA 5
2) Distributed Database
Unlike a centralized database system, in distributed systems, data is distributed among
different database systems of an organization. These database systems are connected via
communication links. Such links help the end-users to access the data easily. Examples of
the Distributed database are Apache Cassandra, HBase, Ignite, etc.
o Homogeneous DDB: Those database systems which execute on the same operating
system and use the same application process and carry the same hardware devices.
3) Relational Database
This database is based on the relational data model, which stores data in the form of
rows(tuple) and columns(attributes), and together forms a table(relation). A relational
database uses SQL for storing, manipulating, as well as maintaining the data. E.F. Codd
invented the database in 1970. Each table in the database carries a key that makes the
data unique from others. Examples of Relational databases are MySQL, Microsoft SQL
Server, Oracle, etc.
What is table/Relation?
Everything in a relational database is stored in the form of relations. The RDBMS database
uses tables to store data. A table is a collection of related data entries and contains rows
and columns to store data. Each table represents some real-world objects such as person,
place, or event about which information is collected. The organized collection of data into
a relational table is known as the logical view of the database.
Properties of a Relation:
Properties of a row:
o The order of the tuple is irrelevant. They are identified by their content, not by their
position.
What is a column/attribute?
A column is a vertical entity in the table which contains all information associated with a
specific field in a table. For example, "name" is a column in the above table which contains
all information about a student's name.
Properties of an Attribute:
Degree
The total number of attributes that comprise a relation is known as the degree of the table.
Domain
The domain refers to the possible values each attribute can contain. It can be specified
using standard data types such as integers, floating numbers, etc. For example, An
attribute entitled Marital_Status may be limited to married or unmarried values.
NULL Values
The NULL value of the table specifies that the field has been left blank during record
creation. It is different from the value filled with zero or a field that contains space.
MUJAHID RAZA 8
Data Integrity
There are the following categories of data integrity exist with each RDBMS:
Domain integrity: It enforces valid entries for a given column by restricting the type, the
format, or the range of values.
Referential integrity specifies that rows cannot be deleted, which are used by other
records.
User-defined integrity: It enforces some specific business rules defined by users. These
rules are different from the entity, domain, or referential integrity.
Atomicity :- This ensures the data operation will complete either with success or with
failure. It follows the 'all or nothing' strategy. For example, a transaction will either be
committed or will abort.
Example: If Remo has account A having $30 in his account from which he wishes to send
$10 to Sheero's account, which is B. In account B, a sum of $ 100 is already present. When
$10 will be transferred to account B, the sum will become $110. Now, there will be two
operations that will take place. One is the amount of $10 that Remo wants to transfer will
be debited from his account A, and the same amount will get credited to account B, i.e.,
into Sheero's account. Now, what happens - the first operation of debit executes
successfully, but the credit operation, however, fails. Thus, in Remo's account A, the value
becomes $20, and to that of Sheero's account, it remains $100 as it was previously present.
MUJAHID RAZA 9
In the above diagram, it can be seen that after crediting $10, the amount is still $100 in
account B. So, it is not an atomic transaction.
The below image shows that both debit and credit operations are done successfully. Thus
the transaction is atomic.
Thus, when the amount loses atomicity, then in the bank systems, this becomes a huge
issue, and so the atomicity is the main focus in the bank systems.
Consistency :- If we perform any operation over the data, its value before and after the
operation should be preserved. For example, the account balance before
and after the transaction should be correct, i.e., it should remain conserved.
MUJAHID RAZA 10
Isolation :- There can be concurrent users for accessing data at the same time from the
database. Thus, isolation between the data should remain isolated. For
example, when multiple transactions occur at the same time, one transaction
effects should not be visible to the other transactions in the database.
Example: If two operations are concurrently running on two different accounts, then the
value of both accounts should not get affected. The value should remain persistent. As
you can see in the below diagram, account A is making T1 and T2 transactions to account
B and C, but both are executing independently without affecting each other. It is known
as Isolation.
Durability :- It ensures that once it completes the operation and commits the data, data
changes should remain permanent.
1.
MUJAHID RAZA 11
4) NoSQL Database
Non-SQL/Not Only SQL is a type of database that is used for storing a wide range of data
sets. It is not a relational database as it stores data not only in tabular form but in several
different ways. It came into existence when the demand for building modern applications
increased. Thus, NoSQL presented a wide variety of database technologies in response to
the demands. We can further divide a NoSQL database into the following four types
a. Key-value storage: It is the simplest type of database storage where it stores every
single item as a key (or attribute name) holding its value, together.
b. Document-oriented Database: A type of database used to store data as JSON-
like document. It helps developers in storing data by using the same document-
model format as used in the application code.
c. Graph Databases: It is used for storing vast amounts of data in a graph-like
structure. Most commonly, social networking websites use the graph database.
d. Wide-column stores: It is similar to the data represented in relational databases.
Here, data is stored in large columns together, instead of storing in rows.
5) Cloud Database
A type of database where data is stored in a virtual environment and executes over the
cloud computing platform. It provides users with various cloud computing services (SaaS,
PaaS, IaaS, etc.) for accessing the database. There are numerous cloud platforms, but the
best options are:
6) Object-oriented Databases
The type of database that uses the object-based data model approach for storing data in
the database system. The data is represented and stored as objects which are similar to
the objects used in the object-oriented programming language.
7) Hierarchical Databases
It is the type of database that stores data in the form of parent-children relationship nodes.
Here, it organizes data in a tree-like structure.
MUJAHID RAZA 13
8) Network Databases
It is the database that typically follows the network data model. Here, the representation
of data is in the form of nodes connected via links between them. Unlike the hierarchical
database, it allows each record to have multiple children and parent nodes to form a
generalized graph structure.
9) Personal Database
Collecting and storing data on the user's system defines a Personal Database. This
database is basically designed for a single user.
CAP Theorem :-
The CAP theorem states that it is not possible to guarantee all three of the desirable
The theorem states that networked shared-data systems can only strongly support two
Consistency
Consistency means that all clients see the same data at the same time, no matter which node
they connect to. For this to happen, whenever data is written to one node, it must be instantly
forwarded or replicated to all the other nodes in the system before the write is deemed
‘successful.’
MUJAHID RAZA 14
Availability
Availability means that any client making a request for data gets a response, even if one or more
nodes are down. Another way to state this—all working nodes in the distributed system return a
valid response for any request, without exception.
Partition tolerance
The system prioritizes availability over consistency and can respond with
The system prioritizes availability over consistency and can respond with
The system prioritizes consistency over availability and responds with the
SQL
SQL is a “standard language for accessing and manipulating databases”.
Characteristics of SQL :
• SQL is an “ANSI” and “ISO” standard computer language for
creating and manipulating database.
• It allows the user to create, update, delete and retrieve data from
a database.
• It is very simple and easy to learn.
• It is used specifically for relational databases.
• It works with database programs like DB2, Oracle, MS Access, etc.
MySQL
MySQL is currently the most popular database management system software used for
Characteristics of MySQL:
Relational Database Management System (RDBMS)
Easy to use
MySQL is easy to use. We have to get only the basic knowledge of SQL. We can build
and interact with MySQL by using only a few simple SQL statements.
It is secure
MySQL consists of a solid data security layer that protects sensitive data from intruders.
Also, passwords are encrypted in MySQL.
Free to download
MySQL is free to use so that we can download it from MySQL official website without
any cost.
It is scalable
MySQL supports multi-threading that makes it easily scalable. It can handle almost any
amount of data, up to as much as 50 million rows or more. The default file size limit is
about 4 GB. However, we can increase this number to a theoretical limit of 8 TB of data.
Speed
MUJAHID RAZA 18
MySQL is considered one of the very fast database languages, backed by a large number
of the benchmark test.
High Flexibility
MySQL supports a large number of embedded applications, which makes MySQL very
flexible.
MySQL is compatible to run on many operating systems, like Novell NetWare, Windows*
Linux*, many varieties of UNIX* (such as Sun* Solaris*, AIX, and DEC* UNIX), OS/2,
FreeBSD*, and others. MySQL also provides a facility that the clients can run on the same
computer as the server or on another computer (communication via a local network or
the Internet).
Allows roll-back
Memory efficiency
Its efficiency is high because it has a very low memory leakage problem.
High Performance
MySQL is faster, more reliable, and cheaper because of its unique storage engine
architecture. It provides very high-performance results in comparison to other databases
without losing an essential functionality of the software. It has fast loading utilities
because of the different cache memory.
High Productivity
MySQL uses Triggers, Stored procedures, and views that allow the developer to give
higher productivity.
Platform Independent
It can download, install, and execute on most of the available operating systems.
Partitioning
MUJAHID RAZA 19
This feature improves the performance and provides fast management of the large
database.
GUI Support
MySQL provides a unified visual database graphical user interface tool named "MySQL
Workbench" to work with database architects, developers, and Database
administration, backup, and many more. MySQL has a fully GUI supports from MySQL
Server version 5.6 and higher.
corporation. Corporation.
MySQL follows the working of Client-Server Architecture. This model is designed for the
end-users called clients to access the resources from a central computer known as a
server using network services. Here, the clients make requests through a graphical user
interface (GUI), and the server will give the desired output as soon as the instructions are
matched. The process of MySQL environment is the same as the client-server model.
Application of MySQL :
• MySQL used in E-Commerce websites.
• MySQL used in Data Warehousing.
MUJAHID RAZA 21
Disadvantages/Drawback of MySQL
Following are the few disadvantages of MySQL:
o MySQL version less than 5.0 doesn't support ROLE, COMMIT, and stored
procedure.
o MySQL does not support a very large database size as efficiently.
o MySQL doesn't handle transactions very efficiently, and it is prone to data
corruption.
o MySQL is accused that it doesn't have a good developing and debugging tool
compared to paid databases.
o MySQL doesn't support SQL check constraints.
We can determine the data type in MySQL with the following characteristics:
MySQL supports a lot number of SQL standard data types in various categories. It uses
many different data types that can be broken into the following categories: numeric, date
and time, string types, spatial types, and JSON data types.
5.6M
635
Features of Java - J
Next
supports BIT datatype to store bit values. In MySQL, numeric data types are categories
into two types, either signed or unsigned except for bit data type.
The following table contains all numeric data types that support in MySQL:
The following table illustrates all date and time data types that support in MySQL:
The following table illustrates all string data types that support in MySQL:
MUJAHID RAZA 24
The following table shows all Binary Large Object data types that support in MySQL:
The JSON data type has the following advantages over storing JSON-format strings in a
string column:
SQL Commands
o SQL commands are instructions. It is used to communicate with the database. It is
also used to perform specific tasks, functions, and queries of data.
o SQL can perform various tasks like create a table, add data to tables, drop the table,
modify the table, set permission for users.
SQL COMMANDS
o DDL changes the structure of the table like creating a table, deleting a table, altering
a table, etc.
o All the command of DDL are auto-committed that means it permanently save all
the changes in the database.
2. DROP :- It is used to delete both the structure and record stored in the table
3. ALTER :- It is used to alter the structure of the database. Allow you to add,
modify and delete columns of an existing table.
4. TRUNCATE :- It is used to drop all the rows from the table. It is similar to the
delete statement with no where clause.
o DML commands are used to modify the database. It is responsible for all form of
changes in the database.
o The command of DML is not auto-committed that means it can't permanently save
all the changes in the database. They can be rollback.
2. UPDATE :- This command is used to update or modify the value of a column in the
table.
UPDATE table_name
SET [column_name1= value1,...column_nameN = valueN]
[WHERE CONDITION]
These operations are automatically committed in the database that's why they cannot be
used while creating tables or dropping them.
1. Commit :- Commit command is used to save all the transactions to the database.
2. Rollback :- Rollback command is used to undo transactions that have not already
been saved to the database.
SAVEPOINT SAVEPOINT_NAME;
SELECT :- This is the same as the projection operation of relational algebra. It is used to
select the attribute based on the condition described by WHERE clause.
SELECT expressions
FROM TABLES
WHERE conditions;
city VARCHAR(50)
);
# Modify existing column in a table or change of name column or increase lenght of name
COLUMN
alter table employees modify column name varchar(100);
# Rename the column name to emp_name
alter table employees rename column name to emp_name;
# Delete existing column from given table or remove DOB column from employee table.
MUJAHID RAZA 32
name salary
mujahid 10000
ali 20000
kamran 11000
azam 12000
maya 17000
MUJAHID RAZA 33
# Print unique hiring_date from the employees table when employees joined it.
select distinct(hiring_date) from employees;
select distinct(hiring_date) as distinct_hiring_date from employees;
distinct_hiring_date
12-08-2021
17-08-2021
19-08-2021
total_unique_age
5
# Increment salary of each employee by 20% display final result with new salary
select id,
name,
salary as old_salary,
(salary + salary*0.2) as new_salary
from employees;
# Update command.
set SQL_SAFE_UPDATES = 0;
# Update the salary of employee who joined the company on 2021-08-17 to 80000
update employees set salary = 80000 where hiring_date = '2021-08-12';
select * from employees;
MySQL Constraints
The constraint in MySQL is used to specify the rule that allows or restricts
what values/data will be stored in the table. They provide a suitable method
to ensure data accuracy and integrity inside the table. It also helps to limit the
type of data that will be inserted inside the table. If any interruption occurs
between the constraint and data action, the action is failed.
1. Column Level Constraints: These constraints are applied only to the single column
that limits the type of particular column data.
2. Table Level Constraints: These constraints are applied to the entire table that
limits the type of data for the whole table.
• NOT NULL
• CHECK
• DEFAULT
• PRIMARY KEY
• AUTO_INCREMENT
• UNIQUE
• INDEX
• ENUM
• FOREIGN KEY
MUJAHID RAZA 36
1. This constraint specifies that the column cannot have NULL or empty values.
2. The NOT NULL constraint enforces a field to always contain a value. This means
that you cannot insert a new record, or update a record without adding a value to
this field.
UNIQUE Constraint
1. This constraint ensures that all values inserted into the column will be unique. It
means a column cannot stores duplicate values.
2. The UNIQUE and PRIMARY KEY constraints both provide a guarantee for
uniqueness for a column or set of columns.
3. A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on
it.
4. Note that you can have many UNIQUE constraints per table, but only one
PRIMARY KEY constraint per table.
CHECK Constraint
It controls the value in a particular column. It ensures that the inserted value in a column
must be satisfied with the given condition.
1. The CHECK constraint is used to limit the value range that can be placed in a
column.
2. If you define a CHECK constraint on a single column it allows only certain values
for this column.
3. If you define a CHECK constraint on a table it can limit the values in certain
columns based on values in other columns in the row.
2. The default value will be added to all new records, if no other value is specified.
MUJAHID RAZA 38
AUTO INCREMENT
ENUM Constraint
The ENUM data type in MySQL is a string object. It allows us to limit the value chosen from
a list of permitted values in the column specification at the time of table creation. It is short
for enumeration, which means that each column may have one of the specified possible
The following illustration creates a table named "shirts" that contains three columns: id,
name, and size. The column name "size" uses the ENUM data type that contains small,
id name size
1 t-shirt medium
2 casual-shirt small
3 formal-shirt large
Operators
In simple operator can be defined as an entity used to perform operations in a table.
Operators are the foundation of any programming language. We can define operators
as symbols that help us to perform specific mathematical and logical computations on
operands. In other words, we can say that an operator operates the operands.
• Arithmetic operator
• Comparison operator
• Logical operator
Arithmetic Operators
We can use various arithmetic operators on the data stored in the tables. Arithmetic
Operators are:
Operator Description
/ This operator works with the ‘ALL’ keyword and it calculates division operations.
Comparison Operators
Operator Description
= Equal to.
Logical Operators
The Logical operators are those that are true or false. They return true or false values to
combine one or more true or false values.
In SQL, the AND & OR operators are used for filtering the data and getting precise results
based on conditions. The SQL AND & OR operators are also used to combine multiple
conditions. These two operators can be combined to test for multiple conditions in a
SELECT, INSERT, UPDATE, or DELETE statement.
• The AND and OR operators are used with the WHERE clause.
• These two operators are called conjunctive operators.
Operator Description
Logical AND compares two Booleans as expressions and returns true when
AND
both expressions are true.
Logical OR compares two Booleans as expressions and returns true when one
OR
of the expressions is true.
Not takes a single Boolean as an argument and change its value from false
NOT
to true or from true to false.
Special Operators
Operators Description
SOME operator evaluates the condition between the outer and inner
SOME tables and evaluates to true if the final result returns any one row. If not,
then it evaluates to false.
UNIQUE The UNIQUE operator searches every unique row of a specified table.
Arithmetic Operators
Table :- employee
Comparison Operators
Query :- list all employees who are getting salary more than 12000;
select * from employee where salary > 12000;
Query :- list all employees who are getting salary more than or equal to 20000;
select * from employee where salary >= 20000;
Query :- list all employees who are getting salary less than 12000;
select * from employee where salary < 12000;
Query :- list all employees who are getting salary less than or equal to 12000;
select * from employee where salary <= 12000;
Logical Operators
Query :- find the employee who join the company on '2021-08-17' and their salary is
less than 11500
select * from employee where hiring_date = '2021-08-17' and salary < 11500;
MUJAHID RAZA 47
Query :- find the employee who join the company after '2021-08-17' or their salary is
less than 15000
select * from employee where hiring_date > '2021-08-17' or salary < 15000;
Query :-
select * from employee where not city = 'agra';
Between Operator
The SQL BETWEEN condition allows you to easily test if an expression is within a range
of values (inclusive). The values can be text, date, or numbers. It can be used in
a SELECT, INSERT, UPDATE, or DELETE statement.
Query :- Get all employees date who joined the company between hiring_date
'2021-08-15' to '2021-08-20'
Query :- Get all employees date who are getting salary in the range of 15000 to 28000.
Like Operator
LIKE operator is used to perform pattern matching to find the correct result. It is used in SELECT,
INSERT, UPDATE and DELETE statement with the combination of WHERE clause.
Query :- Get all the employees whose name start with 'A'
select * from employee where name like 'A%';
Query :- Get all the employees whose name end with 'x'
select * from employee where name like '%x';
id name age hiring_date salary city
Query :- Get all the employees whose name start with 'R' and end with 'a'
select * from employee where name like 'R%a';
id name age hiring_date salary city
Query :- Get all those employees whose name will exact 4 characters
select * from employee where name like '____';
Table : - persons
IN Operator
The MySQL IN condition is used to reduce the use of multiple OR conditions in a SELECT,
INSERT, UPDATE and DELETE statement.
SELECT *
FROM table
WHERE column_name IN (value1, value2…………);
Table :- employee
Query :-
SELECT *
FROM employee
Keys
Keys are one of the basic requirements of a relational database model. It is widely used to
identify the tuples(rows) uniquely in the table. We also use keys to set up relations
amongst various columns and tables of a relational database.
Primary Key:
There can be more than one candidate key in students table out of which one can be
chosen as the primary key. For Example, stud_id, as well as phone, are candidate keys for
students table but stud_id can be chosen as the primary key (only one out of many
candidate keys).
• It is a unique key.
• MySQL does not allow us to insert a new row with the existing primary key.
• It is recommended to use INT or BIGINT data type for the primary key column.
It is used to identify each record in a It also determines each row of the table
table uniquely. uniquely in the absence of a primary key.
It does not allow to store a NULL value It can accept only one NULL value into
into the primary key column. the unique key column.
A table can have only one primary key. A table can have more than one unique
key.
Candidate Key :-
A part from primary key if there is any column or set of column which can uniquely identify
record.
The remaining properties, with the exception of the primary key, are considered candidate
keys. Candidates have the same strength as the primary key.
Take a look at the “students” table as an example. Stud_id, stud_name, address and
phone are the four attributes in this table. Stud_id and phone will have unique values,
however, stud_name and address may contain duplicate values because multiple students
Super Key :-
The set of attributes that can uniquely identify a tuple is known as Super Key. A super key
is a group of single or multiple keys that identifies rows in a table. It supports NULL
values.
• Adding zero or more attributes to the candidate key generates the super key.
• A candidate key is a super key but vice versa is not true.
For example, (stud_id, stud_name) in the following students database, two students’
names can be the same, but their stud_id cannot
Alternate Key :-
The candidate key other than the primary key is called an alternate key.
• All the keys which are not primary keys are called alternate keys.
• It is a secondary key.
• It contains two or more fields to identify two or more records.
• These values are repeated.
MUJAHID RAZA 55
Example : - In the students table, stud_id and phone are candidate key for student but phone will
be an alternate key.
MUJAHID RAZA 56
Foreign Key :-
The foreign key is used to link one or more than one table together. It is also known as
the referencing key. A foreign key matches the primary key field of another table. It means
a foreign key field in one table refers to the primary key field of the other table. It identifies
each row of another table uniquely that maintains the referential integrity in MySQL.
A foreign key makes it possible to create a parent-child relationship with the tables. In this
relationship, the parent table holds the initial column values, and column values of child table
reference the parent column values. MySQL allows us to define a foreign key constraint on
• It is a key it acts as a primary key in one table and it acts as secondary key in
another table.
• It combines two or more relations (tables) at a time.
Syntax
Following are the basic syntax used for defining a foreign key using CREATE TABLE OR ALTER
TABLE statement in the MySQL:
[CONSTRAINT constraint_name]
FOREIGN KEY [foreign_key_name] (col_name, ...)
ON UPDATE referenceOption
Constraint_name :- It specifies the name of the foreign key constraint. If we have not
provided the constraint name, MySQL generates its name automatically.
Col_name :- It is the names of the column that we are going to make foreign key.
MUJAHID RAZA 57
Parent_tbl_name :- It specifies the name of a parent table followed by column names that
Refrence_option :- It is used to ensure how foreign key maintains referential integrity using
ON DELETE and ON UPDATE clause between parent and child table.
MySQL contains five different referential options, which are given below:
CASCADE :- It is used when we delete or update any row from the parent table, the values
of the matching rows in the child table will be deleted or updated automatically.
SET NULL :- It is used when we delete or update any row from the parent table, the values
of the foreign key columns in the child table are set to NULL.
RESTRICT :- It is used when we delete or update any row from the parent table that has a
matching row in the reference(child) table, MySQL does not allow to delete or update rows
in the parent table.
NO ACTION :- It is similar to RESTRICT. But it has one difference that it checks referential
SET DEFAULT :- The MySQL parser recognizes this action. However, the InnoDB and NDB
tables both rejected this action.
('Rahul' ,30);
EER Diagram :-
The "PersonID" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
The FOREIGN KEY constraint prevents invalid data from being inserted into the foreign key
column, because it has to be one of the values contained in the parent table.
on the tables. This variable is dynamic in nature so that it supports global and session scopes
both.
Sometimes there is a need for disabling the foreign key checking, which is very useful when:
SET foreign_key_checks = 0;
SET foreign_key_checks = 1;
Composite Key :-
Sometimes, a table might not have a single column/attribute that uniquely identifies all the
records of a table. To uniquely identify rows of a table, a combination of two or more
columns/attributes can be used. It still can give duplicate values in rare cases. So, we need
to find the optimal set of attributes that can uniquely identify rows in a table.
Example:
Any attribute of Primary key can not While in Candidate key any attribute can
2.
contain NULL value. contain NULL value.
Primary key can be optional to specify But without candidate key there can’t be
3.
any relation. specified any relation.
Primary key specifies the important Candidate specifies the key which can
4.
attribute for the relation. qualify for primary key.
Its confirmed that a primary key is a But Its not confirmed that a candidate
5.
candidate key. key can be a primary key.
MUJAHID RAZA 62
It uniquely identifies a record in the It refers to the field in a table which is the primary key of
relational database table. another table.
It is a combination of UNIQUE and It can contain duplicate values and a table in a relational
Not Null constraints. database.
It does not allow NULL values. It can also contain NULL values.
Clauses
• Where clause
• Distinct
• From
• Order By
• Group By
• Having
Where clause :-
WHERE Clause is used with SELECT, INSERT, UPDATE and DELETE clause to filter the
Syntax:
SELECT expressions
FROM tables
[WHERE conditions];
Table : - orders_data
select * from orders_data where state = 'WDC' and order_id < 110;
Distinct Clause :-
MySQL DISTINCT clause is used to remove duplicate records from the table and fetch only
the unique records. The DISTINCT clause is only used with the SELECT statement.
Syntax:
FROM Clause :-
The MySQL FROM Clause is used to select some records from a table. It can also be used
to retrieve records from multiple tables using JOIN condition.
ORDER BY Clause :-
ORDER BY Clause is used to sort the records in ascending or descending order.
Syntax:
SELECT expressions
FROM tables
[WHERE conditions]
ORDER BY expression [ ASC | DESC ];
GROUP BY Clause :-
The Group By statement is used for organizing similar data into groups. The data is further
organized with the help of equivalent function. It means, if different rows in a precise column
have the same values, it will arrange those rows in a group.
The SELECT statement is used with the GROUP BY clause in the SQL query.
o You can also use some aggregate functions like COUNT, SUM, MIN, MAX, AVG etc. on the
grouped column.
Syntax:
Table : - orders_data
Query :- Write a query to find the total order pay by each state
select state,
max(order_pay) as max_order_pay_by_each_state,
min(order_pay) as min_order_pay_by_each_state,
avg(order_pay) as avg_order_pay_by_each_state
from orders_data group by state;
HAVING Clause :-
MySQL HAVING Clause is used with GROUP BY clause. It always returns the rows where
condition is TRUE.
Syntax:
SELECT expression1, expression2, ... expression_n,
aggregate_function (expression)
FROM tables
[WHERE conditions]
Query :- Write a query to find the country where only 1 order was placed.
MUJAHID RAZA 68
country
UK
country.
country states_in_country
INDIA AP,Bihar,Goa,UP
UK London
USA Seattle,WDC
country states_in_country
INDIA UP,Goa,Bihar,AP
UK London
USA WDC,Seattle
country states_in_country
INDIA UP_Goa_Bihar_AP
UK London
USA WDC_Seattle
Query :- Write a query to print all the orders which were placed in 'seattle' or 'Goa'
Wrong way -> select * from orders_data where state = 'Seattle' or state 'Goa';
SET operation :-
1. UNION
2. UNION ALL
3. INTERSECT
4. EXCEPT
The Union Clause is used to combine two separate select statements and produce the result set as
a union of both select statements. It comes with a default feature that removes
the duplicate rows from the result set.
NOTE:
1. The fields to be used in both the select statements must be in the same order, same
number, and same data type.
2. The Union clause produces distinct values in the result set, to fetch the duplicate values
too UNION ALL must be used instead of just UNION.
JOIN UNION
JOIN combines data from many tables based on a SQL combines the result set of two or more SELECT
matched condition between them statements.
It combines data into new columns. It combines data into new rows
The number of columns selected from each table The number of columns selected from each table
may not be the same. should be the same.
Datatypes of corresponding columns selected from The data types of corresponding columns selected
each table can be different. from each table should be the same.
Table :- student
Table :- student2
Query :- We are organising an tournament between college-1 and college-2, we need details of all
students from both college.
--- Case 1 -
--- Case 3 -
stu_id_college1 name
1 Mujahid
2 Raza
3 Amaan
4 Ahmad
5 Siddiqui
6 Ali
8 Zeeshu
10 zubair
stu_id
--- Case 4 - 1
2
select stu_id from student 3
UNION 4
select email from student2; 5
abc@gmail.com
abc5@gmil.com
abc6@gmail.com
abc7@gmail.com
abc4@gmail.com
INTERSECT :-
The INTERSECT operator returns the distinct (common) elements in two sets or
common records from two or more tables. In other words, it compares the result obtained
by two queries and produces unique rows, which are the result returned by both queries.
MUJAHID RAZA 74
EXCEPT :-
The EXCEPT operator returns the unique element from the first table/set, which is not
found in the second table/set. In other words, it will compare the results of two queries and
produces the resultant row from the result set obtained by the first query and not found in
the result set obtained by the second query.
MUJAHID RAZA 75
The following are the rules for a statement that uses the EXCEPT operator:
o The number and order of columns in all the SELECT statements must be the
same.
o The data types of the corresponding columns in both SELECT statements must
be the same or convertible
JOINS
SQL Join statement is used to combine data or rows from two or more tables based on a
common field between them. Different types of Joins are as follows:
• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• FULL JOIN
• NATURAL JOIN
Table :- orders
INNER JOIN :-
The INNER JOIN keyword selects all rows from both the tables as long as the condition is
satisfied. This keyword will create the result-set by combining all rows from both the tables
where the condition satisfies i.e value of the common field will be the same.
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
MUJAHID RAZA 77
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
Query :- Get the customer information for each order , if the value of customer id present in the
order table
select
o.*,c.*
from orders o
inner join customers c
on o.cust_id = c.cust_id;
LEFT JOIN :-
This join returns all the rows of the table on the left side of the join and
matches rows for the table on the right side of the join. For the rows for
which there is no matching row on the right side, the result-set will
contain null. LEFT JOIN is also known as LEFT OUTER JOIN.
select
o.*,c.*
from orders o
left join customers c
on o.cust_id = c.cust_id;
MUJAHID RAZA 78
RIGHT JOIN :-
RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows of the table
on the right side of the join and matching rows for the table on the left side
of the join. For the rows for which there is no matching row on the left side,
the result-set will contain null. RIGHT JOIN is also known as RIGHT OUTER
JOIN.
select
o.*,c.*
from orders o
Right join customers c
on o.cust_id = c.cust_id;
CROSS JOIN :-
MySQL CROSS JOIN is used to combine all possibilities of the two or more tables and returns
the result that contains every row from all contributing tables. The CROSS JOIN is also known
as CARTESIAN JOIN, which provides the Cartesian product of all associated tables. The
Cartesian product can be explained as all rows present in the first table multiplied by all
rows present in the second table. It is similar to the Inner Join, where the join condition is
not available with this clause.
SELECT *
from orders o
Query :- Get the orders information along with customers full details. if oders amount more
than 400.
Natural Join :-
A natural join is a type of join operation that creates an implicit join by combining
tables based on columns with the same name and data type. It is similar to
the INNER or LEFT JOIN, but we cannot use the ON or USING clause with natural join as we
used in them.
Points to remember :-
o There is no need to specify the column names to join.
o The resultant table always contains unique columns.
o It is possible to perform a natural join on more than two tables.
o Do not use the ON clause.
Syntax:
The following is a basic syntax to illustrate the natural join:
SELECT column_names
FROM table_name1
NATURAL JOIN table_name2;
SELECT *
FROM employee
NATURAL JOIN department;
FROM employee
EMP_NAME MANAGER_NAME
Naila Khan
Mark Khan
BSD RAHUL
John Wood
SELF JOIN :-
A SELF JOIN is a join that is used to join a table with itself. In the previous sections, we have
learned about the joining of the table with the other tables using different JOINS, such as
INNER, LEFT, RIGHT, and CROSS JOIN. However, there is a need to combine data with other
data in the same table itself. In that case, we use Self Join.
We can perform Self Join using table aliases. The table aliases allow us not to use the same
table name twice with a single statement. If we use the same table name more than one
time in a single query without table aliases, it will throw an error.
The table aliases enable us to use the temporary name of the table that we are going to
use in the query. Let us understand the table aliases with the following explanation.
MUJAHID RAZA 83
Suppose we have a table named "student" that is going to use twice in the single query.
Syntax
SELECT s1.col_name, s2.col_name...
FROM table1 s1, table1 s2
WHERE s1.common_col_name = s2.common_col_name;
TABLE ;- Student
AND s1.course_id<>s2.course_id;
MUJAHID RAZA 84
Partitioning
1. Horizontal Partitioning
This partitioning split the rows of a table into multiple tables based on our logic. In
horizontal partitioning, the number of columns is the same in each table, but no need to
keep the same number of rows. It physically divides the table but logically treated as a
whole. Currently, MySQL supports this partitioning only.
2. Vertical Partitioning
This partitioning splits the table into multiple tables with fewer columns from the original
table. It uses an additional table to store the remaining columns. Currently, MySQL does
not provide supports for this partitioning.
MUJAHID RAZA 85
Benefits of Partitioning
o It optimizes the query performance. When we query on the table, it scans only the
portion of a table that will satisfy the particular statement.
o It is possible to store extensive data in one table that can be held on a single disk
or file system partition.
o It provides more control to manage the data in your database.
Range Partitioning :-
cust_name VARCHAR(50),
product_id VARCHAR(10),
sales_year int,
amount DECIMAL(10, 2),
Hash Partitioning :-
The data will be distributed across four partitions based on the hash value of
the customer_id. The number of partitions (4 in this case) should be chosen based on the
expected data distribution and performance requirements.
Key Partitioning :-
The data will be divided into six partitions based on the combination of id And
customer_id. The key partitioning is useful when the primary key is a composite key, and
it can help in distributing the data evenly across partitions.
Subpartitioning :-
cust_code VARCHAR(15),
AMOUNT DECIMAL(8,2),
PRIMARY KEY (BILL_NO, sale_date)
)
PARTITION BY RANGE(YEAR(sale_date))
SUBPARTITION BY HASH(TO_DAYS(sale_date))
SUBPARTITIONS 4 (
PARTITION p0 VALUES LESS THAN (1990),
PARTITION p1 VALUES LESS THAN (2000),
PARTITION p2 VALUES LESS THAN (2010),
PARTITION p3 VALUES LESS THAN MAXVALUE
);
SELECT PARTITION_NAME, TABLE_ROWS FROM
INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME='sales_table'
PARTITION_NAME TABLE_ROWS
p0 0
p0 0
p0 0
p0 0
p1 0
p1 0
p1 0
p1 0
p2 0
p2 0
p2 0
p2 0
p3 0
p3 0
p3 0
p3 0
Window functions
A window function in MySQL used to do a calculation across a set of rows that are
related to the current row. The current row is that row for which function evaluation
occurs. Window functions perform a calculation similar to a calculation done by using
the aggregate functions. But, unlike aggregate functions that perform operations on an
entire table, window functions do not produce a result to be grouped into one row. It
means window functions perform operations on a set of rows and produces an
aggregated value for each row. Therefore each row maintains the unique identities.
Window functions applies aggregate and ranking functions over a particular window
(set of rows). OVER clause is used with window functions to define that window. OVER
clause does two things :
Partition Clause :- This clause is used to divide or breaks the rows into partitions, and the
partition boundary separates these partitions.
ORDER BY Clause :- This clause is used to specify the order of the rows within a partition.
SELECT coulmn_name1,
window_function(cloumn_name2)
OVER([PARTITION BY column_name1] [ORDER BY column_name3]) AS new_column
FROM table_name;
window_function = any aggregate or ranking function
column_name1 = column to be selected
coulmn_name2 = column on which window function is to be applied
column_name3 = column on whose basis partition of rows is to be done
new_column = Name of new column
table_name = Name of table
MUJAHID RAZA 91
Window Functions:
Name Description
CUME_DIST () Cumulative distribution value
DENSE_RANK () Rank of current row within its partition, without gaps
FIRST_VALUE () Value of argument from first row of window frame
LAG () Value of argument from row lagging current row within partition
LAST_VALUE () Value of argument from last row of window frame
LEAD () Value of argument from row leading current row within partition
NTH_VALUE () Value of argument from N-th row of window frame
NTILE () Bucket number of current row within its partition.
PERCENT_RANK () Percentage rank value
RANK () Rank of current row within its partition, with gaps
ROW_NUMBER () Number of current row within its partition
shop_id total_sales_count_by_shop
s1 3
s2 2
s3 2
select *,
sum(sales_amount) over
(order by sales_amount desc) as total_sum_of_sales
from shop_sales_data;
select *,
sum(sales_amount) over
(partition by shop_id) as total_sum_of_sales
from shop_sales_data;
select *,
sum(sales_amount) over
(partition by shop_id order by sales_amount desc) as total_sum_of_sales
from shop_sales_data;
sales_date sales_amount
21-08-2022 500
22-08-2022 600
19-08-2022 300
18-08-2022 200
25-08-2022 800
# Query -> Calculate the data wise Rolling Sum and Average of sales.
select *,
sum(sales_amount) over(order by sales_date) as rolling_sum,
avg(sales_amount) over(order by sales_date) as rolling_avg
from sales_data;
MUJAHID RAZA 94
• DENSE_RANK() –
It assigns rank to each row within partition. Just like rank function first row
is assigned rank 1 and rows having same value have same rank. The
difference between RANK() and DENSE_RANK() is that in DENSE_RANK(), for
the next rank after two same rank, consecutive integer is used, no rank is
skipped.
• ROW_NUMBER() –
It assigns consecutive integers to all the rows within partition. Within a
partition, no two rows can have same row number.
25-02-2022 s1 200
15-02-2022 s2 600
16-02-2022 s2 600
16-02-2022 s3 500
18-02-2022 s3 500
19-02-2022 s3 300
select *,
row_number() over(partition by shop_id order by sales_amount desc) as row_num,
rank() over(partition by shop_id order by sales_amount desc) as ranl_val,
DENSE_RANK() over(partition by shop_id order by sales_amount desc) as dense_rank_val
from shop_sales_data;
Query --> Get one employee from each department who is getting maximum salary
(employee can be random if salary is same)
select
tmp.*
from (select *,
row_number() over(partition by dept_name order by salary desc) as row_num
from employees) tmp
where tmp.row_num = 1;
Query --> Get all employee from each department who is getting maximum salary
SELECT
tmp.*
from (select *,
rank() over(partition by dept_name order by salary desc) as rank_num
from employees) tmp
where tmp.rank_num =1;
Query --> Get all top 2 ranked employee from each department who is getting maximum
salary.
SELECT
tmp.*
from (select *,
dense_rank() over(partition by dept_name order by salary desc) as dense_rank_num
from employees) tmp
where tmp.dense_rank_num <=2;
The LEAD() and LAG() function in MySQL are used to get preceding and succeeding
value of any row within its partition. These functions are termed as nonaggregate
Window functions.
• The LAG() function is used to get value from row that precedes the current row.
• The LEAD() function is used to get value from row that succeeds the current row.
Syntax:
LEAD(expr, N, default)
OVER (Window_specification | Window_name)
For LAG() function-
LAG(expr, N, default)
OVER (Window_specification | Window_name)
Parameters used:
1. expr: It can be a column or any built-in function.
2. N: It is a positive value which determine number of rows preceding/succeeding
the current row. If it is omitted in query then its default value is 1.
3. default: It is the default value return by function in-case no row
precedes/succeedes the current row by N rows. If it is missing then it is by default
NULL.
4. OVER(): It defines how rows are partitioned into groups. If OVER() is empty then
function compute result using all rows.
5. Window_specification: It consist of query partition clause which determines how
the query rows are partitioned and ordered.
Table:- sales_data.
sales_date sales_amount
MUJAHID RAZA 99
21-08-2022 500
22-08-2022 600
19-08-2022 300
18-08-2022 200
25-08-2022 800
Lag :-
select *
Lead :-
select *,
lead(sales_amount, 1) over(order by sales_date) as next_day_sales
from sales_data;
• By defining a frame to be all rows from the partition start to the current row, you can
compute running totals for each row.
• By defining a frame as extending N rows on either side of the current row, you can compute
rolling averages
Rows Between :- Define upper bound & lower bound of row to be covered.
Range Between :- Define upper & lower range of value for a column to be covered.
UNBOUNDED PRECEDING :- Here, the frame starts from the first row of a current partition.
UNBOUNDED FOLLOWING :- It specifies the end of the frame at the final row in the partition.
MUJAHID RAZA 101
Table:- sales_data.
sales_date sales_amount
21-08-2022 500
22-08-2022 600
19-08-2022 300
18-08-2022 200
25-08-2022 800
select *,
sum(sales_amount) over(order by sales_date rows BETWEEN 1 PRECEDING and 1
following) as
pre_plus_next_sales_sum
from sales_data;
select *,
sum(sales_amount) over(order by sales_date rows BETWEEN 1 PRECEDING and current
row) as
pre_plus_current_sales_sum
from sales_data;
select *,
sum(sales_amount) over(order by sales_date rows BETWEEN current row and 1 following) as
current_plus_next_sales_sum
from sales_data;
MUJAHID RAZA 102
select *,
sum(sales_amount) over(order by sales_date rows BETWEEN unbounded PRECEDING and
current
row) as rolling_sum_from_upper
from sales_data;
select *,
sum(sales_amount) over(order by sales_date rows BETWEEN current row and unbounded
following) as rolling_sum_from_bottom
from sales_data;
select *,
sum(sales_amount) over(order by sales_date rows BETWEEN unbounded PRECEDING and
unbounded following) as rows_sum
from sales_data;
Range Between:-
Table:- sales_data.
sales_date sales_amount
21-08-2022 500
22-08-2022 600
19-08-2022 300
18-08-2022 200
25-08-2022 800
20-03-2023 900
23-03-2023 200
25-03-2023 300
29-03-2023 250
select *,
sum(sales_amount) over(order by sales_date range between interval '6' day preceding and
current
row) as running_weekly_sum
from sales_data;
In MySQL, every statement or query produces a temporary result or relation. A common table
expression or CTE is used to name those temporary results set that exist within the execution
scope of that particular statement, such as CREATE, INSERT, SELECT, UPDATE, DELETE, etc.
CTE is a subquery that can be self-referencing using its own name. It is also known
as recursive CTE and can also be referenced multiple times in the same query.
• The first select statement is a non-recursive statement, which provides initial rows
for result set.
• UNION [ALL, DISTINCT] is use to add additional rows to previous result set. Use of
ALL and DISTINCT keyword are used to include or eliminate duplicate rows in the
last result set.
• The second select statement is a recursive statement which produces result set
iteratively until the condition provided in WHERE clause is true.
• The result set produced at each iteration take result set produced at previous
iteration as the base table.
• The recursion ends when the recursive select statement doesn’t produce any
additional rows
MUJAHID RAZA 105
Query - Write a query to print the name of department along with the total salary paid in each
department.
dept_name total_salary
Software 120000
HR 170000
IT 40000
with dept_wise_salary as (select dept_id, sum(salary) as total_salary from employees group by dept_id)
dept_name total_salary
Software 120000
HR 170000
IT 40000
Recursive :- n
1
with recursive cte(n) as 2
3
(select 1 union all select n+1 from cte where n<5)
4
select * from cte; 5
n
with recursive generate_numbers as 1
( 2
select 1 as n 3
UNION 4
select n+1 from generate_numbers where n<10 5
) 6
select * from generate_numbers; 7
8
9
10
WITH RECURSIVE
id n
odd_num (id, n) AS 1 1
2 3
(
3 5
SELECT 1, 1 4 7
5 9
union all
SELECT id+1, n+2 from odd_num where id < 5
)
SELECT * FROM odd_num;
MUJAHID RAZA 107
Table :- emp_mgr
( SELECT id, name, manager_id, designation from emp_mgr where name = 'Anna'
UNION
Indexes
An index is a data structure that allows us to add indexes in the existing table. It enables
you to improve the faster retrieval of records on a database table. It creates an entry for
each value of the indexed columns. We use it to quickly find the record without searching
each row in a database table whenever the table is accessed. We can create an index by
using one or more columns of the table for efficient access to the records.
When a table is created with a primary key or unique key, it automatically creates a special
index named PRIMARY. We called this index as a clustered index. All indexes other than
PRIMARY indexes are known as a non-clustered index or secondary index.
To find the name and contact of the user from table contactbooks, generally, we used to execute
the following query:
SELECT mobile_number FROM contactbooks WHERE first_name = 'Martin' AND last_name = 'Stoke';
This query is very simple and easy. Although it finds the phone number and name of the user fast,
the database searches entire rows of the table until it will not find the rows that you want. Assume,
the contact books table contains millions of rows, then, without an index, the data retrieval takes a
lot of time to find the result. In that case, the database indexing plays an important role in returning
the desired result and improves the overall performance of the query.
CREATE INDEX
Generally, we create an index at the time of table creation in the database. The following statement
creates a table with an index that contains two columns col2 and col3.
MUJAHID RAZA 110
col4 VARCHAR(20),
INDEX (col2,col3)
);
If we want to add index in table, we will use the CREATE INDEX statement as follows:
Drop Index
MySQL allows a DROP INDEX statement to remove the existing index from the table. To delete an
index from a table, we can use the following query:
o First, we have to specify the name of the index that we want to remove.
o Second, name of the table from which your index belongs.
Algorithm Option-
COPY:- This algorithm allows us to copy one table into another new table row by row and then
DROP Index statement performed on this new table. On this table, we cannot perform an INSERT
and UPDATE statement for data manipulation.
INPLACE:- This algorithm allows us to rebuild a table instead of copy the original table. We can
perform all data manipulation operations on this table. On this table, MySQL issues an exclusive
metadata lock during the index removal.
MUJAHID RAZA 111
Lock Option
This clause enables us to control the level of concurrent reads and writes during the index removal.
SHARED:- This mode supports only concurrent reads, not concurrent writes. When the concurrent
reads are not supported, it gives an error.
DEFAULT:- This mode can have the maximum level of concurrency for a specified algorithm. It will
enable concurrent reads and writes if supported otherwise enforces exclusive mode.
NONE:- You have concurrent read and write if this mode is supported. Otherwise, it gives an error.
Show Indexes
OR
Key_name: It is the name of an index. If the table contains a primary key, the index name is always
PRIMARY.
Seq_in_index: It is the sequence number of the column in the index that starts from 1.
Collation: It gives information about how the column is sorted in the index. It contains values
where A represents ascending, D represents descending, and Null represents not sorted.
Cardinality: It gives an estimated number of unique values in the index table where the higher
cardinality represents a greater chance of using indexes by MySQL.
MUJAHID RAZA 112
Sub_part: It is a prefix of the index. It has a NULL value if all the column of the table is indexed.
When the column is partially indexed, it will return the number of indexed characters.
NULL: It contains blank if the column does not have NULL value; otherwise, it returns YES.
Index_type: It contains the name of the index method like BTREE, HASH, RTREE, FULLTEXT, etc.
Comment: It contains the index information when they are not described in its column. For example,
when the index is disabled, it returns disabled.
Index_column: When you create an index with comment attributes, it contains the comment for
the specified index.
Visible: It contains YES if the index is visible to the query optimizer, and if not, it contains NO.
o For functional parts, the expression column represents expression for the key part, and
column_name represents NULL.
o For the non-functional part, the expression represents NULL, and column_name represents
the column indexed by the key part.
UNIQUE INDEX
Generally, we use the primary key constraint to enforce the uniqueness value of one or more
columns. But, we can use only one primary key for each table. So if we want to make multiple sets
of columns with unique values, the primary key constraint will not be used.
MySQL allows another constraint called the UNIQUE INDEX to enforce the uniqueness of values in
one or more columns. We can create more than one UNIQUE index in a single table, which is not
possible with the primary key constraint
Unique indexes are used for the maintenance of the integrity of the data present in the table as
well as for fast performance, it does not allow multiple values to enter into the table.
Definition A clustered index is a table where The indexes other than PRIMARY
the data for the rows are stored. indexes (clustered indexes) called a
In a relational database, if the non-clustered index. The non-
table column contains a primary clustered indexes are also known as
key, MySQL automatically secondary indexes.
creates a clustered index
named PRIMARY.
Use for It can be used to sort the record It creates a logical ordering of data
and store the index in physical rows and uses pointers for accessing
memory. the physical data files.
Data It accesses the data very fast. It has slower accessing power in
Accessing comparison to the clustered index.
Storing It stores records in the leaf node It does not store records in the leaf
Method of an index. node of an index that means it takes
extra space for data.
Type of Key It uses the primary key as a It can work with unique constraints
clustered index. that act as a composite key.
Contains in A table can only one clustered A table can contain one or more
Table index. than a non-clustered index.
Views :-
A view is a database object that has no values. Its contents are based on the base table. It
contains rows and columns similar to the real table. In MySQL, the View is a virtual
table created by a query by joining one or more tables. It is operated similarly to the base table
but does not contain any data of its own. The View and table have one main difference that the
views are definitions built on top of other tables (or views). If any changes occur in the
underlying table, the same changes reflected in the View also.
Syntax -
Parameters:
OR REPLACE : It is optional. It is used when a VIEW already exists. If you do not specify this
clause and the VIEW already exists, the CREATE VIEW statement will return an error.
view_name: It specifies the name of the VIEW that you want to create in MySQL.
WHERE conditions: It is also optional. It specifies the conditions that must be met for the
records to be included in the VIEW.
Advantages :-
Simplify complex query :- It allows the user to simplify complex queries. If we are using the
complex query, we can create a view based on it to use a simple SELECT statement instead of
typing the complex query again.
Increases the Re-usability :- We know that View simplifies the complex queries and converts
them into a single line of code to use VIEWS. Such type of code makes it easier to integrate
with our application. This will eliminate the chances of repeatedly writing the same formula in
every query, making the code reusable and more readable.
MUJAHID RAZA 117
Help in Data Security :- It also allows us to show only authorized information to the users and
hide essential data like personal and banking information. We can limit which information users
can access by authoring only the necessary data to them.
Enable Backward Compatibility :- A view can also enable the backward compatibility in legacy
systems. Suppose we want to split a large table into many smaller ones without affecting the
current applications that reference the table. In this case, we will create a view with the same
name as the real table so that the current applications can reference the view as if it were a
table.
dept_name sum(salary)
Software 40000
IT 60000
HR 50000
drop view dept_wise_salary;
create view specify_salary as select emp_id, emp_name, salary from employees where salary > 20000;
Subquery
In SQL a Subquery can be simply defined as a query within another query. In other
words we can say that a Subquery is a query that is embedded in WHERE clause of
another SQL query. Important rules for Subqueries:
• Subqueries can be used with SELECT, UPDATE, INSERT, DELETE statements along
with expression operator. It could be equality operator or comparison operator
• If the main query does not have multiple columns for subquery, then a subquery
relation with the main query, when there is a co-relation the parser takes the
decision on the fly on which query to execute on precedence and uses the
• We cannot use the ORDER BY clause in a subquery, although it can be used inside
o The subqueries make the queries in a structured form that allows us to isolate each
part of a statement.
o The subqueries provide alternative ways to query the data from the table;
otherwise, we need to use complex joins and unions.
o The subqueries are more readable than complex join or union statements.
MUJAHID RAZA 120
Table :- employee
Query :- Write a query to print all those employee records who are getting more salary
then "Anna"
# Wrong way -> select salary from employee where salary > 12000;
select * from employee where salary > (select salary from employee
where name = 'Anna');
select name, city, salary from employee where salary = ( select max(salary)
from employee);
Table :- customer_order_data
Table :- supplier_data
supplier_id sup_country
300 USA
303 UK
Query :- Write a query to find all customer order data where all customers are from
MUJAHID RAZA 121
The CREATE FUNCTION statement is used for creating a stored function and user-
defined functions. A stored function is a set of SQL statements that perform some
operation and return a single value. The function can be used in SQL queries.
Parameters used:
1. function_name :-
It is the name by which stored function is called. The name should not be
same as native(built_in) function. In order to associate routine explicitly with
2. func_parameter :-
It is the argument whose value is used by the function inside its body. You
3. Datatype :-
It is datatype of value returned by function.
4. Characteristics :-
The CREATE FUNCTION statement is accepted only if at least one of the
);
MUJAHID RAZA 124
Query :-
DELIMITER $$
CREATE FUNCTION add_to_col(a int)
returns int
deterministic
begin
declare b int;
set b = a + 10;
return b;
end $$
DELIMITER ;
add_to_col(15)
select add_to_col(15);
25
quantity add_to_col(quantity)
2 12
3 13
4 14
3 13
5 15
2 12
….. …….
Query :-
DELIMITER $$
deterministic
begin
return final_profit;
end $$
delimiter ;
Query :-
DELIMITER $$
returns int
deterministic
begin
declare final_profit int ;
end $$
DELIMITER ;
Query :-
MUJAHID RAZA 127
DELIMITER $$
CREATE FUNCTION int_to_str(a int)
returns varchar(30)
deterministic
begin
declare b varchar(30) ;
set b = a ;
return b ;
end $$
DELIMITER ;
max(sales) min(sales)
912 36
quantity int_to_str(quantity)
2 2
3 3
4 4
3 3
5 5
2 2
…. ……
Query :-
if sales 1 - 100 = Super affordable product
if sales 100 - 300 = Affordable
if sales 300 - 600 = Moderate
if sales 600 above = Expensive
DELIMITER $$
create function mark_sales(sales int)
returns varchar(30)
deterministic
begin
declare flag_sales varchar(30);
MUJAHID RAZA 128
select mark_sales(30);
mark_sales(30)
Super affordable product
select mark_sales(430);
mark_sales(430)
Moderate
sales mark_sales(sales)
408 Moderate
120 Affordable
66 Super affordable product
45 Super affordable product
114 Affordable
55 Super affordable product
314 Moderate
276 Affordable
912 Expensive
667 Expensive
… ……
MUJAHID RAZA 129
Stored procedures are prepared SQL code that you save so you can reuse it over and
over again. So if you have an SQL query that you write over and over again, save it as a
stored procedure and call it to run it. You can also pass parameters to stored procedures
so that the stored procedure can act on the passed parameter values.
Stored Procedures are created to perform one or more DML operations on Database. It
is nothing but the group of SQL statements that accepts some input in the form of
parameters and performs some task and may or may not return a value.
A procedure is called a recursive stored procedure when it calls itself. Most database
systems support recursive stored procedures. But, it is not supported well in MySQL.
DELIMITER &&
CREATE PROCEDURE procedure_name [[IN | OUT | INOUT] parameter_name datatype [,
parameter datatype]) ]
BEGIN
Declaration_section
Executable_section
END &&
DELIMITER ;
CALL procedure_name ( parameter(s))
MUJAHID RAZA 130
Parameter Explanations
IN parameter
It is the default mode. It takes a parameter as input, such as an attribute. When we define
it, the calling program has to pass an argument to the stored procedure. This parameter's
value is always protected.
OUT parameters
It is used to pass a parameter as output. Its value can be changed inside the stored
procedure, and the changed (new) value is passed back to the calling program. It is noted
that a procedure cannot access the OUT parameter's initial value when it starts.
INOUT parameters
It is a combination of IN and OUT parameters. It means the calling program can pass the
argument, and the procedure can modify the INOUT parameter, and then passes the new
value back to the calling program.
We can use the CALL statement to call a stored procedure. This statement returns
the values to its caller through its parameters (IN, OUT, or INOUT).
MUJAHID RAZA 131
Function
The function contains the set of programming statements enclosed by {}. It provides code
reusability and modularity to the program.
Procedure
The procedure is an important and frequently used programming construct for a compiler.
It is used to generate good code for procedure calls and returns.
It does not deal with an expression. It is defined as the set of commands that are executed
in order.
Call We can call a function via the Whereas it is not possible to call a
procedure. procedure via function.
Expression It has to deal with an expression. It does not have to deal with an
expression.
Query :-
DELIMITER $$
create procedure insert_data()
begin
set @var = 10; var
generate_data : loop 10
insert into loop_table values(@var); 11
set @var = @var + 1;
12
if @var = 100 then
13
leave generate_data;
end if; 14
end loop generate_data; 15
end $$ …….
DELIMITER ;
call insert_data()
Table :- student_info
IN parameter example
The following example creates a stored procedure that finds all student_info according
to subject specified by the input parameter subjectName.
DELIMITER &&
CREATE PROCEDURE student_records( IN subjectName varchar(30) )
BEGIN
SELECT * FROM student_info WHERE `subject` = subjectName;
END &&
DELIMITER ;
Suppose that you want to find students enrolled in the Physics, you need to pass an
argument (‘Physics’) to the stored procedure as shown in the following query:
call student_records('Physics');
Now, with the help of the following query, we will create a stored procedure with OUT
parameter which will count the total of a particular subject by providing the subject
name as the parameter.
DELIMITER &&
CREATE PROCEDURE subjects (
IN stud_subject varchar(30),
out total int )
BEGIN
SELECT COUNT(subject) into total FROM student_info
where subject = stud_subject;
END &&
DELIMITER ;
MUJAHID RAZA 134
‘stud-Subject’ is the IN parameter that is the number of subjects we want to count and
‘total’ is the OUT parameter that stores the number of subjects for a particular subject.
call subjects('Maths', @total);
select @total; @total
2
DELIMITER &&
CREATE PROCEDURE counter ( INOUT count int,
IN increment int)
BEGIN
set count = count + increment;
END &&
DELIMITER ;
Here, ‘count’ is the INOUT parameter, which can store and return values and ‘increment’ is the IN
parameter, which accepts the values from user.
@counter
1
@counter
8
MUJAHID RAZA 135
Trigger
Trigger is a statement that a system executes automatically when there is any
modification to the database.
A trigger in MySQL is a set of SQL statements that reside in a system catalog. It is a special
type of stored procedure that is invoked automatically in response to an event. Each
trigger is associated with a table, which is activated on any DML statement such as INSERT,
UPDATE, or DELETE.
A trigger is called a special procedure because it cannot be called directly like a stored
procedure. The main difference between the trigger and procedure is that a trigger is called
automatically when a data modification event is made against a table. In contrast, a stored
procedure must be called explicitly.
Generally, triggers are of two types according to the SQL standard: row-level triggers and
statement-level triggers.
Row-Level Trigger: It is a trigger, which is activated for each row by a triggering statement
such as insert, update, or delete. For example, if a table has inserted, updated, or deleted
multiple rows, the row trigger is fired automatically for each row affected by
the insert, update, or delete statement.
Statement-Level Trigger: It is a trigger, which is fired once for each event that occurs on a
table regardless of how many rows are inserted, updated, or deleted.
1. Before Insert: It is activated before the insertion of data into the table.
2. After Insert: It is activated after the insertion of data into the table.
3. Before Update: It is activated before the update of data in the table.
4. After Update: It is activated after the update of the data in the table.
5. Before Delete: It is activated before the data is removed from the table.
6. After Delete: It is activated after the deletion of data from the table.
When we use a statement that does not use INSERT, UPDATE or DELETE query to change
the data in a table, the triggers associated with the trigger will not be invoked.
• Faster application development. Because the database stores triggers, you do not
have to code the trigger actions into each database application.
• Global enforcement of business rules. Define a trigger once and then reuse it for any
application that uses the database.
• Easier maintenance. If a business policy changes, you need to change only the
corresponding trigger program instead of each application program.
• Improve performance in client/server environment. All rules run on the server before
the result returns.
o MySQL triggers do not allow to use of all validations; they only provide extended
validations. For example, we can use the NOT NULL, UNIQUE, CHECK and FOREIGN
KEY constraints for simple validations.
o Triggers are invoked and executed invisibly from the client application. Therefore, it
isn't easy to troubleshoot what happens in the database layer.
o Triggers may increase the overhead of the database server.
MUJAHID RAZA 137
Naming Conventions
Naming conventions are the set of rules that we follow to give appropriate unique names. It
saves our time to keep the work organize and understandable. Therefore, we must use a
unique name for each trigger associated with a table. However, it is a good practice to
have the same trigger name defined for different tables.
The following naming convention should be used to name the trigger in MySQL:
Thus,
Syntax : -
Delimiter //
CREATE TRIGGER trigger_name
(AFTER | BEFORE) (INSERT | UPDATE | DELETE)
ON table_name FOR EACH ROW
BEGIN
--variable declarations
--trigger code
END; //
We assume that you are habituated with "MySQL Stored Procedures". You can use the
following statements of MySQL procedure in triggers:
• Flow-of-control statements (IF, CASE, WHILE, LOOP, WHILE, REPEAT, LEAVE, ITERATE)
• Condition declarations
• Handler declarations
MUJAHID RAZA 138
The show trigger statement contains several columns in the result set. Let us explain each
column in detail.
o Trigger :- It is the name of the trigger that we want to create and must be unique
within the schema.
o Event :- It is the type of operation name that invokes the trigger. It can be either
INSERT, UPDATE, or DELETE operation.
o Statement :- It is the body of the trigger that contains logic for the trigger when it
activates.
o Timing :- It is the activation time of the trigger, either BEFORE or AFTER. It indicates
that the trigger will be invoked before or after each row of modifications occurs on
the table.
o Created :- It represents the time and date when the trigger is created.
o Definer :- It is the name of a user account that created the trigger and should be in
o Database Collation :- It determines the rules that compare and order the character
string. It is the collation of the database with which the trigger belongs.
MUJAHID RAZA 139
delimiter //
create trigger course_before_insert
before insert
on course for each row
begin
set new.create_date = sysdate();
end; //
delimiter //
create trigger course_before_insert1
before insert
on course1 for each row
begin
declare user_val varchar(50);
set new.create_date = sysdate();
select user() into user_val;
set new.user_info = user_val;
end; //
delimiter //
create trigger course_before_insert2
before insert
on course2 for each row
begin
declare user_val varchar(50);
set new.create_date = sysdate();
select user() into user_val;
set new.user_info = user_val;
insert into ref_course values(sysdate(), user_val);
end; //
record_insert_date record_insert_user
28-08-2023 root@localhost
28-08-2023 root@localhost
28-08-2023 root@localhost
delimiter //
create trigger to_update_others
before insert
on test1 for each row
begin
insert into test2 values('MRA', sysdate(), 78786);
insert into test3 values('MRA', sysdate(), 78786);
end; //
c1 c2 c3
John 28-08-2023 109803
John 28-08-2023 109803
c1 c2 c3
MRA 28-08-2023 78786
c1 c2 c3
MRA 28-08-2023 78786
delimiter //
create trigger to_update_others_table
before insert
on test1 for each row
MUJAHID RAZA 143
begin
update test2 set c1 = 'abc' where c1 = 'MRA';
delete from test3 where c1 = 'MRA';
end; //
insert into test1 values('wood' ,sysdate(), 7728);
select * from test1;
select * from test2;
select * from test3;
AFTER DELETE
The AFTER DELETE Trigger in MySQL is invoked automatically whenever a delete event is fired on the
table.
Restrictions
o We can access the OLD rows but cannot update them in the AFTER DELETE trigger.
o We cannot access the NEW rows. It is because there are no NEW row exists.
o We cannot create an AFTER DELETE trigger on a VIEW.
delimiter //
after delete
begin
end; //
c1 c2 c3
After_delete 28-08-2023 57547
MUJAHID RAZA 144
BEFORE DELETE
BEFORE DELETE Trigger in MySQL is invoked automatically whenever a delete operation is
fired on the table.
delimiter //
create trigger before_delete_trigger
before delete
on test1 for each row
begin
insert into test3 values('After_delete', sysdate(), 57547);
end; //
delete from test1 where c1 = 'john';
select * from test3;
delimiter //
create trigger before_delete_trigger_observation1
before delete
on test11 for each row
begin
insert into test12(c1,c2,c3) values(old.c1,old.c2, old.c3);
end; //
insert into test11 values('Aligarh' ,sysdate(), 5346);
MUJAHID RAZA 145
c1 c2 c3
Aligarh 28-08-2023 5346
Delhi 28-08-2023 3528
Agra 28-08-2023 7756
Noida 28-08-2023 8928
c1 c2 c3
Agra 28-08-2023 7756
delimiter //
create trigger after_delete_trigger_observation2
after delete
on test11 for each row
begin
insert into test12(c1,c2,c3) values(old.c1,old.c2, old.c3);
end; //
c1 c2 c3
Agra 28-08-2023 7756
c1 c2 c3
Agra 28-08-2023 7756
Aligarh 28-08-2023 5346
Aligarh 28-08-2023 5346
MUJAHID RAZA 146
The AFTER UPDATE trigger in MySQL is invoked automatically whenever an UPDATE event is
fired on the table associated with the triggers.
delimiter //
c1 c2 c3
Delhi 28-08-2023 3528
Noida 28-08-2023 8928
c1 c2 c3
New York 28-08-2023 3528
Noida 28-08-2023 8928
c1 c2 c3
Agra 28-08-2023 7756
Aligarh 28-08-2023 5346
Aligarh 28-08-2023 5346
MUJAHID RAZA 147
BEFORE UPDATE
delimiter //
create trigger before_update_trigger_
before update
on test11 for each row
begin
insert into test12(c1,c2,c3) values(new.c1,new.c2, new.c3);
end; //
c1 c2 c3
New York 28-08-2023 3528
Rome 28-08-2023 8928
c1 c2 c3
Agra 28-08-2023 7756
Aligarh 28-08-2023 5346
Aligarh 28-08-2023 5346
Rome 28-08-2023 8928
MUJAHID RAZA 148
CASE Statement
CASE statement in MySQL is used to find a value by passing over conditions whenever
any condition satisfies the given statement otherwise it returns the statement in an else
part. However, when a condition is satisfied it stops reading further and returns the
output.
MySQL CASE statement is a part of the control flow function that provides us to write
an if-else or if-then-else logic to a query. This expression can be used anywhere that uses
a valid program or query, such as SELECT, WHERE, ORDER BY clause, etc.
Features:
• This returns the statement in the else part if none of the stated conditions are true.
CASE Syntax :-
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
Return Type
The CASE expression returns the result depending on the context where it is used. For
example:
We can use the CASE statement in two ways, which are as follows:
The first method is to take a value and matches it with the given statement, as shown
below.
MUJAHID RAZA 149
Syntax
CASE value
WHEN [compare_value] THEN result
END
It returns the result when the first compare_value comparison becomes true. Otherwise,
it will return the else clause.
The second method is to consider a search_condition in the WHEN clauses, and if it finds,
return the result in the corresponding THEN clause. Otherwise, it will return the else clause.
If else clause is not specified, it will return a NULL value.
Syntax
CASE
WHEN [condition] THEN result
END
(like WHERE ) between WHEN and THEN. This includes stringing together
multiple conditional statements using AND and OR.
4. We can include multiple WHEN statements and an ELSE statement to counter
with unaddressed conditions.
Prob :-
MUJAHID RAZA 150
from customer
order by
(case
end);
CustomerName Country
Stom France
Almaas India
Shubham India
Naveen India
Jonh UK
Mark UK
Kane USA
Prob :-
create table student( Stud_id Stud_Name Class Age
101 Khan CS 22
Stud_id int, 121 Khan CS 22
Stud_Name varchar(50), 111 Kumar ME 24
184 Ghoni CV 19
Class varchar(10), 151 Wood EE 26
Age int 167 Peter ME 20
);
Prob :-
MUJAHID RAZA 152
select * ,
case
when course_name = 'Mechanical Engineering' then 'This is my course'
else 'This is not my course'
end as statement
from course_detail;
select * ,
case
when length(course_name) = '4' then 'len 4'
when length(course_name) = '3' then 'len 3'
else 'other length'
end as length_desc
from course_detail;
course_name course_id course_title course_fee course_launch_year length_desc
Computer 101 CS 20000 2002 other length
Science
Electrical 104 EE 30000 2001 other length
Engineering
Mechanical 100 ME 50000 2000 other length
Engineering
Civil 107 CV 20000 2005 other length
Engineering
CVS 111 DS 20000 2014 len 3
NLP 161 DS 29000 2022 len 3
nlpe 176 NL 90000 2012 len 4
fsda 198 DA 27000 2020 len 4
Wdpt 191 WD 70000 2018 len 4
UPDATE course_detail
SET course_name = CASE
WHEN course_name = 'NLP' THEN 'Natural Language Processing'
WHEN course_name = 'fsda' THEN 'Full Stack Data Analytics'
END
WHERE course_name IN ('NLP', 'fsda');
select * from course_detail;
course_name course_id course_title course_fee course_launch_year
Computer Science 101 CS 20000 2002
Electrical Engineering 104 EE 30000 2001
Mechanical Engineering 100 ME 50000 2000
Civil Engineering 107 CV 20000 2005
CVS 111 DS 20000 2014
Natural Language 161 DS 29000 2022
Processing
nlpe 176 NL 90000 2012
Full Stack Data Analytics 198 DA 27000 2020
Wdpt 191 WD 70000 2018
MUJAHID RAZA 154
Date Functions
The most difficult part when working with dates is to be sure that the format of the date
you are trying to insert, matches the format of the date column in the database.
As long as your data contains only the date portion, your queries will work as expected.
However, if a time portion is involved, it gets complicated.
Before talking about the complications of querying for dates, we will look at the most
important built-infunctions for working with dates.
DATE() in MySQL
The DATE() function is used to extract the date part from a DateTime expression. This
function is an inbuilt function in MySQL. The DATE() function in MySQL can be used to
know the date for a given date or a DateTime. The DATE() function takes a date value
as an argument and returns the date. The date argument represents the valid date or
DateTime.
Syntax :
select date('Expression');
The following table lists the most important built-in date functions in MySQL:
Function Description
NOW() Returns the current date and time
CURDATE() Returns the current date
CURTIME() Returns the current time
DATE() Extracts the date part of a date or date/time expression
EXTRACT() Returns a single part of a date/time
MUJAHID RAZA 155
The following table lists the most important built-in date functions in SQL Server:
Function Description
GETDATE() Returns the current date and time
DATEPART() Returns a single part of a date/time
DATEADD() Adds or subtracts a specified time interval from a date
DATEDIFF() Returns the time between two dates
CONVERT() Displays date/time data in different formats
MySQL comes with the following data types for storing a date or a date/time value in
the database:
SQL Server comes with the following data types for storing a date or a date/time value
in the database:
order_id int,
product_name varchar(30),
MUJAHID RAZA 156
orderDate date
);
Query :- Now you want to select the record with an Orderdate of "2022-12-15" from
the above table.
order_id int,
product_name varchar(30),
orderDate datetime
);
product_name date(orderDate)
Sweety Supari 05-12-2022
Chutki 15-12-2022
Gagan 05-12-2022
Pass-Pass 25-12-2022
EXTRACT() :-
DAY_MINUTE
DAY_HOUR
YEAR_MONTH
product_name orderDay
Sweety Supari 5
Chutki 15
Gagan 5
Pass-Pass 25
product_name orderSecond
Sweety Supari 44
Chutki 21
Gagan 1
Pass-Pass 43
DATE_ADD() :-
product_name orderDateModified
Sweety Supari 05-12-2024 13:23
Chutki 15-12-2024 15:45
Gagan 05-12-2024 11:34
Pass-Pass 25-12-2024 09:32
MUJAHID RAZA 159
DATE_SUB() :-
Subtracts a specified time interval from a date. The syntax for DATE_SUB is the same as
DATE_ADD just the difference is that DATE_SUB is used to subtract a given interval of
date.
order_id subDate
101 30-11-2022 13:23
102 10-12-2022 15:45
103 30-11-2022 11:34
101 20-12-2022 09:32
DATEDIFF() :-
Syntax:
DATEDIFF(date1, date2);
date1 & date2- date/time expression
datediff
-20
DATE_FORMAT() :-
Displays date/time data in different formats.
Syntax:
DATE_FORMAT(date,format);
The date is a valid date and the format specifies the output format for the date/time.
The formats that can be used are:
Query :
Getting a formatted year as “2020” from the specified date “2020-11-23”.
SELECT DATE_FORMAT("2020-11-23", "%Y");
Output :
2020
Query :
Getting a formatted month name as “November” from the specified date “2020-11-23”.
SELECT DATE_FORMAT("2020-11-23", "%M");
Output :
November
Query :
Getting a day of the month as a numeric value as “23rd” from the specified date “2020-
11-23”.
SELECT DATE_FORMAT("2020-11-23", "%D");
Output :
23rd
Query :
Getting month day and year as “November 23 2020” from the specified date “2020-11-
23”.
SELECT DATE_FORMAT("2020-11-23", "%M %d %Y");
Output :
November 23 2020
Query :
Getting hour and minute as “12 09” from the specified date and time “2020-11-23
12:09:23”.
SELECT DATE_FORMAT("2020-11-23 12:09:23", "%H %i");
Output :
12 09
DATEPART() :-
The DATEPART() function is used to return a single part of a date/time, such as year,
DATEPART(datepart,date)
MUJAHID RAZA 162
Where date is a valid date expression and datepart can be one of the following:
datepart Abbreviation
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw, w
hour hh
minute mi, n
second ss, s
millisecond ms
microsecond mcs
nanosecond ns
The SQL datepart function extracts parts of a datetime datatype for filtering or
Normalization
Functional Dependency
The functional dependency is a relationship that exists between two attributes. It
typically exists between the primary key and non-key attribute within a table. A
functional dependency is denoted by an arrow (→). If an attribute A functionally
determines B, then it is written as A → B.
A B
------
1 3
2 3
4 0
1 3
4 0
Example-
ABC → AB
ABC → A
ABC → ABC
Example:
ID → Name,
Name → DOB
Normalization :-
A large database defined as a single relation may result in data duplication. This repetition
of data may result in:
o It isn't easy to maintain and update data as it would involve searching many records
in relation.
o Wastage and poor utilization of disk space and resources.
o The likelihood of errors and inconsistencies increases.
So to handle these problems, we should analyze and decompose the relations with
redundant data into smaller, simpler, and well-structured relations that are satisfy
desirable properties. Normalization is a process of decomposing the relations into
relations with fewer attributes.
What is Normalization?
o Insertion Anomaly: Insertion Anomaly refers to when one cannot insert a new
tuple into a relationship due to lack of data.
o Deletion Anomaly: The delete anomaly refers to the situation where the deletion
of data results in the unintended loss of some other important data.
o Updatation Anomaly: The update anomaly is when an update of a single data
value requires multiple rows of data to be updated.
Ensuring Data Consistency: Normalization helps in ensuring that the data in the
database is consistent and accurate. By eliminating redundancy, normalization helps in
MUJAHID RAZA 166
preventing inconsistencies and contradictions that can arise due to different versions of
the same data.
Improved Database Design: Normalization helps in improving the overall design of the
database. By organizing the data in a structured and systematic way, normalization
makes it easier to design and maintain the database. It also makes the database more
flexible and adaptable to changing business needs.
Advantages of Normalization
Disadvantages of Normalization
o You cannot start building the database before knowing what the user needs.
o The performance degrades when normalizing the relations to higher normal forms,
i.e., 4NF, 5NF.
o It is very time-consuming and difficult to normalize relations of a higher degree.
MUJAHID RAZA 167
Normalization works through a series of stages called Normal forms. The normal forms
apply to individual relations. The relation is said to be in particular normal form if it satisfies
constraints.
2NF A relation will be in 2NF if it is in 1NF and all non-key attributes are
fully functional dependent on the primary key.
4NF A relation will be in 4NF if it is in Boyce Codd's normal form and has
no multi-valued dependency.
5NF A relation is in 5NF. If it is in 4NF and does not contain any join
dependency, joining should be lossless.
MUJAHID RAZA 168
EMPLOYEE table:
14 John 7272826385, UP
9064738238
The decomposition of the EMPLOYEE table into 1NF has been shown below:
14 John 7272826385 UP
14 John 9064738238 UP
o In the second normal form, all non-key attributes are fully functional dependent on
Example: Let's assume, a school can store the data of teachers and the subjects they teach.
In a school, a teacher can teach more than one subject.
TEACHER table
25 Chemistry 30
25 Biology 30
47 English 35
83 Math 38
83 Computer 38
To convert the given table into 2NF, we decompose it into two tables:
TEACHER_DETAIL table:
TEACHER_ID TEACHER_AGE
25 30
47 35
83 38
MUJAHID RAZA 170
TEACHER_SUBJECT table:
TEACHER_ID SUBJECT
25 Chemistry
25 Biology
47 English
83 Math
83 Computer
o A relation will be in 3NF if it is in 2NF and not contain any transitive partial
dependency.
o 3NF is used to reduce the data duplication. It is also used to achieve the data
integrity.
o If there is no transitive dependency for non-prime attributes, then the relation must
be in third normal form.
A relation is in third normal form if it holds atleast one of the following conditions for
every non-trivial function dependency X → Y.
1. X is a super key.
2. Y is a prime attribute, i.e., each element of Y is part of some candidate key.
Example:
EMPLOYEE_DETAIL table:
Non-prime attributes: In the given table, all attributes except EMP_ID are non-prime.
That's why we need to move the EMP_CITY and EMP_STATE to the new
<EMPLOYEE_ZIP> table, with EMP_ZIP as a Primary key.
EMPLOYEE table:
EMPLOYEE_ZIP table:
201010 UP Noida
02228 US Boston
60007 US Chicago
06389 UK Norwich
462007 MP Bhopal
MUJAHID RAZA 172
o For BCNF, the table should be in 3NF, and for every FD, LHS is super key.
Example: Let's assume there is a company where employees work in more than one
department.
EMPLOYEE table:
EMP_ID → EMP_COUNTRY
The table is not in BCNF because neither EMP_DEPT nor EMP_ID alone are keys.
To convert the given table into BCNF, we decompose it into three tables:
EMP_COUNTRY table:
EMP_ID EMP_COUNTRY
264 India
264 India
MUJAHID RAZA 173
EMP_DEPT table:
EMP_DEPT_MAPPING table:
EMP_ID EMP_DEPT
D394 283
D394 300
D283 232
D283 549
Functional dependencies:
1. EMP_ID → EMP_COUNTRY
2. EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
Candidate keys:
Now, this is in BCNF because left side part of both the functional dependencies is a key.
o A relation will be in 4NF if it is in Boyce Codd normal form and has no multi-valued
dependency.
MUJAHID RAZA 174
o A relation is in 5NF if it is in 4NF and not contains any join dependency and joining
should be lossless.
o 5NF is satisfied when all the tables are broken into as many tables as possible in