CBSE Class 12 Informatic Practices MySQL
CBSE Class 12 Informatic Practices MySQL
CBSE Class 12 Informatic Practices MySQL
com
A PTE
H
8
R
C
• Recall and effectively use the SQL commands and clauses learnt in class XI.
In the previous class, you have learnt some database concepts and SQL commands.
You have also learnt how to access data from a table using various clauses of
SELECT command. In this lesson we are going to revise the database concepts and
SQL studied in class XI. That will make the base for further reading of the concepts
and some more SQL commands which are really very useful in practical
applications.
Database Concepts
You have already studied the following database concepts in class XI:
Software used to manage databases is called Data Base Management System (DBMS).
2. Relational Database: A database in which the data is stored in the form of relations
(also called tables) is called a Relational Database. In other words a Relational
Database is a collection of one or more tables.
244
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
Let us now revise MySQL concepts studied in class XI with the help of a Library database.
Note: As this is a revision lesson, all variations of SQL commands are not covered
here. It is suggested that they are revised through practical assignments.
In a school library, there are books which are issued to the students and the staff. These
books are also returned after a certain period of time. For proper functioning of this
system the librarian maintains a database (an organised collection of data) which
contains the relevant data of books, students, and staff. In a relational database, this
data may be organised as a collection of the following tables:
245
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
BOOKS
STUDENTS
STAFF
Using the data stored in these tables the librarian can find out any type of relevant
information regarding the books at any time. For example, she can find out:
• The list of books in the library which are published by any specific publisher or
author.
• The list of books with price in a specified range.
246
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
• Admission numbers of the students who have not returned the books issued to
them.
• The names of books issued to a particular staff member or student.
For the above tables the primary keys may be set as follows:
In the STUDENTS table LibCardNo can also be the primary key (Why?). It means that there
are two columns in the table STUDENTS that are unique for each row and can be set as the
primary key of the table. These two columns are candidate keys as they are the
candidates for primary key. Out of these two candidate keys we have chosen AdmNo as
the primary key. The other candidate key, i.e., LibCardNo, becomes the Alternate Key of
the table. Had we chosen LibCardNo as the primary key, AdmNo would have been called
the alternate key of the table.
In class XI we have studied that degree of a table is the number of columns in the table,
and the number of rows in a table is called its cardinality.
Can you tell what are the degrees and cardinalities of tables of Library database?
Libraries function without computers also. Is there any advantage of setting up a
computerised Library Database using an RDBMS? Discuss it with your teacher.
We shall be using MySQL for Library database.
Once MySQL is procured (downloaded from net, or copied from somewhere, or received
free with some book), it can be installed and the process of creating Library database can
be started.
247
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
248
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
DROP <columnname>;
249
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
[WHERE <condition>]
[WHERE <condn>];
250
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
Following are the clauses which can be used with SELECT command:
b. WHERE Used to specify the condition based on which rows of a table are
displayed.
c. BETWEEN Used to define the range of values within which the column
values must fall to make a condition true. Range includes both
the upper and the lower values.
f. IS NULL / Used to select rows in which the specified column is NULL (or is
NOT NULL NOT NULL)
Now, let us revise these commands with the help of Library database discussed above.
• After starting MySQL, our first job is to create a database in which all the tables
will be stored. For this we give the statement:
CREATE DATABASE Library;
This statement will create a database with the name Library using the
command CREATE DATABASE.
• Now we have to open this database so that we can create tables and do other
tasks in it. For this we give the following statement
USE Library;
After creating the database and opening it, we have to create tables. Before we create a
table, we should very carefully plan columns that we want to have in the table and type of
data to be stored in each column. The type of data decides what data types should be
251
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
specified for different columns of a table. There are many data types available in MySQL,
but a few which are used most often are:
Text CHAR(size)
VARCHAR(size)
NUMERIC DECIMAL(p,s)
INT(size) or INTEGER(size)
date DATE
After planning, we arrive at the following data types for columns of our tables in the
Library database. Here the column sizes are based on our assumption. These may be
different for different library databases.
BOOKS
Price DECIMAL(7, 2) We assume that the price of a book will not exceed
Rs. 99999.99
252
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
STUDENTS
STAFF
After deciding the columns to be kept in each table and their data types, we are ready to
create the tables.
253
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
(AccNo INTEGER(5),
Title VARCHAR(30),
Author VARCHAR(30),
Publisher VARCHAR(10),
Edition INTEGER(4),
Price DECIMAL(7,2));
(AdmNo VARCHAR(7),
LibCardNo INTEGER(4),
AccNo INTEGER(5),
IssueDate DATE,
ReturnDate DATE);
(EmpNo INTEGER(3),
AccNo INTEGER(5),
IssueDate DATE,
ReturnDate DATE);
254
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
• To further verify that the tables have been created as per the required
specifications, we view the structure of each table by giving the statements:
DESC Books;
DESC Students;
DESC Staff;
(i) Suppose we decide to categorise the books into categories 'A', 'B' and 'C'
based on some criteria. Then a new column Category of type CHAR(1) can
be added to the table by the statement:
ALTER TABLE Books ADD Category CHAR(1);
(ii) Suppose we wish to change the size of column 'publisher' of Books table
from 10 characters to 20 characters, then we can give the statement:
ALTER TABLE Books MODIFY Publisher VARCHAR(20);
(iii) Suppose we decide to remove the ReturnDate column from the table
STAFF. We can do so by the statement:
ALTER TABLE Staff DROP ReturnDate;
• Once the tables are created as per the required specifications, we can
populate these tables with the given sample data as follows:
à
For Books table:
INSERT INTO Books VALUES
255
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
à
For Students table:
INSERT INTO Students VALUES
à
For Staff table:
INSERT INTO Staff VALUES
• Now we can check whether the data has been entered into the tables correctly
or not by entering the statements:
SELECT * FROM Books;
We can use various clauses with SELECT command to display the data. Examples are
shown below:
• To check the names (without repetition) of various publishers whose books are
present in the library, we enter the statement:
SELECT DISTINCT Publisher FROM Books;
• To check the books of publisher 'APH' present in the library, we enter the
statement:
SELECT * from Books WHERE Publisher = 'APH';
• To display the books list for which the price is between ` 250 and ` 500, we
enter the statement:
SELECT * FROM Books WHERE Price > 250 and Price < 500;
256
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
• To display the books list for which the price is from ` 250 to ` 500, we enter the
statement:
SELECT * FROM Books WHERE Price >= 250 and Price <= 500;
OR
SELECT * FROM Books WHERE Price BETWEEN 250 and 500;
• To display the details of books from the publishers 'APH' 'JPH', or 'ABD', we enter
the statement:
SELECT * from Books
OR Publisher = 'JPH'
OR Publisher = 'ABD';
OR
SELECT * from Books
• To list the AccNo, Title, and Price of all the books whose Title contains the word
'History', we can enter the statement:
SELECT AccNo, Title, Price from Books
• To display the AccNo of all the books which have been issued to students but not
returned by them, we enter the statement:
SELECT AccNo FROM Students
• To Display a List of all the Books in the alphabetical ascending order of Titles,
we enter the statement:
SELECT * FROM Books ORDER BY Title;
257
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
• To get the same list in descending order of Titles, we enter the statement:
SELECT * FROM Books ORDER BY Title DESC;
• Now suppose a student returns a Book with AccNo 1245 on 12 August 2010, then
this information can be recorded in the Students table as follows:
UPDATE Students
SET ReturnDate = '2010-08-12'
WHERE AccNo = 1245;
• If we want to delete the records of all the books by the Publisher 'PPP' with
edition year earlier than 1990, we can enter the statement:
DELETE FROM Books
WHERE Publisher = 'PPP' AND Edition < 1990;
Suppose for some analysis we decide to keep track of how many times each book is issued.
This can be done if there is an extra column 'IssueFreq' in the table Books to keep this
count. Every time a book is issued, the corresponding value in this column is incremented
by 1. To add this extra column in the table, we enter the statement:
ALTER TABLE Books ADD IssueFreq INTEGER(3);
Functions in MySQL
Let us now revise the following single-row SQL functions which are available in MySQL:
Numeric Functions:
OR
POW(x,y)
258
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
String Functions:
259
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
10 DAYOFYEAR(date) Return the day of the year for the given date in
numeric format in the range 1 to 366.
260
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
2. Suppose we want to create another table and one column in that table will be
'Author'. In order to optimally utilize the storage space, we wish to find out what is
the maximum length of 'Author' in the table 'Books'. This value will guide us in
deciding the field width of 'Author' in the new table. So we enter the statement:
SELECT Author, LENGTH(Author)
FROM Books ORDER BY LENGTH(Author);
3. In order to produce a report we wish to display the book titles in upper case letters of
the alphabet. So we enter the statement:
SELECT AccNo, UCASE(Title) FROM Books;
4. Suppose a book with accession number 1050 is returned and we wish to enter the
current date as the ReturnDate in the table Students. We can give the statement:
UPDATE Students
SET ReturnDate = CURDATE()
WHERE AccNo = 1050;
5. Suppose we wish to find out which staff members got the books issued in the
previous calender year, then we can give the statement:
SELECT * FROM Staff WHERE YEAR(IssueDate) =
YEAR(CURDATE())-1;
What Next?
So far you have learnt how to work with single table at a time. None of the operations
discussed in class XI or in this lesson required us to access data simultaneously from
multiple tables. But in real life database implementations we have to access data from
multiple tables simultaneously, as will be clear from the following examples.
261
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
262
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
EXERCISES
Complete the following cross-word puzzle using SQL commands and clauses:
1 2 3
4 5 6
7 8
10
263
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
Clues:
Across Down
10. To add another column to the table 5. I don't want data in this table
6. It contains tables
1. MySQL is a /an
c. RDBMS d. Table
a. Power b. Degree
c. Cardinality d. Design
4. A database
264
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
a. Power b. Degree
c. Cardinality d. Design
9. DDL is
c. For inserting similar data in a table d. For deleting data from a table
265
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
LAB EXERCISES
Table: Loan_Accounts
Write SQL commands for the tasks 1 to 35 and write the output for the SQL commands 36
to 40:
Create Database and use it
1. Create the database LOANS.
2. Use the database LOANS.
Create Table / Insert Into
3. Create the table Loan_Accounts and insert tuples in it.
Simple Select
4. Display the details of all the loans.
5. Display the AccNo, Cust_Name, and Loan_Amount of all the loans.
Conditional Select using Where Clause
6. Display the details of all the loans with less than 40 instalments.
7. Display the AccNo and Loan_Amount of all the loans started before 01-04-2009.
266
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
Using NULL
9. Display the details of all the loans whose rate of interest is NULL.
10. Display the details of all the loans whose rate of interest is not NULL.
11. Display the amounts of various loans from the table Loan_Accounts. A loan amount
should appear only once.
12. Display the number of instalments of various loans from the table Loan_Accounts. An
instalment should appear only once..
13. Display the details of all the loans started after 31-12-2008 for which the number of
instalments are more than 36.
14. Display the Cust_Name and Loan_Amount for all the loans which do not have number
of instalments 36.
15. Display the Cust_Name and Loan_Amount for all the loans for which the loan amount
is less than 500000 or int_rate is more than 12.
16. Display the details of all the loans which started in the year 2009.
17. Display the details of all the loans whose Loan_Amount is in the range 400000 to
500000.
18. Display the details of all the loans whose rate of interest is in the range 11% to 12%.
Using IN Operator
19. Display the Cust_Name and Loan_Amount for all the loans for which the number of
instalments are 24, 36, or 48. (Using IN operator)
20. Display the details of all the loans whose Loan_Amount is in the range 400000 to
500000. (Using BETWEEN operator)
21. Display the details of all the loans whose rate of interest is in the range 11% to 12%.
267
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
22. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which the
Cust_Name ends with 'Sharma'.
23. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which the
Cust_Name ends with 'a'.
24. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which the
Cust_Name contains 'a'
25. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which the
Cust_Name does not contain 'P'.
26. Display the AccNo, Cust_Name, and Loan_Amount for all the loans for which the
Cust_Name contains 'a' as the second last character.
27. Display the details of all the loans in the ascending order of their Loan_Amount.
28. Display the details of all the loans in the descending order of their Start_Date.
29. Display the details of all the loans in the ascending order of their Loan_Amount and
within Loan_Amount in the descending order of their Start_Date.
30. Put the interest rate 11.50% for all the loans for which interest rate is NULL.
31. Increase the interest rate by 0.5% for all the loans for which the loan amount is more
than 400000.
33. Delete the records of all the loans whose start date is before 2007.
35. Add another column Category of type CHAR(1) in the Loan table.
268
Downloaded from www.studiestoday.com
Downloaded from www.studiestoday.com
269
Downloaded from www.studiestoday.com