Class-Xii-It-Database Concepts
Class-Xii-It-Database Concepts
CLASS XII
INFORMATION TECHNOLOGY(802)
1
UNIT- - DATABASE CONCEPTS
Database Concepts
The key to organizational success is effective
decision making which requires timely, relevant
WRITE
and accurate information. Hence information
plays a critical role in today's competitive
environment. Database Management Software
(DBMS) simplifies the task of managing the
data and extracting useful information out of it.
we shall learn about the basic concepts of
databases and also learn how to use DBMS for
some applications.
Data is a collection of raw facts which have not
been processed to reveal useful information.
Information is produced by processing data as
WRITE
shown in Figure.
Data Processing
For example, given the data of the test marks of all
WRITE
the students in a class (data), the average,
maximum and minimum marks in the class can be
used as indicators of the performance of
the class (information).
In other words, we can say that we have extracted
the information about average, maximum and
minimum marks for given student data.
WRITE
A database has the following properties:
WRITE
1) A database is a representation of some aspect of
the real world also called miniworld. Whenever
there are changes in this miniworld they are also
reflected in the database.
2) It is designed, built and populated with data for
specific purpose.
3) It can be of any size and complexity.
4) It can be maintained manually or it may be
computerized.
Need for a Database
WRITE
In traditional file processing, data is stored in the form of
files. A number of application programs are written by
programmers to insert, delete, modify and retrieve data
from these files. New application programs will be added
to the system as the need arises. For example,
consider the Sales and Payroll departments of a company.
One user will maintain information about all the
salespersons in the Sales department in some file
say File1 and another user will maintain details
about the payroll of the salesperson in a separate
WRITE file say File2 in the Payroll Department as shown
WRITE
A database management system is a collection of
programs that enables users to create,
maintain and use a database. It enables creation of a
repository of data that is defined once and then
accessed by different users as per their requirements.
Thus there is a single repository of data which is
accessed by all the application programs as shown
WRITE
DBMS Environment
The various operations that need to be performed on
a database are as follows:
1. Defining the Database: It involves specifying the
WRITE
data type of data that will be stored in
the database and also any constraints on that data.
2. Populating the Database: It involves storing the
data on some storage medium that is
controlled by DBMS.
3. Manipulating the Database: It involves modifying
the database, retrieving data or
querying the database, generating reports from the
database etc.
4. Sharing the Database: Allow multiple users to
WRITE access the database at the same time.
5. Protecting the Database: It enables protection of
the database from software/
hardware failures and unauthorized access.
6. Maintaining the Database: It is easy to adapt to the
changing requirements.
Some examples of DBMS are – MySQL, Oracle, DB2,
IMS, IDS etc.
Characteristics of Database Management Systems
The main characteristics of a DBMS are as follows:
1. Self-describing Nature of a Database System: DBMS
contains not only the database but also the description of
WRITE the data that it stores. This description of data is called
metadata.
Meta-data is stored in a database catalogue or data
dictionary. It contains the structure of the data and also the
constraints that are imposed on the data.
2. Insulation Between Programs and Data: Since the
definition of data is stored separately in a DBMS, any
change in the structure of data would be done in the
catalogue and hence programs which access this data
need not be modified. This property is called
Program-Data Independence.
Characteristics of Database Management Systems
WRITE
Employee Table
In relational model,
• A row is called a Tuple.
• A column is called an Attribute.
WRITE
• A table is called as a Relation.
• The data type of values in each column is called the
Domain.
• The number of attributes in a relation is called the Degree
of a relation.
• The number of rows in a relation is called the Cardinality
of a relation.
• Relation Schema R is denoted by R (A1 , A2 , A3 …, An)
where R is the relation name and A1 , A2, A3 ,….An is the
list of attributes.
WRITE • Relation State is the set of tuples in the relation at a point in
time. A relation state r of relation schema R (A1 , A2, ..., An ),
denoted r(R) is a set of n-tuples r = {t1 , t2,...., tm }, where each
n-tuple is an ordered list of values t = <v1 , v2 , ...,vn >, where
vi is in domain of Ai or is NULL. Here n is the degree of the
relation and m is the cardinality of the relation.
Hence in the previous Figure,
• EMPLOYEE table is a relation.
• There are three tuples in EMPLOYEE relation.
• Name, Employee_ID, Gender, Salary, Date_of_Birth are
WRITE attributes.
• The domain is a set of atomic (or indivisible) values. The
domain of a database attribute is the set of all the possible
values that attribute may contain. In order to specify a domain,
we specify the data type of that attribute. Following are the
domain of attributes of the EMPLOYEE relation:
(a) Name – Set of character strings representing names of
persons.
(b) Employee_ID–Set of 4-digit numbers
(c) Gender – male or female
(d) Salary – Number
(e) Date_of_Birth – Should have a valid date, month and year.
The birth year of the employee must be greater than 1985.
Also the format should be dd-mm-yyyy.
WRITE • The degree of the EMPLOYEE relation is 5 as there are five
attributes in this relation.
• The cardinality of the EMPLOYEE relation is 3 as there are
three tuples in this relation.
• Relation Schema – EMPLOYEE (Name, Employee_ID,
Gender, Salary, Date_of_Birth)
Relation State – {<Neha Mehta, 1121,Female,20000,04-03-
1990>,
<Paras Bansal, 2134, Male, 25000, 19-10-1993>,
<Himani Verma, 3145, Female, 20000, 23-11-1992>}
• Relation State –{<Neha Mehta, 1121,Female,20000,04-03-1990>,
WRITE <Paras Bansal, 2134, Male, 25000, 19-10-1993>,
<Himani Verma, 3145, Female, 20000, 23-11-1992>}
Some More Characteristics of Relations:
WRITE
WRITE
Suppose we wish to create a database of all the
teachers working in a school. This database should
WRITE include the Teacher relation (schema given below):
Teacher (Teacher_ID, First_Name, Last_Name, Gender,
Date_of_Birth, Salary, Dept_No)
The above schema stores the information about all the
teachers working in the school such as their unique ID,
first and last name, gender, salary, date of birth and the
department to which the teacher belongs.
Password: tiger
CREATE DATABASE SCHOOL ;
WRITE
USE SCHOOL ;
To create the above relations in SQL, following CREATE
TABLE command is used:
WRITE CREATE TABLE Teacher
(
Teacher_ID INTEGER PRIMARY KEY,
First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2),
Date_of_Birth DATE,
Dept_No INTEGER
);
WRITE
WRITE
WRITE
WRITE
WRITE
a) NOT NULL: An attribute value may not be
permitted to be NULL. For example, the
First name of the Teacher cannot be NULL.
Hence NOT NULL constraint can be
WRITE specified in this case.
CREATE TABLE TEACHER
(
Teacher_ID INTEGER,
First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2),
Date_of_Birth DATE,
Dept_No INTEGER
);
b) DEFAULT : If a user has not entered a value for an
attribute, then default value specified while creating
the table is used. For example, if a teacher's salary has
not been entered, then by default the database
should store 40000 assuming that the minimum salary
WRITE given to every teacher is 40000. This is illustrated as
follows:
CREATE TABLE TEACHER
(
Teacher_ID INTEGER,
First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2) DEFAULT 40000,
Date_of_Birth DATE,
Dept_No INTEGER
);
In MySQL, if you want to look at the structure and
description of the tables created, DESC command can
be used. The description of the table Teacher created
above is as follows:
WRITE
c) CHECK: In order to restrict the values of an attribute within a
range, CHECK constraint may be used. For example Dept_No of any
teacher must not exceed 110.
This can be specified as follows:
CREATE TABLE TEACHER
WRITE ( MySQL does not support CHECK
Teacher_ID INTEGER, constraint, although it will not
First_Name VARCHAR(20) NOT NULL, give any error if you have a check
constraint in your table.
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2) DEFAULT 40000,
Date_of_Birth DATE,
Dept_No INTEGER CHECK (Dept_No<=110)
);
You can also use the CHECK constraint to compare two attributes
within the same relation having same data type.
d) KEY CONSTRAINT: Primary Key of a table can be specified in two
ways. If the primary key of the table consist of a single attribute,
then the corresponding attribute
can be declared primary key along with its description. For
example, if Teacher_ID attribute of the Teacher relation is the
WRITE PRIMARY KEY then it can be specified as
follows:
CREATE TABLE TEACHER
(
Teacher_ID INTEGER PRIMARY KEY,
First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2) DEFAULT 40000,
Date_of_Birth DATE,
Dept_No INTEGER
);
WRITE
As shown above First_Name has value NO in the Null
column as it was specified to be NOT NULL in the Create
table command. Further Teacher_ID being the primary key
WRITE
(PRI in the Key column) cannot be NULL. Also, note that
default value is specified only for SALARY, and shown NULL
for other attributes indicating no default value.
However if primary key contains more than one attribute
then it must be specified separately as a list of attributes it
comprises of, within parenthesis, separated by
commas. For example, the primary key of the TEACHER
relation comprises of Teacher_ID and Date_of_Birth.
CREATE TABLE TEACHER
(
Teacher_ID INTEGER,
WRITE
First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2) DEFAULT 40000,
Date_of_Birth DATE,
Dept_No INTEGER,
PRIMARY KEY (Teacher_ID, Date_of_Birth)
);
By default, Primary keys are NOT NULL and there is no
need to mention this constraint separately.
e) REFERENTIAL INTEGRITY CONSTRAINT- This constraint
is specified by using the foreign key clause. This clause
contains the foreign key and the primary key referred to
WRITE
by this foreign key along with the name of the relation.
For example
consider the following tables created in the School
Database:Department (Dept_ID, Dept_Name)
Teacher (Teacher_ID, First_Name, Last_Name, Gender,
Salary, Date_of_Birth,Dept_No)
In this example Dept_No is the foreign key that
references Dept_ID of Department relation which is a
primary key. The SQL command for creating these tables
would be as follows:
CREATE TABLE Department
(
Dept_ID INTEGER PRIMARY KEY,
Dept_Name VARCHAR(20) NOT NULL
);
WRITE
CREATE TABLE Teacher
(Teacher_ID INTEGER PRIMARY KEY,
First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2) DEFAULT 40000,
Date_of_Birth DATE,
Dept_No INTEGER,
FOREIGN KEY (Dept_No) REFERENCES Department(Dept_ID)
);
The foreign key is created separately by using the key words
FOREIGN KEY followed by the attribute that is the foreign
key within parenthesis, then the keyword REFERENCES
followed by the name of the referred relation and its
primary key within parenthesis.
WRITE
As shown in above Figure, the key column for Dept_No
contains MUL which implies multiple occurrences of a
WRITE given value can appear within the column. This is also
because it is a foreign key.
1. SET NULL
2. CASCADE
3. RESTRICT
Let us discuss each in detail:
CREATE TABLE Teacher
(
Teacher_ID INTEGER PRIMARY KEY,
WRITE First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2) DEFAULT 40000,
Date_of_Birth DATE,
Dept_No INTEGER,
FOREIGN KEY (Dept_No) REFERENCES Department (Dept_ID)
ON DELETE SET NULL ON UPDATE SET NULL
);
Thus if a department with a given value of
WRITE
Dept_ID is deleted in Department table, then
the corresponding tuples that contains the
deleted value for Dept_No attribute in Teacher
table would be set to NULL. Similarly, if Dept_ID
value is updated then also the corresponding
attribute in Teacher table would be set to NULL.
CREATE TABLE Teacher
(
Teacher_ID INTEGER PRIMARY KEY,
WRITE First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2) DEFAULT 40000,
Date_of_Birth DATE,
Dept_No INTEGER,
FOREIGN KEY (Dept_No) REFERENCES Department (Dept_ID)
ON DELETE CASCADE ON UPDATE CASCADE
);
In the above table, if a department with a given value
WRITE
of the Dept_ID attribute in Department table is
deleted, then the corresponding rows in the Teacher
table would also be deleted. However if Dept_ID
value is updated in the Department table, the change
in corresponding value is also reflected in Teacher
Table.
CREATE TABLE Teacher
(
Teacher_ID INTEGER PRIMARY KEY,
WRITE First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2) DEFAULT 40000,
Date_of_Birth DATE,
Dept_No INTEGER,
FOREIGN KEY (Dept_No) REFERENCES Department (Dept_ID)
ON DELETE RESTRICT ON UPDATE RESTRICT
);
WRITE RESTRICT option will reject the delete or update
operation for the referenced table if there are one or
more related foreign key values in a referencing
table, i.e, you cannot delete or update a department
if there are teachers who belong to that department.
Self-Referencing Tables: A foreign key constraint can
WRITE reference columns within the same table. These
tables are called as self-referencing tables. For
example, consider a table Employee that contains
five columns: Employee_ID, Name,Age, Salary and
Manager_ID. Because the manager is also an
employee, there is a foreign key relationship
between the Manager_ID and Employee_ID as
shown below:
CREATE TABLE Employee
(
WRITE Employee_ID INTEGER PRIMARY KEY,
Name VARCHAR(30),
Age INTEGER,
Salary DECIMAL(10,2),
Manager_ID INTEGER,
FOREIGN KEY (Manager_ID) REFERENCES Employee (Employee_ID)
);
1. Naming of Constraint:
WRITE
Result:
Logical comparisons equal to (=), less than (<), greater than (>), less
than or equal to (<=), greater than or equal to(>=), not equal to(<>)
can be used in the WHERE clause.
c) Query: To display Teacher_ID,First_Name,Last_Name
and Dept_No of teachers who belongs to department
number 4 or 7.
WRITE
SELECT Teacher_ID, First_Name,Last_Name, Dept_No
FROM Teacher WHERE Dept_No = 4 OR Dept_No = 7;
d) Query: To retrieve names of all the teachers and
the names and numbers of their respective
departments.
WRITE
Note that the above query requires two tables –
Teacher and Department. Consider the following
query:
SELECT First_Name, Last_Name, Dept_ID,
Dept_Name FROM Teacher, Department;
This query will result in a set in which the number
of rows will be the number of rows in Teacher table
(14) multiplied with number of rows in Department
table (9), i.e. 126 rows.
Since we have not specified any WHERE clause, this
query will combine each row in Teacher table with
each row of Department table resulting in Cartesian
WRITE
product of two tables. This is called as Cross Join.
Now if we have to combine each teacher with
his/her respective department, there should be a
connecting column between the two tables which
can be used to join them. Thus Dept_No (Teacher
table) and Dept_ID (Department table) can be used
to join the two tables. This type of join will result in
joining rows from Teacher and Department table
WRITE
based on the equality between Dept_No and
Dept_ID columns. The query is as follows:
SELECT First_Name, Last_Name, Dept_ID,
Dept_Name FROM Teacher, Department
WHERE Dept_ID=Dept_No;
Here Dept_ID=Dept_No is the join condition.
This would produce the following result:
WRITE
Now suppose we have to retrieve the similar details for
the teacher in Chemistry department, the query would
WRITE be:
SELECT First_Name, Last_Name, Dept_ID, Dept_Name
FROM Teacher, Department WHERE Dept_ID = Dept_No
AND Dept_Name= “Chemistry”;
e) Suppose the teacher and department table both had
WRITE
same names for the department number, say Dept_ID as
shown below:
The above query is a nested query (query within another query). The
outer one is called outer query and the inner one is called as inner
query. The inner query will return those department ID's which have
female teachers and the outer query will retrieve the names
of those departments respectively
We could have also written the above query by using JOIN
WRITE
condition as shown below:
WRITE
Query: To count the number of teachers earning more than
Rs 40000.
WRITE
The difference is in the use of asterisk (*). Asterisk symbol is used to count
the number of rows in the result of the query.
Result:
Empty set
STRUCTURED QUERY LANGUAGE( MySQL )
SQL Commands
PLEASE
WRITE
STRUCTURED QUERY LANGUAGE( MySQL )
(a) To change the Basic salary to 10500 of all those teachers from COLLEGE,
who joined the COLLEGE after 01/02/89 and are above the age of 50.
(b) To display -Name, Age and Basic of all those from COLLEGE, who belong
to Physics and Chemistry department only.
(c) To display all the department names from COLLEGE, with no duplication.
(d) To list names of all teachers from COLLEGE with their date of joining in
ascending order within their Basic salaries in ascending order.
(e) To display maximum salary amongst the female teachers and also amongst
the male teachers from COLLEGE. (Give a single command)
(f) To insert a new row in the table COLLEGE with the following data:
15, "ATIN", 27, "Physics", '15/05/02', 8500, "M"
(g) To delete a row from COLLEGE in which name is VIREN.
PLEASE
WRITE
STRUCTURED QUERY LANGUAGE( MySQL )
PLEASE
WRITE
STRUCTURED QUERY LANGUAGE( MySQL )
(a) Display the names of the students, who have grade ‘C in either
GAME1 or GAME2 or both.
(b) Display the number of students getting grade A in Cricket.
(c) Display the names of the students who have same game for both
GAME1 and GAME2.
(d) Display the games taken up by the students, whose name starts
with A.
(e) Add a new column named MARKS.
(f) Assign a value 200 for Marks for all those, who are getting grade ‘A’
or grade ‘B’ in both GAME1 and GAME2.
PLEASE
WRITE (g) Arrange the whole table in the alphabetical order of Name.
STRUCTURED QUERY LANGUAGE( MySQL )
PLEASE
WRITE
STRUCTURED QUERY LANGUAGE( MySQL )
PLEASE
WRITE
STRUCTURED QUERY LANGUAGE( MySQL )
Write the SQL commands for (a) to (f) and write the
outputs for SQL queries (g) parts (i) to (iv) on the basis
of the table HOSPITAL.
PLEASE (a) To show all information about the patients of Cardiology Department.
WRITE
(b) To list the name of female patients, who are in Orthopaedic Department.
(c) To list names of all patients with their date of admission in ascending order.
(d) To display Patient’s Name, Charges, Age for male patients only.
(f) To insert a new row in the HOSPITAL table with the following data:
{11,'Mustafa',37,'ENT','25/02/98',250,'M'}
STRUCTURED QUERY LANGUAGE( MySQL )
(d) SELECT Name, Charges, Age FROM HOSPITAL WHERE Sex = 'M' ;
PLEASE
WRITE
WRITE
Write SQL commands for the following:
(a) Create the above table.
(j) Write a query to display the name and Job title of those employees
whose Manager is “Amyra”.
SELECT Employee_Name, Job_Title FROM Employee WHERE Manager_ID= 1205 ;
(k) Write a query to display the name and Job title of those employees
aged between 26 years and 30 years (both inclusive)
SELECT Employee_Name, Job_Title FROM Employee WHERE Age BETWEEN 26 AND 30 ;
Q 2.A Railway company uses machines to sell tickets. The
machine details and daily sales information are recorded
in two tables:
WRITE
Q 2.A Railway company uses machines to sell tickets. The
machine details and daily sales information are recorded
in two tables:
WRITE
WRITE
WRITE
INSERT INTO Sales VALUES(“C01”, “2019-03-15”,100,7000),
WRITE
(“C01”, “2019-03-16”,200,10000),
(“C02”, “2019-03-15”,150,8000),
(“C02”, “2019-03-16”,350,15000),
(“M01”, “2019-03-15”,125,6250),
(“M01”, “2019-03-16”,150,8000),
(“M02”, “2019-03-15”,200,9000),
(“M02”, “2019-03-16”,100,6000),
(“N01”, “2019-03-15”,100,5000),
(“N01”, “2019-03-16”,150,7500),
(“N02”, “2019-03-15”,200,10000),
(“N02”, “2019-03-16”,100,5000) ;
c) Write a query to find the number of ticket machines in each
station.
WRITE
SELECT Station,COUNT(*) AS “No: of Machines” FROM Machine
GROUP BY Station ;
Q10. Display the names of the employees whose salary is in the range
20,000 – 50,000.
SELECT Name FROM Employee where Salary BETWEEN 20000 AND 50000 ;
WRITE
Q11. Display the details of employees whose name ends with the
letter ‘n’.
SELECT * FROM Employee WHERE Name LIKE “%n” ;
Q12. Display the names of the employees whose name is starting with
letter ‘A’.
SELECT Name FROM Employee WHERE Name LIKE “A%” ;
Q3. Display A_NAME(Artist Name) and TITLE of all Modern type paintings.
Q4. Display all the details of all the Paintings in descending order of TITLE.
Q6. Display the highest price of paintings in each type from table PAINTING.
SELECT TYPE, MAX(PRICE) FROM PAINTING GROUP BY TYPE ;
WRITE
Q7.Display A_NAME , TITLE and PRICE of all Paintings whose PRICE in the
range 120000 and 300000.
Q10. Find the number of records whose PRICE is less than 30000 from the
table PAINTING.
Q12. Display the sum of PRICE of ABSTRACT Paintings from table PAINTING
under the heading TOTAL.
1. Database
2. A Column
3. % (percentage)
4. Sum()
5. Data is present in the other table
6. ASC
7. LIKE operator
8. Alter table
9. Select
10. Domain
LAB WORKSHEET-3
ANSWERS
WRITE
CREATE TABLE TRAINER(TID integer Primary Key,
TNAME char(20) ,
TCITY char(20) ,
HIREDATE date,
SALARY decimal(8,2) ) ;
Q1. Display the Trainer Name, City & Salary in descending order of their
Hiredate.
Q2. Display the TNAME and CITY of trainer who joined the Institute in the month
of December 2001.
Q5. Display TID, TNAME for those trainers who does not belong to DELHI and
MUMBAI.
Q8. Display the total fees for the course which started before 2018-09-15
Q10. Display the details of TRAINER table whose TNAME starts with the letter
‘M’.
Q11. Display SALARY and 20% of SALARY of all TRAINERS from the table
under the heading ‘BONUS’.
Q14. Display the number of records from COURSE table whose Fees is greater
than 10000.
Q15. Remove the records of TRAINERS who were hired after 2000-01-01.
Q19. Display TNAME, CITY, CNAME and FEES from the two tables.
Q2. To display the details of RESORT in the alphabetical order of the place.
Q3. To display the maximum rent for each type of resort from table RESORT.
Q4. To display the details of all resorts which are started after 31-Dec-04 from
table RESORT.
Q5. To display the owner name(ownedby) of all ‘5 Star’ resorts from tables
RESORT and OWNER.
Q8. To remove the details of all records whose RENT is below 10000.
Q11. To add a new record with the data R106,GOA,13000, 5STAR,24 May 2010
to the table RESORT.
Q12. To insert one more column called ADDRESS with the data type
VARCHAR(30).
ALTER TABLE RESORT ADD ADDRESS varchar(30) ;
WRITE
Q14. To display the details of resorts whose PLACE is ending with the letter ‘A’.
Q16. To show the number of different TYPE from the table RESORT.
Q19. Find the number and maximum rent of resort of each TYPE.