qmp 7.1 d/d channabasaveshwara institute of technology ·  · 2017-01-27database applications...

51
Database Applications Laboratory 2015-16 Dept of ISE, CIT, Gubbi Page 1 QMP 7.1 D/D Channabasaveshwara Institute of Technology (An ISO 9001:2008 Certified Institution) NH 206 (B.H. Road), Gubbi, Tumkur 572 216. Karnataka. Department of Information Science & Engineering SYLLABUS DATABASE APPLICATION LABORATORY (Common to CSE & ISE) Subject Code: 10CSL57 I.A. Marks : 25 Hours/Week : 03 Exam Hours: 03 Total Hours : 42 Exam Marks: 50 I. Consider the following relations: I. Consider the following relations: STUDENT (snum: integer, sname: string, major: string, level: string, age: integer) CLASS (name: string, meets at: string, room: string, d: integer) ENROLLED (snum: integer, cname: string) FACULTY (fid: integer, fname: string, deptid: integer) The meaning of these relations is straightforward; for example, Enrolled has one record per student-class pair such that the student is enrolled in the class. Level is a two character code with 4 different values (example: Junior: JR etc) Write the following queries in SQL. No duplicates should be printed in any of the answers. (i) Find the names of all Juniors (level = JR) who are enrolled in a class taught by Prof.Harshith (ii) Find the names of all classes that either meet in room R128 or have five or more Students enrolled. (iii) Find the names of all students who are enrolled in two classes that meet at the same time. (iv) Find the names of faculty members who teach in every room in which some class is taught. (v) Find the names of faculty members for whom the combined enrollment of the courses that they teach is less than five. ENT (snum: integer, sname: string, major: string, level: string, age: integer) II. The following relations keep track of airline flight information: FLIGHTS (no: integer, from: string, 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) Note that the Employees relation describes pilots and other kinds of employees as well; Every pilot is certified for some aircraft, and only pilots are certified to fly. Write each of the following queries in SQL. (i) Find the names of aircraft such that all pilots certified to operate them have salaries more than Rs.80, 000. (ii) For each pilot who is certified for more than three aircrafts, find the eid and the maximum cruisingrange of the aircraft for which she or he is certified. (iii) Find the names of pilots whose salary is less than the price of the cheapest route from Bengaluru to Frankfurt. (iv) For all aircraft with cruisingrange over 1000 Kms, .find the name of the aircraft and the average salary of all pilots certified for this aircraft. (v) Find the names of pilots certified for some Boeing aircraft. (vi) Find the aids of all aircraft that can be used on routes from Bengaluru to New Delhi.

Upload: doliem

Post on 24-Apr-2018

218 views

Category:

Documents


5 download

TRANSCRIPT

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 1

QMP 7.1 D/D

Channabasaveshwara Institute of Technology (An ISO 9001:2008 Certified Institution)

NH 206 (B.H. Road), Gubbi, Tumkur – 572 216. Karnataka.

Department of Information Science & Engineering

SYLLABUS

DATABASE APPLICATION LABORATORY

(Common to CSE & ISE)

Subject Code: 10CSL57 I.A. Marks : 25

Hours/Week : 03 Exam Hours: 03

Total Hours : 42 Exam Marks: 50

I. Consider the following relations:

I. Consider the following relations:

STUDENT (snum: integer, sname: string, major: string, level: string, age: integer)

CLASS (name: string, meets at: string, room: string, d: integer)

ENROLLED (snum: integer, cname: string)

FACULTY (fid: integer, fname: string, deptid: integer)

The meaning of these relations is straightforward; for example, Enrolled has one record per student-class

pair such that the student is enrolled in the class. Level is a two character code with 4 different values

(example: Junior: JR etc)

Write the following queries in SQL. No duplicates should be printed in any of the answers.

(i) Find the names of all Juniors (level = JR) who are enrolled in a class taught by Prof.Harshith

(ii) Find the names of all classes that either meet in room R128 or have five or more Students enrolled.

(iii) Find the names of all students who are enrolled in two classes that meet at the same time.

(iv) Find the names of faculty members who teach in every room in which some class is taught.

(v) Find the names of faculty members for whom the combined enrollment of the courses that they teach

is less than five.

ENT (snum: integer, sname: string, major: string, level: string, age: integer)

II. The following relations keep track of airline flight information:

FLIGHTS (no: integer, from: string, 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)

Note that the Employees relation describes pilots and other kinds of employees as well;

Every pilot is certified for some aircraft, and only pilots are certified to fly.

Write each of the following queries in SQL.

(i) Find the names of aircraft such that all pilots certified to operate them have salaries more than Rs.80,

000.

(ii) For each pilot who is certified for more than three aircrafts, find the eid and the maximum

cruisingrange of the aircraft for which she or he is certified.

(iii) Find the names of pilots whose salary is less than the price of the cheapest route from Bengaluru to

Frankfurt.

(iv) For all aircraft with cruisingrange over 1000 Kms, .find the name of the aircraft and the average

salary of all pilots certified for this aircraft.

(v) Find the names of pilots certified for some Boeing aircraft.

(vi) Find the aids of all aircraft that can be used on routes from Bengaluru to New Delhi.

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 2

III. Consider the following database of student enrollment in courses and books adopted for each

course.

STUDENT (regno: string, name: string, major: string, bdate: date)

COURSE (course#: int, cname: string, dept: string)

ENROLL (regno: string, course#: int, sem: int, marks: int)

BOOK_ADOPTION (course#: int, sem: int, book-ISBN: int)

TEXT (book-ISBN: int, book-title: string, publisher: string, author: string)

(i) Create the above tables by properly specifying the primary keys and the foreign keys.

(ii) Enter atleast five tuples for each relation.

(iii) Demonstrate how you add a new text book to the database and make this book be adopted by some

department.

(iv) Produce a list of text books (include Course#, Book-ISBN, Book-title) in the alphabetical order for

courses offered by the ‗CS‘ department that use more than two books.

(v) List any department that has all its adopted books published by a specific publisher.

(vi) Generation of suitable reports.

Create suitable front end for querying and displaying the results.

LASS (name: string, meets at: string, room: string, d: integer)

IV. The following tables are maintained by a book dealer.

AUTHOR (author-id: int, name: string, city: string, country: string)

PUBLISHER (publisher-id: int, name: string, city: string, country: string)

CATALOG (book-id: int, title: string, author-id: int, publisher-id: int, category-id: int, year: int, price: int)

CATEGORY (category-id: int, description: string)

ORDER-DETAILS (order-no: int, book-id: int, quantity: int)

i. Create the above tables by properly specifying the primary keys and foreign keys.

ii. Enter atleast five tuples for each tables.

iii. Give the details of the authors who have 2 or more books in the catalog and the price of the books is

greater than the average price of the books in the catalog and the year of publication is after 2000.

iv. Find the author of the book which has maximum sales.

v. Demonstrate how you increase the price of books published by a specific publisher by 10%.

vi. Generation of suitable reports

Create suitable front end for querying and displaying the results.

V. Consider the following database for a banking enterprise.

BRANCH (branch-name: string, branch-city: string, assets: real)

ACCOUNT (accno: int, branch-name: string, balance: real)

DEPOSITOR (customer-name: string, accno: int)

CUSTOMER (customer-name: string, customer-street: string, customer-city: string)

LOAN (loan-number: int, branch-name: string, amount: real)

BORROWER (customer-name: string, loan-number: int) i. Create the above tables by properly specifying the primary keys and foreign keys.

ii. Enter atleast five tuples for each relation.

iii. Find all the customers who have atleast two accounts at the main branch.

iv. Find all the customers who have an account at all the branches located in a specific city.

v. Demonstrate how you delete all account tuples at every branch located in a specific city.

vi. Generation of suitable reports.

Create suitable front end for querying and displaying the results.

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 3

Instructions:

1. The exercises are to be solved in an RDBMS environment like Oracle or DB2.

2. Suitable tuples have to be entered so that queries are executed correctly.

3. Front end may be created using either VB or VAJ or any other similar tool.

4. The student need not create the front end in the examination. The results of the queries may be

displayed directly.

5. Relevant queries other than the ones listed along with the exercises may also be asked in the

examination.

6. Questions must be asked based on lots.

ENROLLED (snum: integer, cname: string)

FACULTY (fid: integer, fname: string, deptid: integer)

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 4

MySQL Database:

MySQL is a freely available open source Relational Database Management System (RDBMS) that uses

Structured Query Language (SQL). SQL is the most popular language for adding, accessing and

managing content in a database. It is most noted for its quick processing, proven reliability, ease and

flexibility of use. MySQL is a fast, easy-to-use RDBMS being used for many small and big businesses.

MySQL is developed, marketed, and supported by MySQL AB, which is a Swedish company. MySQL is

becoming so popular because of many good reasons:

MySQL is released under an open-source license. So you have nothing to pay to use it.

MySQL is a very powerful program in its own right. It handles a large subset of the functionality

of the most expensive and powerful database packages.

MySQL uses a standard form of the well-known SQL data language.

MySQL works on many operating systems and with many languages including PHP, PERL, C,

C++, JAVA, etc.

MySQL works very quickly and works well even with large data sets.

MySQL is very friendly to PHP, the most appreciated language for web development.

MySQL supports large databases, up to 50 million rows or more in a table. The default file size

limit for a table is 4GB, but you can increase this (if your operating system can handle it) to a

theoretical limit of 8 million terabytes (TB).

MySQL occupies very less disk space.

MySQL is customizable. The open-source GPL license allows programmers to modify the

MySQL software to fit their own specific environments.

How to Install MySQL on Ubuntu

Using terminal:

To install MySQL, run the following command from a terminal prompt:

sudo apt-get install mysql-server

During the installation process you will be prompted to enter a password(for ex: root) for the MySQL root

user. Once the installation is complete, the MySQL server should be started automatically.

How to Access the MySQL shell Once you have MySQL installed on your droplet, you can access the MySQL shell by typing the

following command into terminal:

mysql -u root -p

Enter the mysql password: root

-u root -p refers to user root password. Login to MySQL as root user(u) with mysql password(p).

Points to keep in mind:

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 5

All MySQL commands end with a semicolon; if the phrase does not end with a semicolon, the

command will not execute.

Also, although it is not required, MySQL commands are usually written in uppercase and

databases, tables, user names, or text are in lowercase to make them easier to distinguish.

However, the MySQL command line is not case sensitive.

Commands in the command line are not case sensitive. But the tables and database names are

case sensitive. The table name ―stud‖ is not the same as ―STUD‖.

Summary of MySQL Commands

Commands at the Database-Level

1: To Delete the database (irrecoverable!)

DROP DATABASE databaseName

2: Delete if it exists

DROP DATABASE IF EXISTS databaseName

3: Create a new database

CREATE DATABASE databaseName

4: Create only if it does not exists

CREATE DATABASE IF NOT EXISTS databaseName

5: Show all the databases in this server

SHOW DATABASES

6: Set the default (current) database

USE databaseName

7: Show the default database

SELECT DATABASE()

8: Show the CREATE DATABASE statement

SHOW CREATE DATABASE databaseName

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 6

Commands at the Table-Level

1: To Delete the table (irrecoverable!)

DROP TABLE [IF EXISTS] tableName, ...

2: 3: Create a new table

CREATE TABLE [IF NOT EXISTS] tableName (

columnName columnType columnAttribute, ...

PRIMARY KEY(columnName),

FOREIGN KEY (columnNmae) REFERENCES tableName (columnNmae)

)

3: Show all the tables in the default database

SHOW TABLES

4: Describe the details for a table

DESCRIBE|DESC tableName

5: Modify a table, e.g., ADD COLUMN and DROP COLUMN

1: ALTER TABLE tableName ...

2: ALTER TABLE tableName ADD columnDefinition

3: ALTER TABLE tableName DROP columnName

4: ALTER TABLE tableName ADD FOREIGN KEY (columnNmae) REFERENCES tableName

(columnNmae)

5: ALTER TABLE tableName DROP FOREIGN KEY constraintName

6: Show the CREATE TABLE statement for this tableName

SHOW CREATE TABLE tableName

Commands at the Row-Level 1: Insert on all Columns

INSERT INTO tableName VALUES (column1Value, column2Value,...)

2: Insert multiple rows

INSERT INTO tableName VALUES (column1Value, column2Value,...), (column1Value,

column2Value,...)...

3: Insert on selected Columns

INSERT INTO tableName (column1Name, ..., columnNName)VALUES (column1Value, ...,

columnNValue)

4: Deleting particular row

DELETE FROM tableName WHERE criteria

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 7

5: UPDATE tableName SET columnName = expr, ... WHERE criteria

6: SELECT * | column1Name AS alias1, ..., columnNName AS aliasN

FROM tableName

WHERE criteria

GROUP BY columnName

ORDER BY columnName ASC|DESC, ...

HAVING groupConstraints

LIMIT count | offset count

Others

1: Show the warnings of the previous statement

SHOW WARNINGS;

Help & Exit Command

1: To get any information about the commands used in the mysql use "help" command.

Eg: help insert;

help create;

2: Type exit to exit from the MySQL prompt.

How to Create and Delete a MySQL Database

To check what databases are available

show databases;

mysql> show databases;

+--------------------+

| database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| test |

+--------------------+

4 rows in set (0.01 sec)

creating a database

create database database name;

(in this case, for example, we will call our database "company.")

create database company;

mysql> show databases;

+--------------------+

| database |

+--------------------+

| information_schema |

| company |

| mysql |

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 8

| performance_schema |

| test |

+--------------------+

5 rows in set (0.00 sec)

deleting a mysql database

drop database database name;

drop database database company;

how to access a mysql database to open up the database we want to use:

use company;

To check the overview of the tables that the database contains.

show tables;

How to create a mysql table

To create a employee table inside company database.

create table employee

(

id int not null primary key auto_increment,

name varchar(20),

city varchar(30),

gender char(1)

dob not null date

);

This command accomplishes a number of things:

1: It has created a table called employee within the directory, company.

2: We have set up 5 columns in the table—id, name, city, gender, dob.

3: The ―id‖ column has a command (INT NOT NULL PRIMARY KEY AUTO_INCREMENT) that

automatically numbers each row.

4: The ―name‖ column has been limited by the VARCHAR command to be under 20 characters long.

5: The ―city‖ column records the city that each employee belongs to. The VARCHAR limits text to be

under 30 characters.

6: The ―gender‖ column records the gender of the employee with single character, M or F.

7: The ―dob‖ column will show the date of birth of the employee. MySQL requires that dates be written

as yyyy-mm-dd

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 9

mysql> SHOW TABLES;

+------------------+

| Tables_in_events |

+------------------+

| employee |

+------------------+

1 row in set (0.01 sec)

How to display the table’s organization

mysql>DESCRIBE employee;

+-------------+---------------+------+-----+---------+----------------+

| Field | Type | Null | Key | Default | Extra |

+-------------+-------------+------+-----+---------+----------------+

| id | int(11) | NO | PRI | NULL | auto_increment |

| name | varchar(20) | YES | | NULL | |

| city | varchar(30) | YES | | NULL | |

| gender | char(1) | YES | | NULL | |

| dob | date | NO | | 0 | |

+-------------+-------------+------+-----+---------+----------------+

5 rows in set (0.01 sec)

How to Add Information to a MySQL Table

To add information to a table, the command used is ―insert‖.

Insert command can be used in many ways,

1: Insert a row with all the column values.

Syntax: INSERT INTO tableName VALUES (firstColumnValue, ..., lastColumnValue)

mysql>INSERT INTO employee VALUES (1001, ―kumar‖, ―Bangalore‖, ‖M‖ , ‗2012-04-11‘);

2: Inserting multiple rows in one command. Inserting NULL to the auto_increment column results in

max_value + 1

Syntax:

INSERT INTO tableName VALUES

(row1FirstColumnValue, ..., row1lastColumnValue),

(row2FirstColumnValue, ..., row2lastColumnValue),

...

mysql>INSERT INTO employee VALUES

(NULL, 'Santhosh', 'Tumkur', "M", 1992-05-12),

(NULL, 'vandana', 'Hasan', "F", 1992-05-12);

3: To insert a row with values on selected columns only. Missing value for the auto_increment column

also results in max_value + 1

Syntax:

INSERT INTO tableName (column1Name, ..., columnNName) VALUES (column1Value, ...,

columnNValue)

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 10

mysql> INSERT INTO employee (name, city) VALUES ('Santhosh', 'Tumkur'),( 'vandana', 'Hasan');

Alternately, use SET to set the values

INSERT INTO tableName SET column1=value1, column2=value2, ...

4: Missing columns get their default values

mysql> INSERT INTO products (name, city) VALUES ('Nuthan', 'Tumkur');

5: Remove the row

mysql> DELETE FROM employee WHERE id = 1003;

mysql> select * from employee;

+------+----------+-----------+--------+------------+

| id | name | city | gender | dob |

+------+----------+-----------+--------+------------+

| 1001 | kumar | Bangalore | M | 0000-00-00 |

| 1002 | santhosh | Tumkur | M | 0000-00-00 |

| 1003 | nuthan | Hasan | M | 0000-00-00 |

+------+----------+-----------+--------+------------+

3 rows in set (0.00 sec)

Querying the Database - SELECT

1: list all the rows of the specified columns.

syntax : select column1name, column2name, ... from tablename

mysql> select name,city from employee;

2: list all the rows of all columns, * is a wildcard denoting all columns

syntax: select * from tablename

mysql> select * from employee;

3: list rows that meet the specified criteria in where clause

syntax: select column1name, column2name,... from tablename where criteria

mysql> select name, city from employee where id=105;

MySQL Data Types

1: Numeric Data Types:

INT : If signed, the allowable range is from -2147483648 to 2147483647. If unsigned, the

allowable range is from 0 to 4294967295. You can specify a width of up to 11 digits.

TINYINT : If signed, the allowable range is from -128 to 127. If unsigned, the allowable range is

from 0 to 255. You can specify a width of up to 4 digits.

SMALLINT : If signed, the allowable range is from -32768 to 32767. If unsigned, the allowable

range is from 0 to 65535. You can specify a width of up to 5 digits.

MEDIUMINT : If signed, the allowable range is from -8388608 to 8388607. If unsigned, the

allowable range is from 0 to 16777215. You can specify a width of up to 9 digits.

BIGINT : If signed, the allowable range is from -9223372036854775808 to

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 11

9223372036854775807. If unsigned, the allowable range is from 0 to 18446744073709551615.

You can specify a width of up to 20 digits.

FLOAT(M,D) : A floating-point number that cannot be unsigned. You can define the display

length (M) and the number of decimals (D). This is not required and will default to 10,2, where 2

is the number of decimals and 10 is the total number of digits (including decimals). Decimal

precision can go to 24 places for a FLOAT.

DOUBLE(M,D) : A double precision floating-point number that cannot be unsigned. You can

define the display length (M) and the number of decimals (D). This is not required and will

default to 16,4, where 4 is the number of decimals. Decimal precision can go to 53 places for a

DOUBLE. REAL is a synonym for DOUBLE.

DECIMAL(M,D) - An unpacked floating-point number that cannot be unsigned. In unpacked

decimals, each decimal corresponds to one byte. Defining the display length (M) and the number

of decimals (D) is required. NUMERIC is a synonym for DECIMAL.

2: Date and Time Types:

DATE - A date in YYYY-MM-DD format, between 1000-01-01 and 9999-12-31. For example,

December 30th, 1973 would be stored as 1973-12-30.

DATETIME - A date and time combination in YYYY-MM-DD HH:MM:SS format, between

1000-01-01 00:00:00 and 9999-12-31 23:59:59. For example, 3:30 in the afternoon on December

30th, 1973 would be stored as 1973-12-30 15:30:00.

TIMESTAMP - A timestamp between midnight, January 1, 1970 and sometime in 2037. This

looks like the previous DATETIME format, only without the hyphens between numbers; 3:30 in

the afternoon on December 30th, 1973 would be stored as 19731230153000 (

YYYYMMDDHHMMSS ).

TIME - Stores the time in HH:MM:SS format.

YEAR(M) - Stores a year in 2-digit or 4-digit format. If the length is specified as 2 (for example

YEAR(2)), YEAR can be 1970 to 2069 (70 to 69). If the length is specified as 4, YEAR can be

1901 to 2155. The default length is 4.

3: String Types:

CHAR(M) - A fixed-length string between 1 and 255 characters in length (for example

CHAR(5)), right-padded with spaces to the specified length when stored. Defining a length is not

required, but the default is 1.

VARCHAR(M) - A variable-length string between 1 and 255 characters in length; for example

VARCHAR(25). You must define a length when creating a VARCHAR field.

BLOB or TEXT - A field with a maximum length of 65535 characters. BLOBs are "Binary

Large Objects" and are used to store large amounts of binary data, such as images or other types

of files. Fields defined as TEXT also hold large amounts of data; the difference between the two

is that sorts and comparisons on stored data are case sensitive on BLOBs and are not case

sensitive in TEXT fields. You do not specify a length with BLOB or TEXT.

TINYBLOB or TINYTEXT - A BLOB or TEXT column with a maximum length of 255

characters. You do not specify a length with TINYBLOB or TINYTEXT.

MEDIUMBLOB or MEDIUMTEXT - A BLOB or TEXT column with a maximum length of

16777215 characters. You do not specify a length with MEDIUMBLOB or MEDIUMTEXT.

LONGBLOB or LONGTEXT - A BLOB or TEXT column with a maximum length of

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 12

4294967295 characters. You do not specify a length with LONGBLOB or LONGTEXT.

ENUM - An enumeration, which is a fancy term for list. When defining an ENUM, you are

creating a list of items from which the value must be selected (or it can be NULL). For example,

if you wanted your field to contain "A" or "B" or "C", you would define your ENUM as ENUM

('A', 'B', 'C') and only those values (or NULL) could ever populate that field.

DATABASE 1 : College_database

DESCRIPTION:

The following relations keep track of students, their enrollment for classes along with faculty information.

Student (snum: integer, sname: string, major: string, level: string, age: integer)

Class (name: string, meets at: string, room: string, d: integer)

Enrolled (snum: integer, cname: string)

Faculty (fid: integer, fname: string, deptid: integer)

NOTE: The meaning of these relations is straight forward.For example, Enrolled has one record per

student-class pair such that the student is enrolled in the class. Level is a two character code with 4

different values (example: Junior: JR etc)

Queries:

Write the following queries in SQL. No duplicates should be printed in any of the answers.

1. Find the names of all juniors (level=Jr) who are enrolled for class taught by professor Harshith.

2. Find the names of all classes that either meet in room128 or have 5 or more students enrolled.

3. Find the names of all students who are enrolled in two classes that meet at same time.

4. Find the names of faculty members who teach in every room in which some class is taught.

5. Find the names of the faculty members for whom the combined enrollment of the classes that they

teach is less than five.

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 13

ER Diagram

mysqlL> create table student (

snum integer primary key,

sname varchar(15),

major varchar(10),

level1 varchar(2),

age integer

);

mysql> create table faculty (

fid integer primary key,

fname varchar(15),

deptid integer

);

STUDENT CLASS

Snum Snam

Level Majo

Age

Enro

Teach

Snum

Cname

Cname

Meets

Fid

Room

FACULTY Deptid

Fname Fid

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 14

mysql > create table class (

cname varchar(10) primary key,

meetsat varchar(4),

room varchar(5),

fid integer,

foreign key(fid) references faculty(fid)

);

mysql > create table enrolled (

snum integer,

cname varchar(10),

foreign key(snum) references student(snum),

foreign key(cname) references class(cname),

primary key(snum,cname)

);

mysql > desc student;

+--------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+--------+-------------+------+-----+---------+-------+

| snum | int(11) | NO | PRI | NULL | |

| sname | varchar(15) | YES | | NULL | |

| major | varchar(10) | YES | | NULL | |

| level1 | varchar(2) | YES | | NULL | |

| age | int(11) | YES | | NULL | |

+--------+-------------+------+-----+---------+-------+

5 rows in set (0.00 sec)

mysql > insert into student values (1,‖arthi‖, ―os‖,‖jr‖, 21);

mysql >insert into student values (2,‖bhavesh‖,‖is‖,‖sr‖, 22);

mysql >insert into student values (3,‖chanasya‖,‖ec‖,‖jr‖, 21);

mysql >insert into student values (4,‖sowmya‖,‖cs‖,‖sr‖, 23);

mysql >insert into student values (5,‖sanjay‖,‖ec‖,‖jr‖, 21);

mysql >insert into student values (10,‖giri‖,‖ccn‖,‖jr‖, 20);

mysql >insert into student values (20,‖asha‖,‖os‖,‖jr‖, 30);

mysql> select * from stud;

+------+----------+-------+--------+------+

| snum | sname | major | level1 | age |

+------+----------+-------+--------+------+

| 1 | arun | os | jr | 21 |

| 2 | bhanu | ls | sr | 22 |

| 3 | chanasya | ec | jr | 21 |

| 4 | sowmya | cs | sr | 23 |

| 5 | gowri | ccn | jr | 20 |

| 6 | asha | os | jr | 30 |

+------+----------+-------+--------+------+

6 rows in set (0.00 sec)

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 15

mysql > desc faculty;

+--------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+--------+-------------+------+-----+---------+-------+

| fid | int(11) | NO | PRI | NULL | |

| fname | varchar(15) | YES | | NULL | |

| deptid | int(11) | YES | | NULL | |

+--------+-------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

mysql > insert into faculty values(11,‖bharathi‖,111);

mysql > insert into faculty values(22,‖nirmala‖,222);

mysql > insert into faculty values(33,‖prof.Harshith‖,333);

mysql > insert into faculty values(44,‖bhavana‖,111);

mysql > insert into faculty values(55,‖usha‖,111);

mysql> select * from faculty;

+-----+---------------+--------+

| fid | fname | deptid |

+-----+---------------+--------+

| 11 | bharathi | 111 |

| 22 | nirmala | 222 |

| 33 | prof.Harshith | 333 |

| 44 | bhavana | 111 |

| 55 | usha | 111 |

+-----+---------------+--------+

5 rows in set (0.00 sec)

mysql > desc class;

+---------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+---------+-------------+------+-----+---------+-------+

| cname | varchar(10) | NO | PRI | NULL | |

| meetsat | varchar(4) | YES | | NULL | |

| room | varchar(5) | YES | | NULL | |

| fid | int(11) | YES | MUL | NULL | |

+---------+-------------+------+-----+---------+-------+

4 rows in set (0.00 sec)

mysql > insert into class values(―ccna‖,‖10:00‖,‖r126‖,33);

mysql > insert into class values(―cobol‖,‖10:00‖,‖r127‖,22);

mysql > insert into class values(―dotnet‖,‖9:00‖,‖r101‖,33);

mysql > insert into class values(―java‖,‖11:15‖,‖r126‖,22);

mysql > insert into class values(―jcp‖,‖12:15‖,‖r128‖,22);

mysql > insert into class values(―oops‖,‖12:15‖,‖r127‖,22);

mysql > insert into class values(―oracle‖,‖12:15‖,‖r128‖,11);

mysql > insert into class values(―testiing‖,‖11:15‖,‖r101‖,22);

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 16

mysql> select * from class;

+----------+---------+------+------+

| cname | meetsat | room | fid |

+----------+---------+------+------+

| ccna | 10:0 | r126 | 33 |

| cobol | 10:0 | r127 | 22 |

| dotnet | 9:00 | r101 | 33 |

| java | 11:1 | r126 | 22 |

| jcp | 12:1 | r128 | 22 |

| oops | 12:1 | r127 | 22 |

| oracle | 12:1 | r128 | 11 |

| testiing | 11:1 | r101 | 22 |

+----------+---------+------+------+

8 rows in set (0.00 sec)

mysql > desc enrolled;

+-------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| snum | int(11) | NO | PRI | 0 | |

| cname|varchar(10) | NO | PRI | | |

+-------+-------------+------+-----+---------+-------+

2 rows in set (0.00 sec)

mysql > insert into enrolled values(1,'cobol');

mysql > insert into enrolled values(2,'cobol');

mysql > insert into enrolled values(3,'cobol');

mysql > insert into enrolled values(4,'cobol');

mysql > insert into enrolled values(1,'dotnet');

mysql > insert into enrolled values(1,'jcp');

mysql > insert into enrolled values(5,'jcp');

mysql > insert into enrolled values(3,'oracle');

mysql > insert into enrolled values(5,'cobol');

mysql > insert into enrolled values(4,'ccna');

mysql > insert into enrolled values(3,'testing');

mysql > insert into enrolled values(3,'java');

mysql> select * from enrolled;

+------+----------+

| snum | cname |

+------+----------+

| 4 | ccna |

| 1 | cobol |

| 2 | cobol |

| 3 | cobol |

| 4 | cobol |

| 5 | cobol |

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 17

| 1 | dotnet |

| 3 | java |

| 1 | jcp |

| 5 | jcp |

| 3 | oracle |

| 5 | oracle |

| 3 | testing |

+------+----------+

13 rows in set (0.00 sec)

1. Find the names of all Juniors (level = JR) who are enrolled in a class taught by Prof. Harshith

mysql > select distinct s.sname

from student s, class c, enrolled e, faculty f

where s.snum = e.snum and e.cname = c.cname and c.fid = f.fid and

f.fname = 'prof.harshith' and s.level1 = 'jr';

+-------+

| sname |

+-------+

| arun |

+-------+

1 row in set (0.00 sec)

2. Find the names of all classes that either meet in room R128 or have five or more Students

enrolled.

mysql > select c.cname

from class c where c.room = 'r128' or c.cname in

(select e.cname from enrolled e group by e.cname having count(*) >= 5);

+--------+

| cname |

+--------+

| cobol |

| jcp |

| oracle |

+--------+

3 rows in set (0.00 sec)

3. Find the names of all students who are enrolled in two classes that meet at the same time.

mysql > select distinct s.sname

from student s

where s.snum in (select e1.snum

from enrolled e1, enrolled e2, class c1, class c2

where e1.snum = e2.snum and e1.cname<>e2.cname and e1.cname=c1.cname

and e2.cname=c2.cname and c1.meetsat = c2.meetsat);

+--------+

| sname |

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 18

+--------+

| sowmya |

| gowri |

+--------+

2 rows in set (0.00 sec)

4. Find the names of faculty members who teach in every room in which some class is taught.

mysql > select fname, count (distinct room)

from class c, enrolled e, faculty f

where c.fid=f.fid and c.cname=e.cname

group by fname having count(distinct room) in (select count(distinct room)

from class c, enrolled e

where e.cname=c.cname);

+---------+----------------------+

| fname | count(distinct room) |

+---------+----------------------+

| nirmala | 4 |

+---------+----------------------+

1 row in set (0.00 sec)

5. Find the names of faculty members for whom the combined enrollment of the courses that they

teach is less than five.

mysql > select distinct f.fname

from class c, enrolled e, faculty f

where c.fid=f.fid and c.cname=e.cname

group by f.fname having count(*)<5;

+---------------+

| fname |

+---------------+

| bharathi |

| prof.Harshith |

+---------------+

2 rows in set (0.00 sec)

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 19

DATABASE 2 : Airline_Flight_database

DESCRIPTION:

The following relations keep track of airline flight information:

Flights (no: integer, from: string, 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)

Note that the Employees relation describes pilots and other kinds of employees as well; Every pilot is

certified for some aircraft, and only pilots are certified to fly.

Write each of the following queries in SQL.

i. Find the names of aircraft such that all pilots certified to operate them have salaries more than Rs.80,

000.

ii. For each pilot who is certified for more than three aircrafts, find the eid and the maximum

cruisingrange of the aircraft for which she or he is certified.

iii. Find the names of pilots whose salary is less than the price of the cheapest route from Bengaluru to

Frankfurt.

iv. For all aircraft with cruisingrange over 1000 Kms, .find the name of the aircraft and the average salary

of all pilots certified for this aircraft.

v. Find the names of pilots certified for some Boeing aircraft.

vi. Find the aids of all aircraft that can be used on routes from Bengaluru to New Delhi.

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 20

ER Diagram :

mysql > create table flights (

no int primary key,

fro varchar(15),

too varchar(15),

distance integer,

departs char(6),

arrives char(6),

price real

);

mysql > create table aircraft (

aid int primary key,

aname varchar(15),

crurange integer

);

mysql > create table employees (

eid int primary key,

cname varchar(10),

salary integer

);

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 21

mysql > create table certified (

eid int,

aid int,

foreign key(eid) references employees(eid),

foreign key(aid) references aircraft(aid),

primary key(eid,aid)

);

mysql>describe flights;

+----------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+----------+-------------+------+-----+---------+-------+

| no | int(11) | NO | PRI | NULL | |

| fro | varchar(15) | YES | | NULL | |

| too | varchar(15) | YES | | NULL | |

| distance | int(11) | YES | | NULL | |

| departs | char(6) | YES | | NULL | |

| arrives | char(6) | YES | | NULL | |

| price | double | YES | | NULL | |

+----------+-------------+------+-----+---------+-------+

7 rows in set (0.00 sec)

mysql>insert into flights values(4,'bangalore','australia',3000,‘09:30‘,‘12:00‘,150000);

mysql > insert into flights values(2,'delhi','new jersey',4000,‘01:30‘,‘09:00‘,120000);

mysql > insert into flights values(3,'hyderbad','bangalore',700,‘02:30‘,‘03:30‘,5000);

mysql > insert into flights values(5,'angalore','kolkota',1200,‘04:30‘,‘06:30‘,10000);

mysql> insert into flights values(6,'bangalore','newdelhi',2000,‘05:30‘,‘07:30‘,15000);

mysql> insert into flights values(6,'bangalore','frankfrut',4000,‘06:30‘,‘11:30‘,20000);

mysql>select * from Flights;

+----+-----------+------------+----------+---------+---------+--------+

| no | fro | too | distance | departs | arrives | price |

+----+-----------+------------+----------+---------+---------+--------+

| 2 | delhi | new jersey | 4000 | 01:30 | 09:00 | 120000 |

| 3 | hyderbad | bangalore | 700 | 02:30 | 03:30 | 5000 |

| 4 | bangalore | australia | 3000 | 09.30 | 12.00 | 150000 |

| 5 | bangalore | kolkota | 1200 | 04:30 | 06:30 | 10000 |

| 6 | bangalore | newdelhi | 2000 | 05:30 | 07:30 | 15000 |

| 7 | bangalore | frankfrut | 4000 | 06:30 | 11:30 | 20000 |

+----+-----------+------------+----------+---------+---------+--------+

6 rows in set (0.00 sec)

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 22

mysql>describe aircraft;

+----------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+----------+-------------+------+-----+---------+-------+

| aid | int(11) | NO | PRI | NULL | |

| aname | varchar(15) | YES | | NULL | |

| crurange | int(11) | YES | | NULL | |

+----------+-------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

mysql > insert into aircraft values(11, 'airindia', 4000);

mysql > insert into aircraft values(22, 'boeing120', 5000);

mysql > insert into aircraft values(33, 'kingfisher', 3000);

mysql > insert into aircraft values(44, 'jetairways', 4000);

mysql > insert into aircraft values(55, 'boeing240', 6000);

mysql > insert into aircraft values(66, 'bharath', 600);

mysql > insert into aircraft values(77, 'bharath160', 800);

mysql>select * from Aircraft;

+-----+------------+----------+

| aid | aname | crurange |

+-----+------------+----------+

| 11 | airindia | 4000 |

| 22 | boeing120 | 5000 |

| 33 | kingfisher | 3000 |

| 44 | jetairways | 4000 |

| 55 | boeing240 | 6000 |

| 66 | bharath | 600 |

| 77 | bharath160 | 800 |

+-----+------------+----------+

7 rows in set (0.00 sec)

mysql>describe employees;

+--------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+--------+-------------+------+-----+---------+-------+

| eid | int(11) | NO | PRI | NULL | |

| cname | varchar(10) | YES | | NULL | |

| salary | int(11) | YES | | NULL | |

+--------+-------------+------+-----+---------+-------+

3 rows in set (0.01 sec)

mysql > insert into employees values(123, ―kumar‖, 80000);

mysql > insert into employees values(201, ―muruli‖, 200000);

mysql > insert into employees values(301, ―alexander‖, 200000);

mysql > insert into employees values(401, ―mathew‖, 60000);

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 23

mysql > insert into employees values(501, ―philomina‖, 2000);

mysql>select * from Employees;

+-----+-----------+--------+

| eid | cname | salary |

+-----+-----------+--------+

| 123 | kumar | 80000 |

| 201 | muruli | 200000 |

| 301 | alexander | 200000 |

| 401 | mathew | 60000 |

| 501 | philomina | 2000 |

+-----+-----------+--------+

5 rows in set (0.01 sec) [

mysql>describe certified;

+-------+---------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| eid | int(11) | NO | PRI | 0 | |

| aid | int(11) | NO | PRI | 0 | |

+-------+---------+------+-----+---------+-------+

2 rows in set (0.01 sec)

mysql > insert into certified values(123, 11);

mysql > insert into certified values(201, 22);

mysql > insert into certified values(201, 33);

mysql > insert into certified values(301, 33);

mysql > insert into certified values(123, 44);

mysql > insert into certified values(401, 44);

mysql > insert into certified values(501,44);

mysql > insert into certified values(123,55);

mysql > insert into certified values(123, 66);

mysql>select * from certified;

+-----+-----+

| eid | aid |

+-----+-----+

| 123 | 11 |

| 201 | 22 |

| 201 | 33 |

| 301 | 33 |

| 123 | 44 |

| 401 | 44 |

| 501 | 44 |

| 123 | 55 |

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 24

| 123 | 66 |

+-----+-----+

9 rows in set (0.00 sec)

1: Find the names of aircraft such that all pilots certified to operate them have salaries more than

Rs.80, 000.

mysql >select distinct a.aname

from aircraft a,certified c,employees e

where a.aid=c.aid

and c.eid=e.eid

and not exists

(select *

from employees e1

where e1.eid=e.eid

and e1.salary<80000);

+------------+

| aname |

+------------+

| boeing120 |

| kingfisher |

+------------+

3 rows in set (0.00 sec)

2. For each pilot who is certified for more than three aircrafts, find the eid and the maximum

cruisingrange of the aircraft for which she or he is certified.

mysql > select c.eid, max(a.crurange)

from certified c, aircraft a

where c.aid = a.aid

group by c.eid

having count(*) > 3;

+-----+-----------------+

| eid | max(a.crurange) |

+-----+-----------------+

| 123 | 6000 |

+-----+-----------------+

1 row in set (0.00 sec)

3. Find the names of pilots whose salary is less than the price of the cheapest route from Bengaluru

to Frankfurt.

mysql > select distinct e.cname

from employees e

where e.salary < ( select min(f.price) from flights f

where f.fro= 'bangalore' and f.too= 'frankfrut');

+-----------+

| cname |

+-----------+

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 25

| philomina |

+-----------+

1 row in set (0.00 sec)

4. For all aircraft with cruisingrange over 1000 Kms, .find the name of the aircraft and the average

salary of all pilots certified for this aircraft.

mysql >select a.aid, avg (e.salary)

from aircraft a, certified c, employees e

where a.aid = c.aid and c.eid = e.eid and a.crurange > 1000

group by a.aid, a.aname);

+-----+----------------+

| aid | avg (e.salary) |

+-----+----------------+

| 11 | 80000.0000 |

| 22 | 200000.0000 |

| 33 | 200000.0000 |

| 44 | 47333.3333 |

| 55 | 80000.0000 |

+-----+----------------+

5 rows in set (0.00 sec)

5. Find the names of pilots certified for some Boeing aircraft.

mysql > select distinct e.cname

from employees e, certified c, aircraft a

where e.eid = c.eid and c.aid=a.aid and a.aname like 'boeing%';

+--------+

| cname |

+--------+

| kumar |

| muruli |

+--------+

2 rows in set (0.00 sec)

6. Find the aids of all aircraft that can be used on routes from Bengaluru to New Delhi.

mysql > select a.aid from aircraft a

where a.crurange > ( select min(f.distance)

from flights f where f.frm='bengaluru'

and f.too='newdelhi');

+-----+

| aid |

+-----+

| 11 |

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 26

| 22 |

| 33 |

| 44 |

| 55 |

+-----+

5 rows in set (0.00 sec)

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 27

DATABASE 3 : Student_Enrollment_database

DESCRIPTION:

Consider the following database of student enrollment in courses and books adopted for each course.

STUDENT (regno: string, name: string, major: string, bdate: date)

COURSE (course#: int, cname: string, dept: string)

ENROLL (regno: string, course#: int, sem: int, marks: int)

BOOK_ADOPTION (course#: int, sem: int, book-ISBN: int)

TEXT (book-ISBN: int, book-title: string, publisher: string, author: string)

1: Create the above tables by properly specifying the primary keys and the foreign keys.

2: Enter at least five tuples for each relation.

3: Demonstrate how you add a new text book to the database and make this book be adopted by some

department.

4: Produce a list of text books (include Course#, Book-ISBN, Book-title) in the alphabetical order for

courses offered by the ‗CS‘ department that use more than two books.

5: List any department that has all its adopted books published by a specific publisher.

6: Generation of suitable reports.

7: Create suitable front end for querying and displaying the results.

ER-Diagram:

Regno Name Marks Course_no Cname

STUDENT ENROLL COURSE

Major Bdate Sem Dept

BOOKADAPTION

SEM

Publisher TEXT

Author

Book_Isbn booktitle

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 28

mysql>create table student

(

regno varchar(20) primary key not null,

sname varchar(20),

major varchar(20),

bdate date

);

mysql>create table course

( cno int primary key not null,

cname varchar(20),

dept varchar(20)

);

mysql>create table enroll

(

regno varchar(20),

cno int,

sem int,

marks int,

primary key(regno,cno),

foreign key (regno) references student (regno),

foreign key (cno) references course (cno)

);

mysql>create table text

(

book_isbn int primary key not null,

book_title varchar(20),

pubisher varchar(20),

author varchar(20)

);

mysql>create table book_adoption

(

cno int,

sem int,

book_isbn int,

primary key(cno,book_isbn),

foreign key (cno) references course (cno),

foreign key (book_isbn) references text (book_isbn)

);

mysql>describe student;

+-------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| regno | varchar(20) | NO | PRI | NULL | |

| sname | varchar(20) | YES | | NULL | |

| major | varchar(20) | YES | | NULL | |

| bdate | date | YES | | NULL | |

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 29

+-------+-------------+------+-----+---------+-------+

4 rows in set (0.00 sec)

mysql>insert into student values ('1cg04is012','asha','networks',‘1986-06-07‘);

mysql>insert into student values ('1cg04is030','anitha','fafl',1984-04-08);

mysql>insert into student values ('1cg04is064','chandrika','ada', 1990-11-21);

mysql>insert into student values ('1cg04is105','Kumar','coding',1992-12-20);

mysql> insert into student values ('1cg04is110','Gagan','coding',1994-10-10);

mysql> select * from student;

+------------+-----------+----------+------------+

| regno | sname | major | bdate |

+------------+-----------+----------+------------+

| 1cg04is012 | asha | networks | 1986-06-07 |

| 1cg04is030 | anitha | fafl | 1984-04-08 |

| 1cg04is064 | chandrika | ada | 1990-11-21 |

| 1cg04is105 | Kumar | coding | 1992-12-20 |

| 1cg04is110 | Gangan | coding | 1994-10-10 |

+------------+-----------+----------+------------+

5 rows in set (0.00 sec)

mysql>describe course;

+-------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| cno | int(11) | NO | PRI | NULL | |

| cname | varchar(20) | YES | | NULL | |

| dept | varchar(20) | YES | | NULL | |

+-------+-------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

mysql>insert into course values (1,'network','ec');

mysql>insert into course values (2,'fafl','cs');

mysql>insert into course values (3,'ada','cs');

mysql>insert into course values (4,'coding','ec');

mysql>insert into course values (5,'datastr','cs');

mysql>insert into course values (6,'testing','ec');

mysql>select * from course;

+-----+---------+------+

| cno | cname | dept |

+-----+---------+------+

| 1 | network | ec |

| 2 | fafl | cs |

| 3 | ada | cs |

| 4 | coding | ec |

| 5 | datastr | cs |

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 30

| 6 | testing | ec |

+-----+---------+------+

6 rows in set (0.00 sec)

mysql>describe enroll;

+-------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| regno | varchar(20) | NO | PRI | | |

| cno | int(11) | NO | PRI | 0 | |

| sem | int(11) | YES | | NULL | |

| marks | int(11) | YES | | NULL | |

+-------+-------------+------+-----+---------+-------+

4 rows in set (0.01 sec)

mysql>insert into enroll values ('1cg04is012',1,6,85);

mysql>insert into enroll values ('1cg04is030',2,6,90);

mysql>insert into enroll values ('1cg04is064',3,6,92);

mysql>insert into enroll values ('1cg04is105',4,6,95);

mysql>insert into enroll values ('1cg04is110',5,4,90);

mysql>select * from enroll;

+------------+-----+------+-------+

| regno | cno | sem | marks |

+------------+-----+------+-------+

| 1cg04is012 | 1 | 6 | 85 |

| 1cg04is030 | 2 | 6 | 90 |

| 1cg04is064 | 3 | 6 | 92 |

| 1cg04is105 | 4 | 6 | 95 |

| 1cg04is110 | 5 | 4 | 90 |

+------------+-----+------+-------+

5 rows in set (0.00 sec)

mysql>desc text;

+------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+------------+-------------+------+-----+---------+-------+

| book_isbn | int(11) | NO | PRI | NULL | |

| book_title | varchar(20) | YES | | NULL | |

| pubisher | varchar(20) | YES | | NULL | |

| author | varchar(20) | YES | | NULL | |

+------------+-------------+------+-----+---------+-------+

4 rows in set (0.00 sec)

mysql>insert into text values (111,'comp net','tmg','garcia');

mysql>insert into text values (222,'algo des','pearson','leviten');

mysql>insert into text values (333,'automata','pearson','ullman');

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 31

mysql>insert into text values (444,'data str','pearson','tbaum');

mysql>insert into text values (555,'usp','pearson','yash');

mysql>insert into text values (666,'algo2','pearson','leviten');

mysql>insert into text values (777,'networks','pearson','garcia');

mysql>insert into text values (888,'swtesting','sapna‖,'sapna');

mysql>select * from text;

+-----------+------------+----------+---------+

| book_isbn | book_title | pubisher | author |

+-----------+------------+----------+---------+

| 111 | comp net | tmg | garcia |

| 222 | algo des | pearson | leviten |

| 333 | automata | pearson | ullman |

| 444 | data str | pearson | tbaum |

| 555 | usp | pearson | yash |

| 666 | algo2 | pearson | leviten |

| 777 | networks | pearson | garcia |

| 888 | swtesting | sapna | sapna |

+-----------+------------+----------+---------+

8 rows in set (0.00 sec)

mysql>desc book_adoption;

+-----------+---------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-----------+---------+------+-----+---------+-------+

| cno | int(11) | NO | PRI | 0 | |

| sem | int(11) | YES | | NULL | |

| book_isbn | int(11) | NO | PRI | 0 | |

+-----------+---------+------+-----+---------+-------+

3 rows in set (0.01 sec)

mysql>insert into book_adoption values (01,6,111);

mysql>insert into book_adoption values (02,6,333);

mysql>insert into book_adoption values (02,6,666);

mysql>insert into book_adoption values (02,6,111);

mysql>insert into book_adoption values (03,6,222);

mysql>insert into book_adoption values (03,5,555);

mysql>insert into book_adoption values (04,6,555);

mysql>insert into book_adoption values (05,3,444);

mysql>insert into book_adoption values (01,6,666);

mysql>insert into book_adoption values (01,6,777);

mysql>insert into book_adoption values (03,6, 777);

mysql>insert into book_adoption values (04,6, 777);

mysql>insert into book_adoption values (06,6, 888);

mysql>select * from book_adoption;

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 32

+-----+------+-----------+

| cno | sem | book_isbn |

+-----+------+-----------+

| 1 | 6 | 111 |

| 1 | 6 | 666 |

| 1 | 6 | 777 |

| 2 | 6 | 111 |

| 2 | 6 | 333 |

| 2 | 6 | 666 |

| 3 | 6 | 222 |

| 3 | 5 | 555 |

| 3 | 6 | 777 |

| 4 | 6 | 555 |

| 4 | 6 | 777 |

| 5 | 3 | 444 |

| 6 | 6 | 888 |

+-----+------+-----------+

13 rows in set (0.00 sec)

1: Demonstrate how you add a new text book to the database and make this book be adopted by some

department.

mysql>insert into text values (‗999‘,‘multimedia‘,‘phi‘, ‗navathe‘);

mysql>insert into book_adoption values (6,3,999);

mysql>select * from text;

+-----------+------------+----------+---------+

| book_isbn | book_title | pubisher | author |

+-----------+------------+----------+---------+

| 111 | comp net | tmg | garcia |

| 222 | algo des | pearson | leviten |

| 333 | automata | pearson | ullman |

| 444 | data str | pearson | tbaum |

| 555 | usp | pearson | yash |

| 666 | algo2 | pearson | leviten |

| 777 | networks | pearson | garcia |

| 888 | swtesting | sapna | sapna |

| 999 | multimedia | phi | navathe |

+-----------+------------+----------+---------+

9 rows in set (0.01 sec)

2: Produce a list of text books (include Course#, Book-ISBN, Book-title) in the alphabetical order for

courses offered by the ‗CS‘ department that use more than two books.

mysql>select c.cno, t.book_isbn, t.book_title

from course c, book_adoption b, text t

where c.cno=b.cno and t.book_isbn=b.book_isbn and c.dept='cs'

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 33

and c.cno in ( select cno

from book_adoption

group by cno

having count(*)>2)

order by c.cno, t.book_title;

+-----+-----------+------------+

| cno | book_isbn | book_title |

+-----+-----------+------------+

| 2 | 666 | algo2 |

| 2 | 333 | automata |

| 2 | 111 | comp net |

| 3 | 222 | algo des |

| 3 | 777 | networks |

| 3 | 555 | usp |

+-----+-----------+------------+

6 rows in set (0.00 sec) [

3: List any department that has all its adopted books published by a specific publisher.p

mysql>select distinct c.dept

from course c

where c.dept in

( select c.dept

from course c,book_adoption b,text t

where c.cno=b.cno

and t.book_isbn=b.book_isbn

and t.publisher='sapna')

and c.dept not in

(select c.dept

from course c,book_adoption b,text t

where c.cno=b.cno

and t.book_isbn=b.book_isbn

and t.publisher!='sapna'); +------+-----------------------------+

| dept | count(distinct b.book_isbn) |

+------+-----------------------------+

| ec | 6 |

+------+-----------------------------+

1 row in set (0.00 sec)

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 34

DATABASE 4 : Book_dealer_database

DESCRIPTION: The following tables are maintained by a book dealer.

AUTHOR (author-id: int, name: string, city: string, country: string)

PUBLISHER (publisher-id: int, name: string, city: string, country: string)

CATALOG (book-id: int, title: string, author-id: int, publisher-id: int, category-id: int, year: int, price: int)

CATEGORY (category-id: int, description: string)

ORDER-DETAILS (order-no: int, book-id: int, quantity: int)

1: Create the above tables by properly specifying the primary keys and foreign keys.

2: Enter at least five tuples for each tables.

3: Give the details of the authors who have 2 or more books in the catalog and the price of the books is

greater than the average price of the books in the catalog and the year of publication is after 2000.

4: Find the author of the book which has maximum sales.

5: Demonstrate how you increase the price of books published by a specific publisher by 10%.

6: Generation of suitable reports.

ER-Diagram:

Author-id Name Title Year Publisher-id Name

AUTHOR CATALOG PUBLISHER

City Country Price city country

Category-id CATEGORY Description

N

quantity

ORDER-DETAILS

1

ORDER orderno

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 35

mysql> create table author

(

authorid number(5),

name varchar(20),

city varchar(20),

country varchar(10),

primary key(authorid)

);

mysql> create table publisher

(

publisherid number(5),

name char(20),

city char(20),

country char(20),

primary key(publisherid)

);

mysql> create table catagory

(

catagoryid number(5),

description varchar(20),

primary key (catagoryid)

);

mysql> create table catalog

(

bookid number(5),

title varchar(20),

authorid number(5),

publisherid number(5),

catagoryid number(5),

year number(5),

price number(4),

primary key(bookid),

foreign key (authorid) references author(authorid),

foreign key (publisherid) references publisher (publisherid),

foreign key (catagoryid) references catagory(catagoryid)

);

mysql> create table order_details

(

ono int primary key,

bookid int,

quantity int,

foreign key (bookid) references catalog(bookid)

);

mysql>desc author;

+----------+-------------+------+-----+---------+-------+

| Field | Type | Null Key | Default | Extra |

+----------+-------------+------+-----+---------+-------+

| authorid | int(11) | NO | PRI | 0 | |

| name | varchar(20) | YES | | NULL | |

| city | varchar(20) | YES | | NULL | |

| country | varchar(10) | YES | | NULL | |

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 36

+----------+-------------+------+-----+---------+-------+

4 rows in set (0.00 sec)

mysql> insert into author values(1,'harish','delhi','india');

mysql> insert into author values(2,'balu','bangalore','india');

mysql> insert into author values(3,'gopal','goa','india');

mysql> insert into author values(4,'sagar','kerala','india');

mysql> insert into author values(5,'trivedi','washington','usa');

mysql> select * from author;

+----------+---------+------------+---------+

| authorid | name | city | country |

+----------+---------+------------+---------+

| 1 | harish | delhi | india |

| 2 | balu | bangalore | india |

| 3 | gopal | goa | india |

| 4 | sagar | kerala | india |

| 5 | trivedi | washington | usa |

+----------+---------+------------+---------+

5 rows in set (0.00 sec)

mysql> desc publisher;

+-------------+----------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------------+----------+------+-----+---------+-------+

| publisherid | int(11) | NO | PRI | 0 | |

| name | char(20) | YES | | NULL | |

| city | char(20) | YES | | NULL | |

| country | char(20) | YES | | NULL | |

+-------------+----------+------+-----+---------+-------+

4 rows in set (0.00 sec)

mysql> insert into publisher values(10,'pearson','goa','india');

mysql> insert into publisher values(11,'sapna','kerala','india');

mysql> insert into publisher values(12,'sapna','bangalore','india');

mysql> insert into publisher values(13,'sagar','kolkata','india');

mysql> insert into publisher values(14,'tata','washington','usa');

mysql> select * from publisher;

+-------------+---------+------------+---------+

| publisherid | name | city | country |

+-------------+---------+------------+---------+

| 10 | pearson | goa | india |

| 11 | sapna | kerala | india |

| 12 | sapna | bangalore | india |

| 13 | sagar | kolkata | india |

| 14 | tata | washington | usa |

+-------------+---------+------------+---------+

5 rows in set (0.01 sec)

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 37

mysql> desc catagory;

+-------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------------+-------------+------+-----+---------+-------+

| catagoryid | int(11) | NO | PRI | 0 | |

| description | varchar(20) | YES | | NULL | |

+-------------+-------------+------+-----+---------+-------+

2 rows in set (0.00 sec)

mysql> insert into catagory values(111,'moral');

mysql> insert into catagory values(222,'novels');

mysql> insert into catagory values(333,'journals');

mysql> insert into catagory values(444,'technical');

mysql> insert into catagory values(555,'story');

mysql> select * from catagory;

+------------+-------------+

| catagoryid | description |

+------------+-------------+

| 111 | moral |

| 222 | novels |

| 333 | journals |

| 444 | technical |

| 555 | story |

+------------+-------------+

5 rows in set (0.01 sec)

mysql> desc catalog;

+-------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------------+-------------+------+-----+---------+-------+

| bookid | int(11) | NO | PRI | 0 | |

| title | varchar(20) | YES | | NULL | |

| authorid | int(11) | YES | MUL | NULL | |

| publisherid | int(11) | YES | MUL | NULL | |

| catagoryid | int(11) | YES | MUL | NULL | |

| year | int(11) | YES | | NULL | |

| price | int(11) | YES | | NULL | |

+-------------+-------------+------+-----+---------+-------+

7 rows in set (0.00 sec)

mysql> insert into catalog values(20,'computer science',1,11,111,2006,290);

mysql> insert into catalog values(21,'operating system',5,10,222,2002,220);

mysql> insert into catalog values(22,'dbms',2,11,555,2006,250);

mysql> insert into catalog values(23,'unix',1,12,222,2002,350);

mysql> insert into catalog values(24,'ada',2,11,444,2005,450);

mysql> insert into catalog values(25,'cn2',1,11,444,2005,450);

mysql> select * from catalog;

+--------+------------------+----------+-------------+------------+------+-------+

| bookid | title | authorid | publisherid | catagoryid | year | price |

+--------+------------------+----------+-------------+------------+------+-------+

| 20 | computer science | 1 | 11 | 111 | 2006 | 290 |

| 21 | operating system | 5 | 10 | 222 | 2002 | 220 |

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 38

| 22 | dbms | 2 | 11 | 555 | 2006 | 250 |

| 23 | unix | 1 | 12 | 222 | 2002 | 350 |

| 24 | ada | 2 | 11 | 444 | 2005 | 450 |

| 25 | cn2 | 1 | 11 | 444 | 2005 | 450 |

+--------+------------------+----------+-------------+------------+------+-------+

6 rows in set (0.00 sec)

mysql> desc order_details;

+----------+---------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+----------+---------+------+-----+---------+-------+

| ono | int(11) | NO | PRI | NULL | |

| bookid | int(11) | YES | MUL | NULL | |

| quantity | int(11) | YES | | NULL | |

+----------+---------+------+-----+---------+-------+

3 rows in set (0.00 sec)

mysql> insert into order_details values(50,21,100);

mysql> insert into order_details values(51,22,200);

mysql> insert into order_details values(52,24,250);

mysql> insert into order_details values(53,21,150);

mysql> insert into order_details values(54,24,350);

mysql> select * from order_details;

+-----+--------+----------+

| ono | bookid | quantity |

+-----+--------+----------+

| 50 | 21 | 100 |

| 51 | 22 | 200 |

| 52 | 24 | 250 |

| 53 | 21 | 150 |

| 54 | 24 | 350 |

+-----+--------+----------+

5 rows in set (0.00 sec)

1: Give the details of the authors who have 2 or more books in the catalog and the price of the books is

greater than the average price of the books in the catalog and the year of publication is after 2000.

mysql>select c.authorid, a.name, a.city, a.country

from author a, catalog c

where a.authorid=c.authorid and c.year>2000

group by c.authorid having count(a.authorid)>=2 and

sum(price)>(select avg(price) from catalog);

+----------+--------+-----------+---------+

| authorid | name | city | country |

+----------+--------+-----------+---------+

| 1 | harish | delhi | india |

| 2 | balu | bangalore | india |

+----------+--------+-----------+---------+

2 rows in set (0.00 sec)

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 39

2: Find the author of the book which has maximum sales.

mysql> create view SUM_OF_QTY as (select bookid, sum(quantity) as SUM from order_details group

by bookid);

Query OK, 0 rows affected (0.06 sec)

mysql> select * from SUM_OF_QTY;

+--------+------+

| bookid | SUM |

+--------+------+

| 21 | 250 |

| 22 | 200 |

| 24 | 600 |

+--------+------+

3 rows in set (0.00 sec)

mysql> select a.name,o.bookid from author a, catalog c, order_details o

where a.authorid=c.authorid and c.bookid=o.bookid

group by a.name,o.bookid having sum(quantity) >= ( select max(SUM) from SUM_OF_QTY);

+------+--------+

| name | bookid |

+------+--------+

| balu | 24 |

+------+--------+

1 row in set (0.01 sec)

3: Demonstrate how you increase the price of books published by a specific publisher by 10%.

update catalog

set price=price+price price*0.1

where publisherid in(select publisherid from publisher where name=‘pearson‘);

Before update

mysql> select * from catalog;

+--------+------------------+----------+-------------+------------+------+-------+

| bookid | title | authorid | publisherid | catagoryid | year | price |

+--------+------------------+----------+-------------+------------+------+-------+

| 20 | computer science | 1 | 11 | 111 | 2006 | 290 |

| 21 | operating system | 5 | 10 | 222 | 2002 | 220 |

| 22 | dbms | 2 | 11 | 555 | 2006 | 250 |

| 23 | unix | 1 | 12 | 222 | 2002 | 350 |

| 24 | ada | 2 | 11 | 444 | 2005 | 450 |

| 25 | cn2 | 1 | 11 | 444 | 2005 | 450 |

+--------+------------------+----------+-------------+------------+------+-------+

6 rows in set (0.00 sec)

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 40

After update

+--------+------------------+----------+-------------+------------+------+-------+

| bookid | title | authorid | publisherid | catagoryid | year | price |

+--------+------------------+----------+-------------+------------+------+-------+*

| 20 | computer science | 1 | 11 | 111 | 2006 | 290 |

| 21 | operating system | 5 | 10 | 222 | 2002 | 242 |

| 22 | dbms | 2 | 11 | 555 | 2006 | 250 |

| 23 | unix | 1 | 12 | 222 | 2002 | 350 |

| 24 | ada | 2 | 11 | 444 | 2005 | 450 |

| 25 | cn2 | 1 | 11 | 444 | 2005 | 450 |

+--------+------------------+----------+-------------+------------+------+-------+

6 rows in set (0.00 sec)

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 41

DATABASE 5 : Banking_Enterprise_database

DESCRIPTION:

Consider the following database for a banking enterprise

BRANCH(branch-name:string, branch-city:string, assets:real)

ACCOUNT(accno:int, branch-name:string, balance:real)

DEPOSITOR(customer-name:string, accno:int)

CUSTOMER(customer-name:string, customer-street:string, customer-city:string)

LOAN(loan-number:int, branch-name:string, amount:real)

BORROWER(customer-name:string, loan-number:int)

i Create the above tables by properly specifying the primary keys and the foreign keys

ii. Enter at least five tuples for each relation

iii. Find all the customers who have at least two accounts at the Main branch.

iv. Find all the customers who have an account at all the branches located in a specific city.

v. Demonstrate how you delete all account tuples at every branch located in a specific city.

vi. Generate suitable reports.

vii Create suitable front end for querying and displaying the results.

Cust-name Cust-street Accno Branch-name

N N

CUSTOMER DEPOSITOR ACCOUNT

N

Balance

N

ACCOUNT-BRANCH

BORROWER

Branch-city Branch-name

1

Assets BRANCH

1

LOAN-BRANCH

N

LOAN Loan-no

Branch-name Amount

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 42

mysql> create table branch

(

branchname varchar(20) primary key,

branchcity varchar(20),

assets int

);

mysql> create table account

(

accno int primary key,

branchname varchar(20),

balance real,

foreign key (branchname) references branch (branchname)

);

mysql> create table customer

(

customername varchar(20) primary key,

customerstreet varchar(20),

customercity varchar(20)

);

mysql> create table depositor

(

customername varchar(20),

accno int,

primary key(customername,accno),

foreign key(customername) references customer(customername),

foreign key(accno) references account(accno)on delete cascade

);

mysql> create table loan

(

loanno int,

branchname varchar(20),

amount real,

primary key(loanno),

foreign key(branchname) references branch(branchname)

);

mysql> create table borrower

(

customername varchar(20),

loanno int,

primary key(customername,loanno),

foreign key(customername) references customer(customername),

foreign key(loanno) references loan(loanno)

);

mysql> desc branch;

+------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+------------+-------------+------+-----+---------+-------+

| branchname | varchar(20) | NO | PRI | NULL | |

| branchcity | varchar(20) | YES | | NULL | |

| assets | int(11) | YES | | NULL | |

+------------+-------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 43

mysql>insert into branch values( ―basavanagudi‖,‖bangalore‖,25000000);

mysql>insert into branch values( ―greenpark‖,‖NewDelhi‖, 50000000);

mysql>insert into branch values( ―jayanagar‖,‖ bangalore‖, 50000000);

mysql>insert into branch values( ―marinadrive‖,‖ mumbai‖, 55000000);

mysql>insert into branch values( ―Noida‖,‖ NewDelhi‖, 35000000);

mysql> select * from branch;

+--------------+------------+----------+

| branchname | branchcity | assets |

+--------------+------------+----------+

| basavanagudi | bangalore | 25000000 |

| greenpark | NewDelhi | 50000000 |

| jayanagar | bangalore | 50000000 |

| marinadrive | mumbai | 55000000 |

| Noida | NewDelhi | 35000000 |

+--------------+------------+----------+

5 rows in set (0.00 sec)

mysql> desc account;

+------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+------------+---- ---------+------+-----+---------+-------+

| accno | int(11) | NO | PRI | NULL | |

| branchname | varchar(20) | YES | MUL | NULL | |

| balance | double | YES | | NULL | |

+------------+-------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

mysql>insert into account values( 105,‖ jayanagar‖,45000);

mysql>insert into account values( 205,‖ jayanagar‖,50000);

mysql>insert into account values( 250,‖ basavanagudi‖,40000);

mysql>insert into account values( 450,‖ basavanagudi‖,20000);

mysql>insert into account values( 458,‖ noida‖,25000);

mysql>insert into account values( 780,‖ greenpark‖,40000);

mysql>insert into account values( 900,‖ marinadrive‖,100000);

mysql> select * from account;

+-------+--------------+---------+

| accno | branchname | balance |

+-------+--------------+---------+

| 105 | jayanagar | 45000 |

| 205 | jayanagar | 50000 |

| 250 | basavanagudi | 40000 |

| 450 | basavanagudi | 20000 |

| 458 | noida | 25000 |

| 780 | greenpark | 40000 |

| 900 | marinadrive | 100000 |

+-------+--------------+---------+

7 rows in set (0.00 sec)

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 44

mysql> desc loan;

+------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+------------+-------------+------+-----+---------+-------+

| loanno | int(11) | NO | PRI | 0 | |

| branchname | varchar(20) | YES | MUL | NULL | |

| amount | double | YES | | NULL | |

+------------+-------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

mysql>insert into loan values( 1002,‖ jayanagar ‖,255000);

mysql>insert into loan values( 1003,‖ basavanagudi‖,555000);

mysql>insert into loan values( 1004,‖ noida‖,500000);

mysql>insert into loan values( 1005,‖ marinadrive‖,400000);

mysql>insert into loan values( 1006,‖ greenpark‖,600000);

mysql> select * from loan;

+--------+--------------+--------+

| loanno | branchname | amount |

+--------+--------------+--------+

| 1002 | jayanagar | 255000 |

| 1003 | basavanagudi | 555000 |

| 1004 | noida | 500000 |

| 1005 | marinadrive | 400000 |

| 1006 | greenpark | 600000 |

+--------+--------------+--------+

5 rows in set (0.00 sec)

mysql> desc customer;

+----------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+----------------+-------------+------+-----+---------+-------+

| customername | varchar(20) | NO | PRI | NULL | |

| customerstreet | varchar(20) | YES | | NULL | |

| customercity | varchar(20) | YES | | NULL | |

+----------------+-------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

mysql>insert into customer values( ―john‖,‖noida‖,‖newdelhi‖);

mysql>insert into customer values( ―kumar‖,‖basavanagudi‖,‖bangalore‖);

mysql>insert into customer values( ―raju‖,‖jayanagar‖,‖bangalore‖);

mysql>insert into customer values( ―smith‖,‖highhills‖,‖newdelhi‖);

mysql>insert into customer values( ―sujal‖,‖kuvempu‖,‖bangalore‖);

mysql> select * from customer;

+--------------+----------------+--------------+

| customername | customerstreet | customercity |

+--------------+----------------+--------------+

| john | noida | newdelhi |

| kumar | basavanagudi | bangalore |

| raju | jayanagar | bangalore |

| smith | highhills | newdelhi |

| sujal | kuvempu | bangalore |

+--------------+----------------+--------------+

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 45

5 rows in set (0.00 sec)

mysql> desc depositor;

+--------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+--------------+-------------+------+-----+---------+-------+

| customername | varchar(20) | NO | PRI | | |

| accno | int(11) | NO | PRI | 0 | |

+--------------+-------------+------+-----+---------+-------+

2 rows in set (0.00 sec)

mysql>insert into depositor values( ―john‖,105);

mysql>insert into depositor values( ―john‖,205);

mysql>insert into depositor values( ―john‖,250);

mysql>insert into depositor values( ―kumar‖,450);

mysql>insert into depositor values( ―raju‖,458);

mysql>insert into depositor values( ―smith‖,780);

mysql>insert into depositor values( ―sujal‖,900);

mysql> select * from depositor;

+--------------+-------+

| customername | accno |

+--------------+-------+

| john | 105 |

| john | 205 |

| john | 250 |

| kumar | 450 |

| raju | 458 |

| smith | 780 |

| sujal | 900 |

+--------------+-------+

7 rows in set (0.00 sec)

mysql> desc borrower;

+--------------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+--------------+-------------+------+-----+---------+-------+

| customername | varchar(20) | NO | PRI | | |

| loanno | int(11) | NO | PRI | 0 | |

+--------------+-------------+------+-----+---------+-------+

2 rows in set (0.00 sec)

mysql>insert into borrower values( ―john‖,1002);

mysql>insert into borrower values( ―kumar‖,1003);

mysql>insert into borrower values( ―raju‖,1004);

mysql>insert into borrower values( ―smith‖,1005);

mysql>insert into borrower values( ―sujal‖,1006);

mysql> select * from borrower;

+--------------+--------+

| customername | loanno |

+--------------+--------+

| john | 1002 |

| kumar | 1003 |

| raju | 1004 |

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 46

| smith | 1005 |

| sujal | 1006 |

+--------------+--------+

5 rows in set (0.00 sec)

1: Find all the customers who have at least 2 accounts at the main branch.

mysql> select d.customername

from depositor d, account a

where d.accno=a.accno

group by d.customername having count(d.customername)>=2;

+--------------+

| customername |

+--------------+

| john |

+--------------+

1 row in set (0.00 sec)

2: Find all the customers who have an account at all the branches located in a specific city.

mysql> select d.customername , count(distinct b.branchname)

from account a, depositor d, branch b

where a.accno=d.accno and b.branchname =a.branchname and b.branchcity=‖bangalore‖

group by d.customername having count(distinct b.branchname) = (select count(distinct b.branchname)

from branch b where b.branchcity=‖bangalore‖);

+--------------+------------------------------+

| customername | count(distinct b.branchname) |

+--------------+------------------------------+

| john | 2 |

+--------------+------------------------------+

1 row in set (0.00 sec)

3: Demonstrate how you delete all account tuples at every branch located in a specific city.

mysql> delete from account where bname in ( select bname from branch where bcity=‘mumbai‘);

mysql> select * from account;

+-------+--------------+---------+

| accno | branchname | balance |

+-------+--------------+---------+

| 105 | jayanagar | 45000 |

| 205 | jayanagar | 50000 |

| 250 | basavanagudi | 40000 |

| 450 | basavanagudi | 20000 |

| 458 | noida | 25000 |

| 780 | greenpark | 40000 |

+-------+--------------+---------+

6 rows in set (0.00 sec)

mysql> select * from depositor;

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 47

+--------------+-------+

| customername | accno |

+--------------+-------+

| john | 105 |

| john | 205 |

| john | 250 |

| kumar | 450 |

| raju | 458 |

| smith | 780 |

+--------------+-------+

6 rows in set (0.00 sec)

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 48

VIVA QUESTIONS

1. What is Data?

2. What is database?

3. What is DBMS?

4. Define RDBMS.

5. What is a Database system?

6. What are the advantages of DBMS?

7. What are the disadvantages in File Processing System?

8. Describe the three levels of data abstraction?

9. Define the "integrity rules"?

10. What is Data Independence?

11. What is a view?

12. What is Data Model?

13. What is E-R model?

14. What is Object Oriented model?

15. What is an Entity?

16. What is an Entity type?

17. What is an Entity set?

18. What is Weak Entity set?

19. What is an attribute?

20. Define Primary Key.

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 49

21. Define Foreign Key.

22. Define Unique Key.

23. What is a Relation Schema and a Relation?

24. What is degree of a Relation?

25. What is Relationship?

26. What is Relationship set?

27. What is Relationship type?

28. What is degree of Relationship type?

29. Define cardinality.

30. Define cardinality ratio.

31. What is Data Integrity ?

32. What is Entity Integrity?

33. What is Referential Integrity ?

34. What is Domain Integrity?

35. What is DDL (Data Definition Language)?

36. What is DML (Data Manipulation Language)?

37. What is Relational Algebra?

38. What is normalization?

39. What is Functional Dependency?

40. Mention different types of join operators?

41. What is Fully Functional dependency?

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 50

42. Define trivial functional dependency.

43. What is 1 NF (Normal Form)?

44. What is 2NF?

45. What is 3NF?

46. What is BCNF (Boyce-Codd Normal Form)?

47. What is a query?

48. What do you mean by subquery?

49. Mention ACID properties.

50. Define Denormalization.

Database Applications Laboratory 2015-16

Dept of ISE, CIT, Gubbi Page 51

References: TEXT BOOKS:

1.Fundamentals of Database Systems – Elmasri and Navathe, 5th Edition, Addison-Wesley, 2007

2.Database Management Systems – Raghu Ramakrishnan and Johannes Gehrke – 3rd

Edition, McGraw-

Hill, 2003.

REFERENCE BOOKS:

1.Data Base System Concepts – Silberschatz, Korth and Sudharshan, 5th Edition, Mc-GrawHill, 2006.

2.An Introduction to Database Systems – C.J. Date, A. Kannan, S. Swamynatham, 8th

Edition, Pearson

Education, 2006.

Sites refered:

1: https://www.digitalocean.com/community/tutorials/a-basic-mysql-tutorial

2: https://www3.ntu.edu.sg/home/ehchua/programming/sql/MySQL_Beginner.html