12 - Information Practices
12 - Information Practices
What is a Database?
A database is an organized collection of data. The data are typically organized to model
aspects of reality in a way that supports processes requiring this information. The term
"database" can both refer to the data themselves or to the database management
system. The Database management system is a software application for the interaction
between users database itself. Users don't have to be human users .In this chapter we
will learn how SQL organizes & manipulate data.
What is SQL?
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL became a standard of the American National Standards Institute (ANSI) in
1986, and of the International Organization for Standardization (ISO) in1987
What is RDBMS?
RDBMS stands for Relational Database Management System.
RDBMS is the basis for SQL, and for all modern database systems such as MS SQL Server, IBM
DB2, Oracle, MySQL, and Microsoft Access. The data in RDBMS is stored in database objects
called tables.
A table is a collection of related data entries and it consists of columns and rows.
MYSQL TERMINOLOGY
Structure Query Language
A non-procedural 4GL used for querying upon relational database.
Relation:
Table in Database
Domain:
Pool of values from which the actual values appear.
Tuple:
A row/record of a relation or table
Attribute:
A column/field of relation
Degree:
Number of attributes
Cardinality:
Number of tuples/rows/records
View:
Virtual table that does not really exist in its own right.
Primary Key:
A primary key is a field/attribute of the table which uniquely identify each record in a table. It
ensures that no two records have the same value in the column of the primary key field. For
example in a Student table , Rollno or Amno can be made as primary key
Candidate Key:
A Candidate Key is the one that is capable of becoming Primary key i.e., a field or
attribute that has unique value for each row in the relation. E.g In Student table , Rollno, Admno ,
Aadhar No are the candidate keys
Alternate Key:
A candidate key that is not primary key is called alternate key.
Foreign Key:
A non-key attribute, whose values are derived from the primary key of some other table.
SQL Constraint:
A Constraint is a condition or check applicable on a field or set of fields. Different types of
constraint that can be applied to columns in a table are:
NOT NULL
UNIQUE
DEFAULT
CHECK
PRIMARY KEY
FOREIGN KEY
e.g
CREATE TABLE student (Srollno integer NOT NULL, …);
CREATE TABLE student (Srollno integer UNIQUE, …);
CREATE TABLE student (Srollno integer NOT NULL, Sclass integer, Sname varchar(30),
Sclass DEFAULT 12);
CREATE TABLE student (Srollno integer CHECK (Srollno>0), Sclass integer, Sname
varchar(30));
CREATE TABLE student (Srollno integer NOT NULL PRIMARY KEY, Sclass integer,
Sname varchar(30));
CREATE TABLE teacher (Tid integer NOT NULL, FOREIGN KEY (Studentid )
REFRENCES student (Sid));
SELECT COMMAND:
It lets us make queries on the database. It is used to display records from the table.
SELECT * FROM table name WHERE condition;
SELECT * FROM student WHERE Sid=100;
MySQL Functions:
A function is a special type of predefined command set that performs some operation and
returns a single value.
Functions in MySQL are classified into two types:
Single Row Functions
Multiple Row Functions (Aggregate Functions)
Single Row Functions:
These functions operate o a single value to return a single value. They are further categorized into :
Functions Description:
For example:;
Select avg(marks) From student;
c) MAX ( ) : Returns the largest/highest value among the given set of values of any column.
NULL values are ignored.
For example:;
Select max(marks) From student where gender=’M’;
It displays the highest the marks in the table student for only Male students.
Output: 98
d) MIN ( ) : Returns the lowest/ smallest value among the given set of values of any column.
NULL values are ignored.
For example:;
Select min(marks) From student where stream = “Science”;
It displays the lowest the marks in the table student for only science students.
Output: 75
e) COUNT ( ) : is used to count the number of values in a column. It takes one argument,
which can be any column name. Count() function returns the number of non NULL values
in that column. If we use Count(*) , then it will count the total number of records in the
table.
For example:
Select count(marks) From student;
It displays the number values in marks column in the table student which are not null.
Output: 9
SELECT *
FROM student WHERE marks>50
ORDER BY name;
Order By clause sorts the records in ascending order by default. Therefore , in order to sort the
records in descending order , DESC keyword is to be used.
e.g
SELECT *
FROM student WHERE marks>50
ORDER BY name DESC;
To order or sort the result set on multiple columns, you can specify multiple column names in
ORDER by clause along with the desired sort order .
For example:
Select * from student
Order by section asc, marks desc
Explanation: First sort field is section in ascending order and for all records of same section , the
sort field is marks with descending order. It means all the students of same section will be
displayed with highest marks first.
Grouping Result by using Group By: The GROUP BY clause is used in a select statement in
conjunction with aggregate functions to group the result based on DISTINCT or ALL values in a
column. Grouping can be done by column name, or with aggregate functions in which case the
aggregate produces a value for each group. Consider the following table:
Table: Student
RollNo Name Gender Marks DOB Stream
1 Raj Kumar M 93 17-Nov-2000 Science
2 Deep Singh M 98 22-Aug-1996 Commerce
3 Ankit Sharma M 76 02-Feb-2000 Science
4 Radhika Gupta F 78 03-Dec-1999 Humanities
5 Payal Goel F 82 21-April-1998 Vocational
6 Diksha Sharma F 80 17-Dec-1999 Humanities
7 Gurpreet Kaur F NULL 04-Jan-2000 Science
8 Akshay Dureja M 90 05-May-1997 Commerce
9 Shreya Anand F 70 08-Oct-1999 Vocational
10 Prateek Mittal M 75 25-Dec-2000 Science
For example:
1. To display the total number of students in each stream.
Query:
Select Stream, count(*) From Student Group By Stream
Output: Stream Count(*)
Science 4
Commerce 2
Humanities 2
Vocational 2
HAVING Clause
The HAVING clause is used in combination with the GROUP BY clause. The purpose of using
HAVING clause with GROUP BY is to allow aggregate functions to be used along with the
specified condition. This is because the aggregate functions are not allowed to be used with
WHERE clause. Thus, if any aggregate function is to be used after the FROM clause in a SELECT
command, then instead of using WHERE clause, HAVING clause should be used.
Syntax:
SELECT <column1>,<column2>,<aggregate function>...
FROM <tablename>
GROUP BY <column name>
HAVING <condition>
For Example: To display total marks stream wise for only those streams in which highest
marks are less than 85.
Query:
Select Stream, SUM(Marks)
From Student
Group By stream
Having Max(Marks)<85;
Output;
Stream SUM(Marks)
Science 151
Commerce 0
Humanities 168
Vocational 152
AGGREGATE FUNCTIONS & CONDITIONS ON GROUPS (HAVING
CLAUSE)
You may use any condition on group, if required. HAVING clause is used to apply a condition on
a group.
Examples : 1. Select Job, Sum(Pay) From Employee
Group by Job
Having Sum(Pay)>=8000;
WHERE Vs HAVING
WHERE clause works in respect of the whole table but HAVING clause works on GROUP By
only. If WHERE and HAVING both are used, then WHERE will be executed first.
MIND MAP
Class : XII (2020-2021)
Subject: Informatics Practices
ASSIGNMENT
Note: Assignment is to be done in IP register.
Q 9) Ashu wrote the following query which is not giving the correct output. Find
the possible reason
Select * from emp where city =”Delhi” and city=”Bangalore”;
SW01 NULL
SB02 Football
SB03 Hockey
SW04 Swimming
Q 15) A School table is created with fields: School_id, Category, Zone, Strength.
What is wrong with the following statement?
Delete Zone from School;
Q 16) Raman wants to change the Name of the TV Show “Comedy Nights” with
“ Comedy Nights with Kapil”. He is confused which command to use. Help
him write the correct SQL.
Q 17) Sohan needs to display name of customers, who have "o" as the third
character in their name. He wrote the following query.
Select name from customer Where name like
“**o%";
But the query is not producing the result. Identify the problem.
Q 18) Write SQL query to add a column totalprice with datatype numeric and size
10, 2 in a table product.
Q 20) Is there any Difference between Count() and Count(*) function. Justify by
the help of example.
Lab Exercise:
Q1. Consider the table Emp and insert the following records:
Q2. Ms. Shalini wants to keep the record of her students. She needs to create a
table Student having following structure. Write the command to create the
following table and answer the question based on it:
TABLE : Student
Fields Data Type Size
Roll_No INTEGER 4
Name VARCHAR 20
Stipend INTEGER 7
Stream VARCHAR 15
Grade CHAR 1
i. Insert 15 records with relevant information, in the table student
ii. Display all the records of the table Student.
iii. Delete the Student Whose Roll no is 100.
iv. Change the Stream of Student to 'Computer' Whose Roll no. is 536.
v. View structure of the table created by you.
vi. Drop the column Grade from the table Student.
vii. Display the list of streams available.(Value should not be repeated)
Q3. Create the following table item.
Itemno Integer 3
Iname Varchar 15
Quantity Integer 3
Admno Integer 3
Name Varchar 20
Average Integer 3
Sex Char 1
Scode Integer 4
xi. Display students' info. who are either getting average marks of more than 80
or having scode 333 .
xii. Display students' name and average marks, whose scode is 444 and 111.
xvi. Display maximum, minimum and sum of average marks in each scode.
Write queries based upon the above tables-Students (given in Q4) and Stream:
a) To display Admno, Name, Sex and Average from Student's table and Stream
name (Sname) and place from Stream table with respect to Scode.