dbms manual

43
CONTENTS Syllabus Introduction Structured Query Language Lab set Exercises References

Upload: pramod-subramanya

Post on 27-Oct-2014

316 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: DBMS Manual

CONTENTS

Syllabus

Introduction

Structured Query Language

Lab set Exercises

References

Page 2: DBMS Manual

CHAPTER 1

SYLLABUS

VISUAL PROGRAMMING & DATABASE APPLICATIONS LABORATORY

Semester: VI Max. Marks: Exam: 50 IA: 25

1. Consider the Insurance database given below. The primary keys are underlined and the datatypes are specified.

PERSON (driver-id#: string, name: string, address: string)CAR (Regno: string, model: string, year: int)ACCIDENT (report-number: int, acc_date: date, location: string)OWNS (driver-id#: string, Regno: string)PARTICIPATED (driver-id#: string, Regno: string, report-number: int, damage-amount: int)

(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

a. Update the damage amount for the car with a specific Regno in the accident with report number 12 to 25000.

b. Add a new accident to the database.(iv) Find the total number of people who owned cars that were involved in

accidents in 2002.(v) Find the number of accidents in which cars belonging to a specific model

were involved.(vi) Generation of suitable reports.(vii) Create suitable front end for querying and displaying the results.

2. Consider the following relations for an order processing database application in a company.

CUSTOMER (cust#: int, cname: string, city: string)ORDERS (order#: int, odate: date, cust#: int, ord-amt: int)ORDER-ITEM (order#: int, item#: int, qty: int)ITEM (item#: int, unit-price: int)SHIPMENT (order#: int, warehouse#: int ship-date: date)WAREHOUSE (warehouse#: int, city: 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) Produce a listing: CUSTNAME, #oforders, AVG_ORDER_AMT.

where the middle column is the total numbers of orders by the customer and the last column is the average order amount for that customer.

(iv) List the order# for orders that were shipped from all the warehouses that the company has in a specific city.

Page 3: DBMS Manual

(v) Demonstrate how you delete item# 10 from the ITEM table and make that field null in the ORDER-ITEM table.

(vi) Generation of suitable reports.(vii) Create suitable front end for querying and displaying the results.

3. 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.(vii) Create suitable front end for querying and displaying the results.

4. 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.(vii) Create suitable front end for querying and displaying the results.

5. 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)

Page 4: DBMS Manual

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.(vii) Create suitable front end for querying and displaying the results.

INSTRUCTIONS1. 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 of VAJ.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 examinations.6. Questions must be asked based on lots.

REFERENCE BOOKSI] “Develop an Accounting Package using VB” by Mr. Krishna and Ms. Bharathi, VK publishers.II] “Database Programming using VB.Net & SQL Server 2000”, by Mrs Bharthi and Mr Krishna, VK Publishers, 2004

Page 5: DBMS Manual

CHAPTER 2

INTRODUCTION

OVERVIEWDatabases store data and metadata. Data are the individual facts that are used to derive information. Metadata describe the content, quality, condition, availability, and characteristics of data. Database Management Softwares/Systems (DBMS) is used to modify the data.There are varieties of database types: Sequential files, Hierarchical databases, Network databases, and Relational databases. The recent database type is Object-Relational database, which is essentially a relational database with some Object properties.Relational databases become popular because it was easy to modify the schema. It is very easy to add tables and columns to the schema, and doing so does not affect the remainder of the schema, and more important, does not affect the applications that access the database schema. Older databases required the databases to be restructured and the applications to be modified. Avoiding database and application maintenance is an important benefit and the reason for the switch.Relational databases consist of independent tables. Relational Database Management Software (RDBMS) does not know how the records in the table are related.

ORACLEOracle is the largest database manufacturer and the second-largest software manufacturer in the world. The company began as a relational database manufacturer. In the beginning, Oracle touted its software as “being able to run on any platform”. This openness has been most attractive to companies; an Oracle has tried to maintain its image as an open product. Oracle was at a good place when industry became extremely interested in moving away from network databases and the mainframe.The Oracle continues to increase the power of its database. The current version is called Oracle9i. It’s an object-relational database, which has features that allow developers to model objects within the database. The i in the name means that its database can also support the Internet. To this end, Java programming functions can be placed within the database. Oracle can understand applications using the Java functions. Oracle has recognized that Java is an open product and is one of the more important languages of the Web.Oracle also has several other database products. Personal Oracle is a smaller version of the Oracle Enterprise Edition. It resides on the client and is designed primarily as a stand-alone database. Oracle also has a small database called Oracle lite9i, which is designed for use on portable computers and handheld devices.Oracle has very powerful application-development, report-writing, and database analysis software. The application-development package is currently called the Internet Developer Suite. It consists of a number of products, two of which are Developer6i Form Builder and Report Builder. Developer6i Form Builder is Oracle’s Rapid Application Development (RAD) software. Report Builder is Oracle’s main report-writing software.Designer6i is Oracle’s computer-aided software engineering (CASE) product. It is one of the best-selling CASE products in the world.

Page 6: DBMS Manual

BASICSStructured Query Language (SQL), which is an ANSI standard language for interacting with relational databases, is the main tool for extracting the information.A database is a representation of a real-world thing called an Entity. Examples of entities are vehicles, employees, customers, fish, buildings, and even things such as baseball teams. The database stores facts about the entity in an organized framework, model, or schema. These facts are called attributes.An Instance is one occurrence of an entity. Each entity must have an identifier, which is one or more attributes that make each entity instance unique from any other instance. The identifier should contain a value that does not change. Examples of identifiers are student IDs, payroll numbers, or social security numbers. If the entity does not have an attribute that can be used as an identifier, an artificial identifier can be created. The identifier on an entity is often called a primary key. A foreign key is a set of attributes of the considered table that exists as a primary key attributes in another table. Database records are matched (joined) through the use of primary and foreign keys.Normalization is a process consisting of series of steps, which is used to group the database attributes. The purpose of this design is to ensure that the tables within the database are space efficient and performance efficient.

Zero Normal Form—each of the relations (tables) has a unique identifier (primary key).

First Normal Form—Separate the repeating groups of attributes or multivalued attributes into a relation of their own. Be sure to form composite keys.

Second Normal Form—Establish full functional dependency by separating out attributes that are not fully dependent on the full primary keys.

Third Normal Form—Remove transitive dependencies by separating attributes that are dependent on a non key attribute.

Page 7: DBMS Manual

CHAPTER 4

LAB SET EXERCISES

Exercise 1:Aim:

Consider the Insurance database given below. The primary keys are underlined and the datatypes are specified.

PERSON (driver-id#: string, name: string, address: string)CAR (Regno: string, model: string, year: int)ACCIDENT (report-number: int, date: date, location: string)OWNS (driver-id#: string, Regno: string)PARTICIPATED (driver-id#: string, Regno: string, report-number: int, damage-amount: int)

(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

a. Update the damage amount for the car with a specific Regno in the accident with report number 12 to 25000.

b. Add a new accident to the database.(iv) Find the total number of people who owned cars that were involved in

accidents in 2002.(v) Find the number of accidents in which cars belonging to a specific model

were involved.(vi) Generation of suitable reports.(vii) Create suitable front end for querying and displaying the results.

Procedure: First start the oracle database using the option Start->Programs->Personal Oracle8 for

Window NT/9x/xp->Start database. Open the SQL *Plus 8.0 Editor to create, update, retrieve the table information. The

Editor is opened as Start->Programs->Oracle for Window NT/9x/xp->SQL *Plus 8.0Enter the Suitable Username and Password (for example Username: Scott and Password: Tiger).

Use the SQL queries for creating and working with tables. For this Exercise do the following steps

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

Table: PERSON (driver-id#: string, name: string, address: string)Query: create table person (driver_id# varchar2(20) primary key, name varchar2(30), address varchar2(50));Output:SQL>create table person (driver_id# varchar2(20) primary key, name varchar2(30), address varchar2(50));Table created.

Page 8: DBMS Manual

SQL> desc person; Name Null? Type ------------------------------- ---------------- ---- DRIVER_ID# NOT NULL VARCHAR2(20) NAME VARCHAR2(30) ADDRESS VARCHAR2(50)

Result:

Table: CAR (Regno: string, model: string, year: int)Query: create table car (regno varchar2(20) primary key, model varchar2(30), year number);Output:SQL> create table car (regno varchar2(20) primary key, model varchar2(30), year number);Table created.

SQL> desc car; Name Null? Type ------------------------------- -------- ------------- REGNO NOT NULL VARCHAR2(20) MODEL VARCHAR2(30) YEAR NUMBERResult:

Table: ACCIDENT (report-number: int, acc_date: date, location: string)Query: create table accident (report_number number primary key, acc_date date, location varchar2(30));Output:SQL> create table accident (report_number number primary key, acc_date date, location varchar2(30));Table created.SQL> desc accident; Name Null? Type ------------------------------- -------- ---- REPORT_NUMBER NOT NULL NUMBER ACC_DATE DATE

Page 9: DBMS Manual

LOCATION VARCHAR2(30)Result:

Table: OWNS (driver-id#: string, Regno: string)Query: create table owns (driver_id# varchar2(20) references person, regno varchar2(20) references car, primary key (driver_id#, regno));Output:SQL> create table owns (driver_id# varchar2(20) references person, regno varchar2(20) references car, primary key (driver_id#, regno));Table created.

SQL> desc owns; Name Null? Type ------------------------------- -------- ----------- DRIVER_ID# NOT NULL VARCHAR2(20) REGNO NOT NULL VARCHAR2(20)Result:

Table: PARTICIPATED (driver-id#: string, Regno: string, report-number: int, damage-amount: int)Query: create table participated (driver_id# varchar2(20) references person, regno varchar2(20) references car, report_number number references accident, damage_amount number, primary key (driver_id#, regno, report_number));Output:SQL> create table participated (driver_id# varchar2(20) references person, regno varchar2(20) references car, report_number number references accident, damage_amount number, primary key (driver_id#, regno, report_number));Table created.

SQL> desc participated; Name Null? Type ------------------------------- -------- ---- DRIVER_ID# NOT NULL VARCHAR2(20) REGNO NOT NULL VARCHAR2(20) REPORT_NUMBER NOT NULL NUMBER DAMAGE_AMOUNT NUMBER

Result:

(ii) Enter atleast five tuples for each relation.

Page 10: DBMS Manual

a. Inserting Tuples for PERSON tableinsert into person values ('a111','rajiv','bangalore');insert into person values ('a112','sanjay','bangalore');insert into person values ('a113','kumar','bangalore');insert into person values ('a114','praveen','bangalore');insert into person values ('a115','pradeep','bangalore');insert into person values ('a116','raj','bangalore');

b. Inserting Tuples for CAR tableinsert into car values ('ka06a2222','maruthi 800',2001);insert into car values ('ka06b2345','ford ikon',2003);insert into car values ('ka06f4356','mercedes benz',2005);insert into car values ('ka06g3009','maruthi zen',2002);insert into car values ('ka06v3678','jaquar s-type',2005);insert into car values ('ka06a0000','Honda CR-V',2006);

c. Inserting Tuples for OWNS tableinsert into owns values ('a111','ka06a2222');insert into owns values ('a112','ka06b2345');insert into owns values ('a113','ka06f4356');insert into owns values ('a114','ka06g3009’);insert into owns values ('a115','ka06v3678');insert into owns values ('a116','ka06a0000');

d. Inserting Tuples for ACCIDENT tableinsert into accident values (111,'14-jul-05',’bangalore’);insert into accident values (112,'15-apr-05',’bangalore’);insert into accident values (113,'19-dec-05',’bangalore’);insert into accident values (114,'20-nov-04’,’bangalore’);insert into accident values (115,'30-oct-04',’bangalore’);insert into accident values (116,'02-jun-04',’bangalore’);

e. Inserting Tuples for PARTICIPATED tableinsert into participated values ('a111','ka06a2222',111,20000);insert into participated values ('a112','ka06b2345',112,10000);insert into participated values ('a113','ka06f4356',113,5000);insert into participated values ('a114','ka06g3009',114,4000);insert into participated values ('a115','ka06v3678',115,10000);insert into participated values ('a116','ka06a0000',116,10000);

Result:

(iii) Demonstrate how you

Page 11: DBMS Manual

a. Update the damage amount for the car with a specific Regno in the accident with report number 12 to 25000. Query: Update participated set damage_amount=25000 where regno=’ka06a2222’ and report_number between 12 and 25000; ORUpdate participated set damage_amount=20000 where regno=’ka06a2222’ and report_number=111;Result:

b. Add a new accident to the database.Query: Insert into accident values(2000,’11-dec-05’,’bangalore’);Insert into participated values(‘a111’,’ka06a2222’,2000,1000);Result:

(iv) Find the total number of people who owned cars that were involved in accidents in 2002.Query:Select count(distinct p.driver_id#) from participated p, accident a, owns o where p.regno=o.regno and a.report_number=p.report_number and acc_date like ‘%%-%%%-02’;Result:

(v) Find the number of accidents in which cars belonging to a specific model were involved.Query:Select count(regno) from participated where regno in(select regno from car where model=’maruthi 800’);Result:

Page 12: DBMS Manual

Exercise 2:Aim:Consider the following relations for an order processing database application in a company.

CUSTOMER (cust#: int, cname: string, city: string)ORDERS (order#: int, odate: date, cust#: int, ord-amt: int)ORDER-ITEM (order#: int, item#: int, qty: int)ITEM (item#: int, unit-price: int)SHIPMENT (order#: int, warehouse#: int ship-date: date)WAREHOUSE (warehouse#: int, city: 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) Produce a listing: CUSTNAME, #oforders, AVG_ORDER_AMT. where

the middle column is the total numbers of orders by the customer and the last column is the average order amount for that customer.

(iv) List the order# for orders that were shipped from all the warehouses that the company has in a specific city.

(v) Demonstrate how you delete item# 10 from the ITEM table and make that field null in the ORDER-ITEM table.

(vi) Generation of suitable reports.(vii) Create suitable front end for querying and displaying the results.

Procedure: First start the oracle database using the option Start->Programs->Personal Oracle8 for

Window NT/9x/xp->Start database. Open the SQL *Plus 8.0 Editor to create, update, retrieve the table information. The

Editor is opened as Start->Programs->Oracle for Window NT/9x/xp->SQL *Plus 8.0Enter the Suitable Username and Password (for example Username: Scott and Password: Tiger).

Use the SQL queries for creating and working with tables. For this Exercise do the following steps

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

Table: CUSTOMER (cust#: int, cname: string, city: string)Query: create table customer (cust# number primary key, cname varchar2(30), city varchar2(30));Output:SQL> create table customer (cust# number primary key, cname varchar2(30), city varchar2(30));Table created.

Page 13: DBMS Manual

SQL> desc customer; Name Null? Type ------------------------------- -------- ---- CUST# NOT NULL NUMBER CNAME VARCHAR2(30) CITY VARCHAR2(30)Result:

Table: ORDERS (order#: int, odate: date, cust#: int, ord-amt: int)Query: create table orders (order# number primary key, odate date, cust# number references customer, ord_amt number);Output:SQL> create table orders (order# number primary key, odate date, cust# number references customer, ord_amt number);Table created.SQL> desc orders; Name Null? Type ------------------------------- -------- ---- ORDER# NOT NULL NUMBER ODATE DATE CUST# NUMBER ORD_AMT NUMBERResult:

Table: ORDER-ITEM (order#: int, item#: int, qty: int)Query:create table order_item(order# number references orders, item# number references item, qty number, primary key (order#,item#));Output:SQL> create table order_item(order# number references orders, item# number references item, qty number, primary key (order#,item#));Table created.SQL> desc order_item; Name Null? Type ------------------------------- -------- ---- ORDER# NOT NULL NUMBER ITEM# NOT NULL NUMBER

Page 14: DBMS Manual

QTY NUMBER

Result:

Table: ITEM (item#: int, unit-price: int)Query: create table item (item# number primary key, unit_price number);Output:SQL> create table item (item# number primary key, unit_price number);Table created.SQL> desc item; Name Null? Type ------------------------------- -------- ---- ITEM# NOT NULL NUMBER UNIT_PRICE NUMBERResult:

Table: SHIPMENT (order#: int, warehouse#: int ship-date: date)Query:create table shipment (order# number references orders, warehouse# number references warehouse ,ship_date date, primary key (order#,warehouse#));Output:SQL> create table shipment (order# number references orders, warehouse# number references warehouse, ship_date date, primary key (order#,warehouse#));Table created.SQL> desc shipment; Name Null? Type ------------------------------- -------- ---- ORDER# NOT NULL NUMBER WAREHOUSE# NOT NULL NUMBER SHIP_DATE DATEResult:

Table: WAREHOUSE (warehouse#: int, city: string)Query:create table warehouse (warehouse# number primary key, city varchar2(30));Output:SQL> create table warehouse (warehouse# number primary key, city varchar2(30));

Page 15: DBMS Manual

Table created.SQL> desc warehouse; Name Null? Type ------------------------------- -------- ---- WAREHOUSE# NOT NULL NUMBER CITY VARCHAR2(30)Result:

(ii) Enter atleast five tuples for each relation.

a. Inserting Tuples for CUSTOMER tableinsert into customer values (111,'rajiv','bangalore');insert into customer values (112,'sanjay','bangalore');insert into customer values (113,'kumar','bangalore');insert into customer values (114,'praveen','bangalore');insert into customer values (115,'pradeep','bangalore');insert into customer values (116,'raj','bangalore');

b. Inserting Tuples for ORDERS tableinsert into orders values (2001,’10-dec-2005’,111,1000);insert into orders values (2002,’11-dec-2005’,112,1000);insert into orders values (2003,’13-dec-2005’,113,1000);insert into orders values (2004,’14-dec-2005’,114,1000);insert into orders values (2005,’15-dec-2005’,115,1000);insert into orders values (2006,’16-dec-2005’,116,1000);

c. Inserting Tuples for ITEM tableinsert into item values (11,500);insert into item values (12,600);insert into item values (13,700);insert into item values (14,300);insert into item values (15,200);insert into item values (16,1000);

d. Inserting Tuples for ORDER_ITEM tableinsert into order_item values (2001,11,2);insert into order_item values (2002,16,1);insert into order_item values (2003,15,5);insert into order_item values (2004,14,3);insert into order_item values (2005,13,3);insert into order_item values (2006,12,1);

e. Inserting Tuples for WAREHOUSE tableinsert into warehouse values (31,’tumkur’);insert into warehouse values (32,’bangalore’);insert into warehouse values (33,’bombay’);

Page 16: DBMS Manual

insert into warehouse values (34,’delhi’);insert into warehouse values (35,’bihar’);insert into warehouse values (36,’locknow’);

f. Inserting Tuples for SHIPMENT tableinsert into shipment values (2001,31,’20-dec-2005’);insert into shipment values (2002,32,’21-dec-2005’);insert into shipment values (2003,33,’22-dec-2005’);insert into shipment values (2004,34,’23-dec-2005’);insert into shipment values (2005,35,’24-dec-2005’);insert into shipment values (2006,36,’25-dec-2005’);

Result:

(iii) Produce a listing: CUSTNAME, #oforders, AVG_ORDER_AMT. where the middle column is the total numbers of orders by the customer and the last column is the average order amount for that customer.Query:

Select cname, count (order#) as number of orders, sum (ord_amt) as Average order Amount from customer, orders where orders.cust#=customer.cust# group by cname;Result:

(iv) List the order,# for orders that were shipped from all the warehouses that the company has in a specific city.Query:select order# from shipment where warehouse in (select warehouse from warehouse where city=’bangalore’;Result:

(v) Demonstrate how you delete item# 10 from the ITEM table and make that field null in the ORDER-ITEM table.Query:update order_item set item#=0 , qty=0 where item#=13;Delete from item where item#=13;Result:

Exercise 3:

Page 17: DBMS Manual

Aim: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.(vii) Create suitable front end for querying and displaying the results.

Procedure: First start the oracle database using the option Start->Programs->Personal Oracle8 for

Window NT/9x/xp->Start database. Open the SQL *Plus 8.0 Editor to create, update, retrieve the table information. The

Editor is opened as Start->Programs->Oracle for Window NT/9x/xp->SQL *Plus 8.0Enter the Suitable Username and Password (for example Username: Scott and Password: Tiger).

Use the SQL queries for creating and working with tables. For this Exercise do the following steps

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

Table: STUDENT (regno: string, name: string, major: string, bdate: date)Query: create table student (regno varchar2(20) primary key, name varchar2(30), major varchar2(50), bdate date);Output:SQL> create table student (regno varchar2(20) primary key, name varchar2(30), major varchar2(50), bdate date);Table created.SQL> desc student; Name Null? Type ------------------------------- ---------------- ---------------- REGNO NOT NULL VARCHAR2(20) NAME VARCHAR2(30) MAJOR VARCHAR2(50)

Page 18: DBMS Manual

BDATE DATEResult:

Table: COURSE (course#: int, cname: string, dept: string)Query: create table course (course# number primary key, cname varchar2(30), dept varchar(30));Output:SQL> create table course (course# number primary key, cname varchar2(30), dept varchar(30));Table created.

SQL> desc course; Name Null? Type ----------------------------------- ----------------------- ----------------- COURSE# NOT NULL NUMBER CNAME VARCHAR2(30) DEPT VARCHAR2(30)Result:

Table: ENROLL (regno: string, course#: int, sem: int, marks: int)Query: create table enroll (regno varchar2(30) references student, course# number references course, sem number, marks number, primary key(regno, course#, sem));Output:SQL> create table enroll (regno varchar2(30) references student, course# number references course, sem number, marks number, primary key(regno, course#, sem));Table created.SQL> desc enroll; Name Null? Type ------------------------------- -------- ---- REGNO NOT NULL VARCHAR2(30) COURSE# NOT NULL NUMBER SEM NOT NULL NUMBER MARKS NUMBERResult:

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

Page 19: DBMS Manual

Query: create table book_adoption(course# number references course, sem number, book_isdn number, primary key (course#, sem));Output:SQL> create table book_adoption(course# number references course, sem number, book_isdn number, primary key (course#, sem));Table created.SQL> desc book_adoption Name Null? Type ------------------------------- -------- ---- COURSE# NOT NULL NUMBER SEM NOT NULL NUMBER BOOK_ISDN NUMBERResult:

Table: TEXT (book-ISBN: int, book-title: string, publisher: string, author: string)Query: create table text (book_isdn number primary key, book_title varchar2(30), publisher varchar2(30), author varchar2(30));Output:SQL> create table text (book_isdn number primary key, book_title varchar2(30), publisher varchar2(30), author varchar2(30));Table created.

SQL> desc text; Name Null? Type ------------------------------- -------- ---- BOOK_ISDN NOT NULL NUMBER BOOK_TITLE VARCHAR2(30) PUBLISHER VARCHAR2(30) AUTHOR VARCHAR2(30)

Result:

(vi) Enter atleast five tuples for each relation.

Page 20: DBMS Manual

f. Inserting Tuples for STUDENT tableinsert into student values ('1cio5cs001','rajiv','computer',’12-dec-83’);insert into student values ('1cio5ec002','sanjay', 'electronics',’22-oct-81’);insert into student values ('1cio5cs003','kumar', 'computer',’22-dec-78’);insert into student values ('1cio5is004','praveen', 'information',’20-jan-76’);insert into student values ('1cio5cs005','pradeep', 'computer',’15-apr-77’);insert into student values ('1cio5me006','raj', 'mechanical',’22-nov-80’);

g. Inserting Tuples for COURSE tableinsert into course values (1,’computer',’dept of computer science’);insert into course values (2,'information’, ’dept of computer science’);insert into course values (3,'Electrical', ’dept of electrical science’);insert into course values (4,’Electronics', ’dept of Electronics’);insert into course values (5,'Mechanical', ’dept of Mechanical’);insert into course values (6,'Civil', ’dept of Civil’);

h. Inserting Tuples for ENROLL tableinsert into enroll values ('1cio5cs001',1,3,200);insert into enroll values ('1cio5ec002',4,5,300);insert into enroll values ('1cio5cs003',1,5,500);insert into enroll values ('1cio5is004',2,3,200);insert into enroll values ('1cio5cs005',1,7,400);insert into enroll values ('1cio5me006',5,3,300);

i. Inserting Tuples for BOOK_ADOPTION tableinsert into book_adoption values (1,1,1);insert into book_adoption values (2,2,2);insert into book_adoption values (3,3,3);insert into book_adoption values (4,4,4);insert into book_adoption values (5,3,5);insert into book_adoption values (6,5,6);

j. Inserting Tuples for TEXT tableinsert into text values (1,'computer networks',’tata’ ,’tannenbum’);insert into text values (2,'VLSI design',’tata’,’morris mano’);insert into text values (3,'compiler design',’tata’,’ullman’);insert into text values (4,'java programming complete',’tata’,’patrick naughton’);insert into text values (5,'database management systems',’tata’,’ellswari navathe’);insert into text values (6,'computer concepts and c language',’tata’,’balaguruswamy’);

Result:

Page 21: DBMS Manual

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

Query:Insert into text values (1,’computer networks’,’mcgraw hill’,’andrew tannanbum’);Insert into book_adoption values(1,1,1);Result:

(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.

Query:Select distinct(c.course#),c.cname, b.book_isdn,t.book_title from course c, text t, book_adoption b where c.course#=b.course# and b.book_isdn=t.book_isdn and c.dept=’computer’ and c.course# in (select course# from book_adoption group by course# having count(course#)>=2 order by c.cname;Result:

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

Query:Select dept from (select c.dept, count(b.book_isdn) from course c, book_adoption b, where c.course#=b.course# group by c.dept) INTERSECT (select c.dept,count(book_isdn) from course c, book_adoption b, text t where c.course#=b.course# and b.book_isdn=t.book_isdn and t.publisher=’tata mcgraw hil’ group by c.dept);Result:

Exercise 4:Aim:The following tables are maintained by a book dealer.

Page 22: DBMS Manual

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.(vii) Create suitable front end for querying and displaying the results.

Procedure: First start the oracle database using the option Start->Programs->Personal Oracle8 for

Window NT/9x/xp->Start database. Open the SQL *Plus 8.0 Editor to create, update, retrieve the table information. The

Editor is opened as Start->Programs->Oracle for Window NT/9x/xp->SQL *Plus 8.0Enter the Suitable Username and Password (for example Username: Scott and Password: Tiger).

Use the SQL queries for creating and working with tables. For this Exercise do the following steps

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

Table: AUTHOR (author-id: int, name: string, city: string, country: string)Query: create table author (author number primary key, name varchar2(30), city varchar2(30), country varchar2(30));Output:SQL> create table author (author number primary key, name varchar2(30), city varchar2(30), country varchar2(30));Table created.

Result:

Table: PUBLISHER (publisher-id: int, name: string, city: string, country: string)Query: create table publisher (publisher_id number primary key, name varchar2(30), city varchar(30),country varchar2(30));Output:

Page 23: DBMS Manual

SQL> create table publisher (publisher_id number primary key, name varchar2(30), city varchar(30),country varchar2(30));Table created.Result:

Table: CATALOG (book-id: int, title: string, author-id: int, publisher-id: int, category-id: int, year: int, price: int)Query: create table catalog1 (book_id number primary key, title varchar2(30), author_id number references author, publisher_id number references publisher, category_id number references category, year number, price number);Output:SQL> create table catalog1 (book_id number primary key, title varchar2(30), author_id number references author, publisher_id number references publisher, category_id number references category, year number, price number);Table created.Result:

Table: CATEGORY (category-id: int, description: string)Query: create table category(category_id number primary key, description varchar2(30));Output:SQL> create table category(category_id number primary key, description varchar2(30));Table created.Result:

Table: ORDER-DETAILS (order-no: int, book-id: int, quantity: int)Query: create table order_details (order_no number primary key, book_id number, quantity number);Output:SQL> create table order_details (order_no number primary key, book_id number, quantity number);Table created.

Result:(vii) Enter atleast five tuples for each relation.

k. Inserting Tuples for AUTHOR tableinsert into author values (1,’balaguruswamy’,’tumkur’,’india’);insert into author values (2,’rajaram’,’tumkur’,’india’);

Page 24: DBMS Manual

insert into author values (3,’naughton’,’los angles’, ’USA’);insert into author values (4,’james’,’bangalore’,’india’);insert into author values (5,’tannabahum’,’mysore’,’india’);insert into author values (6,’kumar’,’tumkur’,’india’);

l. Inserting Tuples for PUBLISHER tableinsert into publisher values (1,’oreilly’,’tumkur’,’india’);insert into publisher values (2,’tata’,’tumkur’,’india’);insert into publisher values (3,’pearson’,’tumkur’,’india’);insert into publisher values (4,’bpb publication’,’tumkur’,’india’);insert into publisher values (5,’white publishers’,’tumkur’,’india’);insert into publisher values (6,’d&d publishers’,’tumkur’,’india’);

m. Inserting Tuples for CATALOG1 tableinsert into catalog1 values (1,”computer concepts”,1,1,1,2001,450);insert into catalog1 values (2,”computer programming using c”,1,2,1,2003,500);insert into catalog1 values (3,”java programming”,3,2,2,2002,450);insert into catalog1 values (4,”java database programming”,3,1,2,2004,450);insert into catalog1 values (5,”oracle database systems”,4,4,1,2000,450);insert into catalog1 values (6,”VB programming in 21 days”,6,5,1,2002,450);

n. Inserting Tuples for CATEGORY tableinsert into category values (1,’category 1’);insert into category values (2, ’category 2’);insert into category values (3, ’category 3’);insert into category values (4, ’category 4’);insert into category values (5, ’category 5’);insert into category values (6, ’category 6’);

o. Inserting Tuples for ORDER_DETAILS tableinsert into order_details values (1,1,10);insert into order_details values (2,3,30);insert into order_details values (3,4,40);insert into order_details values (4,3,20);insert into order_details values (5,5,50);insert into order_details values (6,6,30);

Result:

(iv) 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.

Query:Select * from author where authored in (select catalog.authorid from catalog where year>2000 and price>(select avg(price) from catalog) and catalog.authorid in (select

Page 25: DBMS Manual

catalog.authorid from catalog group by catalog.authorid having count(catalog.authorid)>1));Result:

(v) Find the author of the book which has maximum sales.Query:Select name from author where authored in ( select authored from catalog where bookid in ( select bookid from orderdetails group by bookid having sum(qty) in (select max(sum(qty)) from orderdetails group by bookid)));Result:

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

Query:updateResult:

Exercise 5:

Page 26: DBMS Manual

Aim: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.(vii) Create suitable front end for querying and displaying the results.

Procedure: First start the oracle database using the option Start->Programs->Personal Oracle8 for

Window NT/9x/xp->Start database. Open the SQL *Plus 8.0 Editor to create, update, retrieve the table information. The

Editor is opened as Start->Programs->Oracle for Window NT/9x/xp->SQL *Plus 8.0Enter the Suitable Username and Password (for example Username: Scott and Password: Tiger).

Use the SQL queries for creating and working with tables. For this Exercise do the following steps

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

Table: BRANCH (branch-name: string, branch-city: string, assets: real)Query: create table branch (branch_name varchar2(30) primary key, branch_city varchar2(30),assets real);Output:SQL> create table branch (branch_name varchar2(30) primary key, branch_city varchar2(30),assets real);Table created.

SQL> desc branch; Name Null? Type ------------------------------- -------- ---- BRANCH_NAME NOT NULL VARCHAR2(30) BRANCH_CITY VARCHAR2(30) ASSETS NUMBER(63)Result:Table: ACCOUNT (accno: int, branch-name: string, balance: real)

Page 27: DBMS Manual

Query: create table account (accno number primary key, branch_name varchar2(30) references branch ,balance real);Output:SQL> create table account (accno number primary key, branch_name varchar2(30) references branch ,balance real);Table created.Result:

Table: DEPOSITOR (customer-name: string, accno: int)Query: create table depositor (customer_name varchar2(30) references customer, accno number references account, primary key ( customer_name, accno));Output:SQL> create table depositor (customer_name varchar2(30) references customer, accno number references account, primary key ( customer_name, accno));Table created.Result:

Table: CUSTOMER (customer-name: string, customer-street: string, customer-city: string)Query: create table customer(customer_name varchar2(30) primary key, customer_street varchar2(30), customer_city varchar2(30));Output:SQL> create table customer(customer_name varchar2(30) primary key, customer_street varchar2(30), customer_city varchar2(30));Table created.Result:

Table: LOAN (loan-number: int, branch-name: string, amount: real)Query: create table loan (loan_number number primary key, branch_name varchar2(30) references branch, amount real);Output:SQL> create table loan (loan_number number primary key, branch_name varchar2(30) references branch, amount real);Table created.Result:Table: BORROWER (customer-name: string, loan-number: int)

Page 28: DBMS Manual

Query: create table borrower (customer_name varchar2(30) primary key, loan_number number references loan);Output:SQL>create table borrower (customer_name varchar2(30) primary key, loan_number number references loan);Table created.Result:

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)

(vii) Enter atleast five tuples for each relation.p. Inserting Tuples for BRANCH table

insert into branch values (‘malleshwaram branch’,’bangalore’,200000.00);insert into branch values (‘jayanagar branch’,’bangalore’,400000.00);insert into branch values (‘vijayanagar branch’,’bangalore’,300000.00);insert into branch values (‘rajajinagar branch’,’bangalore’,600000.00);insert into branch values (‘Electronic city branch’,’bangalore’,900000.00);insert into branch values (‘yeshwantpura branch’,’bangalore’,200000.00);

q. Inserting Tuples for ACCOUNT tableinsert into account values (1,’malleshwaram branch',2000.00);insert into account values (2,’jayanagar branch',3000.00);insert into account values (3,’electronic city branch',6000.00);insert into account values (4,’yeshwantpura branch',4000.00);insert into account values (5,’jayanagar branch',3000.00);insert into account values (6,’malleshwaram branch',6000.00);

r. Inserting Tuples for DEPOSITIOR tableinsert into depositor values (‘ramu’,1);insert into depositor values ('krishna',4);insert into depositor values ('jack',3);insert into depositor values ('kumar',2);insert into depositor values ('umasheker',5);insert into depositor values ('raju',6);

s. Inserting Tuples for CUSTOMER tableinsert into customer values (‘ramu’,’kh road’,’bangalore’);insert into customer values (‘krishna’,’west chord road’,’bangalore’);insert into customer values (‘kumar’,’magadi road’,’bangalore’);insert into customer values (‘jack’,’mg road’,’bangalore’);insert into customer values (‘umasheker’,’richmond road’,’bangalore’);insert into customer values (‘raju’,’mysore road’,’bangalore’);

t. Inserting Tuples for LOAN tableinsert into loan values (1,'malleshwaram branch',10000.00);insert into loan values (2,'jayanagar branch',12000.00);insert into loan values (3,'malleshwaram branch',20000.00);insert into loan values (4,'electronic city branch',30000.00);

Page 29: DBMS Manual

insert into loan values (5,'yeshwantpura branch',40000.00);insert into loan values (6,'jayanagar branch',15000.00);

u. Inserting Tuples for BORROWER tableinsert into borrower values ('ramu',1);insert into loan values (‘raju’,6);insert into loan values ('jack',4);insert into loan values (‘krishna’,5);insert into loan values (‘umashankar’,2);insert into loan values (‘kumar’,6);

Result:

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

Query:

Result:

(vii) 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.

Query:

Result:

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

Query:

Result:

Page 30: DBMS Manual