SlideShare a Scribd company logo
Lesson plan
Class – XI (Science & Commerce)
Subject – Informatics Practices (IP)
Topic – UNIT-4 (Database concepts and the Structured Query Language)
No. of sessions – 4
Objectives:
To enable students to
🠶 State what a database is.
🠶 Express the relationship between a database and table.
🠶 Recognize different parts of a table like Row and Column.
🠶 Define DBMS related terms like Primary key, Candidate key, Alternate key etc.
🠶 State what is MySQL.
🠶 State categories of SQL statements.
🠶 Manipulate and manage the Database tables in an efficient way.
Subject Matter:
🠶 Database Concepts
🠶 Relational data model
🠶 SQL
🠶 SQL Languages
Links:
https://www.youtube.com/watch?v=FR4QIeZaPeM
https://drive.google.com/open?id=1PVc_BnKw8RA3zX5AFjwvsd76HWRJAIoN
https://drive.google.com/open?id=1m2TWp6CiRWWqvklRna0tuKHhKS2QXACX
TEACHING PLATFORM:
ZOOM ONLINE CLASSES
SESSION 1
DATABASE CONCEPTS
🠶 Introduction to database concepts
🠶 Need of Database
🠶 Database Management System (DBMS)
🠶 Advantages of DBMS
🠶 Data Models
Introduction to Database Concepts
🠶 A Database is a collection of DATA/INFORMATION that is organized so that it can be
easily accessed, managed and updated.
🠶 In Database, Data is organized into rows, columns and tables, and it is indexed to make
it easier to find relevant information.
🠶 It works like a container which contains the various object like Tables, Queries, Reports
etc. in organized way.
WHY DO WE NEED DATABASE?
🠶 To manage large chunks of data: if size of data increases into thousands of records, it
will simply create a problem to manage. Database can manage large amount of
data.
🠶 Accuracy: Through validation rule in database ,data accuracy can be maintained.
🠶 Ease of updating data: With the database, we can flexibly update the data according
to our convenience. Moreover, multiple people can also edit data at same time.
🠶 Security of data: With databases we have security groups and privileges to restrict
access.
🠶 Data integrity: In databases, we can be assured of accuracy and consistency of data
due to the built in integrity checks and access controls.
DBMS (Database Management System)
🠶 A DBMS refers to a software that is responsible for storing, maintaining and utilizing
database in an efficient way.
🠶 A Database along with DBMS software is called Database System.
🠶 Example of DBMS software are
🠶 Oracle,
🠶 MS SQL Server,
🠶 MS Access,
🠶 Paradox,
🠶 DB2 and
🠶 MySQL etc.
🠶 MySQL is open source and freeware DBMS.
Advantages of Database System
🠶 Databases reduces Redundancy:
It removes duplication of data because data are kept at one place and all the
application refers to the centrally maintained database.
🠶 Database controls Inconsistency:
When two copies of the same data do not agree to each other, then it is called
Inconsistency. By controlling redundancy, the inconsistency is also controlled.
🠶 Database facilitate Sharing of Data:
Data stored in the database can be shared among several users.
🠶 Database ensures Security:
Data are protected against accidental or intentional disclosure to unauthorized
person or unauthorized modification.
🠶 Database maintains Integrity:
It enforces certain integrity rules to insure the validity or correctness of data. For ex.
A date can’t be like 31/31/2000.
Data Model – Way of data representation
Data model is a model or presentation which shows How data is organized ? or stored in
the database. A data is modelled by one of the following given:
🠶 Relational Data Model:
In this model data is organized into Relations or Tables (i.e. Rows and Columns). A
row in a table represents a relationship of data to each other and also called a Tuple
or Record. A column is called Attribute or Field.
🠶 Network Data Model:
In this model, data is represented by collection of records and relationship
among data is shown by Links.
🠶 Hierarchical Data Model:
In this model, Records are organized as Trees. Records at top level is called Root
record and this may contains multiple directly linked children records.
🠶 Object Oriented Data Model:
In this model, records are represented as a objects. The collection of similar types
of object is called class.
FORMATIVE ASSESSMENT
Answer the following questions:
1. Define the following terms:
a) Database
b) DBMS
c) Relational Data Model
2. What is the relationship between a Database and a Table?
3. What is DBMS? Write names of any 4 DBMSs.
4. Write any 4 advantages of Database System.
5. What is Data Model? Write the names of different data models.
6. State the need of a database.
SESSION 2
RECAPITULATION
1. What is Database?
2. Why do we need database?
3. What is DBMS? Give examples of different DBMS software's.
4. What are the advantages of database system?
5. What are the different Data Models?
Relational data model
🠶 RELATIONAL DATABASE
🠶 Relational Database Terms
🠶 Keys in a Database
🠶 Introduction of SQL
🠶 Introduction of MySQL
🠶 MySQL Features
🠶 Types of SQL Commands
RELATIONAL DATABASE
🠶 A Relational database is a collective set of multiple data sets organized by tables, records
and columns.
🠶 Relational database establish a well-defined relationship between database tables.
🠶 Tables communicate and share information, which facilitates data searcheability,
organization and reporting.
🠶 A Relational database use Structured Query Language (SQL), which is a standard user
application that provides an easy programming interface for database interaction.
RELATIONAL DATABASE TERMS
RELATIONAL DATABASE TERMS
🠶 Relation (Table):
A Relation or Table is Matrix like structure arranged in Rows and Columns. It has the
following properties:
Atomicity: Each column assigned a unique name and must have atomic(indivisible) value
i.e. a value that can not be further subdivided.
No duplicity: No two rows of relation will be identical i.e. in any two rows value in at least
one column must be different.
All items in a column are homogeneous i.e. same data type.
Ordering of rows and column is immaterial.
🠶 Domain: It is collection of values from which the value is derived for a column.
🠶 Tuple / Entity / Record: Rows of a table is called Tuple or Record.
🠶 Attribute/ Field: Column of a table is called Attribute or Field.
🠶 Degree: Number of columns (attributes) in a table.
🠶 Cardinality: Number of rows (Records) in a table.
KEYS IN A DATABASE
🠶 Key plays an important role in relational database;
🠶 it is used for identifying unique rows from table & establishes relationship among tables on need.
🠶 Types of keys in DBMS:
1. Primary Key – A primary is a column or set of columns in a table that uniquely identifies tuples
(rows) in that table.
2. Candidate Key – It is an attribute or a set of attributes or keys participating for Primary Key, to
uniquely identify each record in that table.
3. Alternate Key – Out of all candidate keys, only one gets selected as primary key, remaining
keys are known as alternate or secondary keys.
4. Foreign Key – Foreign keys are the columns of a table that points to the primary key of
another table. They act as a cross-reference between tables.
KEYS IN A DATABASE
Introduction of SQL
🠶 SQL is an acronym of Structured Query Language.
🠶 It is a standard language developed and used for accessing and modifying relational
databases.
🠶 The SQL language was originally developed at the IBM research laboratory in San José, in
connection with a project developing a prototype for a relational database management
system called System R in the early 70s.
🠶 SQL is being used by many database management systems. Some of them are:
🠶 MySQL
🠶 PostgreSQL
🠶 Oracle
🠶 SQLite
🠶 Microsoft SQL Server
Introduction of MySQL
🠶 MySQL is currently the most popular open source database software.
🠶 It is a multi-user, multithreaded database management system.
🠶 MySQL is especially popular on the web.
🠶 It is one of the parts of the very popular LAMP platform.
🠶 MySQL AB was founded by Michael Widenius (Monty), David Axmark and Allan
Larsson in Sweden in year 1995.
MySQL Features
🠶 Open Source & Free of Cost:
It is Open Source and available at free of cost.
🠶 Portability:
Small enough in size to install and run it on any types of Hardware and OS like Linux,MS
Windows or Mac etc.
🠶 Security:
Its Databases are secured & protected with password.
🠶 Connectivity:
Various APIs are developed to connect it with many programming languages.
🠶 Query Language:
It supports SQL (Structured Query Language) for handling database.
Types of SQL Commands
🠶 DDL (Data Definition Language):
To create database and table structure-commands like CREATE , ALTER , DROP etc.
🠶 DML (Data Manipulation Language):
Record/rows related operations. commands like SELECT...., INSERT..., DELETE..., UPDATE....
etc.
🠶 DCL (Data Control Language):
Used to control the transactions. commands like COMMIT, ROLLBACK, SAVEPOINT etc.
🠶 TCL (Transactional control Language):
Used to manipulate permissions or access rights to the tables. commands like GRANT ,
REVOKE etc.
FORMATIVE ASSESSMENT
MCQ’s:
1. The vertical subset of a table is known as
a) Tuple b) Row c) Attribute d) Relation
2. Which of the following columns in a Student table can be used as the primary key?
a) Class b) Section c) First Name d) Admission No
3. A tuple is also known as a _________________.
a) table b) relation c) row d) field
4. An attribute is also known as a _________________.
a) table b) relation c) row d) column
5. A relation can have only one ________ key and may have more than one _______ keys.
a) Primary, Candidate b) Candidate, Alternate
c) Candidate, Primary d) Alternate, Candidate
SESSION 3
RECAPITULATION
1. What is MySQL? What are the features of MySQL?
2. What is a Primary key? What is its purpose in a table?
3. How is data organized in a table?
4. Distinguish between Primary key and Candidate key.
5. What are the different types of SQL commands?
6. What are the different keys in a Database?
7. What are the different Relational database terms?
MySQL Datatypes
Data Types
NUMERIC TEXT DATE TIME
INT DECIMAL
CHAR VARCHAR
MySQL Datatypes
Class Data Type Description Example
Numeric
INT
Or INTEGER
It is used for storing integer values. You can specify a width up to 11
digits.
76
DECIMAL(size,
d)
It can represent number with or without the fractional part. The
maximum number of digits may be specified in the size parameter.
The maximum number of digits to the right of the decimal point is
specified in the d parameter.
17.32
Text
CHAR(size)
A fixed-length string from 1 to 255 characters in length right-padded
with spaces to the specified length when stored.
Values must be enclosed in single quotes or double quotes.
'Mathsā€˜
"Text"
VARCHAR(size)
A variable-length string from 1 to 255 characters in length; for example
VARCHAR(25).Values must be enclosed in single quotes or double
quotes.
'Computerā€˜
"Me and u"
Date DATE
It represents the date including day, month and year
Note: Range of years 1000 to 9999
'2009-07-02’
Time TIME
It represents time. Format: HH:MM:SS
Note: The supported range is from '-838:59:59' to '838:59:59'
10:30:12
Categories of SQL Commands
SQL commands can be classified into the following categories:
1. Data Definition Language (DDL) Commands:
The DDL part of SQL permits database tables to be created or deleted. It also defines indices
(keys), specifies links between tables, and imposes constraints on tables. Examples of DDL
commands in SQL are:
ā– CREATE DATABASE - creates a new database
ā– CREATE TABLE - creates a new table
ā– ALTER TABLE - modifies a table
ā– DROP TABLE - deletes a table
2. The Data Manipulation Language (DML) Commands:
The query and update commands form the DML part of SQL: Examples of DDL commands are:
ā– INSERT INTO - inserts new data into a table
ā– SELECT - extracts data from a table
ā– UPDATE - updates data in a table
ā– DELETE - deletes data from a table
Data Definition Language (DDL) Commands
1. CREATE DATABASE:
🠶 Ms. Sujata is a Class teacher of Class XI. She wants to store data of her students i.e. Names
and marks secured, in a database.
🠶 A database is used to house data in the form of tables.
🠶 To display list of existing Databases:
mysql> SHOW DATABASES;
🠶 To create a new Database:
mysql> CREATE DATABASE School;
🠶 To open an existing Database:
mysql> USE School;
🠶 To check the currently opened Database name:
mysql> SELECT DATABASE();
🠶 To display existing tables list in a database:
mysql> SHOW tables;
Data Definition Language (DDL) Commands
2. CREATE TABLE:
After creating a database, the next step is creation of tables in the database. For this CREATE
TABLE statement is used.
Syntax:
CREATE TABLE <table-name> (< column name><data type> [<size>],
(< column name><data type> [<size>], …);
Example:
CREATE TABLE Learner(RollNo INTEGER, Name VARCHAR(25));
Try this out:
Create table student with following columns by using appropriate data types:
Roll no, Name, Gender, Marks
------------------------------------------------------------------------------------------------------------------------------------
Note: When naming tables and columns, be sure to keep it simple with letters and numbers.
Spaces and symbols are invalid characters except for underscore(_). Column names like
first_name, last_name, email are valid column names.
Data Definition Language (DDL) Commands
Viewing Structure of Table:
🠶 The DESCRIBE statement can be used to see the structure of a table as indicated in the
Create Statement.
🠶 It displays the Column names, their data types, whether Column must contain data,
whether the Column is a Primary key etc.
🠶 Syntax:
🠶 Example:
DESCRIBE <table name>;
OR
DESC <table name>;
DESCRIBE Student;
OR
DESC Student;
Data Definition Language (DDL) Commands - Changing Structure of table
3. ALTER TABLE:
🠶 When we create a table we define its structure.
🠶 We can also change its structure i.e. add, remove or change its column(s) using the
ALTER TABLE statement.
🠶 Syntax:
🠶 Example: Ms. Sujata adds a column named Games.
Now, suppose we want to change the newly added Games column to hold integers(in place of
character data) using ALTER TABLE statement:
Ms. Sujata deletes the Games column using the ALTER TABLE statement:
ALTER TABLE <table_name> ADD/DROP <column_name> [datatype];
ALTER TABLE <table_name> MODIFY <column_name> <new_definition>;
ALTER TABLE Student ADD Games VARCHAR(20);
ALTER TABLE Student MODIFY Games INTEGER;
ALTER TABLE Student DROP Games;
Data Definition Language (DDL) Commands - Removing Structure of table
3. DROP TABLE:
🠶 Sometimes there is a requirement to remove a table from the database.
🠶 In such cases we don't want merely to delete the data from the table, but we want to
delete the table itself. DROP TABLE command is used for this purpose.
🠶 Syntax:
🠶 Example:
DROP TABLE <tablename>;
DROP TABLE Student;
FORMATIVE ASSESSMENT
MCQ’s:
1. A database
a. Contains tables b. Is a part of a table
c. Is same as a table d. Removes data from a table
2. Number of columns in a table is called
a. Power b. Degree
c. Cardinality d. Design
3. USE <databasename> command
a. is not used in MySQL b. is given before quitting MySQL
c. opens a table d. opens a database
4. Number of rows in a table is called
a. Power b. Degree
c. Cardinality d. Design
5. A table can have
a. Only one candidate key b. Only one primary key
c. Only one alternate key d. No alternate key
FORMATIVE ASSESSMENT
6. Which of the following are valid column names?
a. Marks Eng b. 66_Marks c. Marks_Eng d. #Eng_Marks
7. DDL is
a. a part of SQL b. a part of DML
c. a part of DCL d. None of the above
8. A Primary key column
a. can have NULL values b. can have duplicate values
c. Both (a) and (b) d. Neither (a) nor (b)
9. Which of the following columns in a Student table can be used as the primary key?
a. Class b. Section
c. First Name d. Adm_No
10. Which SQL statement is used to delete table from database?
a. DELETE b. DROP
c. REMOVE d. TRUNCATE
SESSION 4
RECAPITULATION
1. How is a database related to table(s)?
2. Which SQL statement is used to view names of all the tables contained in the current
database?
3. List two categories into which MySQL statements can be categorized. Give examples of 2
statements in each category.
4. What happens if you give a CREATE TABLE statement to create a table named 'Item' and a
table named 'Item' already exists?
5. Write any 4 things that are displayed when you display the structure of a table using DESCRIBE
statement.
6. Which statement is used to remove a column from a table?
7. What are the different data types used in MySQL?
8. What is the difference between CHAR and VARCHAR data types?
Data Manipulation Language (DML) Commands
1. INSERT INTO:
🠶 To add data in the table, which is also known as populating table with rows.
🠶 To add row(s) in the table the INSERT INTO statement can be used.
Syntax:
INSERT INTO <TableName> VALUES (<Value1>,<Value2>,… ,<ValueN>);
Example:
INSERT INTO Student VALUES (1,'Siddharth Sehgal','M',93);
INSERT INTO Student VALUES (2,'Gurpreet Kaur','F',91);
Try this out:
Insert the following records in the table student:
3,'Monica Rana','F',93
4,'Jatinder Sen','M',78
5,'George Jacob','M',76
Data Manipulation Language (DML) Commands
2. SELECT:
🠶 SELECT statement is used to view the contents of the table.
🠶 It is used to retrieve or fetch data from one or more database tables.
🠶 RETRIEVING ALL COLUMNS:
Syntax:
SELECT * FROM <TableName>;
Example:
SELECT * FROM Student;
🠶 RETRIEVING SINGLE COLUMN:
SELECT statement to retrieve a single column from a table.
Syntax:
SELECT <column name> FROM <TableName>;
Example:
SELECT Name FROM Student;
Data Manipulation Language (DML) Commands
🠶 RETRIEVING MULTIPLE COLUMNS:
SELECT statement to retrieve multiple columns from a table.
Syntax:
SELECT <column name1>,<column name2> FROM <table name>;
Example:
SELECT Rollno, Name FROM Student;
Data Manipulation Language (DML) Commands
ELIMINATING DUPLICATE VALUES:
🠶 By default data is displayed from all the rows of the table, even if the data in the
result is duplicated.
🠶 Using the keyword DISTINCT, the duplicate values can be eliminated in the result.
🠶 When DISTINCT keyword is specified, only one instance of the duplicated data is
shown.
Syntax:
SELECT DISTINCT <column name1> FROM <table name>;
Example:
SELECT DISTINCT Marks1 FROM Student;
Data Manipulation Language (DML) Commands
USING OPERATORS WITH SELECT:
1. Using Arithmetic Operators-
🠶 Arithmetic operators perform mathematical calculations.
🠶 In SQL the following arithmetic operators are used:
🠶 Examples:
SELECT Marks1+5 FROM Student; SELECT 7*3+1;
SELECT Name,Marks1-10 FROM Student ; SELECT 71+34;
SELECT Name,Marks1/2 FROM Student ; SELECT 86-21;
Operator What it does
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (or remainder)
Data Manipulation Language (DML) Commands
🠶 Using Column Alias:
⮚ Column alias lets different name (heading) to appear for a column than the actual one in the
output.
⮚ Example:
⮚ Using Column Alias does not rename a column. It simply displays a different column name in the
output.
⮚ The AS keyword between the column name and alias is optional. We can also write:
SELECT Marks1 AS "Marks Secured" FROM Student;
SELECT Marks1 "Marks Secured" FROM Student;
FORMATIVE ASSESSMENT
SESSION 5
RECAPITULATION
Data Manipulation Language (DML) Commands
🠶 Retrieving specific rows - WHERE clause
⮚ Certain rows can be displayed based on the criteria for selection of rows using the keyword
WHERE.
⮚ The WHERE clause is used to filter records.
⮚ It is used to extract only those records that fulfil a specified criterion.
⮚ Syntax:
⮚ Example:
1. To display the names and marks of all those students who have secured marks above 80.
2. What would be the marks of the students if they were increased by 5 for all those students who
secured marks below 80?
SELECT <column name1> [,<column name> ,….]
FROM <table name>
WHERE <condition>;
SELECT Name,Marks1 FROM Student
WHERE Marks1 > 80;
SELECT Name,Marks1+5 FROM Student
WHERE marks1 <80;
Data Manipulation Language (DML) Commands
2. Using Relational Operators-
🠶 Relational operators are used to compare two values.
🠶 The result of the comparison is True or False.
🠶 They are used with WHERE clause.
🠶 Given below are all the relational operators used in MySQL along with their functions:
🠶 Examples:
SELECT * FROM Student WHERE Marks1>=93;
SELECT * FROM Student WHERE Name = 'Gurpreet Kaur';
SELECT RollNo,Marks1 FROM Student WHERE Rollno <>3;
Operator What it does
= Addition
> Subtraction
< Multiplication
>= Division
<= Modulus (or remainder)
!= or <> Not equal to
Data Manipulation Language (DML) Commands
3. Using Logical Operators -
🠶 OR, AND, NOT logical operators are used in SQL.
🠶 Logical operators OR and AND are used to connect relational expressions in the WHERE clause.
🠶 OR requires any one condition to be true in order to return TRUE.
🠶 AND requires both conditions to be true in order to return TRUE.
🠶 NOT negates a condition.
🠶 The symbol || can be used in place of OR, && can be used in place of AND, ! can be used in
place of NOT operator.
🠶 Examples:
SELECT Rollno, Name,Marks1 FROM Student
WHERE Marks1 > 70 AND Marks1 < 80;
SELECT Empnumber, EmpName FROM Employee
WHERE Department = 'Accoumts' OR Department = 'Personnel';
SELECT Name,TotalMarks FROM Candidate
WHERE writtenmarks>80 || Interviewmarks>10;
Data Manipulation Language (DML) Commands
CONDITION BASED ON RANGE:
1. BETWEEN Operator-
🠶 The BETWEEN operator defines the range of values within which the column values must fall into
to make the condition true.
🠶 The range includes both the upper and lower values.
🠶 Examples:
2. IN Operator –
🠶 The IN operator selects values that match any value in the given list of values.
🠶 Examples:
SELECT Rollno,Name,Marks1 FROM Student
WHERE Marks1 BETWEEN 70 AND 80;
SELECT Rollno,Name,Marks1 FROM Student
WHERE Marks1 NOT BETWEEN 70 AND 80;
SELECT Rollno, Name, Marks1 FROM Student WHERE Marks1 IN (68,76,78);
SELECT * FROM Employee WHERE State IN ('DELHI','MUMBAI','UP');
Data Manipulation Language (DML) Commands
CONDITION BASED ON PATTERN MATCHES:
MySQL has 2 wildcards.
1. Percent (%) symbol – It is used to represent any sequence of zero or more characters.
2. Underscore (_) symbol – It is used to represent a single character.
LIKE clause:
🠶 LIKE clause is used to fetch data which matches the specified pattern from a table.
🠶 The LIKE clause uses wildcards in comparison won't be doing a strict comparison like = or
< or >.
🠶 Syntax:
SELECT <column name>, [<column name>…] FROM <table name>
WHERE <column name> LIKE Pattern [AND [OR]] <Condition2>;
A…..
……T
Data Manipulation Language (DML) Commands
CONDITION BASED ON PATTERN MATCHES:
🠶 Examples:
1. To display details of students who have their names ending with 'Senā€˜.
2. To display rows from the table Student with names starting with 'Gā€˜.
3. To display rows that have names starting with 'G' and ending with 'bā€˜.
4. To display rows from the table Student that have 'Sen' anywhere in their names.
SELECT * FROM Student WHERE Name LIKE '%Sen';
A…..
……T
SELECT * FROM Student WHERE Name LIKE 'G%';
SELECT * FROM Student WHERE Name LIKE 'G%b';
SELECT * FROM Student WHERE Name LIKE '%Sen%';
Data Manipulation Language (DML) Commands
CONDITION BASED ON PATTERN MATCHES:
🠶 Some more Examples:
1. 'Am%' matches any string starting with Am
2. '%Singh%' matches any string containing 'Singhā€˜
3. '%a' matches any string ending with 'a'
4. '_ _ _' matches any string that is exactly 3 characters long.
5. '_ _ %' matches any string that has at least 2 characters.
6. '_ _ _ g' matches any string that is 4 characters long with any 3 characters in the
beginning but 'g' as the 4th character.
NOT LIKE clause:
The keyword NOT LIKE is used to select the rows that do not match the specified pattern.
Example: To display rows from the table Student that have names not starting with 'G'
A…..
……T
SELECT * FROM Student WHERE Name NOT LIKE 'G%';
Data Manipulation Language (DML) Commands
NULL:
🠶 NULL means a value that is unavailable, unassigned, unknown or inapplicable.
🠶 NULL is not the same as zero or a space or any other character.
🠶 In a table, NULL is searched for using IS NULL keywords.
🠶 Example:
NOT NULL:
🠶 NOT NULL values in a table can be searched using IS NOT NULL.
🠶 Example:
Note: If any column value involved in an arithmetic expression is NULL, the result of the arithmetic
expression is also NULL.
SELECT * FROM Student WHERE Name IS NULL;
SELECT * FROM Employee WHERE Commission IS NULL;
SELECT * FROM Employee WHERE Commission IS NOT NULL;
FORMATIVE ASSESSMENT
SESSION 6
RECAPITULATION
Data Manipulation Language (DML) Commands
Sorting the Results- ORDER BY
🠶 The results of the SELECT statement can be displayed in the ascending or descending
values of a single column or multiple columns using ORDER BY clause.
🠶 Syntax:
🠶 Example:
1. To display data of students in ascending order of their marks.
2. To display data of students in ascending order of their names (meaning alphabetically
sorted on names).
SELECT <column name>, [<column name>…] FROM <table name>
[WHERE <Condition list>]
ORDER BY <column name>;
SELECT * FROM Student ORDER BY Marks;
SELECT * FROM Student ORDER BY Name;
Data Manipulation Language (DML) Commands
Sorting the Results- ORDER BY
To display data in descending order, DESC keyword is used in ORDER BY clause.
Example:
3. To display details of her students in descending order of marks.
4. To display details of students who have secured marks above 90 in descending order of
names.
SELECT * FROM Student ORDER BY Marks DESC;
SELECT * FROM Student WHERE Marks1 > 90 ORDER BY Name DESC;
Data Manipulation Language (DML) Commands
3. UPDATE:
🠶 UPDATE statement is used to modify existing data in the table.
🠶 Syntax:
🠶 The statement can be used to update one or more columns together.
🠶 WHERE clause helps in updation of particular rows in a table.
🠶 Example: The following statement sets the marks(Mark) of all the rows to 94.
🠶 The following statement sets the marks(Mark) of the row with name as 'Monica Rana' to
94.
UPDATE <table_name>
SET <column name> = <value>, [ <column name> = <value>, …]
[WHERE <condn>];
UPDATE Student SET Marks1 = 94;
UPDATE Student SET Marks1 = 94 WHERE name = 'Monica Rana';
Data Manipulation Language (DML) Commands
4. DELETE:
🠶 DELETE statement is used to delete rows from a table.
🠶 DELETE removes the entire row, not the individual column values.
🠶 Syntax:
🠶 Example:
1. To delete the student with Roll number 14.
2. DELETE statement can be used to delete all rows of the table also.
DELETE FROM < tablename> [ WHERE < condn>];
DELETE FROM Student WHERE Rollno = 14;
DELETE from Student;
WORKSHEET
Write the following Programs :–
THANK YOU

More Related Content

PPTX
unit 1.pptx
PPT
Dbms Lec Uog 02
PDF
Database_Concepts_Final.pptx.pdf for class 12
PPTX
database concepts.pptx
PPTX
Unit 10 - Realtional Databases.pptxxxxxxxxx
PPTX
SQL things ace series of the thing useful
PPTX
RDMS AND SQL
PPTX
Dbms Basics
unit 1.pptx
Dbms Lec Uog 02
Database_Concepts_Final.pptx.pdf for class 12
database concepts.pptx
Unit 10 - Realtional Databases.pptxxxxxxxxx
SQL things ace series of the thing useful
RDMS AND SQL
Dbms Basics

Similar to IP-Lesson_Planning(Unit4 - Database concepts and SQL).pptx (20)

PDF
Database Concepts & SQL(1).pdf
PPTX
9 rdbms of digital documentaion of class ix
PPTX
unit 1.pptx
PDF
Unit 3 rdbms study_materials-converted
PPTX
Database Management System ppt
PDF
csedatabasemanagementsystemppt-170825044344.pdf
PPT
Info systems databases
Ā 
PPTX
DATABASE MANAGEMENT SYSTEM-MRS. LAXMI B PANDYA FOR 25TH AUGUST,2022.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PPT
Database system concepts
Ā 
PPTX
Database-management-system-dbms-ppt.pptx
PPTX
7. SQL.pptx
PDF
SQL Complete Tutorial. All Topics Covered
PPTX
Codds rules & keys
PPTX
IET MySQL PPT Ver9ZESXRDCTFYVGBUHNIJOMK.pptx
PPT
Dbms relational model
PPTX
codd rules of dbms given by E F codd who is called father of dbms
PPTX
Relational Database.pptx
PDF
rdbms8.pdf
PPTX
MODERN DATABASES (2).pptx in which modern types of data bases
Database Concepts & SQL(1).pdf
9 rdbms of digital documentaion of class ix
unit 1.pptx
Unit 3 rdbms study_materials-converted
Database Management System ppt
csedatabasemanagementsystemppt-170825044344.pdf
Info systems databases
Ā 
DATABASE MANAGEMENT SYSTEM-MRS. LAXMI B PANDYA FOR 25TH AUGUST,2022.pptx
MYSQL Presentation for SQL database connectivity
Database system concepts
Ā 
Database-management-system-dbms-ppt.pptx
7. SQL.pptx
SQL Complete Tutorial. All Topics Covered
Codds rules & keys
IET MySQL PPT Ver9ZESXRDCTFYVGBUHNIJOMK.pptx
Dbms relational model
codd rules of dbms given by E F codd who is called father of dbms
Relational Database.pptx
rdbms8.pdf
MODERN DATABASES (2).pptx in which modern types of data bases
Ad

Recently uploaded (20)

DOCX
mcsp232projectguidelinesjan2023 (1).docx
DOC
field study for teachers graduating samplr
PPTX
ESD MODULE-5hdbdhbdbdbdbbdbdbbdndbdbdbdbbdbd
PPTX
E-Commerce____Intermediate_Presentation.pptx
PPTX
OCCULAR MANIFESTATIONS IN LEPROSY.pptx bbb
PDF
Daisia Frank: Strategy-Driven Real Estate with Heart.pdf
PPTX
PMP (Project Management Professional) course prepares individuals
PDF
Josh Gao Strength to Strength Book Summary
PPTX
1-4 Chaptedjkfhkshdkfjhalksjdhfkjshdljkfhrs.pptx
PPTX
Autonomic_Nervous_SystemM_Drugs_PPT.pptx
DOCX
How to Become a Criminal Profiler or Behavioural Analyst.docx
PDF
MCQ Practice CBT OL Official Language 1.pptx.pdf
PPTX
internship presentation of bsnl in colllege
PPTX
Cerebral_Palsy_Detailed_Presentation.pptx
PPTX
PE3-WEEK-3sdsadsadasdadadwadwdsdddddd.pptx
PPTX
Nervous_System_Drugs_PPT.pptxXXXXXXXXXXXXXXXXX
PPTX
Condensed_Food_Science_Lecture1_Precised.pptx
PPTX
The Stock at arrangement the stock and product.pptx
PPTX
CORE 1 HOUSEKEEPING TOURISM SECTOR POWERPOINT
PDF
313302 DBMS UNIT 1 PPT for diploma Computer Eng Unit 2
mcsp232projectguidelinesjan2023 (1).docx
field study for teachers graduating samplr
ESD MODULE-5hdbdhbdbdbdbbdbdbbdndbdbdbdbbdbd
E-Commerce____Intermediate_Presentation.pptx
OCCULAR MANIFESTATIONS IN LEPROSY.pptx bbb
Daisia Frank: Strategy-Driven Real Estate with Heart.pdf
PMP (Project Management Professional) course prepares individuals
Josh Gao Strength to Strength Book Summary
1-4 Chaptedjkfhkshdkfjhalksjdhfkjshdljkfhrs.pptx
Autonomic_Nervous_SystemM_Drugs_PPT.pptx
How to Become a Criminal Profiler or Behavioural Analyst.docx
MCQ Practice CBT OL Official Language 1.pptx.pdf
internship presentation of bsnl in colllege
Cerebral_Palsy_Detailed_Presentation.pptx
PE3-WEEK-3sdsadsadasdadadwadwdsdddddd.pptx
Nervous_System_Drugs_PPT.pptxXXXXXXXXXXXXXXXXX
Condensed_Food_Science_Lecture1_Precised.pptx
The Stock at arrangement the stock and product.pptx
CORE 1 HOUSEKEEPING TOURISM SECTOR POWERPOINT
313302 DBMS UNIT 1 PPT for diploma Computer Eng Unit 2
Ad

IP-Lesson_Planning(Unit4 - Database concepts and SQL).pptx

  • 1. Lesson plan Class – XI (Science & Commerce) Subject – Informatics Practices (IP) Topic – UNIT-4 (Database concepts and the Structured Query Language) No. of sessions – 4
  • 2. Objectives: To enable students to 🠶 State what a database is. 🠶 Express the relationship between a database and table. 🠶 Recognize different parts of a table like Row and Column. 🠶 Define DBMS related terms like Primary key, Candidate key, Alternate key etc. 🠶 State what is MySQL. 🠶 State categories of SQL statements. 🠶 Manipulate and manage the Database tables in an efficient way. Subject Matter: 🠶 Database Concepts 🠶 Relational data model 🠶 SQL 🠶 SQL Languages Links: https://www.youtube.com/watch?v=FR4QIeZaPeM https://drive.google.com/open?id=1PVc_BnKw8RA3zX5AFjwvsd76HWRJAIoN https://drive.google.com/open?id=1m2TWp6CiRWWqvklRna0tuKHhKS2QXACX
  • 5. DATABASE CONCEPTS 🠶 Introduction to database concepts 🠶 Need of Database 🠶 Database Management System (DBMS) 🠶 Advantages of DBMS 🠶 Data Models
  • 6. Introduction to Database Concepts 🠶 A Database is a collection of DATA/INFORMATION that is organized so that it can be easily accessed, managed and updated. 🠶 In Database, Data is organized into rows, columns and tables, and it is indexed to make it easier to find relevant information. 🠶 It works like a container which contains the various object like Tables, Queries, Reports etc. in organized way.
  • 7. WHY DO WE NEED DATABASE? 🠶 To manage large chunks of data: if size of data increases into thousands of records, it will simply create a problem to manage. Database can manage large amount of data. 🠶 Accuracy: Through validation rule in database ,data accuracy can be maintained. 🠶 Ease of updating data: With the database, we can flexibly update the data according to our convenience. Moreover, multiple people can also edit data at same time. 🠶 Security of data: With databases we have security groups and privileges to restrict access. 🠶 Data integrity: In databases, we can be assured of accuracy and consistency of data due to the built in integrity checks and access controls.
  • 8. DBMS (Database Management System) 🠶 A DBMS refers to a software that is responsible for storing, maintaining and utilizing database in an efficient way. 🠶 A Database along with DBMS software is called Database System. 🠶 Example of DBMS software are 🠶 Oracle, 🠶 MS SQL Server, 🠶 MS Access, 🠶 Paradox, 🠶 DB2 and 🠶 MySQL etc. 🠶 MySQL is open source and freeware DBMS.
  • 9. Advantages of Database System 🠶 Databases reduces Redundancy: It removes duplication of data because data are kept at one place and all the application refers to the centrally maintained database. 🠶 Database controls Inconsistency: When two copies of the same data do not agree to each other, then it is called Inconsistency. By controlling redundancy, the inconsistency is also controlled. 🠶 Database facilitate Sharing of Data: Data stored in the database can be shared among several users. 🠶 Database ensures Security: Data are protected against accidental or intentional disclosure to unauthorized person or unauthorized modification. 🠶 Database maintains Integrity: It enforces certain integrity rules to insure the validity or correctness of data. For ex. A date can’t be like 31/31/2000.
  • 10. Data Model – Way of data representation Data model is a model or presentation which shows How data is organized ? or stored in the database. A data is modelled by one of the following given: 🠶 Relational Data Model: In this model data is organized into Relations or Tables (i.e. Rows and Columns). A row in a table represents a relationship of data to each other and also called a Tuple or Record. A column is called Attribute or Field. 🠶 Network Data Model: In this model, data is represented by collection of records and relationship among data is shown by Links. 🠶 Hierarchical Data Model: In this model, Records are organized as Trees. Records at top level is called Root record and this may contains multiple directly linked children records. 🠶 Object Oriented Data Model: In this model, records are represented as a objects. The collection of similar types of object is called class.
  • 11. FORMATIVE ASSESSMENT Answer the following questions: 1. Define the following terms: a) Database b) DBMS c) Relational Data Model 2. What is the relationship between a Database and a Table? 3. What is DBMS? Write names of any 4 DBMSs. 4. Write any 4 advantages of Database System. 5. What is Data Model? Write the names of different data models. 6. State the need of a database.
  • 13. RECAPITULATION 1. What is Database? 2. Why do we need database? 3. What is DBMS? Give examples of different DBMS software's. 4. What are the advantages of database system? 5. What are the different Data Models?
  • 14. Relational data model 🠶 RELATIONAL DATABASE 🠶 Relational Database Terms 🠶 Keys in a Database 🠶 Introduction of SQL 🠶 Introduction of MySQL 🠶 MySQL Features 🠶 Types of SQL Commands
  • 15. RELATIONAL DATABASE 🠶 A Relational database is a collective set of multiple data sets organized by tables, records and columns. 🠶 Relational database establish a well-defined relationship between database tables. 🠶 Tables communicate and share information, which facilitates data searcheability, organization and reporting. 🠶 A Relational database use Structured Query Language (SQL), which is a standard user application that provides an easy programming interface for database interaction.
  • 17. RELATIONAL DATABASE TERMS 🠶 Relation (Table): A Relation or Table is Matrix like structure arranged in Rows and Columns. It has the following properties: Atomicity: Each column assigned a unique name and must have atomic(indivisible) value i.e. a value that can not be further subdivided. No duplicity: No two rows of relation will be identical i.e. in any two rows value in at least one column must be different. All items in a column are homogeneous i.e. same data type. Ordering of rows and column is immaterial. 🠶 Domain: It is collection of values from which the value is derived for a column. 🠶 Tuple / Entity / Record: Rows of a table is called Tuple or Record. 🠶 Attribute/ Field: Column of a table is called Attribute or Field. 🠶 Degree: Number of columns (attributes) in a table. 🠶 Cardinality: Number of rows (Records) in a table.
  • 18. KEYS IN A DATABASE 🠶 Key plays an important role in relational database; 🠶 it is used for identifying unique rows from table & establishes relationship among tables on need. 🠶 Types of keys in DBMS: 1. Primary Key – A primary is a column or set of columns in a table that uniquely identifies tuples (rows) in that table. 2. Candidate Key – It is an attribute or a set of attributes or keys participating for Primary Key, to uniquely identify each record in that table. 3. Alternate Key – Out of all candidate keys, only one gets selected as primary key, remaining keys are known as alternate or secondary keys. 4. Foreign Key – Foreign keys are the columns of a table that points to the primary key of another table. They act as a cross-reference between tables.
  • 19. KEYS IN A DATABASE
  • 20. Introduction of SQL 🠶 SQL is an acronym of Structured Query Language. 🠶 It is a standard language developed and used for accessing and modifying relational databases. 🠶 The SQL language was originally developed at the IBM research laboratory in San JosĆ©, in connection with a project developing a prototype for a relational database management system called System R in the early 70s. 🠶 SQL is being used by many database management systems. Some of them are: 🠶 MySQL 🠶 PostgreSQL 🠶 Oracle 🠶 SQLite 🠶 Microsoft SQL Server
  • 21. Introduction of MySQL 🠶 MySQL is currently the most popular open source database software. 🠶 It is a multi-user, multithreaded database management system. 🠶 MySQL is especially popular on the web. 🠶 It is one of the parts of the very popular LAMP platform. 🠶 MySQL AB was founded by Michael Widenius (Monty), David Axmark and Allan Larsson in Sweden in year 1995.
  • 22. MySQL Features 🠶 Open Source & Free of Cost: It is Open Source and available at free of cost. 🠶 Portability: Small enough in size to install and run it on any types of Hardware and OS like Linux,MS Windows or Mac etc. 🠶 Security: Its Databases are secured & protected with password. 🠶 Connectivity: Various APIs are developed to connect it with many programming languages. 🠶 Query Language: It supports SQL (Structured Query Language) for handling database.
  • 23. Types of SQL Commands 🠶 DDL (Data Definition Language): To create database and table structure-commands like CREATE , ALTER , DROP etc. 🠶 DML (Data Manipulation Language): Record/rows related operations. commands like SELECT...., INSERT..., DELETE..., UPDATE.... etc. 🠶 DCL (Data Control Language): Used to control the transactions. commands like COMMIT, ROLLBACK, SAVEPOINT etc. 🠶 TCL (Transactional control Language): Used to manipulate permissions or access rights to the tables. commands like GRANT , REVOKE etc.
  • 24. FORMATIVE ASSESSMENT MCQ’s: 1. The vertical subset of a table is known as a) Tuple b) Row c) Attribute d) Relation 2. Which of the following columns in a Student table can be used as the primary key? a) Class b) Section c) First Name d) Admission No 3. A tuple is also known as a _________________. a) table b) relation c) row d) field 4. An attribute is also known as a _________________. a) table b) relation c) row d) column 5. A relation can have only one ________ key and may have more than one _______ keys. a) Primary, Candidate b) Candidate, Alternate c) Candidate, Primary d) Alternate, Candidate
  • 26. RECAPITULATION 1. What is MySQL? What are the features of MySQL? 2. What is a Primary key? What is its purpose in a table? 3. How is data organized in a table? 4. Distinguish between Primary key and Candidate key. 5. What are the different types of SQL commands? 6. What are the different keys in a Database? 7. What are the different Relational database terms?
  • 27. MySQL Datatypes Data Types NUMERIC TEXT DATE TIME INT DECIMAL CHAR VARCHAR
  • 28. MySQL Datatypes Class Data Type Description Example Numeric INT Or INTEGER It is used for storing integer values. You can specify a width up to 11 digits. 76 DECIMAL(size, d) It can represent number with or without the fractional part. The maximum number of digits may be specified in the size parameter. The maximum number of digits to the right of the decimal point is specified in the d parameter. 17.32 Text CHAR(size) A fixed-length string from 1 to 255 characters in length right-padded with spaces to the specified length when stored. Values must be enclosed in single quotes or double quotes. 'Mathsā€˜ "Text" VARCHAR(size) A variable-length string from 1 to 255 characters in length; for example VARCHAR(25).Values must be enclosed in single quotes or double quotes. 'Computerā€˜ "Me and u" Date DATE It represents the date including day, month and year Note: Range of years 1000 to 9999 '2009-07-02’ Time TIME It represents time. Format: HH:MM:SS Note: The supported range is from '-838:59:59' to '838:59:59' 10:30:12
  • 29. Categories of SQL Commands SQL commands can be classified into the following categories: 1. Data Definition Language (DDL) Commands: The DDL part of SQL permits database tables to be created or deleted. It also defines indices (keys), specifies links between tables, and imposes constraints on tables. Examples of DDL commands in SQL are: ā– CREATE DATABASE - creates a new database ā– CREATE TABLE - creates a new table ā– ALTER TABLE - modifies a table ā– DROP TABLE - deletes a table 2. The Data Manipulation Language (DML) Commands: The query and update commands form the DML part of SQL: Examples of DDL commands are: ā– INSERT INTO - inserts new data into a table ā– SELECT - extracts data from a table ā– UPDATE - updates data in a table ā– DELETE - deletes data from a table
  • 30. Data Definition Language (DDL) Commands 1. CREATE DATABASE: 🠶 Ms. Sujata is a Class teacher of Class XI. She wants to store data of her students i.e. Names and marks secured, in a database. 🠶 A database is used to house data in the form of tables. 🠶 To display list of existing Databases: mysql> SHOW DATABASES; 🠶 To create a new Database: mysql> CREATE DATABASE School; 🠶 To open an existing Database: mysql> USE School; 🠶 To check the currently opened Database name: mysql> SELECT DATABASE(); 🠶 To display existing tables list in a database: mysql> SHOW tables;
  • 31. Data Definition Language (DDL) Commands 2. CREATE TABLE: After creating a database, the next step is creation of tables in the database. For this CREATE TABLE statement is used. Syntax: CREATE TABLE <table-name> (< column name><data type> [<size>], (< column name><data type> [<size>], …); Example: CREATE TABLE Learner(RollNo INTEGER, Name VARCHAR(25)); Try this out: Create table student with following columns by using appropriate data types: Roll no, Name, Gender, Marks ------------------------------------------------------------------------------------------------------------------------------------ Note: When naming tables and columns, be sure to keep it simple with letters and numbers. Spaces and symbols are invalid characters except for underscore(_). Column names like first_name, last_name, email are valid column names.
  • 32. Data Definition Language (DDL) Commands Viewing Structure of Table: 🠶 The DESCRIBE statement can be used to see the structure of a table as indicated in the Create Statement. 🠶 It displays the Column names, their data types, whether Column must contain data, whether the Column is a Primary key etc. 🠶 Syntax: 🠶 Example: DESCRIBE <table name>; OR DESC <table name>; DESCRIBE Student; OR DESC Student;
  • 33. Data Definition Language (DDL) Commands - Changing Structure of table 3. ALTER TABLE: 🠶 When we create a table we define its structure. 🠶 We can also change its structure i.e. add, remove or change its column(s) using the ALTER TABLE statement. 🠶 Syntax: 🠶 Example: Ms. Sujata adds a column named Games. Now, suppose we want to change the newly added Games column to hold integers(in place of character data) using ALTER TABLE statement: Ms. Sujata deletes the Games column using the ALTER TABLE statement: ALTER TABLE <table_name> ADD/DROP <column_name> [datatype]; ALTER TABLE <table_name> MODIFY <column_name> <new_definition>; ALTER TABLE Student ADD Games VARCHAR(20); ALTER TABLE Student MODIFY Games INTEGER; ALTER TABLE Student DROP Games;
  • 34. Data Definition Language (DDL) Commands - Removing Structure of table 3. DROP TABLE: 🠶 Sometimes there is a requirement to remove a table from the database. 🠶 In such cases we don't want merely to delete the data from the table, but we want to delete the table itself. DROP TABLE command is used for this purpose. 🠶 Syntax: 🠶 Example: DROP TABLE <tablename>; DROP TABLE Student;
  • 35. FORMATIVE ASSESSMENT MCQ’s: 1. A database a. Contains tables b. Is a part of a table c. Is same as a table d. Removes data from a table 2. Number of columns in a table is called a. Power b. Degree c. Cardinality d. Design 3. USE <databasename> command a. is not used in MySQL b. is given before quitting MySQL c. opens a table d. opens a database 4. Number of rows in a table is called a. Power b. Degree c. Cardinality d. Design 5. A table can have a. Only one candidate key b. Only one primary key c. Only one alternate key d. No alternate key
  • 36. FORMATIVE ASSESSMENT 6. Which of the following are valid column names? a. Marks Eng b. 66_Marks c. Marks_Eng d. #Eng_Marks 7. DDL is a. a part of SQL b. a part of DML c. a part of DCL d. None of the above 8. A Primary key column a. can have NULL values b. can have duplicate values c. Both (a) and (b) d. Neither (a) nor (b) 9. Which of the following columns in a Student table can be used as the primary key? a. Class b. Section c. First Name d. Adm_No 10. Which SQL statement is used to delete table from database? a. DELETE b. DROP c. REMOVE d. TRUNCATE
  • 38. RECAPITULATION 1. How is a database related to table(s)? 2. Which SQL statement is used to view names of all the tables contained in the current database? 3. List two categories into which MySQL statements can be categorized. Give examples of 2 statements in each category. 4. What happens if you give a CREATE TABLE statement to create a table named 'Item' and a table named 'Item' already exists? 5. Write any 4 things that are displayed when you display the structure of a table using DESCRIBE statement. 6. Which statement is used to remove a column from a table? 7. What are the different data types used in MySQL? 8. What is the difference between CHAR and VARCHAR data types?
  • 39. Data Manipulation Language (DML) Commands 1. INSERT INTO: 🠶 To add data in the table, which is also known as populating table with rows. 🠶 To add row(s) in the table the INSERT INTO statement can be used. Syntax: INSERT INTO <TableName> VALUES (<Value1>,<Value2>,… ,<ValueN>); Example: INSERT INTO Student VALUES (1,'Siddharth Sehgal','M',93); INSERT INTO Student VALUES (2,'Gurpreet Kaur','F',91); Try this out: Insert the following records in the table student: 3,'Monica Rana','F',93 4,'Jatinder Sen','M',78 5,'George Jacob','M',76
  • 40. Data Manipulation Language (DML) Commands 2. SELECT: 🠶 SELECT statement is used to view the contents of the table. 🠶 It is used to retrieve or fetch data from one or more database tables. 🠶 RETRIEVING ALL COLUMNS: Syntax: SELECT * FROM <TableName>; Example: SELECT * FROM Student; 🠶 RETRIEVING SINGLE COLUMN: SELECT statement to retrieve a single column from a table. Syntax: SELECT <column name> FROM <TableName>; Example: SELECT Name FROM Student;
  • 41. Data Manipulation Language (DML) Commands 🠶 RETRIEVING MULTIPLE COLUMNS: SELECT statement to retrieve multiple columns from a table. Syntax: SELECT <column name1>,<column name2> FROM <table name>; Example: SELECT Rollno, Name FROM Student;
  • 42. Data Manipulation Language (DML) Commands ELIMINATING DUPLICATE VALUES: 🠶 By default data is displayed from all the rows of the table, even if the data in the result is duplicated. 🠶 Using the keyword DISTINCT, the duplicate values can be eliminated in the result. 🠶 When DISTINCT keyword is specified, only one instance of the duplicated data is shown. Syntax: SELECT DISTINCT <column name1> FROM <table name>; Example: SELECT DISTINCT Marks1 FROM Student;
  • 43. Data Manipulation Language (DML) Commands USING OPERATORS WITH SELECT: 1. Using Arithmetic Operators- 🠶 Arithmetic operators perform mathematical calculations. 🠶 In SQL the following arithmetic operators are used: 🠶 Examples: SELECT Marks1+5 FROM Student; SELECT 7*3+1; SELECT Name,Marks1-10 FROM Student ; SELECT 71+34; SELECT Name,Marks1/2 FROM Student ; SELECT 86-21; Operator What it does + Addition - Subtraction * Multiplication / Division % Modulus (or remainder)
  • 44. Data Manipulation Language (DML) Commands 🠶 Using Column Alias: ⮚ Column alias lets different name (heading) to appear for a column than the actual one in the output. ⮚ Example: ⮚ Using Column Alias does not rename a column. It simply displays a different column name in the output. ⮚ The AS keyword between the column name and alias is optional. We can also write: SELECT Marks1 AS "Marks Secured" FROM Student; SELECT Marks1 "Marks Secured" FROM Student;
  • 48. Data Manipulation Language (DML) Commands 🠶 Retrieving specific rows - WHERE clause ⮚ Certain rows can be displayed based on the criteria for selection of rows using the keyword WHERE. ⮚ The WHERE clause is used to filter records. ⮚ It is used to extract only those records that fulfil a specified criterion. ⮚ Syntax: ⮚ Example: 1. To display the names and marks of all those students who have secured marks above 80. 2. What would be the marks of the students if they were increased by 5 for all those students who secured marks below 80? SELECT <column name1> [,<column name> ,….] FROM <table name> WHERE <condition>; SELECT Name,Marks1 FROM Student WHERE Marks1 > 80; SELECT Name,Marks1+5 FROM Student WHERE marks1 <80;
  • 49. Data Manipulation Language (DML) Commands 2. Using Relational Operators- 🠶 Relational operators are used to compare two values. 🠶 The result of the comparison is True or False. 🠶 They are used with WHERE clause. 🠶 Given below are all the relational operators used in MySQL along with their functions: 🠶 Examples: SELECT * FROM Student WHERE Marks1>=93; SELECT * FROM Student WHERE Name = 'Gurpreet Kaur'; SELECT RollNo,Marks1 FROM Student WHERE Rollno <>3; Operator What it does = Addition > Subtraction < Multiplication >= Division <= Modulus (or remainder) != or <> Not equal to
  • 50. Data Manipulation Language (DML) Commands 3. Using Logical Operators - 🠶 OR, AND, NOT logical operators are used in SQL. 🠶 Logical operators OR and AND are used to connect relational expressions in the WHERE clause. 🠶 OR requires any one condition to be true in order to return TRUE. 🠶 AND requires both conditions to be true in order to return TRUE. 🠶 NOT negates a condition. 🠶 The symbol || can be used in place of OR, && can be used in place of AND, ! can be used in place of NOT operator. 🠶 Examples: SELECT Rollno, Name,Marks1 FROM Student WHERE Marks1 > 70 AND Marks1 < 80; SELECT Empnumber, EmpName FROM Employee WHERE Department = 'Accoumts' OR Department = 'Personnel'; SELECT Name,TotalMarks FROM Candidate WHERE writtenmarks>80 || Interviewmarks>10;
  • 51. Data Manipulation Language (DML) Commands CONDITION BASED ON RANGE: 1. BETWEEN Operator- 🠶 The BETWEEN operator defines the range of values within which the column values must fall into to make the condition true. 🠶 The range includes both the upper and lower values. 🠶 Examples: 2. IN Operator – 🠶 The IN operator selects values that match any value in the given list of values. 🠶 Examples: SELECT Rollno,Name,Marks1 FROM Student WHERE Marks1 BETWEEN 70 AND 80; SELECT Rollno,Name,Marks1 FROM Student WHERE Marks1 NOT BETWEEN 70 AND 80; SELECT Rollno, Name, Marks1 FROM Student WHERE Marks1 IN (68,76,78); SELECT * FROM Employee WHERE State IN ('DELHI','MUMBAI','UP');
  • 52. Data Manipulation Language (DML) Commands CONDITION BASED ON PATTERN MATCHES: MySQL has 2 wildcards. 1. Percent (%) symbol – It is used to represent any sequence of zero or more characters. 2. Underscore (_) symbol – It is used to represent a single character. LIKE clause: 🠶 LIKE clause is used to fetch data which matches the specified pattern from a table. 🠶 The LIKE clause uses wildcards in comparison won't be doing a strict comparison like = or < or >. 🠶 Syntax: SELECT <column name>, [<column name>…] FROM <table name> WHERE <column name> LIKE Pattern [AND [OR]] <Condition2>; A….. ……T
  • 53. Data Manipulation Language (DML) Commands CONDITION BASED ON PATTERN MATCHES: 🠶 Examples: 1. To display details of students who have their names ending with 'Senā€˜. 2. To display rows from the table Student with names starting with 'Gā€˜. 3. To display rows that have names starting with 'G' and ending with 'bā€˜. 4. To display rows from the table Student that have 'Sen' anywhere in their names. SELECT * FROM Student WHERE Name LIKE '%Sen'; A….. ……T SELECT * FROM Student WHERE Name LIKE 'G%'; SELECT * FROM Student WHERE Name LIKE 'G%b'; SELECT * FROM Student WHERE Name LIKE '%Sen%';
  • 54. Data Manipulation Language (DML) Commands CONDITION BASED ON PATTERN MATCHES: 🠶 Some more Examples: 1. 'Am%' matches any string starting with Am 2. '%Singh%' matches any string containing 'Singhā€˜ 3. '%a' matches any string ending with 'a' 4. '_ _ _' matches any string that is exactly 3 characters long. 5. '_ _ %' matches any string that has at least 2 characters. 6. '_ _ _ g' matches any string that is 4 characters long with any 3 characters in the beginning but 'g' as the 4th character. NOT LIKE clause: The keyword NOT LIKE is used to select the rows that do not match the specified pattern. Example: To display rows from the table Student that have names not starting with 'G' A….. ……T SELECT * FROM Student WHERE Name NOT LIKE 'G%';
  • 55. Data Manipulation Language (DML) Commands NULL: 🠶 NULL means a value that is unavailable, unassigned, unknown or inapplicable. 🠶 NULL is not the same as zero or a space or any other character. 🠶 In a table, NULL is searched for using IS NULL keywords. 🠶 Example: NOT NULL: 🠶 NOT NULL values in a table can be searched using IS NOT NULL. 🠶 Example: Note: If any column value involved in an arithmetic expression is NULL, the result of the arithmetic expression is also NULL. SELECT * FROM Student WHERE Name IS NULL; SELECT * FROM Employee WHERE Commission IS NULL; SELECT * FROM Employee WHERE Commission IS NOT NULL;
  • 59. Data Manipulation Language (DML) Commands Sorting the Results- ORDER BY 🠶 The results of the SELECT statement can be displayed in the ascending or descending values of a single column or multiple columns using ORDER BY clause. 🠶 Syntax: 🠶 Example: 1. To display data of students in ascending order of their marks. 2. To display data of students in ascending order of their names (meaning alphabetically sorted on names). SELECT <column name>, [<column name>…] FROM <table name> [WHERE <Condition list>] ORDER BY <column name>; SELECT * FROM Student ORDER BY Marks; SELECT * FROM Student ORDER BY Name;
  • 60. Data Manipulation Language (DML) Commands Sorting the Results- ORDER BY To display data in descending order, DESC keyword is used in ORDER BY clause. Example: 3. To display details of her students in descending order of marks. 4. To display details of students who have secured marks above 90 in descending order of names. SELECT * FROM Student ORDER BY Marks DESC; SELECT * FROM Student WHERE Marks1 > 90 ORDER BY Name DESC;
  • 61. Data Manipulation Language (DML) Commands 3. UPDATE: 🠶 UPDATE statement is used to modify existing data in the table. 🠶 Syntax: 🠶 The statement can be used to update one or more columns together. 🠶 WHERE clause helps in updation of particular rows in a table. 🠶 Example: The following statement sets the marks(Mark) of all the rows to 94. 🠶 The following statement sets the marks(Mark) of the row with name as 'Monica Rana' to 94. UPDATE <table_name> SET <column name> = <value>, [ <column name> = <value>, …] [WHERE <condn>]; UPDATE Student SET Marks1 = 94; UPDATE Student SET Marks1 = 94 WHERE name = 'Monica Rana';
  • 62. Data Manipulation Language (DML) Commands 4. DELETE: 🠶 DELETE statement is used to delete rows from a table. 🠶 DELETE removes the entire row, not the individual column values. 🠶 Syntax: 🠶 Example: 1. To delete the student with Roll number 14. 2. DELETE statement can be used to delete all rows of the table also. DELETE FROM < tablename> [ WHERE < condn>]; DELETE FROM Student WHERE Rollno = 14; DELETE from Student;
  • 63. WORKSHEET Write the following Programs :–