DBMS Lab Manual Program

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 65

PEO’s Program Educational Objectives Statements

PEO1 Be successful in solving engineering problems associated with computer science and
engineering domains.

PEO2 Work collaboratively on multidisciplinary projects and acquire high levels of


professionalism backed by ethics.

PEO3 Communicate effectively and exhibit leadership qualities, team spirit necessary for a
successful career in either industry, research or entrepreneurship.

PEO4 Continue to learn and advance their career through participation in the activities of
professional bodies, obtaining professional certification, pursue of higher education.

PSO’s Program Specific Outcome Statements

PSO1 Apply software engineering practices and strategies in diversified areas of computer
science for solving problems using open source environment.

PSO2 Develop suitable algorithms and codes for applications in areas of cognitive technology,
computer networks with software engineering principles and practices.

Program Outcomes

1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering


fundamentals, and an engineering specialization to the solution of complex engineering
problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems
and design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and
research methods including design of experiments, analysis and interpretation of data, and
synthesis of the information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to complex engineering
activities with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities
relevant to the professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and need
for sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and
write effective reports and design documentation, make effective presentations, and give and
receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological change.

Course Overview

Subject: DBMS Laboratory with Mini Project Subject Code: 21CSL58


A database management system (DBMS) is computer application software that
provides a way to manage data. The requirement of modern days is to have an automated
system that manages, modifies, and updates data accurately. This is achieved by a DBMS
in robust, correct, and non-redundant way. Structured Database Management Systems
(DBMS) based on relational and other models have long formed the basis for such
databases. Consequently, Oracle, Microsoft SQL Server, Sybase etc. have emerged as
leading commercial systems while MySQL, PostgreSQL etc. lead in open source and free
domain. The Course allows students to apply the conceptual design model to construct the
real-world requirement. Course gives familiarity of Database Concepts were students can
analyse the various constraints to populate the database and examine different working
concepts of DBMS to infer the most suitable pattern of documentation.
DBMS lab with mini project aims at practicing and achieving this aim by using
MySQL. While also gain capability to design database and its hierarchical structure for
given real world application.
Course Objectives

The objectives of this course are to make students to learn-


 Foundation knowledge in database concepts, technology, and practice to groom students into well-
informed database application developers.
 Strong practice in SQL programming through a variety of database problems.
 Develop database applications using front-end tools and back-end DBMS.

Course Outcomes

COs Description

21CSL55. Demonstrate the Basics Concepts and SQL Queries of Database Management System.
1
21CSL55. Apply the Conceptual Design Model and Database Hierarchical Structure to construct the
2 real-world requirement.
21CSL55. Analyze the various constraints to populate the database through SQL Queries.
3
21CSL55. Implement different working concepts of DBMS using SQL Queries.
4
21CSL55. Present the result of database creation and querying process, document it.
5
Syllabus

Subject: DBMS Laboratory with Mini Project Subject Code:18CSL58

Part-A: SQL Programming


Note:
 Design, develop, and implement the specified queries for the following problems using Oracle,
MySQL, MS SQL Server, or any other DBMS under LINUX/Windows environment.
 Create Schema and insert at least 5 records for each table. Add appropriate database constraints.
1. Consider the following schema for a Library Database:
BOOK(Book_id, Title, Publisher_Name, Pub_Year)
BOOK_AUTHORS(Book_id, Author_Name)
PUBLISHER(Name, Address, Phone)
BOOK_COPIES(Book_id, Programme_id, No-
of_Copies)
BOOK_LENDING(Book_id, Programme_id, Card_No, Date_Out,
Due_Date) LIBRARY_PROGRAMME(Programme_id,
Programme_Name, Address) Write SQL queries to
1. Retrieve details of all books in the library – id, title, name of publisher, authors, number of
copies in each Programme, etc.
2. Get the particulars of borrowers who have borrowed more than 3 books, but from Jan 2017 to
Jun 2017.
3. Delete a book in BOOK table. Update the contents of other tables to reflect this data
manipulation operation.
4. Partition the BOOK table based on year of publication. Demonstrate its working with a simple
query.
5. Create a view of all books and its number of copies that are currently available in the Library.
2. Consider the following schema for Order Database:
SALESMAN(Salesman_id, Name, City, Commission)
CUSTOMER(Customer_id, Cust_Name, City, Grade, Salesman_id)
ORDERS(Ord_No, Purchase_Amt, Ord_Date, Customer_id,
Salesman_id) Write SQL queries to
1. Count the customers with grades above Bangalore’s average.
2. Find the name and numbers of all salesman who had more than one customer.
3. List all the salesman and indicate those who have and do not have customers in their cities (Use
UNION operation.)
4. Create a view that finds the salesman who has the customer with the highest order of a day.
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his orders
must also be deleted.
3. Consider the schema for Movie Database:
ACTOR(Act_id, Act_Name, Act_Gender)
DIRECTOR(Dir_id, Dir_Name, Dir_Phone)
MOVIES(Mov_id, Mov_Title, Mov_Year, Mov_Lang,
Dir_id) MOVIE_CAST(Act_id, Mov_id, Role)
RATING(Mov_id,
Rev_Stars) Write SQL
queries to
1. List the titles of all movies directed by ‘Hitchcock’.
2. Find the movie names where one or more actors acted in two or more movies.
3. List all actors who acted in a movie before 2000 and in a movie after 2015 (use JOIN operation).
4. Find the title of movies and number of stars for each movie that has at least one rating and
find the highest number of stars that movie received. Sort the result by movie title.
5. Update rating of all movies directed by ‘Steven Spielberg’ to 5.
4. Consider the schema for College Database:
STUDENT(USN, SName, Address, Phone,
Gender) SEMSEC(SSID, Sem, Sec)
CLASS(USN, SSID)
COURSE(Subcode, Title, Sem, Credits)
IAMARKS(USN, Subcode, SSID, Test1, Test2, Test3,
FinalIA) Write SQL queries to
1. List all the student details studying in fourth semester ‘C’ section.
2. Compute the total number of male and female students in each semester and in each section.
3. Create a view of Test1 marks of student USN ‘1BI15CS101’ in all Courses.
4. Calculate the FinalIA (average of best two test marks) and update the corresponding
table for all students.
5. Categorize students based on the following
criterion: If FinalIA = 17 to 20 then CAT =
‘Outstanding’
If FinalIA = 12 to 16 then CAT =
‘Average’ If FinalIA< 12 then CAT =
‘Weak’
Give these details only for 8th semester A, B, and C section students.
5. Consider the schema for Company Database:
EMPLOYEE(SSN, Name, Address, Sex, Salary, SuperSSN,
DNo) DEPARTMENT(DNo, DName, MgrSSN,
MgrStartDate) DLOCATION(DNo,DLoc)
PROJECT(PNo, PName, PLocation,
DNo) WORKS_ON(SSN, PNo, Hours)
Write SQL queries to
1. Make a list of all project numbers for projects that involve an employee whose last name is
‘Scott’,
either as a worker or as a manager of the department that controls the project.
2. Show the resulting salaries if every employee working on the ‘IoT’ project is given a 10 percent
raise.
3. Find the sum of the salaries of all employees of the ‘Accounts’ department, as well as the
maximum salary, the minimum salary, and the average salary in this department.
4. Retrieve the name of each employee who works on all the projects controlled by department
number 5 (use NOT EXISTS operator).
5. For each department that has more than five employees, retrieve the department number and the
number
of its employees who are making more than Rs. 6,00,000.
Part-B: Mini Project
Note:
 Use Java, C#, PHP, Python, or any other similar front-end tool. All applications must be
demonstrated on desktop/laptop as a stand-alone or web-based application (Mobile apps on
Android/IOS are not permitted.)
For any problem selected Make sure that the application should have five or more tables.
Indicative areas include: health care.
Index

Subject: DBMS Laboratory with Mini Project Subject Code: 18CSL58

SL. Contents Page


No. No.

1 Basic Concepts of SQL 1-4

2 A: Lab Program-1 Library Database 5-15

3 B: Lab Program-2 Order Database 15-22

4 C: Lab Program-3 Movie Database 23-32

5 D: Lab Program-4 College Database 33-47

6 E: Lab Program-5 Company Database 48-60

7 Viva Questions & Answers 61-70


BASIC CONCEPTS OF SQL

Introduction to SQL
SQL stands for “Structured Query Language” and can be pronounced as “SQL” or
“sequel – (Structured English Query Language)”. It is a query language used for accessing and
modifying information in the database. IBM first developed SQL in 1970s. Also it is an
ANSI/ISO standard. It has become a Standard Universal Language used by most of the relational
database management systems (RDBMS). Some of the RDBMS systems are: Oracle, Microsoft
SQL server, Sybase etc. Most of these have provided their own implementation thus enhancing
its feature and making it a powerful tool. Few of the SQL commands used in SQL programming
are SELECT Statement, UPDATE Statement, INSERT INTO Statement, DELETE Statement,
WHERE Clause, ORDER BY Clause, GROUP BY Clause, ORDER Clause, Joins, Views,
GROUP Functions, Indexes etc.
SQL Commands
SQL commands are instructions used to communicate with the database to perform
specific task that work with data. SQL commands can be used not only for searching the
database but also to perform various other functions like, for example, you can create tables, add
data to tables, or modify data, drop the table, set permissions for users.

CREATE TABLE Statement


The CREATE TABLE Statement is used to create tables to store data. Integrity Constraints like
primary key, unique key and foreign key can be defined for the columns while creating the table. The
integrity constraints can be defined at column level or table level. The implementation and the syntax
of the CREATE Statements differs for different RDBMS.

The Syntax for the CREATE TABLE Statement is:

CREATE TABLE
table_name
(column_name1 datatype constraint,

column_name2 datatype,...

column_nameNdatatyp
e);
SQL Data Types:

char(size) Fixed-length character string. Size is specified in parenthesis. Max


255 bytes.
Varchar2( Variable-length character string. Max size is specified in
siz e) parenthesis.
number(si
ze Number value with a max number of column digits specified in
) parenthesis.
or int
Date Date value in „dd-mon-yy‟. Eg., ‟07-jul-2004‟

number(si Number value with a maximum number of digits of "size" total,


ze, d) or with a maximum number of "d" digits to the right of the decimal.
real

SQL Integrity Constraints:


Integrity Constraints are used to apply business rules for the database tables.The constraints
available in SQL are Foreign Key, Primary key, Not Null, Unique, Check.
Constraints can be defined in two ways:
1. The constraints can be specified immediately after the column definition. This is called column-
level definition.
2. The constraints can be specified after all the columns are defined. This is called table- level definition.
1) Primary key:
This constraint defines a column or combination of columns which uniquely identifies each row in
the table.
Syntax to define a Primary key at column level:

Column_namedatatype [CONSTRAINT constraint_name] PRIMARY KEY

Syntax to define a Primary key at table level:


[CONSTRAINTconstraint_name]PRIMARYKEY(column_name1,

column_name2,..)

2) Foreign key or Referential Integrity:


This constraint identifies any column referencing the PRIMARY KEY in another table. It
establishes a relationship between two columns in the same table or between different tables. For a
column to be defined as a Foreign Key, it should be a defined as a Primary Key in the table which it is
referring. One or more columns can be defined as foreign key.
Syntax to define a Foreign key at column level:
[CONSTRAINT constraint_name] REFERENCES

referenced_table_name(column_name)

3) Not Null Constraint:


This constraint ensures all rows in the table contain a definite value for the column which is
specified as not null. Which means a null value is not allowed.
Syntax to define a Not Null constraint:
[CONSTRAINT constraint name] NOT NULL

4) Unique Key:
This constraint ensures that a column or a group of columns in each row have a distinct value.
A column(s) can have a null value but the values cannot be duplicated.
Syntax to define a Unique key at column level:
[CONSTRAINT constraint_name] UNIQUE

Syntax to define a Unique key at table level:


[CONSTRAINT constraint_name] UNIQUE(column_name)

5) Check Constraint:
This constraint defines a business rule on a column. All the rows must satisfy this rule. The
constraint can be applied for a single column or a group of columns.

Syntax to define a Check constraint:

[CONSTRAINT constraint_name] CHECK (condition)

ALTER TABLE Statement


The SQL ALTER TABLE command is used to modify the definition structure) of a table by
modifying the definition of its columns. The ALTER command is used to perform the following
functions.
1) Add, drop, modify table columns
2) Add and drop constraints
3) Enable and Disable constraints
The HAVING clause
The HAVING clause can be used to restrict the display of grouped rows. The result of the
grouped query is passed on to the HAVING clause for output filtration.

The INSERT INTO Statement


The INSERT INTO statement is used to insert a new row in a table.
The UPDATE Statement

The UPDATE statement is used to update existing records in a table.

The DELETE Statement

The DELETE statement is used to delete rows in a table. SQL

DELETE

Commit command

Commit command is used to permanently save any transaaction into database

Rollback command
This command restores the database to last commited state. It is also use with savepoint command to
jump to a savepoint in a transaction.
Savepoint command

savepoint command is used to temporarily save a transaction so that you can rollback to that point
whenever necessary.
LAB EXPERIMENTS

PART A: SQL PROGRAMMING

A. Consider the following schema for a Library Database:

BOOK (Book_id, Title, Publisher_Name, Pub_Year)


BOOK_AUTHORS (Book_id, Author_Name)
PUBLISHER (Name, Address, Phone)
BOOK_COPIES (Book_id, Branch_id, No-
of_Copies)
BOOK_LENDING (Book_id, Branch_id, Card_No, Date_Out,
Due_Date) LIBRARY_BRANCH (Branch_id, Branch_Name, Address)

Write SQL queries to


1. Retrieve details of all books in the library – id, title, name of publisher, authors,
number of copies in each branch, etc.
2. Get the particulars of borrowers who have borrowed more than 3 books, but from Jan
2017 to Jun 2017
3. Delete a book in BOOK table. Update the contents of other tables to reflect this data
manipulation operation.
4. Partition the BOOK table based on year of publication. Demonstrate its working with
a simple query.
5. Create a view of all books and its number of copies that are currently available in the
Library.

Solution:
Entity-Relationship Diagram

Author_Name
Book_id Title

Pub_YearM N

written-by
Book Book_Authors
N

Has
N No_of_copies
Branch_id
Published-by
Publisher_Name

M M N
1 In
Branch_Name
Book_Copies Library_Branch
Address

N Address
Publisher Date_out
Book_Lending
Phone
Card_No
Due_date

N
Card
PROGRAM

Table Creations and Descriptions:-

>create table Publisher(Name varchar(20) primary key, Address varchar(50) not null,phone
varchar(10));

>desc Publisher;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| Name | varchar(20) | NO | PRI | NULL | |
| Address | varchar(50) | NO | | NULL | |
| phone | varchar(10) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+

> create table Book(Book_Id integer primary key,Title varchar(20) not


null,Publisher_Name varchar(20) references Publisher(Name) on delete Cascade, Pub_year
varchar(5));

> desc Book;


+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| Book_Id | int(11) | NO | PRI | NULL | |
| Title | varchar(20) | NO | | NULL | |
| Publisher_Name | varchar(20) | YES | | NULL | |
| Pub_year | varchar(5) | YES | | NULL | |
+----------------+-------------+------+-----+---------+-------+

> create table Book_Author(Book_Id integer references Book(Book_Id) on delete


cascade,Author_Name varchar(20) not null,primary key(Book_Id));

> desc Book_Author;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| Book_Id | int(11) | NO | PRI | NULL | |
| Author_Name | varchar(20) | NO | | NULL | |
+-------------+-------------+------+-----+---------+-------+

> create table Library_Branch(Branch_Id integer primary key,Branch_Name varchar(20) not


null,Address varchar(50));

> desc Library_Branch;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| Branch_Id | int(11) | NO | PRI | NULL | |
| Branch_Name | varchar(20) | NO | | NULL | |
| Address | varchar(50) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
> create table Book_Lending(Book_Id integer references Book(Book_Id) on delete
cascade,
-> Branch_Id integer references Library_Branch(Branch_Id) on delete cascade,
-> Card_No integer not null,Date_out date,Due_Date date,
-> primary key(Book_Id,Branch_Id));

> desc Book_Lending;


+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| Book_Id | int(11) | NO | PRI | NULL | |
| Branch_Id | int(11) | NO | PRI | NULL | |
| Card_No | int(11) | NO | | NULL | |
| Date_out | date | YES | | NULL | |
| Due_Date | date | YES | | NULL | |
+-----------+---------+------+-----+---------+-------+

> create table Book_Copies(Book_Id integer references Book(Book_Id) on delete


cascade,
-> Branch_Id integer references Library_Branch(Branch_Id) on delete cascade,
-> Num_of_Copies integer, primary key(Book_Id,Branch_Id));

> desc Book_Copies;


+---------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------+------+-----+---------+-------+
| Book_Id | int(11) | NO | PRI | NULL | |
| Branch_Id | int(11) | NO | PRI | NULL | |
| Num_of_Copies | int(11) | YES | | NULL | |
+---------------+---------+------+-----+---------+-------+

Insertion of values to table:-

Insertion Values For Publisher:

> insert into Publisher values('Pearson','New Delhi',9966551122);

> insert into Publisher values('Oxford','Mumbai',9911228877);

> insert into Publisher values('Mc Graw Hill','Chennai',9922115566);

> insert into Publisher values('O_Reilly','Manglore',9988443322);

> insert into Publisher values('Dreamtech','Maharashtra',9977664455);

> select * from Publisher;


+--------------+-------------+------------+
| Name | Address | phone |
+--------------+-------------+------------+
| Dreamtech | Maharashtra | 9977664455 |
| Mc Graw Hill | Chennai | 9922115566 |
| Oxford | Mumbai | 9911228877 |
| O_Reilly | Manglore | 9988443322 |
| Pearson | New Delhi | 9966551122 |
+--------------+-------------+------------+
Insertion Values For Book:

> insert into Book values(2001,'DBMS','Pearson',2015);

> insert into Book values(2002,'Computer Networks','Oxford',2019);

> insert into Book values(2003,'Java','Mc Graw Hill',2016);

> insert into Book values(2004,'C Programming','O_Reilly',2014);

> insert into Book values(2005,'PHP','Dreamtech',2017);

> select * from Book;


+---------+-------------------+----------------+----------+
| Book_Id | Title | Publisher_Name | Pub_year |
+---------+-------------------+----------------+----------+
| 2001 | DBMS | Pearson | 2015 |
| 2002 | Computer Networks | Oxford | 2019 |
| 2003 | Java | Mc Graw Hill | 2016 |
| 2004 | C Programming | O_Reilly | 2014 |
| 2005 | PHP | Dreamtech | 2017 |
+---------+-------------------+----------------+----------+

Insertion Values For Book_Author:

> insert into Book_Author values(2001,'Kanishka Bedi');

> insert into Book_Author values(2002,'Poornima M');

> insert into Book_Author values(2003,'P C Tripathi');

> insert into Book_Author values(2004,'P N Reddy');

> insert into Book_Author values(2005,'Vishwa Kiran');

> select * from Book_Author;


+---------+---------------+
| Book_Id | Author_Name |
+---------+---------------+
| 2001 | Kanishka Bedi |
| 2002 | Poornima M |
| 2003 | P C Tripathi |
| 2004 | P N Reddy |
| 2005 | Vishwa Kiran |
+---------+---------------+

Insertion Values For Library_Branch:

> insert into Library_Branch values(3001,'R T Nagar','Banglore');

> insert into Library_Branch values(3002,'Malleswaram','Banglore');

> insert into Library_Branch values(3003,'Sector 21','Noida');

> insert into Library_Branch values(3004,'VTU','Belgaum');

> insert into Library_Branch values(3005,'Yelhanka','Banglore');


> select * from Library_Branch;
+-----------+-------------+----------+
| Branch_Id | Branch_Name | Address |
+-----------+-------------+----------+
| 3001 | R T Nagar | Banglore |
| 3002 | Malleswaram | Banglore |
| 3003 | Sector 21 | Noida |
| 3004 | VTU | Belgaum |
| 3005 | Yelhanka | Banglore |
+-----------+-------------+----------+

Insertion Values For Book_Lending:

> insert into Book_Lending values(2001,3001,4001,'2017-01-02','2017-02-02');

> insert into Book_Lending values(2002,3002,4001,'2017-01-07','2017-02-07');

> insert into Book_Lending values(2003,3003,4003,'2017-01-10','2017-02-10');

> insert into Book_Lending values(2004,3004,4004,'2017-01-15','2017-02-15');

> insert into Book_Lending values(2005,3005,4005,'2017-01-20','2017-02-20');

> select * from Book_Lending;


+---------+-----------+---------+------------+------------+
| Book_Id | Branch_Id | Card_No | Date_out | Due_Date |
+---------+-----------+---------+------------+------------+
| 2001 | 3001 | 4001 | 2017-01-02 | 2017-02-02 |
| 2002 | 3002 | 4001 | 2017-01-07 | 2017-02-07 |
| 2003 | 3003 | 4001 | 2017-01-10 | 2017-02-10 |
| 2004 | 3004 | 4001 | 2017-01-15 | 2017-02-15 |
| 2005 | 3005 | 4005 | 2017-01-20 | 2017-02-20 |
+---------+-----------+---------+------------+------------+

Insertion Values For Book_copies:

> insert into Book_Copies values(2001,3001,10);

> insert into Book_Copies values(2002,3002,15);

> insert into Book_Copies values(2003,3003,10);

> insert into Book_Copies values(2004,3004,05);

> insert into Book_Copies values(2005,3005,20);

> select * from Book_Copies;


+---------+-----------+---------------+
| Book_Id | Branch_Id | Num_of_Copies |
+---------+-----------+---------------+
| 2001 | 3001 | 10 |
| 2002 | 3002 | 15 |
| 2003 | 3003 | 10 |
| 2004 | 3004 | 5 |
| 2005 | 3005 | 20 |
+---------+-----------+---------------+
QUERIES

1. Retrieve details of all books in the library – id, title, name of


publisher, authors, number of copies in each branch, etc.

> select
B.Book_Id,B.Title,B.Publisher_Name,A.Author_Name,C.Num_of_Copies,C.Branch_Id
-> from Book B,Book_Author A, Book_Copies C where B.Book_Id=A.Book_Id and
B.Book_Id=C.Book_Id;

+---------+-------------------+----------------+---------------+---------------
+-----------+
| Book_Id | Title | Publisher_Name | Author_Name | Num_of_Copies |
Branch_Id |
+---------+-------------------+----------------+---------------+---------------
+-----------+
| 2001 | DBMS | Pearson | Kanishka Bedi | 10 |
3001 |
| 2002 | Computer Networks | Oxford | Poornima M | 15 |
3002 |
| 2003 | Java | Mc Graw Hill | P C Tripathi | 10 |
3003 |
| 2004 | C Programming | O_Reilly | P N Reddy | 5 |
3004 |
| 2005 | PHP | Dreamtech | Vishwa Kiran | 20 |
3005 |
+---------+-------------------+----------------+---------------+---------------
+-----------+

2. Get the particulars of borrowers who have borrowed more than 3


books, but from Jan 2017 to JuL 2017.

MariaDB [library_database]> select L.Card_No from Book_Lending L where Date_out


between '2017-01-02' and '2017-07-01' group by L.Card_No having count(Card_No)>3;
+---------+
| Card_No |
+---------+
| 4001 |
+---------+

3. Delete a book in BOOK table. Update the contents of other tables to


reflect this data manipulation operation.

> select * from Book;


+---------+-------------------+----------------+----------+
| Book_Id | Title | Publisher_Name | Pub_year |
+---------+-------------------+----------------+----------+
| 2001 | DBMS | Pearson | 2015 |
| 2002 | Computer Networks | Oxford | 2019 |
| 2003 | Java | Mc Graw Hill | 2016 |
| 2004 | C Programming | O_Reilly | 2014 |
| 2005 | PHP | Dreamtech | 2017 |
+---------+-------------------+----------------+----------+

> delete from Book where Book_Id=2004;


> select * from Book;
+---------+-------------------+----------------+----------+
| Book_Id | Title | Publisher_Name | Pub_year |
+---------+-------------------+----------------+----------+
| 2001 | DBMS | Pearson | 2015 |
| 2002 | Computer Networks | Oxford | 2019 |
| 2003 | Java | Mc Graw Hill | 2016 |
| 2005 | PHP | Dreamtech | 2017 |
+---------+-------------------+----------------+----------+

4. Partition the BOOK table based on year of publication. Demonstrate its


working with a simple query.

> create view vw_Pub_year as select pub_year from Book;

> select * from vw_Pub_year;

+----------+
| pub_year |
+----------+
| 2015 |
| 2019 |
| 2016 |
| 2017 |
+----------+

5. Create a view of all books and its number of copies that are currently
available in the Library.

> create view vw_BK_Copies as select B.Book_Id,B.Title,C.Num_of_Copies from Book B,


-> Book_Copies C where B.Book_Id=C.Book_Id;

> select * from vw_BK_Copies;


+---------+-------------------+---------------+
| Book_Id | Title | Num_of_Copies |
+---------+-------------------+---------------+
| 2001 | DBMS | 10 |
| 2002 | Computer Networks | 15 |
| 2003 | Java | 10 |
| 2005 | PHP | 20 |
+---------+-------------------+---------------+

2 .Consider the following schema for Order Database:

SALESMAN (Salesman_id, Name, City, Commission)


CUSTOMER (Customer_id, Cust_Name, City, Grade,
Salesman_id)
ORDERS (Ord_No, Purchase_Amt, Ord_Date, Customer_id,
Salesman_id) Write SQL queries to
1. Count the customers with grades above Bangalore’s average.
2. Find the name and numbers of all salesmen who had more than one customer.
3. List all salesmen and indicate those who have and don’t have customers in their cities
(Use UNION operation.)
4. Create a view that finds the salesman who has the customer with the highest order of a
day.
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his
orders must also be deleted.

Solution:

Entity-Relationship Diagram
Program CREATE DATABASE

> create database order_database;

> use order_database;


Database changed

TABLE CREATION:

SALESMAN:

> create table SALESMAN(Salesman_id int primary key, Name varchar(20), City
varchar(50), Commission int);

> desc SALESMAN;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| Salesman_id | int(11) | NO | PRI | NULL | |
| Name | varchar(20) | YES | | NULL | |
| City | varchar(50) | YES | | NULL | |
| Commission | int(11) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+

CUSTOMER:

> create table CUSTOMER(Customer_id int primary key, Cust_Name varchar(20) not
null, City varchar(20),Grade int, Salesman_id int,
-> foreign key(Salesman_id) references SALESMAN(Salesman_id) on delete set null);

> desc CUSTOMER;


+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| Customer_id | int(11) | NO | PRI | NULL | |
| Cust_Name | varchar(20) | NO | | NULL | |
| City | varchar(20) | YES | | NULL | |
| Grade | int(11) | YES | | NULL | |
| Salesman_id | int(11) | YES | MUL | NULL | |
+-------------+-------------+------+-----+---------+-------+

ORDERS:

> create table ORDERS(Ord_No int primary key,Purchase_Amt int,Ord_Date


date,Customer_id int,Salesman_id int,
-> foreign key(Customer_id) references CUSTOMER(Customer_id) on delete cascade,
-> foreign key(Salesman_id) references SALESMAN(Salesman_id) on delete cascade);
> desc ORDERS;
+--------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+-------+
| Ord_No | int(11) | NO | PRI | NULL | |
| Purchase_Amt | int(11) | YES | | NULL | |
| Ord_Date | date | YES | | NULL | |
| Customer_id | int(11) | YES | MUL | NULL | |
| Salesman_id | int(11) | YES | MUL | NULL | |
+--------------+---------+------+-----+---------+-------+

INSERTION OF VALUES TO TABLE:

SALESMAN:

> insert into SALESMAN values(1000,'Vinay','Banglore','25');

> insert into SALESMAN values(2000,'Ravi','Banglore','20');

> insert into SALESMAN values(3000,'Pavan','Mysore','15');

> insert into SALESMAN values(4000,'Smith','Delhi','30');

> insert into SALESMAN values(5000,'Mythili','Manglore','15');

> select * from SALESMAN;

+-------------+---------+----------+------------+
| Salesman_id | Name | City | Commission |
+-------------+---------+----------+------------+
| 1000 | Vinay | Banglore | 25 |
| 2000 | Ravi | Banglore | 20 |
| 3000 | Pavan | Mysore | 15 |
| 4000 | Smith | Delhi | 30 |
| 5000 | Mythili | Manglore | 15 |
+-------------+---------+----------+------------+

CUSTOMER:

> insert into CUSTOMER values(11,'Preethi','Udupi',100,1000);

> insert into CUSTOMER values(22,'Poojavan','Banglore',200,1000);

> insert into CUSTOMER values(33,'Hemanth','Banglore',300,2000);

> insert into CUSTOMER values(44,'Chethan','Mandya',200,3000);

> insert into CUSTOMER values(55,'Savitha','Banglore',400,4000);

> select * from CUSTOMER;

+-------------+-----------+----------+-------+-------------+
| Customer_id | Cust_Name | City | Grade | Salesman_id |
+-------------+-----------+----------+-------+-------------+
| 11 | Preethi | Udupi | 100 | 1000 |
| 22 | Poojavan | Banglore | 200 | 1000 |
| 33 | Hemanth | Banglore | 300 | 2000 |
| 44 | Chethan | Mandya | 200 | 3000 |
| 55 | Savitha | Banglore | 400 | 4000 |
+-------------+-----------+----------+-------+-------------+

ORDERS:

> insert into ORDERS values(91,4230,'2017-05-04',11,1000);

> insert into ORDERS values(92,540,'2017-01-20',11,2000);

> insert into ORDERS values(93,8999,'2017-02-24',33,3000);

> insert into ORDERS values(94,1500,'2017-04-13',44,2000);

> insert into ORDERS values(95,599,'2017-03-09',22,2000);

> select * from ORDERS;


+--------+--------------+------------+-------------+-------------+
| Ord_No | Purchase_Amt | Ord_Date | Customer_id | Salesman_id |
+--------+--------------+------------+-------------+-------------+
| 91 | 4230 | 2017-05-04 | 11 | 1000 |
| 92 | 540 | 2017-01-20 | 11 | 2000 |
| 93 | 8999 | 2017-02-24 | 33 | 3000 |
| 94 | 1500 | 2017-04-13 | 44 | 2000 |
| 95 | 599 | 2017-03-09 | 22 | 2000 |
+--------+--------------+------------+-------------+-------------+

SQL QUERIES:

1. Count the customers with grades above Bangalore’s average.

> select count(Customer_id),Grade,Customer_id from CUSTOMER


-> where Grade>(select avg(Grade) from CUSTOMER where City like '%Banglore%')
-> group by Grade,Customer_id;

+--------------------+-------+-------------+
| count(Customer_id) | Grade | Customer_id |
+--------------------+-------+-------------+
| 1 | 400 | 55 |
+--------------------+-------+-------------+

2. Find the name and numbers of all salesman who had more than one customer.

> select Name,count(Customer_id) from SALESMAN S, CUSTOMER C where


-> S.Salesman_id=C.Salesman_id group by Name having count(Customer_id)>1;

+-------+--------------------+
| Name | count(Customer_id) |
+-------+--------------------+
| Vinay | 2 |
+-------+--------------------+
3. List all the salesman and indicate those who have and don’t have customers in
their cities (Use UNION operation.)

> select S.Salesman_id, Name,Cust_Name,Commission from SALESMAN S,CUSTOMER C


-> where S.City=C.City
-> union
-> select Salesman_id,Name,'no match',Commission from SALESMAN where not
City=any(select City from CUSTOMER) order by 2 desc;

+-------------+---------+-----------+------------+
| Salesman_id | Name | Cust_Name | Commission |
+-------------+---------+-----------+------------+
| 1000 | Vinay | Savitha | 25 |
| 1000 | Vinay | Poojavan | 25 |
| 1000 | Vinay | Hemanth | 25 |
| 4000 | Smith | no match | 30 |
| 2000 | Ravi | Poojavan | 20 |
| 2000 | Ravi | Hemanth | 20 |
| 2000 | Ravi | Savitha | 20 |
| 3000 | Pavan | no match | 15 |
| 5000 | Mythili | no match | 15 |
+-------------+---------+-----------+------------+

4. Create a view that finds the salesman who has the customer with the highest
order of a day.

> create view sales_highorder1 as


-> select b.Ord_Date,a.Salesman_id,a.Name
-> from SALESMAN a, ORDERS b
-> where a.Salesman_id=b.Salesman_id
-> and b.Purchase_Amt=(select max(Purchase_Amt) from ORDERS c
-> where c.Ord_Date=b.Ord_Date);

> select * from sales_highorder1;

+------------+-------------+-------+
| Ord_Date | Salesman_id | Name |
+------------+-------------+-------+
| 2017-05-04 | 1000 | Vinay |
| 2017-01-20 | 2000 | Ravi |
| 2017-02-24 | 3000 | Pavan |
| 2017-04-13 | 2000 | Ravi |
| 2017-03-09 | 2000 | Ravi |
+------------+-------------+-------+

5. Demonstrate the DELETE operation by removing salesman with id 1000. All his
orders must also be deleted.

> delete from SALESMAN where Salesman_id=1000;

> select * from SALESMAN;


+-------------+---------+----------+------------+
| Salesman_id | Name | City | Commission |
+-------------+---------+----------+------------+
| 2000 | Ravi | Banglore | 20 |
| 3000 | Pavan | Mysore | 15 |
| 4000 | Smith | Delhi | 30 |
| 5000 | Mythili | Manglore | 15 |
+-------------+---------+----------+------------+

> select * from ORDERS;

+--------+--------------+------------+-------------+-------------+
| Ord_No | Purchase_Amt | Ord_Date | Customer_id | Salesman_id |
+--------+--------------+------------+-------------+-------------+
| 92 | 540 | 2017-01-20 | 11 | 2000 |
| 93 | 8999 | 2017-02-24 | 33 | 3000 |
| 94 | 1500 | 2017-04-13 | 44 | 2000 |
| 95 | 599 | 2017-03-09 | 22 | 2000 |
+--------+--------------+------------+-------------+-------------+

3. Consider the schema for Movie Database:

ACTOR (Act_id, Act_Name,


Act_Gender) DIRECTOR (Dir_id,
Dir_Name, Dir_Phone)
MOVIES (Mov_id, Mov_Title, Mov_Year, Mov_Lang,
Dir_id) MOVIE_CAST (Act_id, Mov_id, Role)
RATING

(Mov_id, Rev_Stars) Write


SQL queries to
1. List the titles of all movies directed by ‘Hitchcock’.
2. Find the movie names where one or more actors acted in two or more
movies.
3. List all actors who acted in a movie before 2000 and also in a movie after 2015
(use JOIN operation).
4. Find the title of movies and number of stars for each movie
that has at least one rating and find the highest number of
stars that movie received. Sort the result by movie title.
5. Update rating of all movies directed by ‘Steven Spielberg’ to 5.

Solution:

Entity-Relationship Diagram
Dir_id Dir_Name
Act_id Act_Name

Dir_Phone
Act_Gender Actor Director
1

M
Has
Movie_Cast
N

Role
Rev_Stars
N
Movies

Mov_Lang
Mov_id

Mov_Title Mov_Year
CREATE DATABASE:

> create database movie_database;

> use movie_database;


Database changed

TABLE CREATION:

ACTOR:

> create table ACTOR(act_id int,act_name varchar(20),act_gender char(8), primary


key(act_id));

> desc ACTOR;

+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| act_id | int(11) | NO | PRI | NULL | |
| act_name | varchar(20) | YES | | NULL | |
| act_gender | char(8) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+

DIRECTOR:

> create table DIRECTOR(dir_id int,dir_name varchar(20),dir_phone


varchar(10),primary key(dir_id));

> desc DIRECTOR;

+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| dir_id | int(11) | NO | PRI | NULL | |
| dir_name | varchar(20) | YES | | NULL | |
| dir_phone | varchar(10) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+

MOVIES:

> create table MOVIES(mov_id int primary key,mov_title varchar(20),mov_year


int,mov_lang varchar(15),
-> dir_id int,foreign key(dir_id) references DIRECTOR(dir_id));

> desc MOVIES;

+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| mov_id | int(11) | NO | PRI | NULL | |
| mov_title | varchar(20) | YES | | NULL | |
| mov_year | int(11) | YES | | NULL | |
| mov_lang | varchar(15) | YES | | NULL | |
| dir_id | int(11) | YES | MUL | NULL | |
+-----------+-------------+------+-----+---------+-------+

MOVIE_CAST:
> create table MOVIE_CAST(act_id int,mov_id int,role varchar(20),
-> primary key(act_id,mov_id),
-> foreign key(act_id) references ACTOR(act_id),
-> foreign key(mov_id) references MOVIES(mov_id));

> desc MOVIE_CAST;

+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| act_id | int(11) | NO | PRI | NULL | |
| mov_id | int(11) | NO | PRI | NULL | |
| role | varchar(20) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+

RATING:

> create table RATING(mov_id int,rev_stars int,primary key(mov_id),


-> foreign key(mov_id) references MOVIES(mov_id));

> desc RATING;

+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| mov_id | int(11) | NO | PRI | NULL | |
| rev_stars | int(11) | YES | | NULL | |
+-----------+---------+------+-----+---------+-------+

INSERTION OF VALUES TO TABLES:

ACTOR:

> insert into ACTOR values(31,'Anushka','F');

> insert into ACTOR values(32,'Ajith','M');

> insert into ACTOR values(33,'Keerthi','F');

> insert into ACTOR values(34,'James','M');

> insert into ACTOR values(35,'Jackiechan','M');

> insert into ACTOR values(36,'Jeremy','M');

> select * from ACTOR;

+--------+------------+------------+
| act_id | act_name | act_gender |
+--------+------------+------------+
| 31 | Anushka | F |
| 32 | Ajith | M |
| 33 | Keerthi | F |
| 34 | James | M |
| 35 | Jackiechan | M |
| 36 | Jeremy | M |
+--------+------------+------------+

DIRECTOR:

> insert into DIRECTOR values(51,'Hitchcock',9966553322);

> insert into DIRECTOR values(52,'Herald',9988556611);

> insert into DIRECTOR values(53,'Rajamouli',9977558833);

> insert into DIRECTOR values(54,'Kishore',8877995566);

> insert into DIRECTOR values(55,'samMendes',7979808066);

> insert into DIRECTOR values(56,'venkat',7975797588);

> insert into DIRECTOR values(57,'steven speilberg',9900114455);

> select * from DIRECTOR;

+--------+------------------+------------+
| dir_id | dir_name | dir_phone |
+--------+------------------+------------+
| 51 | Hitchcock | 9966553322 |
| 52 | Herald | 9988556611 |
| 53 | Rajamouli | 9977558833 |
| 54 | Kishore | 8877995566 |
| 55 | samMendes | 7979808066 |
| 56 | venkat | 7975797588 |
| 57 | steven speilberg | 9900114455 |
+--------+------------------+------------+

MOVIES:

> insert into MOVIES values(11,'Skyfall',2012,'English',55);

> insert into MOVIES values(12,'NenuShilaja',2016,'Telugu',54);

> insert into MOVIES values(13,'KarateKid',2010,'English',52);

> insert into MOVIES values(14,'Bahubali',2017,'Telugu',53);

> insert into MOVIES values(15,'TheBirds',1963,'English',51);

> insert into MOVIES values(16,'Gambler',2011,'Tamil',56);

> insert into MOVIES values(17,'WarHouse',2011,'English',57);

> insert into MOVIES values(18,'Physco',1975,'English',51);

> select * from MOVIES;

+--------+-------------+----------+----------+--------+
| mov_id | mov_title | mov_year | mov_lang | dir_id |
+--------+-------------+----------+----------+--------+
| 11 | Skyfall | 2012 | English | 55 |
| 12 | NenuShilaja | 2016 | Telugu | 54 |
| 13 | KarateKid | 2010 | English | 52 |
| 14 | Bahubali | 2017 | Telugu | 53 |
| 15 | TheBirds | 1963 | English | 51 |
| 16 | Gambler | 2011 | Tamil | 56 |
| 17 | WarHouse | 2011 | English | 57 |
| 18 | Physco | 1975 | English | 51 |
+--------+-------------+----------+----------+--------+

MOVIE_CAST:

> insert into MOVIE_CAST values(36,17,'Hero');

> insert into MOVIE_CAST values(32,16,'Villain');

> insert into MOVIE_CAST values(33,12,'Heroine');

> insert into MOVIE_CAST values(34,15,'Hero');

> insert into MOVIE_CAST values(35,13,'Hero');

> insert into MOVIE_CAST values(31,14,'Heroine');

> insert into MOVIE_CAST values(34,11,'Hero');

> select * from MOVIE_CAST;

+--------+--------+---------+
| act_id | mov_id | role |
+--------+--------+---------+
| 31 | 14 | Heroine |
| 32 | 16 | Villain |
| 33 | 12 | Heroine |
| 34 | 11 | Hero |
| 34 | 15 | Hero |
| 35 | 13 | Hero |
| 36 | 17 | Hero |
+--------+--------+---------+

RATING:

> insert into RATING values(11,4);

> insert into RATING values(12,5);

> insert into RATING values(13,4);

> insert into RATING values(14,4);

> insert into RATING values(15,3);

> insert into RATING values(16,3);

> insert into RATING values(17,4);


> select * from RATING;

+--------+-----------+
| mov_id | rev_stars |
+--------+-----------+
| 11 | 4 |
| 12 | 5 |
| 13 | 4 |
| 14 | 4 |
| 15 | 3 |
| 16 | 3 |
| 17 | 4 |
+--------+-----------+

WRITE SQL QUERIES TO

1. List the titles of all movies directed by ‘Hitchcock’.

> select mov_title from MOVIES m, DIRECTOR d


-> where d.dir_id=m.dir_id and dir_name='Hitchcock';

+-----------+
| mov_title |
+-----------+
| TheBirds |
| Physco |
+-----------+

2. Find the movie names where one or more actors acted in two or more movies.

> select mov_title, m.mov_id, mc.act_id from MOVIES m,


-> MOVIE_CAST mc where m.mov_id=mc.mov_id and mc.act_id in
-> (select act_id from MOVIE_CAST group by act_id having count(mov_id)>=2);
+-----------+--------+--------+
| mov_title | mov_id | act_id |
+-----------+--------+--------+
| Skyfall | 11 | 34 |
| TheBirds | 15 | 34 |
+-----------+--------+--------+

3. List all actors who acted in a movie before 2000 and also in a movie after 2015
(use JOIN
operation).

> select act_name, mov_title, mov_year


-> from ACTOR a join MOVIE_CAST c on a.act_id=c.act_id
-> join MOVIES m on c.mov_id=m.mov_id
-> where m.mov_year not between 2000 and 2015;
+----------+-------------+----------+
| act_name | mov_title | mov_year |
+----------+-------------+----------+
| Anushka | Bahubali | 2017 |
| Keerthi | MenuShilaja | 2016 |
| James | TheBirds | 1963 |
+----------+-------------+----------+

4. Find the title of movies and number of stars for each movie that has at least one
rating and
find the highest number of stars that movie received. Sort the result by movie title.

> select mov_title, rev_stars


-> from MOVIES m, RATING r
-> where m.mov_id=r.mov_id and rev_stars>=1 order by mov_title;
+-------------+-----------+
| mov_title | rev_stars |
+-------------+-----------+
| Bahubali | 4 |
| Gambler | 3 |
| KarateKid | 4 |
| MenuShilaja | 5 |
| Skyfall | 4 |
| TheBirds | 3 |
| WarHouse | 4 |
+-------------+-----------+

5. Update rating of all movies directed by ‘Steven Spielberg’ to 5.

> update RATING set rev_stars=5


-> where mov_id in(select mov_id from MOVIES m,DIRECTOR d where
-> m.dir_id=d.dir_id and dir_name='steven speilberg'); Rows

matched: 1 Changed: 1 Warnings: 0

> SELECT * from RATING;


+--------+-----------+
| mov_id | rev_stars |
+--------+-----------+
| 11 | 4 |
| 12 | 5 |
| 13 | 4 |
| 14 | 4 |
| 15 | 3 |
| 16 | 3 |
| 17 | 5 |
+--------+-----------+

B. Consider the schema for College Database:

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) Write SQL
queries to
1. List all the student details studying in fourth semester ‘C’ section.
2. Compute the total number of male and female students in
each semester and in each section.
3. Create a view of Test1 marks of student USN ‘1BI15CS101’ in all subjects.
4. Calculate the FinalIA (average of best two test
marks) and update the corresponding table for all students.
5. Categorize students based on the following criterion:
If FinalIA = 17 to 20 then CAT = ‘Outstanding’
If FinalIA = 12 to 16 then CAT = ‘Average’ If
FinalIA< 12 then CAT = ‘Weak’
Give these details only for 8th semester A, B,
and C section students. Solution:

Entity - Relationship Diagram


Schema Diagram

Lab Program:4

Consider the schema for College Database:


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)

CREATE DATABASE:

create database student_database;

> use student_database;


Database changed

TABLE CREATION:

STUDENT:

> create table STUDENT (USN varchar(10) primary key,SName varchar(20),


Address varchar(25),Phone varchar(10),Gender char(1));

> desc STUDENT;


+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| USN | varchar(10) | NO | PRI | NULL | |
| SName | varchar(20) | YES | | NULL | |
| Address | varchar(25) | YES | | NULL | |
| Phone | varchar(10) | YES | | NULL | |
| Gender | char(1) | YES | | NULL | |
+ + + + + + +

SEMSEC:

> create table SEMSEC(SSID varchar(5) primary key,Sem int,Sec char(1));

> desc SEMSEC;


+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| SSID | varchar(5) | NO | PRI | NULL | |
| Sem | int(11) | YES | | NULL | |
| Sec | char(1) | YES | | NULL | |
+ + + + + + +

CLASS:

> create table CLASS(USN varchar(10),SSID varchar(5),primary


key(USN,SSID),
-> foreign key(USN) references STUDENT(USN),
-> foreign key(SSID) references SEMSEC(SSID));

> desc CLASS;


+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| USN | varchar(10) | NO | PRI | NULL | |
| SSID | varchar(5) | NO | PRI | NULL | |
+ + + + + + +

SUBJECT:

> create table SUBJECT(Subcode varchar(8) primary key, Title


varchar(30),Sem int,Credits int);

> desc SUBJECT;


+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| Subcode | varchar(8) | NO | PRI | NULL | |
| Title | varchar(30) | YES | | NULL | |
| Sem | int(11) | YES | | NULL | |
| Credits | int(11) | YES | | NULL | |
+ + + + + + +

IAMARKS:

> create table IAMARKS(USN varchar(10), Subcode varchar(8), SSID


varchar(5),
-> Test1 int, Test2 int, Test3 int, FinalIA int,
-> primary key(USN,Subcode,SSID),
-> foreign key(USN) references STUDENT(USN),
-> foreign key(Subcode) references SUBJECT(Subcode),
-> foreign key(SSID) references SEMSEC(SSID));

> desc IAMARKS;


+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| USN | varchar(10) | NO | PRI | NULL | |
| Subcode | varchar(8) | NO | PRI | NULL | |
| SSID | varchar(5) | NO | PRI | NULL | |
| Test1 | int(11) | YES | | NULL | |
| Test2 | int(11) | YES | | NULL | |
| Test3 | int(11) | YES | | NULL | |
| FinalIA | int(11) | YES | | NULL | |
+ + + + + + +

INSERTION OF VALUES TO TABLE:

STUDENT:

> insert into STUDENT


values('1BI17CS101','Soumya','Banglore',7766554411,'F');

> insert into STUDENT


values('1BI17CS102','Supritha','Hassan',8877664411,'F');

> insert into STUDENT


values('1BI17CS103','Aishwarya','Mysore',9911887766,'F');
> insert into STUDENT
values('1BI17CS104','Anitha','Mandya',9977009977,'F');

> insert into STUDENT


values('1BI17CS105','Raghu','Tumkur',8866001122,'M');

> insert into STUDENT


values('1BI17CS106','Prajwal','Manglore',7766990011,'M');

> insert into STUDENT


values('1BI17CS107','Sunil','Dharwad',7766565110,'M');

> insert into STUDENT values('1BI17CS108','Uday','Hubli',9911008866,'M');

> insert into STUDENT


values('1BI17CS109','Ramya','Banglore',7755440000,'F');

> insert into STUDENT


values('1BI17CS110','Ankitha','Belur',9980011223,'F');

> select * from STUDENT;


+ + + + + +
| USN | SName | Address | Phone | Gender |
+ + + + + +
| 1BI17CS101 | Soumya | Banglore | 7766554411 | F |
| 1BI17CS102 | Supritha | Hassan | 8877664411 | F |
| 1BI17CS103 | Aishwarya | Mysore | 9911887766 | F |
| 1BI17CS104 | Anitha | Mandya | 9977009977 | F |
| 1BI17CS105 | Raghu | Tumkur | 8866001122 | M |
| 1BI17CS106 | Prajwal | Manglore | 7766990011 | M |
| 1BI17CS107 | Sunil | Dharwad | 7766565110 | M |
| 1BI17CS108 | Uday | Hubli | 9911008866 | M |
| 1BI17CS109 | Ramya | Banglore | 7755440000 | F |
| 1BI17CS110 | Ankitha | Belur | 9980011223 | F |
+ + + + + +

SEMSEC;

> insert into SEMSEC values('5A',5,'A');

> insert into SEMSEC values('5B',5,'B');

> insert into SEMSEC values('4B',4,'B');

> insert into SEMSEC values('4C',4,'C');

> insert into SEMSEC values('8A',8,'A');

> insert into SEMSEC values('8B',8,'B');

> insert into SEMSEC values('8C',8,'C');

> select * from SEMSEC;


+ + + +
| SSID | Sem | Sec |
+ + + +
| 4B | 4 | B |
| 4C | 4 | C |
| 5A | 5 | A |
| 5B | 5 | B |
| 8A | 8 | A |
| 8B | 8 | B |
| 8C | 8 | C |
+ + + +

CLASS:

> insert into CLASS values('1BI17CS101','5A');

> insert into CLASS values('1BI17CS102','5A');

> insert into CLASS values('1BI17CS103','5B');

> insert into CLASS values('1BI17CS104','5B');

> insert into CLASS values('1BI17CS105','4B');

> insert into CLASS values('1BI17CS106','4B');

> insert into CLASS values('1BI17CS107','4C');

> insert into CLASS values('1BI17CS108','4C');

> insert into CLASS values('1BI17CS109','8A');

> insert into CLASS values('1BI17CS110','8A');

> select * from CLASS;


+ + +
| USN | SSID |
+ + +
| 1BI17CS101 | 5A |
| 1BI17CS102 | 5A |
| 1BI17CS103 | 5B |
| 1BI17CS104 | 5B |
| 1BI17CS105 | 4B |
| 1BI17CS106 | 4B |
| 1BI17CS107 | 4C |
| 1BI17CS108 | 4C |
| 1BI17CS109 | 8A |
| 1BI17CS110 | 8A |
+ + +

SUBJECT:

> insert into SUBJECT values('17CS53','DBMS',5,4);

> insert into SUBJECT values('17CS54','ATC',5,4);

> insert into SUBJECT values('17CS43','DAA',4,4);

> insert into SUBJECT values('17CS45','OOC',4,4);

> insert into SUBJECT values('17CS81','IOT',8,4);

> insert into SUBJECT values('17CS83','NM',8,3);


> select * from SUBJECT;
+ + + + +
| Subcode | Title | Sem | Credits |
+ + + + +
| 17CS43 | DAA | 4 | 4 |
| 17CS45 | OOC | 4 | 4 |
| 17CS53 | DBMS | 5 | 4 |
| 17CS54 | ATC | 5 | 4 |
| 17CS81 | IOT | 8 | 4 |
| 17CS83 | NM | 8 | 3 |
+ + + + +

IAMARKS:

> insert into IAMARKS values('1BI17CS101','17CS53','5A',28,25,20,null);

> insert into IAMARKS values('1BI17CS101','17CS54','5A',22,24,26,null);

> insert into IAMARKS values('1BI17CS102','17CS53','5A',23,27,30,null);

> insert into IAMARKS values('1BI17CS102','17CS54','5A',21,25,29,null);

> insert into IAMARKS values('1BI17CS103','17CS43','5B',20,21,22,null);

> insert into IAMARKS values('1BI17CS103','17CS45','5B',29,27,29,null);

> insert into IAMARKS values('1BI17CS104','17CS53','5B',30,29,29,null);

> insert into IAMARKS values('1BI17CS104','17CS54','5B',30,30,30,null);

> insert into IAMARKS values('1BI17CS105','17CS43','4B',25,23,21,null);

> insert into IAMARKS values('1BI17CS105','17CS45','4B',29,23,25,null);

> insert into IAMARKS values('1BI17CS106','17CS81','4B',25,21,27,null);

> insert into IAMARKS values('1BI17CS107','17CS83','4C',22,21,28,null);

> insert into IAMARKS values('1BI17CS108','17CS81','4C',22,21,28,null);

> insert into IAMARKS values('1BI17CS109','17CS81','8A',20,21,20,null);

> insert into IAMARKS values('1BI17CS110','17CS83','8A',30,29,28,null);

> select * from IAMARKS;


+ + + + + + + +
| USN | Subcode | SSID | Test1 | Test2 | Test3 | FinalIA |
+ + + + + + + +
| 1BI17CS101 | 17CS53 | 5A | 28 | 25 | 20 | NULL |
| 1BI17CS101 | 17CS54 | 5A | 22 | 24 | 26 | NULL |
| 1BI17CS102 | 17CS53 | 5A | 23 | 27 | 30 | NULL |
| 1BI17CS102 | 17CS54 | 5A | 21 | 25 | 29 | NULL |
| 1BI17CS103 | 17CS43 | 5B | 20 | 21 | 22 | NULL |
| 1BI17CS103 | 17CS45 | 5B | 29 | 27 | 29 | NULL |
| 1BI17CS104 | 17CS53 | 5B | 30 | 29 | 29 | NULL |
| 1BI17CS104 | 17CS54 | 5B | 30 | 30 | 30 | NULL |
| 1BI17CS105 | 17CS43 | 4B | 25 | 23 | 21 | NULL |
| 1BI17CS105 | 17CS45 | 4B | 29 | 23 | 25 | NULL |
| 1BI17CS106 | 17CS81 | 4B | 25 | 21 | 27 | NULL |
| 1BI17CS107 | 17CS83 | 4C | 22 | 21 | 28 | NULL |
| 1BI17CS108 | 17CS81 | 4C | 22 | 21 | 28 | NULL |
| 1BI17CS109 | 17CS81 | 8A | 20 | 21 | 20 | NULL |
| 1BI17CS110 | 17CS83 | 8A | 30 | 29 | 28 | NULL |
+ + + + + + + +

WWRITE SQL QUERIES TO

1. List all the student details studying in fourth semester ‘C’ section.

> select s.USN,SName,Address,Phone,Gender


-> from STUDENT s, CLASS c, SEMSEC ss
-> where Sem= 4 and Sec='C' and
-> ss.SSID=c.SSID and
-> c.USN=s.USN;
+ + + + + +
| USN | SName | Address | Phone | Gender |
+ + + + + +
| 1BI17CS107 | Sunil | Dharwad | 7766565110 | M |
| 1BI17CS108 | Uday | Hubli | 9911008866 | M |
+ + + + + +

2. Compute the total number of male and female students in each semester
and in each
section.

> select Sem,Sec,Gender,count(*)


-> from STUDENT s, SEMSEC ss,CLASS c
-> where s.USN =c.USN and
-> c.SSID=ss.SSID
-> group by Sem,Sec,Gender order by Sem;
+ + + + +
| Sem | Sec | Gender | count(*) |
+ + + + +
| 4 | B | M | 2 |
| 4 | C | M | 2 |
| 5 | A | F | 2 |
| 5 | B | F | 2 |
| 8 | A | F | 2 |
+ + + + +

3. Create a view of Test1 marks of student USN ‘1BI17CS101’ in all


subjects.

> create view Test1 as


-> select Subcode,Test1 from IAMARKS
-> where USN='1BI17CS101';
Query OK, 0 rows affected

> select * from Test1;


+ + +
| Subcode | Test1 |
+ + +
| 17CS53 | 28 |
| 17CS54 | 22 |
+ + +
4. Calculate the FinalIA (average of best two test marks) and update the corresponding
table
for all students.

> update IAMARKS m set FinalIA = (select sum(Test1+Test2+Test3)/3


-> from IAMARKS m2 where m2.USN=m.USN and m2.Subcode=m.Subcode); Rows matched: 15

Changed: 15 Warnings: 0

MariaDB [student_database]> select * from IAMARKS;


+ + + + + + + +
| USN | Subcode | SSID | Test1 | Test2 | Test3 | FinalIA |
+ + + + + + + +
| 1BI17CS101 | 17CS53 | 5A | 28 | 25 | 20 | 24 |
| 1BI17CS101 | 17CS54 | 5A | 22 | 24 | 26 | 24 |
| 1BI17CS102 | 17CS53 | 5A | 23 | 27 | 30 | 27 |
| 1BI17CS102 | 17CS54 | 5A | 21 | 25 | 29 | 25 |
| 1BI17CS103 | 17CS43 | 5B | 20 | 21 | 22 | 21 |
| 1BI17CS103 | 17CS45 | 5B | 29 | 27 | 29 | 28 |
| 1BI17CS104 | 17CS53 | 5B | 30 | 29 | 29 | 29 |
| 1BI17CS104 | 17CS54 | 5B | 30 | 30 | 30 | 30 |
| 1BI17CS105 | 17CS43 | 4B | 25 | 23 | 21 | 23 |
| 1BI17CS105 | 17CS45 | 4B | 29 | 23 | 25 | 26 |
| 1BI17CS106 | 17CS81 | 4B | 25 | 21 | 27 | 24 |
| 1BI17CS107 | 17CS83 | 4C | 22 | 21 | 28 | 24 |
| 1BI17CS108 | 17CS81 | 4C | 22 | 21 | 28 | 24 |
| 1BI17CS109 | 17CS81 | 8A | 20 | 21 | 20 | 20 |
| 1BI17CS110 | 17CS83 | 8A | 30 | 29 | 28 | 29 |
+ + + + + + + +

5. Categorize students based on the following criterion: If FinalIA =


25 to 30 then CAT = ‘Outstanding’
If FinalIA = 21 to 24 then CAT = ‘Average’
If FinalIA< 20 then CAT = ‘Weak’

> select S.USN,S.SName,IA.FinalIA,IA.Subcode,


-> (case
-> when IA.FinalIA between 25 and 30 then 'Outstanding'
-> when IA.FinalIA between 21 and 24 then 'Average'
-> when IA.FinalIA<20 then 'Weak'
-> else 'NO_Data'
-> end) AS RESULTS
-> from STUDENT S,SEMSEC SS,IAMARKS IA,SUBJECT SUB
-> where S.USN=IA.USN and
-> SS.SSID=IA.SSID and
-> SUB.Subcode=IA.Subcode and
-> SUB.Sem=8;
+ + + + + +
| USN | SName | FinalIA | Subcode | RESULTS |
+ + + + + +
| 1BI17CS106 | Prajwal | 24 | 17CS81 | Average |
| 1BI17CS107 | Sunil | 24 | 17CS83 | Average |
| 1BI17CS108 | Uday | 24 | 17CS81 | Average |
| 1BI17CS109 | Ramya | 20 | 17CS81 | NO_Data |
| 1BI17CS110 | Ankitha | 29 | 17CS83 | Outstanding |
+ + + + + +

C. Consider the schema for Company Database:

EMPLOYEE (SSN, Name, Address, Sex, Salary,


SuperSSN, DNo) DEPARTMENT (DNo, DName,
MgrSSN, MgrStartDate) DLOCATION (DNo,DLoc)
PROJECT (PNo, PName,
PLocation, DNo) WORKS_ON
(SSN, PNo, Hours)
Write SQL queries to
1. Make a list of all project numbers for projects that involve an
employee whose last name is
‘Scott’, either as a worker or as a manager of the department that controls the project.
2. Show the resulting salaries if every employee working on the
‘IoT’ project is given a 10 percent raise.
3. Find the sum of the salaries of all employees of the ‘Accounts’ department, as well as the
maximum salary, the minimum salary, and the average salary in this department
4. Retrieve the name of each employee who works on all the projects
controlled by department number 5 (use NOT EXISTS operator). For
each department that has more than five employees, retrieve the
department number and the number of its employees who are
making more than Rs. 6,00,000.

Entity-Relationship Diagram
SSN Controlled_by

Name N 1
DNO
Salary

DName
1 N

Manages
Employee Department
Address

MgrStartDate
1
Sex 1
N
M Dlocation
Supervisee

Supervisor Supervision Works_on Controls

N
Hours
PName
Project

PNO PLocation
Lab Program: 5

Consider the schema for Company Database:


EMPLOYEE (SSN, Name, Address, Sex, Salary, SuperSSN, DNo)
DEPARTMENT (DNo, DName, MgrSSN, MgrStartDate)
DLOCATION (DNo, DLoc)
PROJECT (PNo, PName, PLocation, DNo)
WORKS_ON (SSN, PNo, Hours)

CREATE DATABASE:

create database Company_Database;

> use Company_Database;


Database changed

TABLE CREATION:

DEPARTMENT:

> create table DEPARTMENT(DNo int primary key,DName varchar(20),Mgr_start_date


date);

desc DEPARTMENT;
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| DNo | int(11) | NO | PRI | NULL | |
| DName | varchar(20) | YES | | NULL | |
| Mgr_start_date | date | YES | | NULL | |
+----------------+-------------+------+-----+---------+-------+

EMPLOYEE:

> create table EMPLOYEE(SSN varchar(10) primary key, FName varchar(20),LName


varchar(20),
-> Address varchar(20), Sex varchar(3), Salary int, SuperSSN varchar(10), DNo
int,
-> foreign key(SuperSSN) references EMPLOYEE(SSN),
-> foreign key(DNo) references DEPARTMENT(DNo));

desc EMPLOYEE;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| SSN | varchar(10) | NO | PRI | NULL | |
| FName | varchar(20) | YES | | NULL | |
| LName | varchar(20) | YES | | NULL | |
| Address | varchar(20) | YES | | NULL | |
| Sex | varchar(3) | YES | | NULL | |
| Salary | int(11) | YES | | NULL | |
| SuperSSN | varchar(10) | YES | MUL | NULL | |
| DNo | int(11) | YES | MUL | NULL | |
+----------+-------------+------+-----+---------+-------+
After creating DEPARTMENT and EMPLOYEE tables, we must alter department
table to add foreign constraint MGRSSN using sql command.

> alter table DEPARTMENT add Mgr_SSN varchar(10);

Records: 0 Duplicates: 0 Warnings: 0

desc DEPARTMENT;
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| DNo | int(11) | NO | PRI | NULL | |
| DName | varchar(20) | YES | | NULL | |
| Mgr_start_date | date | YES | | NULL | |
| Mgr_SSN | varchar(10) | YES | | NULL | |
+----------------+-------------+------+-----+---------+-------+

> alter table DEPARTMENT add foreign key fk_DEPARTMENT(Mgr_SSN) references


EMPLOYEE(SSN);

Records: 0 Duplicates: 0 Warnings: 0

desc DEPARTMENT;
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| DNo | int(11) | NO | PRI | NULL | |
| DName | varchar(20) | YES | | NULL | |
| Mgr_start_date | date | YES | | NULL | |
| Mgr_SSN | varchar(10) | YES | MUL | NULL | |
+----------------+-------------+------+-----+---------+-------+

DLOCATION:

> create table DLOCATION(DLoc varchar(20),DNo int,


-> primary key(DNo,DLoc),
-> foreign key(DNo) references DEPARTMENT(DNo));

desc DLOCATION;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| DLoc | varchar(20) | NO | PRI | NULL | |
| DNo | int(11) | NO | PRI | NULL | |
+-------+-------------+------+-----+---------+-------+

PROJECT:

> create table PROJECT(PNo int primary key,PName varchar(20),PLocation


varchar(20),DNo int,
-> foreign key(DNo) references DEPARTMENT(DNo));
desc PROJECT;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| PNo | int(11) | NO | PRI | NULL | |
| PName | varchar(20) | YES | | NULL | |
| PLocation | varchar(20) | YES | | NULL | |
| DNo | int(11) | YES | MUL | NULL | |
+-----------+-------------+------+-----+---------+-------+

WORKS_ON

> create table WORKS_ON(SSN varchar(10),PNo int,Hours int,


-> primary key(SSN,PNo),
-> foreign key(SSN) references EMPLOYEE(SSN),
-> foreign key(PNo) references PROJECT(PNo));

desc WORKS_ON;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| SSN | varchar(10) | NO | PRI | NULL | |
| PNo | int(11) | NO | PRI | NULL | |
| Hours | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+

INSERTION OF VALUES TO TABLE:

EMPLOYEE:

> insert into EMPLOYEE


values('11CS','Siddharth','Jain','Bellur','M',795000,null,null);

> insert into EMPLOYEE values('22CS','James','Scott','Texas','M',355000,null,null);

> insert into EMPLOYEE values('33CS','John','Smith','USA','M',450000,null,null);

> insert into EMPLOYEE


values('44CS','Mythili','Sharma','Mysore','F',250000,null,null);

> insert into EMPLOYEE


values('55CS','Sumanth','Rao','Mandya','M',655000,null,null);

> insert into EMPLOYEE


values('66CS','Pooja','Jain','Bengaluru','F',695000,null,null);

> insert into EMPLOYEE


values('77CS','Pavan','Kumar','Bengaluru','M',356400,null,null);

> insert into EMPLOYEE


values('88CS','Sunil','Kumar','Bengaluru','M',265400,null,null);

> select * from EMPLOYEE;


+------+-----------+--------+-----------+------+--------+----------+------+
| SSN | FName | LName | Address | Sex | Salary | SuperSSN | DNo |
+------+-----------+--------+-----------+------+--------+----------+------+
| 11CS | Siddharth | Jain | Bellur | M | 795000 | NULL | NULL |
| 22CS | James | Scott | Texas | M | 355000 | NULL | NULL |
| 33CS | John | Smith | USA | M | 450000 | NULL | NULL |
| 44CS | Mythili | Sharma | Mysore | F | 250000 | NULL | NULL |
| 55CS | Sumanth | Rao | Mandya | M | 655000 | NULL | NULL |
| 66CS | Pooja | Jain | Bengaluru | F | 695000 | NULL | NULL |
| 77CS | Pavan | Kumar | Bengaluru | M | 356400 | NULL | NULL |
| 88CS | Sunil | Kumar | Bengaluru | M | 265400 | NULL | NULL |
+------+-----------+--------+-----------+------+--------+----------+------+

DEPARTMENT:

> insert into DEPARTMENT values(1,'Accounts','2001-01-01','44CS');

> insert into DEPARTMENT values(2,'HRD','2002-08-01','22CS');

> insert into DEPARTMENT values(3,'IoT','2016-06-13','11CS');

> insert into DEPARTMENT values(4,'Research','2008-11-21','66CS');

> insert into DEPARTMENT values(5,'AI','2001-03-04','55CS');

> select * from DEPARTMENT;


+-----+----------+----------------+---------+
| DNo | DName | Mgr_start_date | Mgr_SSN |
+-----+----------+----------------+---------+
| 1 | Accounts | 2001-01-01 | 44CS |
| 2 | HRD | 2002-08-01 | 22CS |
| 3 | IoT | 2016-06-13 | 11CS |
| 4 | Research | 2008-11-21 | 66CS |
| 5 | AI | 2001-03-04 | 55CS |
+-----+----------+----------------+---------+

*After inserting values for EMPLOYEE & DEPARTMENT table, Update


SUPERSSN and DNO attributes of employee table

> update EMPLOYEE set Dno=1 where SSN='77CS';

> update EMPLOYEE set SuperSSN='77CS', Dno=1 where SSN='88CS';

> update EMPLOYEE set Dno=3 where SSN='11CS';

> update EMPLOYEE set SuperSSN='11CS', Dno=4 where SSN='66CS';

> update EMPLOYEE set SuperSSN='66CS', Dno=5 where SSN='55CS';

> update EMPLOYEE set SuperSSN='66CS', Dno=5 where SSN='33CS';

> update EMPLOYEE set Dno=2 where SSN='22CS';

> update EMPLOYEE set Dno=2 where SSN='44CS';

> update EMPLOYEE set SuperSSN='11CS', Dno=2 where SSN='44CS';

> select * from EMPLOYEE;


+------+-----------+--------+-----------+------+--------+----------+------+
| SSN | FName | LName | Address | Sex | Salary | SuperSSN | DNo |
+------+-----------+--------+-----------+------+--------+----------+------+
| 11CS | Siddharth | Jain | Bellur | M | 795000 | NULL | 3 |
| 22CS | James | Scott | Texas | M | 355000 | NULL | 2 |
| 33CS | John | Smith | USA | M | 450000 | 66CS | 5 |
| 44CS | Mythili | Sharma | Mysore | F | 250000 | 11CS | 2 |
| 55CS | Sumanth | Rao | Mandya | M | 655000 | 66CS | 5 |
| 66CS | Pooja | Jain | Bengaluru | F | 695000 | 11CS | 4 |
| 77CS | Pavan | Kumar | Bengaluru | M | 356400 | NULL | 1 |
| 88CS | Sunil | Kumar | Bengaluru | M | 265400 | 77CS | 1 |
+------+-----------+--------+-----------+------+--------+----------+------+

DLOCATION:

> insert into DLOCATION values('Bengaluru',1);

> insert into DLOCATION values('Manglore',2);

> insert into DLOCATION values('Bengaluru',3);

> insert into DLOCATION values('Mysore',4);

> insert into DLOCATION values('Hubli',5);

> select * from DLOCATION;


+-----------+-----+
| DLoc | DNo |
+-----------+-----+
| Bengaluru | 1 |
| Manglore | 2 |
| Bengaluru | 3 |
| Mysore | 4 |
| Hubli | 5 |
+-----------+-----+

PROJECT:

> insert into PROJECT values(100,'IoT','Manglore',2);

> insert into PROJECT values(101,'IPSECURITY','Mysore',4);

> insert into PROJECT values(102,'Machine Learning','Hubli',5);

> insert into PROJECT values(103,'Deep Learning','Bengaluru',5);

> insert into PROJECT values(104,'Network Security','Mysore',4);

> insert into PROJECT values(105,'JS','Mandya',5);

> select * from PROJECT;


+-----+------------------+-----------+------+
| PNo | PName | PLocation | DNo |
+-----+------------------+-----------+------+
| 100 | IoT | Manglore | 2 |
| 101 | IPSECURITY | Mysore | 4 |
| 102 | Machine Learning | Hubli | 5 |
| 103 | Deep Learning | Bengaluru | 5 |
| 104 | Network Security | Mysore | 4 |
| 105 | JS | Mandya | 5 |
+-----+------------------+-----------+------+

WORKS_ON:

> insert into WORKS_ON values('11CS',100,12);

> insert into WORKS_ON values('55CS',102,10);

> insert into WORKS_ON values('55CS',103,6);

> insert into WORKS_ON values('66CS',104,8);

> insert into WORKS_ON values('11CS',101,5);

> insert into WORKS_ON values('66CS',105,7);

> select * from WORKS_ON;


+------+-----+-------+
| SSN | PNo | Hours |
+------+-----+-------+
| 11CS | 100 | 12 |
| 11CS | 101 | 5 |
| 55CS | 102 | 10 |
| 55CS | 103 | 6 |
| 66CS | 104 | 8 |
| 66CS | 105 | 7 |
+------+-----+-------+

Write SQL queries to :

1. Make a list of all project numbers for projects that involve an employee whose
last name is
‘Scott’, either as a worker or as a manager of the department that controls the
project.

> (select distinct P.PNo from PROJECT P, DEPARTMENT D, EMPLOYEE E


-> where D.Mgr_SSN=E.SSN and D.DNo=P.DNo and E.LName='Scott')
-> union
-> (select distinct P1.PNo from PROJECT P1, WORKS_ON W, EMPLOYEE E1
-> where P1.PNo=W.PNo and E1.SSN=W.SSN and E1.LName='Scott');

+-----+
| PNo |
+-----+
| 100 |
+-----+

2. Show the resulting salaries if every employee working on the ‘IoT’ project is
given a 10
percent raise.

> select FName,LName, 1.1*Salary as incremented_Salary from EMPLOYEE E,


-> WORKS_ON W, PROJECT P where E.SSN=W.SSN and W.PNo=P.PNo and P.PName='IoT';
+-----------+-------+--------------------+
| FName | LName | incremented_Salary |
+-----------+-------+--------------------+
| Siddharth | Jain | 874500.0 |
+-----------+-------+--------------------+

3. Find the sum of the salaries of all employees of the ‘Accounts’ department, as well as
the
maximum salary, the minimum salary, and the average salary in this department.

> select sum(Salary),max(Salary),min(Salary),avg(Salary) from EMPLOYEE E,


DEPARTMENT D
-> where DName='Accounts' and D.DNo=E.DNo;
+-------------+-------------+-------------+-------------+
| sum(Salary) | max(Salary) | min(Salary) | avg(Salary) |
+-------------+-------------+-------------+-------------+
| 621800 | 356400 | 265400 | 310900.0000 |
+-------------+-------------+-------------+-------------+

4. Retrieve the name of each employee who works on all the projects controlled by
department
number 5 (use NOT EXISTS operator).

select FName,LName from EMPLOYEE E2 where not exists


-> (select * from EMPLOYEE E1 where E2.SSN=E1.SSN and E1.SSN not in
-> (select E.SSN from PROJECT P,WORKS_ON W, EMPLOYEE E where P.PNo=W.PNo
-> and P.DNo=E.DNo and W.SSN=E.SSN and P.DNo=5));
+---------+-------+
| FName | LName |
+---------+-------+
| Sumanth | Rao |
+---------+-------+

5. For each department that has more than five employees, retrieve the department number
and the number of its employees who are making more than Rs. 6, 00,000.

> select DNo,Count(*) from EMPLOYEE where Salary>600000 and


-> DNo in(select DNo from EMPLOYEE group by DNo having count(SSN)>=2) group by
DNo;
+------+----------+
| DNo | Count(*) |
+------+----------+
| 5 | 1 |
+------+----------+
Viva Questions with Answers

1. What is SQL?
Structured Query Language
2. What is database?
A database is a logically coherent collection of data with some inherent meaning, representing some
aspect of real world and which is designed, built and populated with data for a specific purpose.
3. What is DBMS?
It is a collection of programs that enables user to create and maintain a database. In other words it
is general-purpose software that provides the users with the processes of defining, constructing and
manipulating the database for various applications.
4. What is a Database system?
The database and DBMS software together is called as Database system.
5. Advantages of DBMS?
 Redundancy is controlled.
 Unauthorized access is restricted.
 Providing multiple user interfaces.
 Enforcing integrity constraints.
 Providing backup and recovery.
6. Disadvantage in File Processing System?
 Data redundancy & inconsistency.
 Difficult in accessing data.
 Data isolation.
 Data integrity.
 Concurrent access is not possible.
 Security Problems.
7. Describe the three levels of data abstraction?
There are three levels of abstraction:
 Physical level: The lowest level of abstraction describes how data are stored.
 Logical level: The next higher level of abstraction, describes what data are stored in
database and what relationship among those data.
 View level:The highest level of abstraction describes only part of entire database.
8. Define the "integrity rules"
There are two Integrity rules.
 Entity Integrity:States that ―Primary key cannot have NULLvalue‖
 Referential Integrity:States that ―Foreign Key can be either a NULL value or should be
Primary Key value of other relation.
9. What is extension and intension?
Extension - It is the number of tuples present in a table at any instance. This is time dependent.
Intension -It is a constant value that gives the name, structure of table and the constraints laid on
it.
10. What is Data Independence?
Data independence means that ―the application is independent of the storage structure and
access strategy of data‖. In other words, The ability to modify the schema definition in one level should
not affect the schema definition in the next higher level.
Two types of Data Independence:
 Physical Data Independence: Modification in physical level should not affect the logical level.
 Logical Data Independence: Modification in logical level should affect the view level.
NOTE: Logical Data Independence is more difficult to achieve
11. What is a view? How it is related to data independence?
A view may be thought of as a virtual table, that is, a table that does not really exist in its own
right but is instead derived from one or more underlying base table. In other words, there is no stored file
that direct represents the view instead a definition of view is stored in data dictionary.
Growth and restructuring of base tables is not reflected in views. Thus the view can insulate users
from the effects of restructuring and growth in the database. Hence accounts for logical data
independence.
12. What is Data Model?
A collection of conceptual tools for describing data, data relationships data semantics and
constraints.
13. What is E-R model?
This data model is based on real world that consists of basic objects called entities and of
relationship among these objects. Entities are described in a database by a set of attributes.
14. What is Object Oriented model?
This model is based on collection of objects. An object contains values stored in instance
variables within the object. An object also contains bodies of code that operate on the object. These
bodies of code are called methods. Objects that contain same types of values and the same methods are
grouped together into classes.
15. What is an Entity?
It is an 'object' in the real world with an independent existence.
16. What is an Entity type?
It is a collection (set) of entities that have same attributes.
17. What is an Entity set?
It is a collection of all entities of particular entity type in the database.
18. What is an Extension of entity type?
The collections of entities of a particular entity type are grouped together into an entity set.
19. What is an attribute?
It is a particular property, which describes the entity.
20. What is a Relation Schema and a Relation?
A relation Schema denoted by R(A1, A2, …, An) is made up ofthe relation name
R and the list of attributes Ai that it contains. A relation is defined as a set of tuples. Let r
be the relation which contains set tuples (t1, t2, t3, ...,tn). Each tuple is an ordered list of n- values
t=(v1,v2, ..., vn).
21. What is degree of a Relation?
It is the number of attribute of its relation schema.
22. What is Relationship?
It is an association among two or more entities.
23. What is Relationship set?
The collection (or set) of similar relationships.
24. What is Relationship type?
Relationship type defines a set of associations or a relationship set among a given set of
entity types.
25. What is degree of Relationship type?
It is the number of entity type participating.
26. What is DDL (Data Definition Language)?
A data base schema is specified by a set of definitions expressed by a special
language called DDL.
27. What is VDL (View Definition Language)?
It specifies user views and their mappings to the conceptual schema.
28. What is SDL (Storage Definition Language)?
This language is to specify the internal schema. This language may specify the
mapping between two schemas.
29. What is Data Storage - Definition Language?
The storage structures and access methods used by database system are specified by a
set of definition in a special type of DDL called data storage- definition language.
30. What is DML (Data Manipulation Language)?
This language that enable user to access or manipulate data as organized by appropriate data
model.
 Procedural DML or Low level: DML requires a user to specify what data are needed and how to get
those data.
 Non-Procedural DML or High level: DML requires a user to specify what data are needed
without specifying how to get those data.
31. What is DML Compiler?
It translates DML statements in a query language into low-level instruction that the
query evaluation engine can understand.
32. What is Relational Algebra?
It is a procedural query language. It consists of a set of operations that take one or
two relations as input and produce a new relation.
33. What is Relational Calculus?
It is an applied predicate calculus specifically tailored for relational databases
proposed by E.F. Codd. E.g. of languages based on it are DSL, ALPHA, QUEL.
34. What is normalization?
It is a process of analyzing the given relation schemas based on their Functional
Dependencies (FDs) and primary key to achieve the properties
 Minimizing redundancy
 Minimizing insertion, deletion and update anomalies.
35. What is Functional Dependency?
A Functional dependency is denoted by X Y between two sets of attributes X and Y that are
subsets of R specifies a constraint on the possible tuple that can form a relation state r of
R. The constraint is for any two tuples t1 and t2 in r if t1[X] = t2[X] then they have t1[Y] = t2[Y]. This
means the value of X component of a tuple uniquely determines the value of component Y.
36. When is a functional dependency F said to be minimal?
 Every dependency in F has a single attribute for its right hand side.
 We cannot replace any dependency X A in F with a dependency Y A where Y is a proper subset of X
and still have a set of dependency that is equivalent to F.
 We cannot remove any dependency from F and still have set of dependency that is equivalent to F.
37. What is Multivalued dependency?
Multivalued dependency denoted by X Y specified on relation schema R, where X and Y are
both subsets of R, specifies the following constraint on any relation r of R: if two tuples t1 and t2 exist in
r such that t1[X] = t2[X] then t3 and t4 should also exist in r with the following properties
 t3[x] = t4[X] = t1[X] = t2[X]
 t3[Y] = t1[Y] and t4[Y] = t2[Y]
 t3[Z] = t2[Z] and t4[Z] = t1[Z]
where [Z = (R-(X U Y)) ]
38. What is Lossless join property?
It guarantees that the spurious tuple generation does not occur with respect to relation schemas
after decomposition.
39. What is 1 NF (Normal Form)?
The domain of attribute must include only atomic (simple, indivisible) values.
40. What is Fully Functional dependency?
It is based on concept of full functional dependency. A functional dependency X Y is fully
functional dependency if removal of any attribute A from X means that the dependency does not hold any
more.
41. What is 2NF?
A relation schema R is in 2NF if it is in 1NF and every non-prime attribute A in R is fully
functionally dependent on primary key.
42. What is 3NF?
A relation schema R is in 3NF if it is in 2NF and for every FD X A either of the following is true
 X is a Super-key of R.
 A is a prime attribute of R.
In other words, if every non prime attribute is non-transitively dependent on primary key.
43. What is BCNF (Boyce-Codd Normal Form)?
A relation schema R is in BCNF if it is in 3NF and satisfies additional constraints that for
every FD X A, X must be a candidate key.
44. What is 4NF?
A relation schema R is said to be in 4NF if for every Multivalued dependency X Y
that holds over R, one of following is true
 X is subset or equal to (or) XY = R.
 X is a super key.
45. What is 5NF?
A Relation schema R is said to be 5NF if for every join dependency {R1, R2, ...,Rn} that
holds R, one the following is true
 Ri = R for some i.
 The join dependency is implied by the set of FD, over R in which the left side is key of R.
46. What is meant by query optimization?
The phase that identifies an efficient execution plan for evaluating a query that has the least
estimated cost is referred to as query optimization.

47. What is database Trigger?


A database trigger is a PL/SQL block that can defined to automatically execute for insert,
update, and delete statements against a table. The trigger can e defined to execute once for the entire
statement or once for every row that is inserted, updated, or deleted. For any one table, there are twelve
events for which you can define database triggers. A database trigger can call database procedures that
are also written in PL/SQL.
48. What are stored-procedures? And what are the advantages of using them.
Stored procedures are database objects that perform a user defined operation. A stored procedure
can have a set of compound SQL statements. A stored procedure executes the SQL commands and
returns the result to the client. Stored procedures are used to reduce network traffic.
SQL Questions:
1. Which is the subset of SQL commands used to manipulate Oracle Database structures,
including tables?
Data Definition Language (DDL)
2. What operator performs pattern matching?
LIKE operator
3. What operator tests column for the absence of data?
IS NULL operator
4. Which command executes the contents of a specified file?
START <filename> or @<filename>
5. What is the parameter substitution symbol used with INSERT INTO command?
&
6. Which command displays the SQL command in the SQL buffer, and then executes it?
RUN
7. What are the wildcards used for pattern matching?
For single character substitution and % for multi-character substitution
8. State true or false. EXISTS, SOME, ANY are operators in SQL.
True
9. State true or false. !=, <>, ^= all denote the same operation.
True
10. What are the privileges that can be granted on a table by a user to others?
Insert, update, delete, select, references, index, execute, alter, all
11. What command is used to get back the privileges offered by the GRANT command?
REVOKE
12. Which system tables contain information on privileges granted and privileges obtained?
USER_TAB_PRIVS_MADE, USER_TAB_PRIVS_RECD
13. Which system table contains information on constraints on all the tables created?
USER_CONSTRAINTS
14. TRUNCATE TABLE EMP;
DELETE FROM EMP;
Will the outputs of the above two commands differ?
Both will result in deleting all the rows in the table EMP.
15. What the difference is between TRUNCATE and DELETE commands?
TRUNCATE is a DDL command whereas DELETE is a DML command. Hence DELETE
operation can be rolled back, but TRUNCATE operation cannot be rolled back. WHERE clause can be
used with DELETE and not with TRUNCATE.
16. What command is used to create a table by copying the structure of another table?
Answer:
CREATE TABLE AS SELECT command
Explanation:
To copy only the structure, the WHERE clause of the SELECT command should
contain a FALSE statement as in the following.
CREATE TABLE NEWTABLE AS SELECT * FROM EXISTINGTABLE WHERE 1=2;
If the WHERE condition is true, then all the rows or rows satisfying the condition will be copied
to the new table.
17. What will be the output of the following query?
SELECT REPLACE (TRANSLATE(LTRIM(RTRIM('!! ATHEN !!','!'), '!'), 'AN',
'**'),'*','TROUBLE') FROM DUAL;
TROUBLETHETROUBLE
18. What will be the output of the following query?
SELECT DECODE(TRANSLATE('A','1234567890','1111111111'), '1','YES', 'NO' );
Answer : NO
Explanation :
The query checks whether a given string is a numerical digit.
19. What does the following query do?
SELECT SAL + NVL(COMM,0) FROM EMP;
This displays the total salary of all employees. The null values in the
commission column will be replaced by 0 and added to salary.
20. Which date function is used to find the difference between two dates?
MONTHS_BETWEEN
21. Why does the following command give a compilation error?
DROP TABLE &TABLE_NAME;
Variable names should start with an alphabet. Here the table name starts with
an '&' symbol.
22.What is the advantage of specifying WITH GRANT OPTION in the
GRANT command?
The privilege receiver can further grant the privileges he/she has
obtained from the owner to any other user.

23. What is the use of the DROP option in the ALTER TABLE command?
It is used to drop constraints specified on the table.
24.What is the value of ‘comm’ and ‘sal’ after executing the following query if the
initial value of ‘sal’ is 10000?
UPDATE EMP SET SAL = SAL + 1000, COMM = SAL*0.1;
sal = 11000, comm = 1000
25. What is the use of CASCADE CONSTRAINTS?
When this clause is used with the DROP command, a parent table can be dropped
even when a child table exists.

You might also like