Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (1 vote)
5K views

Practical No: 1: TASK: Given The Following Tables For A Database LIBRARY

The document describes tables for books and issued books in a library database. It includes sample data and queries. The books table has fields for book ID, name, author, publisher, price, type and quantity. The issued table tracks books issued by book ID and quantity. Several queries are provided to select, update and insert data in the tables, such as finding book names and authors from a specific publisher or increasing book prices.

Uploaded by

Mohit Sakore
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
5K views

Practical No: 1: TASK: Given The Following Tables For A Database LIBRARY

The document describes tables for books and issued books in a library database. It includes sample data and queries. The books table has fields for book ID, name, author, publisher, price, type and quantity. The issued table tracks books issued by book ID and quantity. Several queries are provided to select, update and insert data in the tables, such as finding book names and authors from a specific publisher or increasing book prices.

Uploaded by

Mohit Sakore
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

PRACTICAL NO: 1

TASK : Given the following tables for a database LIBRARY


TABLE: books

+-----------+---------------------+-----------------------+---------------+-------+-----------+------+
| book_id | book_name
| author_name
| publishers | price | type
| qty |
+-----------+---------------------+-----------------------+---------------+-------+-----------+------+
| C0001 | Fast cook
| Lata Kapoor
| EPB
| 355 | Cookery | 5 |
| F0001 | The Tears
| William Hopkins | First Publ | 650 | Fiction |20 |
| T0001 | My first c++
| Brian & Brooke | EPB
| 350 | Text
|10 |
| T0002 | C++ Brainworks | AW Rossaine
| TDH
| 350 | Text
|15 |
| F0002 | Thunderbolts
| Anna Roberts
| First publ | 750 | Fiction |50 |
+-----------+----------------------+----------------------+---------------+-------+------------+------+

TABLE: ISSUED
+---------+--------------------+
| book_id | Quantity_issued |
+---------+--------------------+
| T0001 |
4
|
| C0001 |
5
|
| F0001 |
2
|
+---------+--------------------+

CREATE TABLE COMMAND


CREATE TABLE books
-> ( book_id char(10),
-> book_name char(20),
-> author_name char(20),
-> publishers char(10),
-> price integer,
-> type char(10),
-> qty. integer);
mysql> INSERT INTO books
-> VALUES('C0001','Fast cook','Lata Kapoor','EPB',355,'Cookery',5);
-> INSERT INTO books VALUES('F0001','The Tears','William Hopkins','First
Publ',650,'Fiction',20);

mysql> CREATE TABLE issued


-> ( book_id char(10),
-> Quantity_issued integer);
mysql> INSERT INTO issued VALUES('T0001',4);

QUERY QUESTIONS:
(a) To show Book name, Author name and Price of books of first Publ.
publishers.
SOLUTION:
SELECT book_name, author_name, price
FROM books
WHERE publishers=first publ;
OUTPUT:
+----------------+---------------------+-------+
| book_name | author_name
| price |
+--------------- +---------------------+-------+
| The Tears
| William Hopkins | 650 |
| Thunderbolts | Anna Roberts
| 750 |
+----------------+---------------------+-------+
(b) To list the names from books of Text type.
SOLUTION:
SELECT book_name
FROM books
WHERE type=text;
OUTPUT:
+--------------------+
| book_name
|
+--------------------+
| My first c++
|
| C++ Brainworks |
+--------------------+
(c) To display the names and price from books in ascending order of their price.
SOLUTION:
SELECT book_name, price
FROM books
ORDER BY price;

OUTPUT:
+--------------------+-------+
| book_name
| price |
+--------------------+-------+
| My first c++
| 350 |
| C++ Brainworks | 350 |
| Fast cook
| 355 |
| The Tears
| 650 |
| Thunderbolts
| 750 |
+--------------------+-------+
(d) To increase the price of all books of EPB Publishers by 50.
SOLUTION:
UPDATE books
SET price=price+50
WHERE publishers=EPB;
OUTPUT:
+-------+------------+
| price | publishers |
+-------+------------+
| 405 | EPB
|
| 400 | EPB
|
+-------+------------+
(e) Display book id, book name, quantity issued, for all books which have been
issued.
SOLUTION:
SELECT books.book_id, book_name,Quantity_issued
FROM books,issued
WHERE books.book_id=issued.book_id;
OUTPUT:
+---------+-----------------+---------------------+
| book_id | book_name | Quantity_issued |
+---------+-----------------+---------------------+
| C0001 | Fast cook
|
5
|
| F0001 | The Tears
|
2
|
| T0001 | My first c++ |
4
|
+---------+----------------+----------------------+
(f) To insert a new row in the table Issued having the following data: F0003,1.
SOLUTION:

INSERT INTO issued


VALUES(F0003,1);
OUTPUT:
+------------+----------------------+
| book_id | Quantity_issued |
+------------+----------------------+
| T0001 |
4
|
| C0001 |
5
|
| F0001 |
2
|
| F0003 |
1
|
+------------+----------------------+

(g) Give the output of the following queries:


i)SELECT COUNT(*) FROM books;
OUTPUT:
+--------------+
| COUNT (*) |
+--------------+
|
5
|
+--------------+
ii) SELECT MAX(price) FROM books WHERE qty>=15;
OUTPUT:
+---------------+
| MAX(price) |
+---------------+
|
750
|
+---------------+
iii)SELECT book_name, author_name FROM books WHERE publishers=EPB;
OUTPUT:
+---------------+--------------------+
| book_name | author_name |
+---------------+--------------------+
| Fast cook | Lata Kapoor
|
| My first c++| Brian & Brooke|

+---------------+--------------------+
iv) SELECT COUNT(DISTINCT publishers) FROM books WHERE price>=400;
OUTPUT:
+--------------------------------------+
| COUNT(DISTINCT publishers) |
+--------------------------------------+
|
2
|
+--------------------------------------+

PRACTICAL NO: 2
TASK : Given the following table STUDENT
TABLE: student
+----+-----------+------+-------------+------------+------+------+
|No.|Name
| Age |Dept
| DOA
| Fee | Sex|
+----+-----------+------+-------------+------------+------+------+
|1 | Pankaj |24 |computer |10/01/97| 120 |M |
|2 | Shalini |21 |history
|24/03/98| 200 |F
|
|3 | Sanjay |22 |hindi
|12/12/96| 300 |M |
|4 | Sudha |25 |history
|01/07/99| 400 |F
|
|5 | Rakesh |22 |hindi
|05/09/97| 250 |M |
|6 | Shakeel |30 |history
|27/06/98| 300 |M |
|7 | Surya |34 |computer |25/02/97| 210 |M |
|8 | Shikha |23 |hindi
|31/07/97| 200 |F
|
+----+-----------+------+--------------+------------+------+-----+

CREATE TABLE COMMAND


CREATE TABLE student

->(No. integer,
-> Name char(20),
-> Age integer,
-> Dept char(20),
-> DOA date,
-> fee integer,
-> sex char);
INSERT INTO student
-> VALUES(1,Pankaj',24,'computer',10/01/97,120,M);

QUERY QUESTIONS:
(a)Show all information about students of history.
SOLUTION:
SELECT* FROM student
WHERE Dept=history;

OUTPUT:
+----+----------+-------+---------+------------+-------+------+
|No.|Name | Age |Dept | DOA
| Fee | Sex |
+----+----------+-------+---------+------------+-------+------+
|2 | Shalini |21 |history |24/03/98| 200 |F
|
|4 | Sudha |25 |history |01/07/99| 400 |F
|
|6 | Shakeel|30 |history |27/06/98| 300 |M
|
+----+----------+-------+---------+------------+-------+------+

(b)List the name of female students in hindi department.


SOLUTION:
SELECT Name FROM student
WHERE sec=F and
Dept=hindi;

OUTPUT:
+----------+
|Name |
+----------+
| Shikha |
+----------+
(c) List names of all students with their DOA in ascending order.
SOLUTION:
SELECT name FROM student
ORDER BY DOA;
OUTPUT:
+---------+
|Name |
+---------+
|Sanjay |
|Pankaj |
|Surya |
|Shikha |
|Rakesh |
|Shalini |
|Shakeel|
|Sudha |
+----------+
(d) Display students Name,Fee,age for male students.
SOLUTION:
SELECT Name,Fee,Age
FROM student
WHERE sex=M;
OUTPUT:
+----------+------+-------+
|Name | Age | Fee |
+-----------+-----+-------+
| Pankaj |24 | 120 |
| Sanjay |22 | 300 |
| Rakesh |22 | 250 |

| Shakeel|30 |300 |
| Surya |34 | 210 |
+-----------+----1-+------+
(e) Count no of students with age<23.
SOLUTION:
SELECT COUNT(*) FROM student
WHERE Age>23;
OUTPUT:
+------------+
|COUNT(*)|
+------------+
|3
|
+------------+
(f)Insert new row in student with the following data:
9,Zaheer,36,computer,{12/03/97},230,M
SOLUTION:
INSERT INTO student
VALUES(9,Zaheer,36,computer,{12/03/97},230,M);
OUTPUT:
+----+----------+-------+-------------+------------+-------+------+
|No.|Name | Age |Dept
| DOA
| Fee | Sex |
+----+-----------+------+-------------+------------+-------+------+
|1 | Pankaj |24 |computer |10/01/97| 120 |M
|
|2 | Shalini |21 |history
|24/03/98| 200 |F
|
|3 | Sanjay |22 |hindi
|12/12/96| 300 |M
|
|4 | Sudha |25 |history
|01/07/99| 400 |F
|
|5 | Rakesh |22 |hindi
|05/09/97| 250 |M
|
|6 | Shakeel|30 |history
|27/06/98| 300 |M
|
|7 | Surya |34 |computer |25/02/97| 210 |M
|
|8 | Shikha |23 |hindi
|31/07/97| 200 |F
|
|9 | Zaheer | 36 | computer|12/03/97 |230 |M
|
+----+-----------+------+-------------+-------------+------+------+

PRACTICAL NO: 3
TASK : Given following tables for a database FURNITURE
TABLE: furniture
+----+-----------------+--------------+---------------+--------+-----------+
| no | itemname
| type
| DOS
| price | discount |
+----+-----------------+--------------+---------------+--------+-----------+
| 1 | White lotus | double bed | 2002-02-23 | 30000 |
25 |
| 2 | pink feather | baby cot
| 2002-01-20 | 7000 |
20 |
| 2 | dolphin
| baby cot
| 2002-02-19 | 9500 |
20 |
| 4 | decent
| office table | 2002-01-01 | 25000 |
30 |
| 5 | comfort zone | double bed | 2002-01-12 | 25000 |
25 |
| 6 | donald
| baby cot
| 2002-02-24 | 6500 |
15 |
| 7 | royal finish
| office table | 2002-02-20 | 18000 |
30 |
| 8 | royal tiger
| sofa
| 2002-02-22 | 31000 |
30 |
| 9 | econo sitting | sofa
| 2001-12-13 | 9500 |
25 |
| 10 | eating paradise | dining
| 2002-02-02 | 11500 |
25 |
+----+------------------+--------------+-------------- +--------+-----------+

CREATE TABLE COMMAND


mysql> CREATE TABLE furniture
-> (no integer,
-> itemname char(20),
-> type char(25),
-> DOS DATE,
-> price integer,
-> discount integer);
> INSERT INTO furniture VALUES (1,"White lotus","double bed",'2002-02-23',3
0000,25);
arrivals
+----+------------------+----------- -+--------------+---------+-----------+
| no | itemname
| type
| DOS
| price | discount |
+----+------------------+-------------+---------------+--------+-----------+
| 11 | Wood comfort | double bed | 2003-03-03 | 25000 |
25 |
| 12 | old fox
| sofa
| 2003-02-20 | 17000 |
20 |
| 13 | micky
| baby cot | 2003-02-21 | 7500 |
15 |
+----+-----------------+--------------+---------------+---------+----------+

CREATE TABLE COMMAND


mysql> CREATE TABLE arrivals
-> (no integer,
-> itemname char(20),
-> type char(25),
-> DOS DATE,
-> price integer,
-> discount integer);
mysql> INSERT INTO arrivals VALUES (11,"Wood comfort","double bed",'2003-03-03',
25000,25);
.
.
QUERY QUESTIONS:
(a) show all information about baby cots from the furniture table.
SOLUTION:
SELECT* FROM furniture WHERE type=baby cot;
OUTPUT:
+----+--------------+----------+---------------+-------+-----------+
| no | itemname | type
| DOS
| price | discount |
+----+--------------+-----------+--------------+-------+-----------+
| 2 | pink feather | baby cot | 2002-01-20 | 7000 |
20 |
| 3 | dolphin
| baby cot | 2002-02-19 | 9500 |
20 |
| 6 | donald
| baby cot | 2002-02-24 | 6500 |
15 |
+----+--------------+-----------+--------------+--------+----------+
(b) list itemname from furniture table which are priced above 15000.
SOLUTION:
SELECT itemname FROM furniture WHERE price>15000;
OUTPUT:
+---------------+
| itemname |
+---------------+
| White lotus |
| decent
|
| comfort zone |
| royal finish |
| royal tiger
|
+----------------+
(c)list ITEMNAME and TYPE of those item , in which DOS is before 2002/01/22 from furniture
in descending order of itemname.

SOLUTION:
SELECT itemname, type FROM furniture WHERE DOS<2002/01/22 ORDER BY itemname;
OUTPUT:
+---------------+----------------+
| itemname | type
|
+---------------+----------------+
| comfort zone | double bed |
| decent
| office table |
| econo sitting | sofa
|
| pink feather | baby cot
|
+---------------+----------------+
(d) display itemname and DOS of those items ,in which discount% is more than 25, from
furniture table.
SOLUTION:
SELECT itemname,DOS FROM furniture WHERE discount>25;
OUTPUT:
+--------------+--------------+
| itemname | DOS
|
+--------------+--------------+
| decent
| 2002-01-01 |
| royal finish | 2002-02-20 |
| royal tiger | 2002-02-22 |
+--------------+--------------+
(e) Count the no of items, whose type is sofa from furniture table.
SOLUTION:
SELECT Count(*) FROM furniture WHERE type=sofa;
OUTPUT:
+----------+
| Count(*) |
+----------+
|
2
|
+----------+
(f) FIND THE OUTPUTS:
i)
SELECT COUNT(distinct TYPE) FROM furniture;
ii)
SELECT MAX(DISCOUNT) FROM furniture, arrivals;
iii)
SELECT AVG(DISCOUNT) FROM furniture where type=baby cot;
iv)
SELECT SUM(PRICE) FROM furniture where DOS<2002-02-12;

SOLUTION:
i)
5
ii)
30
iii)
18.33
iv)
66500
(g) insert new values in arrival: 14,"velvet touch","double bed",'2003-03-25',25000,30
SOLUTION:
INSERT INTO arrivals VALUES(14,"velvet touch","double bed",'2003-03-25',2
5000,30);

OUTPUT:
+------+----------------+---------------+---------------+--------+---------- +
| no
| itemname | type
| DOS
| price | discount |
+------+----------------+---------------+---------------+--------+-----------+
| 11 | Wood comfort | double bed | 2003-03-03 | 25000 |
25 |
| 12 | old fox
| sofa
| 2003-02-20 | 17000 |
20 |
| 13 | micky
| baby cot
| 2003-02-21 | 7500 |
15 |
| 14 | velvet touch | double bed | 2003-03-25 | 25000 |
30 |
+----+------------------+---------------+---------------+---------+-----------+

You might also like