IP XII U2 Ch5 MySQL
IP XII U2 Ch5 MySQL
as per
CBSE
Class XII (CBSE Board) Curriculu
m 2021-
22
Chapter: 5
Relational Database
& Query using
MySQL
Visit
Authoredwww.ip4you.blogspot.com
By:- Rajesh Kumar Mishra, PGT (Comp.Sc.)
Kendriyafor more….
Vidyalaya Khanapara, Guwahati (Assam)
e-mail : [email protected]
Expected Learning
Outcome:
CBSE Syllabus (2021-22) Covered in this presentation:
Math functions: POWER (), ROUND (), MOD ().
Text functions: UCASE ()/UPPER (), LCASE ()/LOWER (), MID
()/SUBSTRING ()/SUBSTR (), LENGTH (), LEFT (), RIGHT (), INSTR (),
LTRIM (), RTRIM (), TRIM ().
Date Functions: NOW (), DATE (), MONTH (), MONTHNAME (), YEAR (),
DAY (), DAYNAME ().
Aggregate Functions: MAX (), MIN (), AVG (), SUM (), COUNT (); using
COUNT (*).
Querying and manipulating data using Group by, Having, Order by.
Value of Admission
Number is unique i.e. it ADM_NO Name Class Stream Subject
can’t be same for any two 1224 Amar 12 Science Comp.Sc
records in the table, even
they can have same 2456 Amar 12 Science Comp.Sc.
values for other columns.
Types of Keys
A table may have several columns having unique value in the
table. For example in a Admission number and Roll number
both are unique in a class.
Even a combination of columns like Name+Father Name or
Name+Date of Birth may have unique value and these group
of columns may also considered as keys.
So, a relation/table may have multiple keys which are having
distinct values and can identify a record uniquely.
As per utility and applicability of Key columns in the Database,
Keys can be categorized as-
Primary Key
Candidate Key
Alternate Key
Foreign key
Visit www.ip4you.blogspot.com for more teaching-learning materials...
Types of Keys
Primary Key : A set of one or more attributes (column) which is
used to identify or search a record in the relation at first preference
is called Primary Key.
Candidate Key : Some times, a table may have multiple keys
which can identify a record in a table. All such columns are called
Candidate Keys because they all are having candidature to become
as Primary key. So, Primary Key is one of the candidate keys.
A table may have several candidate keys but definitely has only one
primary key.
Alternate Key : A Candidate Key that is not a Primary key
is called Alternate key, since it can be used in place of primary
key.
Foreign Key: A Foreign key is non-key column in a table whose
value is derived from the Primary key of some other table. Foreign
Key establishes relationship among two tables. It maintains
Referential integrity, since each time when a record is inserted or
updated in the table, the other table is referenced.
Example of Keys:
Consider the following Student Table containing records of students
in a class. The RollNo, AADHAR_No and Adm_No columns are
having unique values. So, we can consider-
Candidate Keys– Adm_No, AADHAR_No and RollNo
Primary Key – Roll No (Commonly used at first preference)
Alternate Key – Adm_No, Aadhar_No
Adm_No AADHAR_ No RollNo Name Class Marks
2301 45782154 1 Seema Chauhan 12 15
1501 47869852 2 Ajay Kumar 12 23
1678 26845783 3 Vansh Pratap 12 20
7003 85674281 4 Ajay Kumar 12 15
Emp
empID ename city pay
Creating Tables with
Constraints
An Integrity Constraints or Constraints are the rules, condition
or checks applicable to a column or table which ensures the
integrity or validity of data.
The following constraints are commonly used in MySQL.
S.N Constraints Description
1 NOT NULL Ensures that a column cannot have NULL value.
2 DEFAULT Provides a default value for a column, when
nothing is given.
3 UNIQUE Ensures that all values in a column are different.
4 CHECK Ensures that all values in a column satisfy
certain condition.
5 PRIMARY KEY Used to identify a row uniquely.
6 FOREIGN KEY Used to ensure Referential Integrity of the data.
Numeric, String and Date-Time functions are called Single row functions
because they applied on row-to-row basis. When applied on a table,
they return a single result for every row of the queried table.
Aggregate Functions are called Multiple row functions because they
operate on a set of rows to return a single value.
Math or Numeric Functions
These functions may accept some numeric values and
performing required operation, returns numeric values as result.
UPPER() or UCASE()
Converts given string in upper case.
UPPER (Str)
mysql> SELECT UPPER(‘abcD’ ) FROM DUAL;
ABCD
mysql> SELECT UPPER(Name) FROM Student;
mysql> SELECT UCASE(Fname) FROM Student;
Text or String Functions
LTRIM() : Returns string after removing leading spaces.
mysql> SELECT LTRIM(‘ abcd’ ) FROM DUAL;
abcd
mysql> SELECT LTRIM(Name) FROM Student;
SYSDATE()
Returns current date and time as YYYY-MM-DD HH:MM:SS
mysql> SELECT SYSDATE() ;
2014-01-30 10:30:20
NOW()
Returns current date and time as YYYY-MM-DD HH:MM:SS
mysql> SELECT SYSDATE() FROM DUAL
2010-01-30 10:30:20
MONTH()
Returns month of the given date expression.
MONTH (Dt)
mysql> SELECT MONTH(‘2008-12-31’) ; 12
mysql> SELECT MONTH( CURDATE());
Visit www.ip4you.blogspot.com for more teaching-learning materials...
Date & Time
Functions
DAYNAME()
Returns the name of Week day of the given date expression.
DAYNAME (Dt)
mysql> SELECT DAYNAME(‘2008-12-31’) ;
SUNDAY
mysql> SELECT DAYNAME( CURDATE()) ;
mysql> SELECT DAYNAME( DOB) FROM Student;
DAY() / DAYOFMONTH()
Returns day of month of the given date expression.
DAYOFMONTH (Dt)
mysql> SELECT DAYOFMONTH(‘2008-12-31’) ;
31
mysql> SELECT DAY( CURDATE()) ;
mysql> SELECT DAY( DOB) FROM Student;
Date & Time
Functions
DAYOFWEEK()
Returns day of week i.e. 1- Sunday, 2- Tuesday.. etc. of given
date.
DAYOFWEEK (Dt)
mysql> SELECT DAYOFWEEK(‘2008-12-31’) ;
1
mysql> SELECT DAYOFWEEK(CURDATE()) ;
DAYOFYEAR()
Returns the day of year of the given date expression.
DAYOFYAER (Dt)
mysql> SELECT DAYOFYAER(‘2010-02-05’) ;
36
mysql> SELECT DAYOFYAER( CURDATE()) ;
mysql> SELECT DAYOFYEAR( DOB) FROM Student;
Name Purpose
SUM() Returns the sum of given column.
MIN() Returns the minimum value in the given column.
MAX() Returns the maximum value in the given column.
AVG() Returns the Average value of the given column.
COUNT() Returns the total number of values/ records as per given
column.
Aggregate Functions &
Group
An Aggregate function may applied on a column with DISTINCT
or ALL keyword. If nothing is given ALL is assumed.
Using SUM (<Column>)
This function returns the sum of values in given column or
expression.
mysql> Select Sum(Sal) from EMP;
mysql> Select Sum(DISTINCT Sal) from EMP;
mysql> Select Sum (Sal) from EMP where
City=‘Kanpur’;
mysql> Select Sum (Sal) from EMP Group By City;
mysql> Select Job, Sum(Sal) from EMP Group By Job;
Using MIN (<column>)
This functions returns the Minimum value in the given column.
mysql> Select Min(Sal) EMP;
from
mysql> Select Min(Sal) EMP Group By City;
from
mysql> Select Job, from EMP Group By
Aggregate Functions &
Group
Using MAX (<Column>)
This function returns the Maximum value in given column.
mysql> Select Max(Sal) from EMP;
mysql> Select Max(Sal) from EMP where
City=‘Kanpur’;
mysql> Select Max(Sal) from EMP Group By City;
Using AVG (<column>)
This functions returns the Average value in the given column.
mysql> Select AVG(Sal) from EMP;
mysql> Select AVG(Sal) from EMP Group By City;
Using COUNT (<*|column>)
This functions returns the number of rows in the given column.
mysql> Select Count (*) from EMP;
mysql> Select Count(Sal) from EMP Group By City;
mysql> Select Count(DISTINCT Designation) from
EMP; mysql> Select Count(*), Sum(Sal) from EMP
Group By Job;
Aggregate Functions & Conditions (HAVING)