Final DBMSLab Manual
Final DBMSLab Manual
COURSE OVERVIEW:
This course introduces the core principles and techniques required in the design and Implementation of
database systems. This introductory application-oriented course covers the relational database systems
RDBMS - the predominant system for business scientific and Engineering applications at present. It includes
Entity-Relational model, Normalization, Relationa lmodel, Relational algebra, and data access queries as well
as an introduction to SQL. It also covers essential DBMS concepts such as: Transaction Processing,
Concurrency Control and Recovery. It also provides students with theoretical knowledge and practical
skillsin the use of databases and database management systems in information technology applications.
CO1 Apply the basic concepts of Database Systems and Applications. 1 to 5,9,10,12 1,3
CO4 √
CO5 √
CO6 √
PO10
PO11
PO12
PSO1
PSO2
PSO3
PO1
PO2
PO3
PO4
PO5
PO6
PO7
PO8
PO9
CO#/
Pos
2 3 3 3
CO1 3 3 2 2 2 2
2 3 3 3
CO2 3 3 2 3 1 2
2 3 3 3 3
CO3 3 3 2 3 3 2
2 3 3 3
CO4 3 3 2 3 1 2
3 3 3 3
CO5 3 2 3 3 3 3
3 3 3 3
CO6 3 3 2 3 3 3
Note: 1-Low, 2-Medium, 3-High
PART-A: MySQL Programming
1. Design, develop, and implement the specified queries for the following problems using Oracle,
MySQL,SQL Server, or any other DBMS under LINUX/Windows environment.
2. Create Schema and insert at least 5 records for each table.
3. Add appropriate database constraints.
1. Use Java, C#, PHP, Python, or any other similar front-end tool.
2. All applications must be demonstrated on desktop/laptop as a stand-alone or web based application
(Mobile apps on Android/IOS are not permitted.)
6 Consider the following schema for a Flight Database and draw an ER Diagram.
FLIGHT(Flight no: integer, Flight from: string, Flight to: string, distance: integer, departs:
time, arrives: time, price: real)
AIRCRAFT (aid: integer, aname:string, cruisingrange:integer)
CERTIFIED (eid:integer, aid:integer)
EMPLOYEES (eid:integer, ename:string, salary: integer)
2. For each pilot who is certified for more than three aircrafts, find the eid and the maximum
cruising range of the aircraft for which he/she is certified.
3. Find the names of all pilots whose salary is less than the price of the cheapest route from
Bangalore to Frankfurt.
4. For all aircrafts with cruising range over 1000 kms, find the name of the aircraft and the
average salary of all pilots certified for this aircraft.
5. Find the names of pilots certified for some Boeing aircraft.
6. Find the aid's of all aircraft whose cruising range is greater than the minimum
distancefrom the routes from Bangalore to Delhi.
7 Consider the schema for College Database and draw and ER Diagram.
STUDENT (USN, SName, Address, Phone, Gender)
SEMSEC (SSID, Sem, Sec)
CLASS (USN, SSID)
SUBJECT (Subcode, Title, Sem, Credits)
IAMARKS (USN, Subcode, SSID, Test1, Test2, Test3, FinalIA)
14-15
2 Study & Implementation of different types of constraints with suitable examples.
Consider the following schema for a Flight Database and draw an ER Diagram.
FLIGHT(Flight no: integer, Flight from: string, Flight to: string, distance: integer,
departs: time, arrives: time, price: real)
AIRCRAFT (aid: integer, aname:string, cruisingrange:integer) CERTIFIED
(eid:integer, aid:integer)
EMPLOYEES (eid:integer, ename:string, salary: integer)
Write SQL queries to
1. Find the names of aircraft such that all pilots certified to operate them have
salaries more than Rs 80,000.
6
2. For each pilot who is certified for more than three aircrafts, find the eid and the
maximum cruising range of the aircraft for which he/she is certified.
3. Find the names of all pilots whose salary is less than the price of the cheapest
route from Bangalore to Frankfurt.
4. For all aircrafts with cruising range over 1000 kms, find the name of the aircraft
and the average salary of all pilots certified for this aircraft
5. Find the names of pilots certified for some Boeing aircraft.
6. Find the aid's of all aircraft whose cruising range is greater than the minimum
distancefrom the routes from Bangalore to Delhi
1|P a g e
DBMS LAB IV SEMESTER
37-50
Consider the schema for College Database and draw and ER Diagram.
STUDENT (USN, SName, Address, Phone, Gender) SEMSEC (SSID, Sem, Sec)
CLASS (USN, SSID) SUBJECT (Subcode, Title, Sem, Credits) IAMARKS
(USN, Subcode, SSID, Test1, Test2, Test3, FinalIA)
Give these details only for 8th semester A, B, and C section students.
PART-B(Mini Project)
51
For any problem selected
Make sure that the application should have five or more tables
Indicative areas include; health care, Laboratory Outcomes:
The student should be able to: Create, Update and query on the
database
Demonstrate the working of different concepts of DBMS
Implement, analyze and evaluate the project developed for an
application with the front end compatibility.
2|P a g e
DBMS LAB IV SEMESTER
1.Lab Requirements
Following are the required hardware and software for this lab, which is available in the
laboratory.
Hardware: Desktop system having i5 Processor, 4 GB RAM and 500 GB Hard Disk
with Windows 10 Operating System.
Software:
MySQL
Installation of MySQL
Note: At the end of instructions, users have the option to fix their passwords and it is
mandatory.
CREATING DATABASE
CREATE DATABASE <DATABASE NAME>;
USE <DATABASE NAME>;
3|P a g e
DBMS LAB IV SEMESTER
2. Guidelines to Students
Equipment in the lab for the use of student community. Students need to maintain
a proper decorum in the computer lab. Students must use the equipment with
care. Any damage is caused is punishable.
Students are required to carry their observation / programs book with completed
exercises while entering the lab.
Students are supposed to occupy the machines allotted to them and are not
supposed to talk or make noise in the lab. The allocation is put up on the lab
notice board.
Lab can be used in free time / lunch hours by the students who need to use the
systems should take prior permission from the lab in-charge.
4|P a g e
DBMS LAB IV SEMESTER
INTRODUCTION TO SQL
SQL commands are like instructions to a table. It is used to interact with the database with
some operations. It is also used to perform specific tasks, functions, and queries of data. SQL
can perform various tasks like creating a table, adding data to tables, dropping the table,
modifying the table, set permission for users.
Experiment-1
DDL is a set of SQL commands used to create, modify, and delete database structures but
not data. These commands are normally not used by a general user, who should be accessing
the database via an application.
5|P a g e
DBMS LAB IV SEMESTER
Data Definition in SQL
CREATE, ALTER and DROP
table…………………………………….……relation
row……………………………………..…….tuple
column………………………………….……attribute
DATA TYPES
Numeric: NUMBER, NUMBER(s,p), INTEGER, INT, FLOAT, DECIMAL
Character: CHAR(n), VARCHAR(n), VARCHAR2(n), CHAR VARYING(n)
Bit String: BLOB, CLOB
Boolean: true, false, and null
Date and Time: DATE (YYYY-MM-DD) TIME (HH:MM:SS)
Timestamp: DATE + TIME
USER Defined types
Consider the COMPANY DATABASE, We need to create a database schema design based on
the following (simplified) requirements of the COMPANY Database: The company is
organized into DEPARTMENTs. Each department has a name, number and an employee who
manages the department. We keep track of the start date of the department manager. A
department may have several locations. Each department controls a number of PROJECTs.
Each project has a unique name, unique number and is located at a single location.
The database will store each EMPLOYEE’s social security number, address, salary, sex, and
birthdate. Each employee works for one department but may work on several projects. The DB
will keep track of the number of hours per week that an employee currently works on each
project. It is required to keep track of the direct supervisor of each employee. Each employee
may have a number of DEPENDENTs. For each dependent, the DB keeps a record of name,
sex, birthdate, and relationship to the employee.
6|P a g e
DBMS LAB IV SEMESTER
7|P a g e
DBMS LAB IV SEMESTER
CREATE SCHEMA
Specifies a new database schema by giving it a name
CREATE TABLE
Specifies a new base relation by giving it a name, and specifying each of its attributes
and their data types
Syntax of CREATE Command:
CREATE TABLE <table name> ( <Attribute A1> <Data Type D1> [<
Constarints>], <Attribute A2> <Data Type D2> [< Constarints>],
…….
<Attribute An> <Data Type Dn> [< Constarints>],
[<integrity-constraint1>, <integrity-constraint k> ] );
- A constraint NOT NULL may be specified on an
attribute A constraint NOT NULL may be specified on
an attribute
Ex: CREATE TABLE DEPARTMENT (
DNAME VARCHAR (10) NOT NULL,
DNUMBER INTEGER NOT NULL,
MGRSSN CHAR (9), MGRSTARTDATE CHAR (9) );
Specifying the unique, primary key attributes, secondary keys, and referential integrity
constraints (foreign keys).
Ex: CREATE TABLE DEPT (
DNAME VARCHAR(10) NOT
NULL,
DNUMBER INTEGER NOT
NULL, MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9),
PRIMARY KEY
(DNUMBER), UNIQUE
(DNAME),
FOREIGN KEY (MGRSSN) REFERENCES EMP(SSN));
8|P a g e
DBMS LAB IV SEMESTER
We can specify RESTRICT, CASCADE, SET NULL or SET DEFAULT on referential
integrity constraints (foreign keys)
Ex: CREATE TABLE DEPT (
DNAME VARCHAR(10) NOT
NULL,
DNUMBER INTEGER NOT NULL,
MGRSSN CHAR(9), MGRSTARTDATE CHAR(9),
PRIMARY KEY
(DNUMBER), UNIQUE
(DNAME),
FOREIGN KEY (MGRSSN) REFERENCES EMP
ON DELETE SET DEFAULT ON UPDATE CASCADE);
DROP TABLE
Used to remove a relation (base table) and its definition.
The relation can no longer be used in queries, updates, or any other commands since its
description no longer exists
Example: DROP TABLE DEPENDENT;
ALTER TABLE:
Used to add an attribute to/from one of the base relations drop constraint -- The new
attribute will have NULLs in all the tuples of the relation right after the command is
executed; hence, the NOT NULL constraint is not allowed for such an attribute.
Example: ALTER TABLE EMPLOYEE ADD JOB VARCHAR (12);
The database users must still enter a value for the new attribute JOB for each
EMPLOYEE tuple. This can be done using the UPDATE command.
The SQL commands that deal with the manipulation of data present in the database belong to
DML or Data Manipulation Language and this includes most of the SQL statements. It is the
component of the SQL statement that controls access to data and to the database. Basically,
DCL statements are grouped with DML statements.
List of DML commands:-
INSERT: It is used to insert data into a table.
UPDATE: It is used to update existing data within a table.
DELETE: It is used to delete records from a database table.
There are three SQL commands to modify the database: INSERT, DELETE, and UPDATE.
INSERT
10 | P a g e
DBMS LAB IV SEMESTER
Note: The DEPTS_INFO table may not be up-to-date if we change the tuples in either the
DEPARTMENT or the EMPLOYEE relations after issuing the above. We have to create a
view (see later) to keep such a table up to date.
DELETE
Removes tuples from a relation. Includes a WHERE-clause to select the tuples to be
deleted
Referential integrity should be enforced
Tuples are deleted from only one table at a time (unless CASCADE is specified on a
referential integrity constraint)
A missing WHERE-clause specifies that all tuples in the relation are to be deleted; the
table then becomes an empty table
The number of tuples deleted depends on the number of tuples in the relation that
satisfy the WHERE-clause
Examples:
1: DELETE FROM EMPLOYEE WHERE LNAME='Brown‘;
2: DELETE FROM EMPLOYEE WHERE SSN='123456789‘;
3: DELETE FROM EMPLOYEE WHERE DNO IN (SELECT DNUMBER
FROM DEPARTMENT WHERE DNAME='Research');
UPDATE
Used to modify attribute values of one or more selected tuples
A WHERE-clause selects the tuples to be modified
An additional SET-clause specifies the attributes to be modified and their new values
Each command modifies tuples in the same relation
Referential integrity should be enforced
11 | P a g e
DBMS LAB IV SEMESTER
Example1: Change the location and controlling department number of project number 10 to
'Bellaire' and 5, respectively.
UPDATE PROJECT
SET PLOCATION = 'Bellaire', DNUM = 5 WHERE PNUMBER=10;
Example2: Give all employees in the 'Research' department a 10% raise in salary.
UPDATE EMPLOYEE
SET SALARY = SALARY *1.1
WHERE DNO IN (SELECT DNUMBER FROM DEPARTMENT
WHERE DNAME='Research');
You can grant users various privileges to tables. These permissions can be any combination of
SELECT, INSERT, UPDATE, DELETE, INDEX, CREATE, ALTER, DROP, GRANT
OPTION or ALL.
Syntax
The syntax for granting privileges on a table in MySQL is:
DQL statements are used for performing queries on the data within schema objects. The
purpose of the DQL Command is to get some schema relation based on the query passed to
it. We can define DQL as follows it is a component of SQL statement that allows getting data
from the database and imposing order upon it. It includes the SELECT statement. This
command allows getting the data out of the database to perform operations with it. When a
SELECT is fired against a table or tables the result is compiled into a further temporary table,
which is displayed or perhaps received by the program i.e. a front-end.
List of DQL:
12 | P a g e
DBMS LAB IV SEMESTER
SELECT: It is used to retrieve data from the database
Syntax:
COMMIT;
ROLLBACK: Rollbacks a transaction in case of any error occurs.
Syntax:
ROLLBACK;
SAVEPOINT: Sets a save point within a transaction.
Syntax:
SAVEPOINT SAVEPOINT_NAME;
13 | P a g e
DBMS LAB IV SEMESTER
Experiment-2
Study & Implementation of different types of constraints with suitable
examples
MySQL Constraints
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table. This ensures the accuracy
and reliability of the data in the table. If there is any violation between the constraint and the
data action, the action is aborted. Constraints can be column level or table level. Column level
constraints apply to a column, and table level constraints apply to the whole table.
Consider the example: Company database to illustrate the use of constraints while creating
table.
14 | P a g e
DBMS LAB IV SEMESTER
15 | P a g e
DBMS LAB IV SEMESTER
Experiment -3
Implementation of different types of function, operators, Joins with suitable
examples.
Query 2: Retrieve the name and address of all employees who work for the 'Research'
department.
SELECT FNAME, LNAME, ADDRESS FROM EMPLOYEE, DEPARTMENT WHERE
DNAME='Research' AND DNUMBER=DNO
16 | P a g e
DBMS LAB IV SEMESTER
SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS FROM PROJECT,
DEPARTMENT, EMPLOYEE WHERE DNUM=DNUMBER AND MGRSSN=SSN
AND PLOCATION='Stafford'
In Q2, there are two join conditions The join condition DNUM=DNUMBER relates a project
to its controlling department The join condition MGRSSN=SSN relates the controlling
department to the employee who manages that department
AGGREGATE FUNCTIONS
Include COUNT, SUM, MAX, MIN, and AVG
1. Find the maximum salary, the minimum salary, and the average salary among all
employees.
SELECT MAX (SALARY), MIN (SALARY), AVG (SALARY)
FROM EMPLOYEE
Note: Some SQL implementations may not allow more than one function in the SELECT-
clause
2. Find the maximum salary, the minimum salary, and the average salary among
employees who work for the 'Research' department.
Q12: SELECT MAX (SALARY), MIN(SALARY), AVG(SALARY)
FROM
EMPLOYEE, DEPARTMENT WHERE DNO=DNUMBER AND DNAME='Research'
3. Retrieve the total number of employees in the company (Q13), and the number of
employees in the 'Research' department (Q14).
Q13: SELECT COUNT (*) FROM EMPLOYEE
Q14: SELECT COUNT (*) FROM EMPLOYEE, DEPARTMENT
17 | P a g e
DBMS LAB IV SEMESTER
Example
Query 1: For each employee, retrieve the employee's name, and the name of his or her
immediate supervisor.
UNSPECIFIED WHERE-clause
A missing WHERE-clause indicates no condition; hence, all tuples of the relations in the
FROM-clause are selected. This is equivalent to the condition WHERE TRUE Example:
Note: It is extremely important not to overlook specifying any selection and join conditions in
the WHERE-clause; otherwise, incorrect and very large relations may result
USE OF *
To retrieve all the attribute values of the selected tuples, a * is used, which stands for all the
attributes
Examples:
Retrieve all the attribute values of EMPLOYEES who work in department 5.
SELECT * FROM EMPLOYEE WHERE DNO=5
18 | P a g e
DBMS LAB IV SEMESTER
Retrieve all the attributes of an employee and attributes of DEPARTMENT he works in
for every employee of ‘Research’ department.
USE OF DISTINCT
SQL does not treat a relation as a set; duplicate tuples can appear. To eliminate
duplicate tuples in a query result, the keyword DISTINCT is used
Example: the result of Q1c may have duplicate SALARY values whereas Q1d does not have
any duplicate values
SET OPERATIONS
SQL has directly incorporated some set operations such as union operation (UNION),
set difference (MINUS) and intersection (INTERSECT) operations. The resulting relations of
these set operations are sets of tuples; duplicate tuples are eliminated from the result. The set
operations apply only to union compatible relations; the two relations must have the same
attributes and the attributes must appear in the same order
Query 1: Make a list of all project numbers for projects that involve an employee whose
last name is 'Smith' as a worker or as a manager of the department that controls the
project.
(SELECT PNAME FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE
DNUM=DNUMBER AND MGRSSN=SSN AND LNAME='Smith')
UNION
The standard arithmetic operators '+', '-'. '*', and '/' (for addition, subtraction,
multiplication, and division, respectively) can be applied to numeric values in an SQL query
result
Show the effect of giving all employees who work on the 'ProductX' project a
10% raise.
Q20: SELECT FNAME, LNAME, 1.1*SALARY
19 | P a g e
DBMS LAB IV SEMESTER
FROM EMPLOYEE, WORKS_ON, PROJECT
WHERE SSN=ESSN
AND PNO=PNUMBER AND PNAME='ProductX‘
NESTING OF QUERIES
A complete SELECT query, called a nested query, can be specified within the
WHERE-clause of another query, called the outer query. Many of the previous queries can be
specified in an alternative form using nesting
Query 1: Retrieve the name and address of all employees who work for the 'Research'
department.
20 | P a g e
DBMS LAB IV SEMESTER
SELECT E.FNAME, E.LNAME FROM EMPLOYEE E, DEPENDENT D WHERE
E.SSN=D.ESSN AND E.FNAME=D.DEPENDENT_NAME
Note: In the correlated nested query retrieves all DEPENDENT tuples related to an
EMPLOYEE tuple. If none exist, the EMPLOYEE tuple is selected
EXPLICIT SETS
It is also possible to use an explicit (enumerated) set of values in the WHERE-clause
rather than a nested query
Query 2: Retrieve the social security numbers of all employees who work on project
number 1, 2, or 3.
Note: If a join condition is specified, tuples with NULL values for the join attributes are not
included in the result
SUBSTRING COMPARISON
21 | P a g e
DBMS LAB IV SEMESTER
The LIKE comparison operator is used to compare partial strings. Two reserved
characters are used: '%' (or '*' in some implementations) replaces an arbitrary number of
characters, and '_' replaces a single arbitrary character.
Query 1:Retrieve all employees whose address is in Houston, Texas. Here, the value of the
ADDRESS attribute must contain the substring 'Houston,TX‘ in it.
SELECT FNAME, LNAME
FROM EMPLOYEE WHERE ADDRESS LIKE '%Houston,TX%'
Query 2: Retrieve all employees who were born during the 1950s.
Here, '5' must be the 8th character of the string (according to our format for date), so
the BDATE value is '_______5_', with each underscore as a place holder for a single arbitrary
character.
SELECT FNAME, LNAME
FROM EMPLOYEE WHERE BDATE LIKE '_______5_‘
Note: The LIKE operator allows us to get around the fact that each value is considered atomic
and indivisible. Hence, in SQL, character string attribute values are not atomic
JOINS
The concept of a joined table (or joined relation) was incorporated into SQL to permit users to
specify a table resulting from a join operation in the FROM clause of a query. This construct
may be easier to comprehend than mixing together all the select and join conditions in the
WHERE clause. For example, consider query Q1, which retrieves the name and address of
every employee who works for the ‘Research’ department. It may be easier to specify the join
of the EMPLOYEE and DEPARTMENT relations in the WHERE clause, and then to select the
desired tuples and attributes.
Natural Join
If the names of the join attributes are not the same in the base relations, it is possible to rename
the attributes so that they match, and then to apply NATURAL JOIN. In this case, the AS
22 | P a g e
DBMS LAB IV SEMESTER
construct can be used to rename a relation and all its attributes in the FROM clause. This is
illustrated above where the DEPARTMENT relation is renamed as DEPT and its attributes are
renamed as Dname, Dno (to match the name of the desired join attribute Dno in the
EMPLOYEE table), Mssn, and Msdate. The implied join condition for this NATURAL JOIN
is EMPLOYEE.Dno = DEPT.Dno, because this is the only pair of attributes with the same
name after renaming:
The default type of join in a joined table is called an inner join, where a tuple is included in the
result only if a matching tuple exists in the other relation.
If the user requires that all employees be included, a different type of join called OUTER JOIN
must be used explicitly (see Section 8.4.4 for the definition of OUTER JOIN in relational
algebra). There are several variations of OUTER JOIN, as we shall see.
23 | P a g e
DBMS LAB IV SEMESTER
Experiment -4
Study and Implementation of: Group By & having clause, Order by clause ,
Indexing, Views , Sub queries
GROUPING
In many cases, we want to apply the aggregate functions to subgroups of tuples in a
relation
Each subgroup of tuples consists of the set of tuples that have the same value for the
grouping attribute(s)
The function is applied to each subgroup independently
SQL has a GROUP BY-clause for specifying the grouping attributes, which must also
appear in the SELECT-clause
Query 1: For each department, retrieve the department number, the number
of employees in the department, and their average salary.
SELECT DNO, COUNT (*), AVG (SALARY)
FROM EMPLOYEE GROUP BY DNO
In Q1, the EMPLOYEE tuples are divided into groups. Each group having the same
value for the grouping attribute DNO
The COUNT and AVG functions are applied to each such group of tuples separately
The SELECT-clause includes only the grouping attribute and the functions to be
applied on each group of tuples
A join condition can be used in conjunction with grouping
Query 2: For each project, retrieve the project number, project name, and the number of
employees who work on that project.
SELECT PNUMBER, PNAME, COUNT (*)
FROM PROJECT, WORKS_ON
WHERE PNUMBER=PNO
GROUP BY PNUMBER, PNAME
24 | P a g e
DBMS LAB IV SEMESTER
THE HAVING-CLAUSE
Sometimes we want to retrieve the values of these functions for only those groups that
satisfy certain conditions. The HAVING-clause is used for specifying a selection condition on
groups (rather than on individual tuples)
Query 3: For each project on which more than two employees work, retrieve the project
number, project name, and the number of employees who work on that project.
SELECT PNUMBER, PNAME, COUNT (*)
FROM PROJECT, WORKS_ON WHERE
PNUMBER=PNO GROUP BY PNUMBER,
PNAME HAVING COUNT (*) > 2
ORDER BY
The ORDER BY clause is used to sort the tuples in a query result based on the values
of some attribute(s)
Query 21: Retrieve a list of employees and the projects each works in, ordered by the
employee's department, and within each department ordered alphabetically by employee
last name.
Q21: SELECT DNAME, LNAME, FNAME, PNAME
FROM DEPARTMENT, EMPLOYEE, WORKS_ON, PROJECT
WHERE DNUMBER=DNO
AND SSN=ESSN
AND PNO=PNUMBER
ORDER BY DNAME, LNAME
The default order is in ascending order of values. We can specify the keyword DESC if
we want a descending order; the keyword ASC can be used to explicitly specify ascending
order, even though it is the default
Ex: ORDER BY DNAME DESC, LNAME ASC, FNAME ASC
SUB QUERIES:
Query1: Retrieve the names of all employees who have two or more dependents.
SELECT LNAME, FNAME FROM
EMPLOYEE
WHERE (SELECT COUNT (*) FROM DEPENDENT
WHERE SSN=ESSN) ≥ 2);
Query 2: List the names of managers who have least one dependent.
25 | P a g e
DBMS LAB IV SEMESTER
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE EXISTS (SELECT * FROM DEPENDENT WHERE SSN=ESSN)
VIEWS
A view in SQL terminology is a single table that is derived from other tables.6 These other
tables can be base tables or previously defined views. A view does not necessarily exist in
physical form; it is considered to be a virtual table, in contrast to base tables, whose tuples are
always physically stored in the database. This limits the possible update operations that can be
applied to views, but it does not provide any limitations on querying a view
In SQL, the command to specify a view is CREATE VIEW. The view is given a (virtual) table
name (or view name), a list of attribute names, and a query to specify the contents of the view.
INDEXING
An index is a schema object. It is used by the server to speed up the retrieval of rows by using
a pointer. It can reduce disk I/O(input/output) by using a rapid path access method to locate
data quickly.
An index helps to speed up select queries and where clauses, but it slows down data input,
with the update and the insert statements. Indexes can be created or dropped with no effect on
the data. In this article, we will see how to create, delete, and use the INDEX in the
database.
Creating an Index
Syntax:-
Removing an Index
Remove an index from the data dictionary by using the DROP INDEX command.
26 | P a g e
DBMS LAB IV SEMESTER
Syntax
To drop an index, you must be the owner of the index or have the DROP ANY
INDEX privilege.
Experiment-5
Identifying entities, attributes, keys and relationships between entities,
cardinalities, generalization, specialization etc. using Conceptual Designing
(Ex:- ER Diagrams).
For example a specific employee entity may have Name='John Smith', SSN='123456789',
Address ='731, Fondren, Houston, TX', Sex='M', BirthDate='09-JAN-55‘ .Each attribute has a
value set (or data type) associated with it – e.g. integer, string, date, enumerated type, …
A relationship relates two or more distinct entities with a specific meaning. For example,
EMPLOYEE John Smith works on the ProductX PROJECT, or EMPLOYEE Franklin Wong
manages the Research DEPARTMENT.
Relationships of the same type are grouped or typed into a relationship type.
For example, the WORKS_ON relationship type in which EMPLOYEEs and PROJECTs
participate, or the MANAGES relationship type in which EMPLOYEEs and DEPARTMENTs
participate.
The degree of a relationship type is the number of participating entity types. Both
MANAGES and WORKS_ON are binary relationships.
Experiment 6
2. For each pilot who is certified for more than three aircrafts, find the eid
and the maximum cruising range of the aircraft for which he/she is certified.
3. Find the names of all pilots whose salary is less than the price of the
cheapest route from Bangalore to Frankfurt.
4. For all aircrafts with cruising range over 1000 kms, find the name of the
aircraft and the average salary of all pilots certified for this aircraft
6. Find the aid's of all aircraft whose cruising range is greater than the
minimum distance from the routes from Bangalore to Delhi
TABLE CREATION
mysql> CREATE TABLE flight (no INT (11), from VARCHAR(20), too VARCHAR(20),
distance INT(11), departs VARCHAR (20), arrives VARCHAR(20), price double,
PRIMARY KEY (no) );
29 | P a g e
DBMS LAB IV SEMESTER
mysql> CREATE TABLE employees( eid INT(20), ename VARCHAR(20), salary INT (20),
PRIMARY KEY (eid) );
30 | P a g e
DBMS LAB IV SEMESTER
mysql> CREATE TABLE certified(eid INT(20), aid INT(10), PRIMARY KEY (eid,aid),
FOREIGN KEY (eid) REFERENCES employees (eid),FOREIGN KEY (aid) REFERENCES
aircraft (aid) );
32 | P a g e
DBMS LAB IV SEMESTER
INSERT INTO employees VALUES (1,'Ajay',30000);
INSERT INTO employees VALUES (2,'Ajith',85000);
INSERT INTO employees VALUES (3,'Arnab',50000);
INSERT INTO employees VALUES(4,'Harry',45000);
INSERT INTO employees VALUES(5,'Ron',90000);
INSERT INTO employees VALUES (6,'Josh',75000);
INSERT INTO employees VALUES (7,'Ram',100000);
33 | P a g e
DBMS LAB IV SEMESTER
Queries:
1.Find the names of aircraft such that all pilots certified to operate them have
salaries more than Rs 80,000.
2.For each pilot who is certified for more than three aircrafts, find the eid and the
maximum cruising range of the aircraft for which he/she is certified.
34 | P a g e
DBMS LAB IV SEMESTER
SELECT c.eid, MAX(cruisingrange) from certified c, aircraft a where c.aid = a.aid group by
c.eid having count(*)>3;
3.Find the names of all pilots whose salary is less than the price of the cheapest route from
Bangalore to Frankfurt.
SELECT DISTINCT e.ename from employees e where e.salary < (select MIN(f.price) from
flight f where f.frm = ‘Bangalore’ AND f.too = ‘Frankfurt’);
4.For all aircrafts with cruisingrange over 1000 kms,find the name of the aircraft
and the average salary of all pilots certified for this aircraft.
35 | P a g e
DBMS LAB IV SEMESTER
Select a.aid, a.aname, AVG(e.salary) from aircraft a, certified c, employees e where a.aid =
c.aid AND c.eid =e.eid AND a.cruisingrange > 1000 GROUP BY a.aid, a.aname;
SELECT distinct e. ename from employees e, aircraft a, certified c where e.eid = c.eid AND
c.aid = a.aid AND a. aname = “Boeing”;
6.Find the aid's of all aircraft whose crusing range is grater than the minimum
distance from Bangalore to Delhi.
36 | P a g e
DBMS LAB IV SEMESTER
Select aid from aircraft where cruisingrange > (Select min(distance) from flight where frm=
“Bangalore” and too= “Delhi”);
37 | P a g e
DBMS LAB IV SEMESTER
Experiment 7
Consider the schema for College Database and draw and ER Diagram.
STUDENT (USN, SName, Address, Phone, Gender) SEMSEC (SSID, Sem,
Sec)
CLASS (USN, SSID) SUBJECT (Subcode, Title, Sem, Credits) IAMARKS
(USN, Subcode, SSID, Test1, Test2, Test3, FinalIA)
Give these details only for 8th semester A, B, and C section students.
2,SHOW DATABASES;
38 | P a g e
DBMS LAB IV SEMESTER
3. USE COLLEGE;
Table created
39 | P a g e
DBMS LAB IV SEMESTER
3. Create Table for CLASS
CREATE TABLE CLASS(USN CHAR(10),SSID CHAR(2),CONSTRAINT D PRIMARY
KEY(USN,SSID),CONSTRAINT E FOREIGN KEY(USN) REFERENCES STUDENT(USN)
ON DELETE CASCADE,CONSTRAINT F FOREIGN KEY(SSID) REFERENCES
SEMSEC(SSID) ON DELETE CASCADE);
Table created
40 | P a g e
DBMS LAB IV SEMESTER
row-12 created
42 | P a g e
DBMS LAB IV SEMESTER
mysql> INSERT INTO SUBJECT(SUBCODE,TITLE,SEM,CREDITS) VALUES
('15CS21','M2',2,4),('15PCD23','PCD',2,4),('15CS42','SE',4,4),('15CS44','MP',4,4),('15CS64','C
G',6,4),('15CS62','USP',6,4),('10CS81','SA',8,4),('10CS842','ST',8,3);
row-8 created
43 | P a g e
DBMS LAB IV SEMESTER
Querie-1
Syntax:
SELECT S.USN,S.SNAME,S.ADDRESS,S.PHONE,S.GENDER
FROM STUDENT S,CLASS C,SEMSEC SS WHERE S.USN=C.USN AND
SS.SSID=C.SSID AND SS.SEM=4 AND SS.SEC='C';
Output:
Querie-2
44 | P a g e
DBMS LAB IV SEMESTER
2] Compute the total no.of male and female students in each semester and in each sec
Syntax:
SELECT SS.SEM,SS.SEC,S.GENDER,COUNT(S.GENDER) FROM STUDENT
S,SEMSEC SS,CLASS C WHERE S.USN=C.USN AND SS.SSID=C.SSID GROUP BY
SS.SEM,SS.SEC,S.GENDER;
Output:
Querie-3
Syntax:
CREATE VIEW TEST1_MARKS AS SELECT USN,SUBCODE,TEST1 FROM IAMARKS
WHERE USN='1MV15CS060';
View created
Output:
45 | P a g e
DBMS LAB IV SEMESTER
Update Commands:
Syntax:
UPDATE IAMARKS SET TEST1=19,TEST2=18,TEST3=17 WHERE USN='1MV14CS001';
1 row updated
Syntax:
UPDATE IAMARKS SET TEST1=11,TEST2=0,TEST3=14 WHERE USN='1MV14CS060';
1 row updated
Syntax:
UPDATE IAMARKS SET TEST1=10,TEST2=0,TEST3=7 WHERE USN='1MV14CS130';
1 row updated
Output:
46 | P a g e
DBMS LAB IV SEMESTER
Querie-4
4] Calculate the final IA marks and update the corresponding table for all students
Syntax:
UPDATE IAMARKS SET Finalia=(GREATEST (Test1+Test2,Test2+Test3,
Test3+Test1)/2);
Output:
Querie-5
Give these details only for 8th semester A, B and C section students
a) SELECT S.USN,S.SNAME,S.ADDRESS,S.PHONE,S.GENDER,
( CASE
WHEN IA.FINALIA BETWEEN 17 AND 20
THEN 'OUTSTANDING'
WHEN IA.FINALIA BETWEEN 12 AND 16
THEN 'AVERAGE'
ELSE 'WEAK'
END
) AS CAT
FROM STUDENT S,SEMSEC SS,IAMARKS IA
WHERE S.USN=IA.USN AND
SS.SSID=IA.SSID AND
SS.SEM=8;
Output:
48 | P a g e
DBMS LAB IV SEMESTER
Output:
49 | P a g e
DBMS LAB IV SEMESTER
For Example:
50 | P a g e