DBM S Manual Final
DBM S Manual Final
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.
Instructions:
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 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 is customizable. The open-source GPL license allows programmers to modify the
MySQL software to fit their own specific environments.
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.
-u root -p refers to user root password. Login to MySQL as root user(u) with mysql password(p).
Points to keep in mind:
Dept of ISE, CIT, Gubbi Page 4
Database Applications Laboratory 2015-16
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‖.
2: Delete if it exists
SHOW DATABASES
USE databaseName
SELECT DATABASE()
SHOW TABLES
4: Describe the details for a table
DESCRIBE|DESC tableName
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;
show databases;
mysql> show databases;
+--------------------+
| database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.01 sec)
creating a database
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
use company;
To check the overview of the tables that the database contains.
show tables;
How to create a mysql table
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
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)
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
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)
mysql> INSERT INTO employee (name, city) VALUES ('Santhosh', 'Tumkur'),( 'vandana', 'Hasan');
2: list all the rows of all columns, * is a wildcard denoting all columns
syntax: select * from tablename
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
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 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.
ER Diagram
Meets
Fid
Cname
Snum
Snam Snum
Room
STUDENT CLASS
Enro
Level
Majo Cname Teach
Age
FACULTY
Deptid
Fid Fname
| 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.
3. Find the names of all students who are enrolled in two classes that meet at the same time.
+--------+
| 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.
5. Find the names of faculty members for whom the combined enrollment of the courses that they
teach is less than five.
DATABASE 2 : Airline_Flight_database
DESCRIPTION:
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.
ER Diagram :
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)
+----+-----------+------------+----------+---------+---------+--------+
| 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)
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)
+-----+------------+----------+
| 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);
+-----+-----------+--------+
| 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)
+-----+-----+
| eid | aid |
+-----+-----+
| 123 | 11 |
| 201 | 22 |
| 201 | 33 |
| 301 | 33 |
| 123 | 44 |
| 401 | 44 |
| 501 | 44 |
| 123 | 55 |
| 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.
3. Find the names of pilots whose salary is less than the price of the cheapest route from Bengaluru
to Frankfurt.
| 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.
6. Find the aids of all aircraft that can be used on routes from Bengaluru to New Delhi.
| 22 |
| 33 |
| 44 |
| 55 |
+-----+
5 rows in set (0.00 sec)
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:
BOOKADAPTION
SEM
Publisher TEXT
Author
Book_Isbn booktitle
+-------+-------------+------+-----+---------+-------+
| 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 | |
+-------+-------------+------+-----+---------+-------+
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>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)
+-----+---------+------+
| cno | cname | dept |
+-----+---------+------+
| 1 | network | ec |
| 2 | fafl | cs |
| 3 | ada | cs |
| 4 | coding | ec |
| 5 | datastr | cs |
Dept of ISE, CIT, Gubbi Page 29
Database Applications Laboratory 2015-16
| 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)
+------------+-----+------+-------+
| 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)
+-----------+------------+----------+---------+
| 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)
+-----+------+-----------+
| 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.
+-----------+------------+----------+---------+
| 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.
3: List any department that has all its adopted books published by a specific publisher.p
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:
quantity
ORDER-DETAILS
ORDER orderno
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 | |
Dept of ISE, CIT, Gubbi Page 35
Database Applications Laboratory 2015-16
+----------+-------------+------+-----+---------+-------+
4 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.
+----------+--------+-----------+---------+
| authorid | name | city | country |
+----------+--------+-----------+---------+
| 1 | harish | delhi | india |
| 2 | balu | bangalore | india |
+----------+--------+-----------+---------+
2 rows in set (0.00 sec)
mysql> create view SUM_OF_QTY as (select bookid, sum(quantity) as SUM from order_details group
by bookid);
+--------+------+
| 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)
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 5 : Banking_Enterprise_database
DESCRIPTION:
N N
CUSTOMER DEPOSITOR ACCOUNT
N
Balance
N
ACCOUNT-BRANCH
BORROWER
Branch-city Branch-name
1
Assets BRANCH
LOAN-BRANCH
N
LOAN Loan-no
Branch-name Amount
Dept of ISE, CIT, Gubbi Page 41
Database Applications Laboratory 2015-16
+------------+-------------+------+-----+---------+-------+
| 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)
| 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.
+--------------+
| 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.
+--------------+------------------------------+
| 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‘);
+-------+--------------+---------+
| 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;
+--------------+-------+
| customername | accno |
+--------------+-------+
| john | 105 |
| john | 205 |
| john | 250 |
| kumar | 450 |
| raju | 458 |
| smith | 780 |
+--------------+-------+
6 rows in set (0.00 sec)
VIVA QUESTIONS
1. What is Data?
2. What is database?
3. What is DBMS?
4. Define RDBMS.
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