ADBMS Lab Manual

33
7/21/2019 ADBMS Lab Manual http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 1/33 Advanced Database Lab 2MCS5 APEX INSTITUTE OF ENGINEERING AND TECHNOLOGY Department of Computer Science En!ineerin!

description

lab

Transcript of ADBMS Lab Manual

Page 1: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 1/33

Advanced Database Lab

2MCS5

APEX INSTITUTE OF ENGINEERING AND TECHNOLOGY

Department of Computer Science En!ineerin!

Page 2: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 2/33

Li"t of E#periment"

$% O&er&ie' of S(L DDL) D*L an+ DCL Comman+" 'it, e#amp-e"%

.% /ritin! an+ Practice of Simp-e (uerie"%

0% /ritin! (uerie" u"in! GROUP 1Y an+ ot,er c-au"e"%

2% To 'rite 3uerie" u"in! Set operation" an+ to 'rite ne"te+ 3uerie"%

4% To 'rite 3uerie" u"in! "in!-e ro' function" an+ !roup function"%

5% To 'rite 3uerie" on &ie'"%

6% Data7a"e Sc,ema for a cu"tomer8"a-e "cenario

9% Data7a"e Sc,ema for a Stu+ent Li7rar: "cenario

;% Data7a"e Sc,ema for a <i+eo Li7rar: "cenario

$=% O7"er&ation of ne"te+ 3uer: an+ >oin 3uer:%

Page 3: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 3/33

Intro+uction of Data 1a"e

/,at i" a +ata7a"e? 

A database is a collection of information that is organized so that it can easily be accessed, managed, and updated

one view, databases can be classified according to types of content: bibliographic, full-text, numeric, and images.

In computing, databases are sometimes classified according to their organizational approach. The most prevaapproach is the relational database, a tabular database in which data is defined so that it can be reorganized

accessed in a number of different ways. A distributed database is one that can be dispersed or replicated am

different points in a networ. An ob!ect-oriented programming database is one that is congruent with the data defin ob!ect classes and subclasses.

"omputer databases typically contain aggregations of data records or files, such as sales transactions, prod

catalogs and inventories, and customer profiles. Typically, a database manager provides users the capabilitie

controlling read#write access, specifying report generation, and analyzing usage. $atabases and database manaare prevalent in large  mainframe  systems, but are also present in smaller distributed worstation and mid-ra

systems such as the A%#&'' and on personal computers. %() *%tructured (uery )anguage+ is a standard languagemaing interactive ueries from and updating a database such as I/s $0, icrosoft/s Access, and datab

 products from 1racle, %ybase, and "omputer Associates.

A Data7a"e *ana!ement S:"tem  *D1*S+ is a set of computer programs that controls the creation, maintena

and the use of a database. It allows organizations to place control of database development in the hands of databadministrators *$As+ and other specialists. A $% is a system software pacage that helps the use of integra

collection of data records and files nown as databases. It allows different user application programs to easily ac

the same database. $%s may use any of a variety of database models, such as the networ model or  relati

model. In large systems, a $% allows users and other software to store and retrieve data in a structured  w

Instead of having to write computer programs to extract information, user can as simple uestions in a ulanguage. Thus, many $% pacages provide 2ourth-generation programming language  *&3)s+ and o

application development features. It helps to specify the logical organization for a database and access and useinformation within a database. It provides facilities for controlling data access, enforcing data integrity, mana

concurrency, and restoring the database from bacups. A $% also provides the ability to logically present datab

information to usersA $% is a set of software programs that controls the organization, storage, management, and retrieval of  data 

database. $%s are categorized according to their data structures or types. The $% accepts reuests for d

from an application program and instructs the operating system  to transfer the appropriate data. The ueries 

responses must be submitted and received according to a format that conforms to one or more applicable protoc4hen a $% is used, information systems can be changed much more easily as the organization/s informa

reuirements change. 5ew categories of data can be added to the database without disruption to the existing system

$atabase servers are computers that hold the actual databases and run only the $% and related software. $atabservers are usually multiprocessor  computers, with generous memory and 6AI$ dis arrays used for stable stor

7ardware database accelerators, connected to one or more servers via a high-speed channel, are also used in la

volume transaction processing environments. $%s are found at the heart of most database applications. $

may be built around a custom multitasing ernel with built-in  networing support, but modern $%s typicrely on a standard operating system to provide these functions.

A database management system *$%+, sometimes !ust called a database manager , is a program that lets on

more computer users create and access data in a database. The $% manages user reuests *and reuests from o

 programs+ so that users and other programs are free from having to understand where the data is physically located

Page 4: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 4/33

storage media and, in a multi-user system, who else may also be accessing the data. In handling user reuests,

$% ensures the integrity of the data *that is, maing sure it continues to be accessible and is consiste

organized as intended+ and security *maing sure only those with access privileges can access the data+. The mtypical $% is a relational database management system *6$%+. A standard user and program interface is

%tructured (uery )anguage *%()+. A newer ind of $% is the ob!ect-oriented database management sys

*1$%+.A $% can be thought of as a file manager  that manages data in databases rather than files in file systems. In I

mainframe  operating systems, the nonrelational data managers were *and are, because these legacy applica

systems are still used+ nown as access methods.

A $% is usually an inherent part of a database product. 1n 8"s, icrosoft Access is a popular example osingle- or small-group user $%. icrosoft/s %() %erver  is an example of a $% that serves database reu

from multiple *client+ users. 1ther popular $%s *these are all 6$%s, by the way+ are I/s $0, 1racle/s

of database management products, and %ybase/s products.

I/s Information anagement %ystem *I%+ was one of the first $%s. A $% may be used by or combiwith transaction managers, such as I/s "ustomer Information "ontrol %ystem *"I"%+.

U"e of Data7a"e in Rea- Life

 Real-Life Database Examples

To say that the databases are everywhere would be an understatement. They virtually permeate our lives: 1nline

stores, health care providers, clubs, libraries, video stores, beauty salons, travel agencies, phone companies,government agencies lie 2I, I5%, I6%, and 5A%A 9 they all use databases. These databases can be very

different in their nature and usually have to be specifically designed to cater to some special customer needs.

7ere are some examples.

Note All relational databases can be divided into two main categories according to their primary

function 9 online transaction processing  *1)T8+ and data warehouse systems. 1)T8 typ

has many users simultaneously creating and updating individual records in other words it/svolatile and computation-intensive. $ata warehouse is a database designed for information

 processing and analysis, with focus on planning for the future rather than on day-to-day

operations. The information in these is not going to change very often, which ensures the

information consistency *repeatable result+ for the users. In the real world most systems arehybrids of these two, unless specifically designed as data warehouse.

Or+er mana!ement ":"tem +ata7a"e

A typical database for a company that sells building materials might be arranged as follows: The company musthave at least one customer. ;ach customer in the database is assigned one or more addresses, one or more conta

 phones, and a default salesperson who is the liaison between the customer and the company. The company sells

variety of products. ;ach product has a price, a description, and some other characteristics. 1rders can be placedfor one or more product at a time. ;ach product logically forms an order line. 4hen an order is complete it can

 be shipped and then invoiced. Invoice number and shipment number are populated automatically in the database

and can not be changed by users. ;ach order has a status assigned to it: "18);T;, %7I88;$, I5<1I";$,

and so on. The database also contains specific shipment information *bill of lading number, number of boxesshipped, dates, and so on+. =sually one shipment contains one order, but the database is designed in such a way

that one order can be distributed between more than one shipment, as well as one shipment can contain more

than one order. %ome constraints also exist in the database. 2or example, some fields cannot be empty, and somother fields can contain only certain types of information.

>ou already now that a database is a multi user environment by definition. It/s a common practice to group

users according to the functions they perform and security levels they are entitled to. The order management

Page 5: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 5/33

system described here could have three different user groups: %ales department clers/ function is to enter or

modify order and customer information shipping department employees create and update shipment data

warehouse supervisors handle products. In addition, all three user groups view diverse database informationunder different angles, using reports and ad-hoc ueries.

4e/ll use this database, which we/ll call A";, throughout this boo for examples and exercises. A";database is a simplified version of a real production database. It has only ?@ tables, and the real one would easil

have over a hundred.7ealth care provider database

A health provider company has multiple offices in many different states. any doctors wor for the company,

and each doctor taes care of multiple patients. %ome doctors !ust wor in one office, and others wor in

different offices on different days. The database eeps information about each doctor, such as name, address,contact phones, area of specialization, and so on. ;ach patient can be assigned to one or more doctors. %pecific

 patient information is also ept in the database *name, address, phones, health record number, date of birth,

history of appointments, prescriptions, blood tests, diagnoses, etc.+. "ustomers can schedule and cancelappointments and order prescription drugs either over the phone or using the company 4eb site. %ome

restrictions apply 9 for example, to see a specialist, the patient needs an approval from his#her primary

 physician to order a prescription the patient should have at least one valid refill left, and so on.

 5ow, what are the main database user groups 8atients should be able to access the database using a 4eb

 browser to order prescriptions and mae appointments. This is all that patients may do in the database. $octorsand nurses can browse information about their patients, write and renew prescriptions, schedule blood tests and

B-6ays, and so on. Administrative staff *receptionists, pharmacy assistants+ can schedule appointments for

 patients, fill prescriptions, and run specific reports.

Again, in real life this database would be far more complicated and would have many more business rules, but

our main goal now is !ust to give a general idea what ind of information a database could contain.The health provider and order management system databases are both examples of a typical hybrid  database

*though the former is probably closer to an 1)T8+.%cientific database

A database for genome research and related research areas in molecular and cellular biology can be a goodexample of a scientific database. It contains gene catalogs for completely seuenced genomes and some partial

genomes, genome maps and organism information, and data about seuence similarities among all nown genesin all organisms in the database. It also contains information on molecular interaction networs in the cell and

chemical compounds and reactions.

A database of an antiue automobile club can be pretty simple. Also, such an organization would not typically

have too many members, so the database is not going to be very large. >ou need to store members/ personal

information such as address, phone number, area of interest, and so on. The database might also contain theinformation about the autos *brand, year, color, condition, etc.+. Autos are tied to their owners *members of the

club+. ;ach member can have one or more vehicles, and a vehicle can be owned by !ust one member.

The database would only have a few users 9 possibly, the chairman of the club, an assistant, and a secretary.

The last two examples are not business-critical databases and don/t have to be implemented on expensiveenterprise software. The data still have to be ept safely and should not be lost, but in case of, let/s say, hardwar

failure it probably can wait a day or two before the database is restored from a bacup. %o, the use of a free

database, lie my%(), 8ostgre%(), or even nonrelational 8osgres is appropriate. Another good choice might be% Access, which is a part of icrosoft 1ffice Tools if you bought % 1ffice !ust because you want to use

4ord and ;xcel, you should be aware that you/ve got a free relational database as well. *% Access wors well

with up to ?C users.+

Page 6: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 6/33

E#p $@ O&er&ie' of S(L DDL) D*L an+ DCL Comman+" /it, E#amp-e"%

DDL i" Data Definition Lan!ua!e "tatement"% Some e#amp-e"@

"6;AT; - to create ob!ects in the database

A)T;6 - alters the structure of the database

$618 - delete ob!ects from the database

T6=5"AT; - remove all records from a table, including all spaces allocated for the records are removed"1;5T - add comments to the data dictionary

36A5T - gives user/s access privileges to database

6;<1D; - withdraw access privileges given with the 36A5T command$) is $ata anipulation )anguage statements. %ome examples:

%;);"T - retrieve data from the a database

I5%;6T - insert data into a table=8$AT; - updates existing data within a table

$;);T; - deletes all records from a table, the space for the records remain

"A)) - call a 8)#%() or Eava subprogram;B8)AI5 8)A5 - explain access path to data

)1"D TA); - control concurrency$") is $ata "ontrol )anguage statements. %ome examples:

"1IT - save wor done%A<;81I5T - identify a point in a transaction to which you can later roll bac 

61))A"D - restore database to original since the last "1IT

%;T T6A5%A"TI15 - "hange transaction options lie what rollbac segment to use

1a"ic S(L DDL Comman+"%

$% S(L 8 CREATE TA1LE

S:nta#@ CREATE TA1LE ta7-ename co-umnBname +ataB t:pe con"traint")

E#amp-e@

INPUT@

%()F "6;AT; TA); ;mp * ;mp5o short "15%T6AI5T 8Dey 86IA6> D;>,

;5ame <ar"har*?C+, Eob "har*?'+ "15%T6AI5T =ni? =5I(=;,gr short "15%T6AI5T 2Dey? 6;2;6;5";% ;8 *;mp5o+,

7iredate $ate, $ept5o short "15%T6AI5T 2Dey0 6;2;6;5";% $;8T*$ept5o++

RESULT@ Table created.%()F"reate table prog0' *pname varchar0*0'+ not null+, do! date not null,dob date not null,

sex varchar*?+ not null, prof? varchar*0'+,prof0 varchar*0'+,salary number*G,0+ not null+

RESULT@

Table created.%()Fdesc prog0'

 5ame 5ull Type

--------------------------------- -------- ----------------------------85A; 51T 5=)) <A6"7A60*0'+

$1E 51T 5=)) $AT;

$1 51T 5=)) $AT;%;B 51T 5=)) <A6"7A60*?+

8612? <A6"7A60*0'+

86120 <A6"7A60*0'+%A)A6> 51T 5=)) 5=;6*G,0+

Page 7: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 7/33

.% S(L 8 ALTER TA1LE

INPUT@

%()FA)T;6 TA); ;8 A$$ "15%T6AI5T 8ey? 86IA6> D;> *;mp5o+

RESULT@ Table Altered.

%imilarly, A)T;6 TA); ;8 $618 "15%T6AI5T 8ey?

0% S(L 8 DROP TA1LE

 H $eletes table structure H "annot be recovered H =se with caution

INPUT@S(L $618 TA); ;8 7ere ;8 is table name

RESULT@ Table $ropped.

&% TRUNCATE T6=5"AT; TA); TA); 5A;F

1a"ic S(L D*L Comman+"%

$% S(L 8 INSERT INTO

%yntax: I5%;6T I5T1 tablename <A)=;% *value list+ %ingle-row insertI5%;6T I5T1 % <A)=;%*J%@K,K%=8@K,K)16;K,?'+

 Inserting one row, many columns at a timeI5%;6T I5T1 % *%51, %5A;+ <A)=;% *J%?K, J%mithK+%?K %mithK Inserting many rows, all#some columns at a time.I5%;6T I5T1 5;4L%=88)I;6 *%51, %5A;+

%;);"T %51, %5A; 261 %

47;6; "IT> I5 *J)16;K,KA$6A%K+

Ot,er E#amp-e"@

INPUT@

%()FInsert into prog values *JK,K'C-may-CMK+

RESULT@ ? row created.

INPUT@

%()FInsert into prog0' values*J7emaK,K0C-sept-'?K0N-!an-NCK,KfK,KcK,KcOOK,K0C'''K+

RESULT@ ? row created.

INPUT@

%()FInsert into prog values*JPpnameK,KPdo!K+

%()F Insert into prog values*/Ppname/,/Pdo!/+;nter value for pname: ravi

;nter value for do!: ?C-!une-N?

RESULT@

old ?: Insert into prog values*/Ppname/,/Pdo!/+new ?: Insert into prog values*/ravi/,/?C-!une-N?/+

? row created.

.% S(L 8 UPDATE

%yntax: =8$AT; tablename %;T columnLname Qvalue R 47;6; conditionS

E#amp-e"@

=8$AT; %;T "IT> Q JDA58=6K 47;6; %51QJ%?K

=8$AT; ;8 %;T %A) Q ?.?' %A)

%()F update emp set salQ0'''' where empnoQG@MU

0% S(L 8 DELETE FRO*

%yntax: $;);T; 261 tablename 47;6; condition

Page 8: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 8/33

E#amp-e"@

$;);T; 261 %8 47;6; 851Q J8?K

$;);T; 261 %8

INPUT@

%()F$elete from emp where empnoQG@MU

RESULT@ ? row deleted.

1a"ic S(L DCL Comman+"%

$% CO**ITSa&e c,an!e" tran"actiona-%

S:nta#@

"1IT R416DS R"1;5T /comment_text /S"1IT R416DS R216"; / force_text / R,int S S

FORCE 8 'i-- manua--: commit an in8+ou7t distributed tran"action

forceBte#t 8 tran"action i+entifier "ee t,e D1AB.PCBPENDING &ie' )

int 8 "et" a "pecific SCN%

If a net'or or mac,ine fai-ure pre&ent" a +i"tri7ute+ tran"action from committin!

proper-:) Orac-e 'i-- "tore an: commit comment in t,e +ata +ictionar: a-on! 'it, t,e

tran"action ID%

INPUT@S(Lcommit

RESULT@ "ommit complete.

.% ROLL1AC 

Un+o 'or +one tran"actiona-%

S:nta#@

61))A"D R416DS RT1 R%A<;81I5TS/ savepoint_text_identifier /S

61))A"D R416DS R216"; / force_text /S216"; - will manually rollbac an in-doubt distributed transaction

INPUT@

%()FrollbacRESULT@6ollbac complete.

0% SA<EPOINT

Sa&e c,an!e" to a point tran"actiona-%

S:nta#@

%A<;81I5T text_identifier 

E#amp-e:

=8$AT; employees

%;T salary Q UC'''

47;6; lastLname Q /%mith/%A<;81I5T !ustsmith

=8$AT; employees

%;T salary Q ?''''''%A<;81I5T everyone

%;);"T %=*salary+ 261 employees

61))A"D T1 %A<;81I5T !ustsmith"1IT

Page 9: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 9/33

E#p . /ritin! an+ Practice of Simp-e (uerie"%

?% Get t,e +e"cription of E*P ta7-e%

%()Fdesc emp

RESULT@

 5ame 5ull Type

-------------------------------- ----------------------- -------------------------

;851 51T 5=)) 5=;6*&+

;5A; <A6"7A60*?'+E1 <A6"7A60*U+

36 5=;6*&+

7I6;$AT; $AT;%A) 5=;6*G,0+

"1 5=;6*G,0+

$;8T51 5=;6*@+A3; 5=;6*@+

;%A) 5=;6*?'+

.% Get t,e +e"cription DEPT ta7-e%

%()Fdesc deptRESULT@

 5ame 5ull Type--------------------------------- --------------------- ---------------------------

$;8T51 51T 5=)) 5=;6*0+

$5A; <A6"7A60*?&+)1" <A6"7A60*?@+

0%Li"t a-- emp-o:ee +etai-"%

%()Fselect from emp

RESULT@

;851 ;5A; E1 36 7I6;$AT; %A) "1 $;8T51 A3; ;%A)-------- ---------- --------- ---------- --------- ---------- ---------- ---------- ---------- -----------------G@MU %IT7 ");6D GU'0 ?G-$;"-N' N'' ' 0' 0C '

G&UU A));5 %A);%A5 GMUN 0'-2;-N? ?M'' @'' @' 0C '

GC0? 4A6$ %A);%A5 GMUN 00-2;-N? ?0C' C'' @' 0C 'GCMM E15;% A5A3;6 GN@U '0-A86-N? 0UGC C'' 0' 0C '

GMUN )AD; A5A3;6 GN@U '?-A>-N? 0NC' ?&'' @' 0C '

2%Li"t a-- emp-o:ee name" an+ t,eir "a-arie") ',o"e "a-ar: -ie" 7et'een

$4==8 an+ 04==8 7ot, inc-u"i&e%

INPUT

%()Fselect ename from emp where sal between ?C'' and @C''RESULT

A));5

E15;%)AD;

")A6D 

%"1TTT=65;6 

216$

russelgreg

U rows selected.

Page 10: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 10/33

4% Li"t a-- emp-o:ee name" an+ t,eir an+ t,eir mana!er ',o"e mana!er i"

6;=. or 6455 =r 669;%

INPUT %()Fselect ename from emp where mgr in*GM'0,GCMM,GGNU+

RESULT

 ENAME -------

%"1TT

216$

5% Li"t a-- emp-o:ee" ',ic, "tart" 'it, eit,er J or T%

INPUT %()Fselect ename from emp where ename lie JEVK or ename lie JTVK

RESULT@

;5A;

---------

E15;%T=65;6 

EA;%

6% Li"t a-- emp-o:ee name" an+ >o7") ',o"e >o7 tit-e inc-u+e" * or P%

INPUT %()Fselect ename,!ob from emp where !ob lie JVK or !ob lie J8VKRESULT@

 ENAME JOB---------- ---------

E15;% A5A3;6 

)AD; A5A3;6 ")A6D A5A3;6 

DI53 86;%I$;5T

9% Li"t a-- >o7" a&ai-a7-e in emp-o:ee ta7-e%

INPUT %()Fselect distinct !ob from emp

RESULT@ JOB---------

A5A)>%T

");6D A5A3;6 

86;%I$;5T

%A);%A5assistant

cler 

G rows selected.

;% Li"t a-- emp-o:ee" ',o 7e-on!" to t,e +epartment $= or .=%

INPUT %()Fselect ename from emp where deptno in *?',0'+

RESULT@

;5A;

----------

%IT7E15;%

")A6D 

%"1TTDI53

A$A%

Page 11: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 11/33

216$

I));6 

N rows selected.

$=% Li"t a-- emp-o:ee name" ) "a-ar: an+ $4K ri"e in "a-ar:%

INPUT %()Fselect ename , sal , salO'.?C sal from emp

RESULT@

;5A; %A) %A)O'.?C%A)

---------- ---------- ------------

%IT7 N'' U0'A));5 ?M'' ?N&'

4A6$ ?0C' ?&@G.C

E15;% 0UGC @&0?.0CA6TI5 ?0C' ?&@G.C

)AD; 0NC' @0GG.C

")A6D 0&C' 0N?G.CG rows selected.

$$% Li"t minimum ) ma#imum ) a&era!e "a-arie" of emp-o:ee%

INPUT %()Fselect min*sal+,max*sal+,avg*sal+ from emp

RESULT@

I5*%A)+ AB*%A)+ A<3*%A)+--------- ---------- ----------

@ C''' [email protected]&??N

$.% Fin+ ,o' man: >o7 tit-e" are a&ai-a7-e in emp-o:ee ta7-e%

INPUT %()Fselect count *distinct !ob+ from emp

RESULT@

"1=5T*$I%TI5"TE1+

------------------G

$0% /,at i" t,e +ifference 7et'een ma#imum an+ minimum "a-arie" of emp-o:ee" in t,e or!aniation?

INPUT %()Fselect max*sal+-min*sal+ from emp

RESULT@

AB*%A)+-I5*%A)+-----------------&UUG

$2% Di"p-a: a-- emp-o:ee name" an+ "a-ar: ',o"e "a-ar: i" !reater t,an

minimum "a-ar: of t,e compan: an+ >o7 tit-e "tart" 'it, M*%

INPUT %()Fselect ename,sal from emp where !ob lie JVK and sal F *select min *sal+

from emp+

RESULT

ENA*E SAL---------- ----------

E15;% 0UGC

)AD; 0NC'")A6D 0&C'

$4% Fin+ ,o' muc, amount t,e compan: i" "pen+in! to'ar+" "a-arie"%

INPUT %()Fselect sum *sal+ from emp

RESULT

%=*%A)+---------

@0U0N

Page 12: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 12/33

$5% Di"p-a: name of t,e +ept% 'it, +eptno .=%

INPUT %()Fselect ename from emp where deptno Q 0'

RESULT

ENA*E

----------%IT7

E15;%

%"1TT

A$A%

$6% Li"t ename ',o"e commi""ion i" NULL%

INPUT %()Fselect ename from emp where comm is null

ENA*E

RESULT ----------

")A6D %"1TT

DI53

A$A%EA;%

216$M rows selected.

$9% Fin+ no%of +ept in emp-o:ee ta7-e%

INPUT %()Fselect count *distinct ename+ from emp

RESULT

"1=5T*$I%TI5"T;5A;

--------------------

?G

Page 13: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 13/33

E#p0% /ritin! (uerie" u"in! GROUP 1Y an+ ot,er c-au"e"%

Source ta7-e"@ emp, dept, programmer, software, study.

Or+er 7: @ The order by clause is used to display the results in sorted order.

Group 7: : The attribute or attributes given in the clauses are used to form groups. Tuples

with the same value on all attributes in the group by clause are placed in one group.

Ha&in!: %() applies predicates *conditions+ in the having clause after groups have been

formed, so aggregate function be used.

$% Di"p-a: tota- "a-ar: "pent for eac, >o7 cate!or:%

INPUT %()Fselect !ob,sum *sal+ from emp group by !ob

RESULT

E1 %=*%A)+--------- ----------

A5A)>%T M'''");6D 0@'C'

A5A3;6 N0GC86;%I$;5T C'''

%A);%A5 CM''

assistant 00''cler 0''@

G rows selected.

.% Di"p-a: -o'e"t pai+ emp-o:ee +etai-" un+er eac, mana!er%

INPUT %()Fselect ename, sal from emp where sal in *select min*sal+ from emp group by

mgr+RESULT

 ENAME SAL

---------- ----------

chai @EA;% UC'

I));6 ?'''

A$A% ??''russel 00''

C rows selected.

0% Di"p-a: num7er of emp-o:ee" 'orin! in eac, +epartment an+ t,eir+epartment name%

INPUT %()F select dname, count *ename+ from emp, dept where emp.deptnoQdept.deptno

group by dname

RESULT

$5A; "1=5T*;5A;+

-------------- ------------A""1=5TI53 @

6;%;A6"7 C

 SALES 9

Page 14: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 14/33

2% Di"p-a: t,e "a-e" co"t of paca!e +e&e-ope+ 7: eac, pro!rammer%

INPUT %()Fselect pname, sum*scost+ from software group by pname

RESULT

85A; %=*%"1%T+

-------------------- ---------- !ohn ?0'''

amala ?0'''

ra!u ?0@@@

@ rows selected.

4% Di"p-a: t,e num7er of paca!e" "o-+ 7: eac, pro!rammer%

INPUT %()Fselect pname, count*title+ from software group by pname

RESULT

85A; "1=5T*TIT);+

-------------------- ------------ !ohn ?

amala ?

ra!u ?ramana ?

rani ?C rows selected.

5% Di"p-a: t,e num7er of paca!e" in eac, -an!ua!e for ',ic, t,e

+e&e-opment co"t i" -e"" t,an t,ou"an+%

INPUT %()Fselect devin, count*title+ from software where dcost ?''' group by devin

RESULT

$;<I5 "1=5T*TIT);+

---------- ------------cobol ?

6% Di"p-a: eac, in"titute name 'it, num7er of "tu+ent"%INPUT %()Fselect splace, count*pname+ from study group by splace

RESULT

%8)A"; "1=5T*85A;+

-------------------- ------------$8% 0

IT% ?

56I))IA5I ?"1IT ?

7>$ ?

C rows selected.

9% Ho' man: copie" of paca!e ,a&e t,e -ea"t +ifference 7et'een

+e&e-opment an+ "e--in! co"t) 'ere "o-+?

INPUT %()Fselect sold from software where scost H dcostQ*select min*scost H dcost+ fromsoftware+

RESULT

 SOLD

---------

??

Page 15: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 15/33

;% /,ic, i" t,e co"t-ie"t paca!e +e&e-ope+ in Pa"ca-%

INPUT %()Fselect title from software where devin Q J8A%"A)K and dcost Q *select

max*dcost+from software where devin Q J8A%"A)K+

RESULT

no rows selected

$=% /,ic, -an!ua!e 'a" u"e+ to +e&e-op mo"t no %of paca!e"%INPUT %()Fselect devin, count *+ from software group by devin having count*+ Q *select

max*count*+ + from software group by devin+

RESULT

$;<I5 "1=5T*+

---------- ----------

 !sp 0

$$%/,o are t,e ma-e pro!rammer" earnin! 7e-o' t,e a&era!e "a-ar: of 

fema-e pro!rammer"?

INPUT %()Fselect pname from programmer where sal *select avg*sal+ from programmer 

where sex Q J2K+ and sex Q JKRESULT

 PNAME 

--------------------

vi!ay

$.% Di"p-a: t,e +etai-" of "oft'are +e&e-ope+ 7: t,e ma-e pro!rammer"

earnin! more t,an 0===8%

INPUT %()Fselect programmer.pname, title, devin from programmer, software where sal F@''' and sex Q JK and programmer.pname Q software.pname

RESULT

no rows selected

$0% Di"p-a: t,e +etai-" of "oft'are +e&e-ope+ in c -an!ua!e 7: fema-e

pro!rammer" of pra!at,i%

INPUT %()Fselect software.pname, title, devin, scost, dcost, sold from programmer,software, study where devin Q JcK and sex QK2K and splace Q JpragathiK and

 programmer.pname Q software.pname and software.pname Q study.pname

$2% /,ic, -an!ua!e ,a" 7een "tate+ 7: t,e mo"t of t,e pro!rammer" a"

proficienc: one?

INPUT %()Fselect prof?, count*+ from programmer group by prof? having count *+ Q

*select max *count *+ + from programmer group by prof?+

Page 16: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 16/33

E#p 2% To 'rite 3uerie" u"in! Set operation" an+ to 'rite ne"te+ 3uerie"%

 Set Operations:

=5I15 - 16 I5T;6%;"T - A5$

;B";8T - - 51T

 5;%T;$ (=;6>:- A nested uery maes use of another sub-uery to compute or retrieve

the information.

$% Fin+ t,e name of t,e in"titute in ',ic, t,e per"on "tu+ie+ an+

+e&e-ope+ t,e co"t-ie"t paca!e%

INPUT %()Fselect splace, pname from study where pname Q *select pname from software

where scost Q *select max *scost+ from software+

RESULT

 SPLAE PNAME 

------------ -------------

%A77A6I A6>

.. Fin+ t,e "a-ar: an+ in"titute of a per"on ',o +e&e-ope+ t,e ,i!,e"t"e--in! paca!e%

INPUT %()F select study.pname, sal, splace from study, programmer where study.pname Q programmer.pname and study.pname Q *select pname from software where scost Q *select

max *scost+ from software++

RESULT

 PNAME SAL SPLAE 

----------- ------ -----------

A6> &C'' %A7A6I

0% Ho' man: paca!e" 'ere +e&e-ope+ 7: t,e per"on ',o +e&e-ope+ t,e

c,eape"t paca!e%INPUT %()Fselect pname, count *title+ from software where dcost Q *select min*dcost+from software+ group by pname

RESULT

85A; "1=5T*TIT);+------------- ----------------------

<IEA> ?

2% Ca-cu-ate t,e amount to 7e reco&ere+ for t,o"e paca!e" ',o"e

+e&e-opment co"t ,a" not :et reco&ere+%

INPUT %()Fselect title , *dcost-scost+ from software where dcost F scost

4% Di"p-a: t,e tit-e) "co"t) +co"t) +ifference of "co"t an+ +co"t in t,e

+e"cen+in! or+er of +ifference%

INPUT %()F select title, scost, dcost, *scost - dcost+ from software descending order by*scost-dcost+

5% Di"p-a: t,e +etai-" of t,o"e ',o +ra' t,e "ame "a-ar:%

INPUT %()F select p.pname, p.sal from programmer p, programmer t where p.pname F

t.pname and p.sal Q t.sal*or+

INPUT %()Fselect pname,sal from programmer t where pnameFt.pname and salQ t.sal

Page 17: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 17/33

E#p 4% To 'rite 3uerie" u"in! "in!-e ro' function" an+ !roup function"%

$% Di"p-a: t,e name" an+ +o7 of a-- pro!rammer" ',o 'ere 7orn in

 >anuar:%

INPUT %()Fselect pname, dob from programmer where toLchar *dob,K15K+QKEA5K

.% Ca-cu-ate t,e e#perience in :ear" of eac, pro!rammer an+ +i"p-a: a-on!

'it, pro!rammer name in +e"cen+in! or+er%INPUT %()F select pname, round *monthsLbetween*sysdate, do!+#?0, 0+ W;B8;6I;5";W

from programmer order by monthsLbetween *sysdate, do!+ desc

0% Li"t out t,e pro!rammer name" ',o 'i-- ce-e7rate t,eir 7irt,+a:"

+urin! current mont,%

INPUT %()Fselect pname from programmer where toLchar*dob,K15K+ lie toLchar *sysdate, J15K+

2% Di"p-a: t,e -ea"t e#perience+ pro!rammer" +etai-"%

INPUT %()Fselect from programmer where do! Q *select max *do!+ from programmer+

4% /,o i" t,e mo"t e#perience+ pro!rammer no'in! pa"ca-%

INPUT %()Fselect pname from programmer where do! Q *select min *do!+ from programmer+

5% /,o i" t,e :oun!e"t pro!rammer 7orn in $;54%

INPUT %()F select pname , dob from programmer where dob Q *select max *dob+ from

 programmer where toLchar *dob,/yy/+ Q MC+

6% In ',ic, :ear) mo"t of t,e pro!rammer" are 7orn%

INPUT %()Fselect toLchar *dob , J>>K+ from programmer group by toLchar *dob, J>>K+

having count*+ Q *select max *count*++ from programmer group by toLchar*dob,K>>K+

9% In ',ic, mont, mo"t num7er of pro!rammer" are >oine+%

INPUT %()Fselect toLchar *do!,K>>K+ from programmer group by toLchar *do!,K>>K+

having count *+ Q *select max *count*++ from programmer group by toLchar *do!,K>>K+

;% /,at i" t,e -en!t, of t,e ",orte"t name in pro!rammer ta7-e ?

INPUT %()Fselect length *pname+ from programmer where length *pname+ Q select min* length *pname+ from programmer+

$=% Di"p-a: t,e name" of t,e pro!rammer" ',o"e name contain" up to 4

c,aracter"%INPUT %()Fselect pname from programmer where length *pname+ QC

$$% Di"p-a: a-- paca!e" name" in "ma-- -etter" an+ corre"pon+in!

pro!rammer name" in upperca"e -etter"%

INPUT %()Fselect lower *title+, upper *pname+ from software

Page 18: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 18/33

E#p 5@ To 'rite 3uerie" on &ie'"%

$% Create a &ie' from "in!-e ta7-e containin! a-- co-umn" from t,e 7a"e

ta7-e%

%()Fcreate view view? as *select from programmer+

.% Create a &ie' from "in!-e ta7-e 'it, "e-ecte+ co-umn"%

%()Fcreate a view view0 as *select pname,dob,do!,sex,sal from programmer+

0% Create a &ie' from t'o ta7-e" 'it, a-- co-umn"%

%()Fcreate view xyz as select from programmer full natural !oin software

2% Create a &ie' from t'o ta7-e" 'it, "e-ecte+ co-umn"%

%()F create view lmn as *select programmer, pname, title, devin from programmer, software

where sal @''' and programmer.pname Q software.pname+

4% C,ec a-- D*L comman+" 'it, a7o&e 2 &ie'"%

INPUT %()F insert into view? values *JramuK,K?0-sep-'@K,K0N-!an-NCK,KfK,KdbaseK,KoracleK,G&'''+

RESULT? row created

INPUT %()Fupdate view? set salary QC'''' where pname lie Jra!uK

RESULT ? row updated.

 5ote: update command does not wors for all ueries on views.

INPUT %()Fdelete from view? where pname lie Jra!uK

RESULT ? row deleted.

5% Drop &ie'" ',ic, :ou !enerate+%

INPUT %()Fdrop view view?

RESULT <iew dropped

INPUT %()Fdrop view view0RESULT <iew dropped

INPUT %()Fdrop view xyz

E#p 6 Data7a"e Sc,ema for a cu"tomer8"a-e "cenario

Page 19: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 19/33

"ustomer*Cu"t i+ @ inte!er) custLname: string+

Item*itemBi+@ inte!er, itemLname: string, price: integer+

%ale*7i--Bno@ inte!er, billLdata: date, cu"tBi+@ inte!er) itemBi+@ inte!er, tyLsold: integer+

2or the above schema, perform the following9 

a+ "reate the tables with the appropriate integrity constraints b+ Insert around ?' records in each of the tables

c+ )ist all the bills for the current date with the customer names and item numbers

d+ )ist the total ill details with the uantity sold, price of the item and the final amount

e+ )ist the details of the customer who have bought a product which has a priceF0''f+ 3ive a count of how many products have been bought by each customer 

g+ 3ive a list of products bought by a customer having custLid as C

h+ )ist the item details which are sold as of todayi+ "reate a view which lists out the billLno, billLdate, custLid, itemLid, price, tyLsold, amount

 !+ "reate a view which lists the daily sales date wise for the last one wee 

%()F create table customer? *custLid number*C+ primary ey, custLname varchar0*?C++

Output@ Table created.

%()F desc customer?

Output@

 5ame 5ull Type----------------------------------------- -------- ----------------

"=%TLI$ 51T 5=)) 5=;6*C+

"=%TL5A; <A6"7A60*?C+

<a-i+ Te"t Data

7  %()F insert into customer? values*Pcustid,/Pcustname/+

%()F select from customer?

Output@"=%TLI$ "=%TL5A;---------- ---------------

?'' ramu

?'? amal?'0 ra!u

?'@ ra!u sundaram

?'& lawrence

%()F create table item*itemLid number*&+ primary ey,

 itemLname varchar0*?C+,price number*M,0++

%()F dsec item

Output@

 5ame 5ull TypeXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

"ustLid 51T 5=)) 5=;6*&+

ItemLname <A6"7A60*?C+86I"; 5=;6*M,0+

%()Finsert into item values*PitemLid,KPitemLnameK,Pprice+

%()F select from item

Output@

Page 20: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 20/33

IT;LI$ IT;L5A; 86I";

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..

0@@& geera M.0C&C@0 corn soup @&.MC

0?0& lays chips 0'

&C@? setwet UU.UU0@?U duracell &C.C

%()Fcreate table sale*billLno number*C+ primary ey,billLdate date, custLid number*C+ references

customer*custLid+, itemLid number*&+ references item*itemLid+,tyLsold number*&++

Out put: Table "reated.

%()Fdsec sale

Output@

 5ame 5ull Type

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..

I))L51 51T 5=)) 5=;6*&+I))L$AT; $AT;

"=%TLI$ 5=;6*C+IT;LI$ 5=;6*&+

(T>L%1)$ 5=;6*&+

%()Finsert into %ale values*PbillLno, KPbillLdateK,

PcustLid, PitemLid, PtyLsold+

%()Fselect from sale

Output@

I))L51 I))L$AT; "=%TLI$ IT;LI$ (T>L%1)$

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX...

?&C' '&-EA5-'M ?'' 0?0& 0?&C? '&-EA5-'M ?'? 0@?U ??&C0 '&-EA5-'M ?'@ &C@? 0

?&C@ '&-EA5-'M ?'0 0@@& @

?&C& '&-EA5-'M ?'& &C@0 @

c  )ist all the bills for the current date with the customer names and item numbers

%()F select c.custname, i.itemid, s.billno from customer c, item I, sale swhere c.custidQs.custid and

s.billdateQtoLchar*sysdate+

"=%T5A; IT;I$ I))51------------- --------- ---------

Eohn C''? @@0

+ )ist the total ill details with the uantity sold, price of the item and the final amount

Page 21: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 21/33

%()F select i.price, s.ty,*i.prices.ty+ total from item I, sale s where i.itemidQs.itemid

86I"; (T> T1TA)------- ----- --------

?0' 0 0&'

0' @ M'C 0 ?'

?' ? ?'

@C' & ?&''

e )ist the details of the customer who have bought a product which has a priceF0''

%()F select c.custid, c.custname from customer c, sale s, item i where i.priceF0'' and

c.custidQs.custid and i.itemidQs.itemid

"=%TI$ "=%T5A;

--------- --------------duffy

f 3ive a count of how many products have been bought by each customer %()F select custid, count*itemid+ from sale group by custid

"=%TI$ "1=5T*IT;I$+

---------- ---------------------? 0

@ ?

& ?C ?

! 3ive a list of products bought by a customer having custLid as C%()F select i.itemname from item i, sale s where s.custidQC and i.itemid-s.itemid

IT;5A;--------------8ens

,  )ist the item details which are sold as of today

%()F select i.itemid, i.itemname from item I, sale s where i.itemidQs.itemid

and s.billdateQtoLchar*sysdate+

IT;I$ IT;5A;

--------- -------------

pencil

Page 22: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 22/33

i "reate a view which lists out the billLno, billLdate, custLid, itemLid, price,

tyLsold, amount

%()Fcreate view cust as *select s.billno, s.billdate, c.custid, i. iitemid, i.price, s.ty from customer c,sale s itI where c.custidQs.custid and i.iemidQs.itemid+

view created.

%()Fselect from cust

I))51 I))$AT; "=%TI$ IT;I$ 86I"; (T>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

@&@0 ?0-EA5-'M @ @0&& ?0' 0

&&0& 0'-2;-'M ? @&CM 0' @@@0 ?@-A6-'M ? ?0@& C 0

0@&@ ?'-A6 C C''? ?' ?

?@@? ??-A6-'M & GMGGM @C' &

Page 23: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 23/33

E#p 9% Data7a"e Sc,ema for a Stu+ent Li7rar: "cenario

%tudent*Stu+Bno @ inte!er) %tudLname: string+

embership**emBno@ inte!er, Stu+Bno@ inte!er+oo*7ooBno@ inte!er, booLname:string, author: string+

IssLreci""Bno@inte!er, issLdate: date, *emBno@ inte!er, 7ooBno@ inte!er+

For t,e a7o&e "c,ema) perform t,e fo--o'in! a "reate the tables with the appropriate integrity constraints

7 Insert around ?' records in each of the tables

c )ist all the student names with their membership numbers

+ )ist all the issues for the current date with student and oo names

e )ist the details of students who borrowed boo whose author is "E$AT;

f 3ive a count of how many boos have been bought by each student

! 3ive a list of boos taen by student with studLno as C

, )ist the boo details which are issued as of today

i "reate a view which lists out the issLno, iss Ldate, studLname, boo name

 > "reate a view which lists the daily issues-date wise for the last one wee 

%()Fcreate table student*studLno number*C+ primary ey,studLname varchar0*?C++

%()Fdesc student

 5ame 5ull TypeXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..

%T=$L51 51T 5=)) 5=;6*C+

%T=$L5A; <A6"A760*?C+

<a-i+ Te"t Data@

%()Finsert into student values*PstudLno,KPstudLnameK+

%()Fselect from student

%T=$L51 %T=$L5A;....................................................................

C'N 7A6I%7C?@ A)AEI

C?N 6AD;%7

C0& 8A<A5

C@& E1>";

%()Fcreate table membership*memLno number*C+ primary ey,studLno number*C+ references

student*stud+no++%()Fdsec membership

 5ame 5ull TypeXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.

;L51 51T 5=)) 5=;6*C+

%T=$L51 5=;6*C+

Page 24: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 24/33

%()Finsert into membership values*PmemLno,PstudLno+

;nter value for memLno:C&&'

;nter value for studLno:C?'old ?:insert into membership values*PmemLno,PstudLno+

new ?:insert into membership values*C&&',C?'+

insert into membership values*C&&',C?'+

Error" O7"er&e+@

;6616 at line ?:16A-'00U?:integrity constraint*7A6I%7.%>%L"''0G0&+violated-primary ey not found

%()Fselect from membership;L51 %T=$L51

XXXXXXXXXXXXXXXXXXXXXXXXXXX..

C?@C'N

C?N

C@&C0&

%()Fcreate table boo*booLno number*C+ primary ey,booLname varchar0*0'+,author varchar0*0++

%()Fdesc boo

 5ame 5ull Type

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..11DL51 51T 5=)) 5=;6*C+

11DL5A; <A6"7A60*0'+

A=T716 <A6"7A60*0'+

%()Finsert into boo values*PbooLno,KPbooLnameK,KPauthorK+

%()Fselect from boo

11DL51 11DL5A; A=T716 

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..

U?0@ $% 6ama Drishna0@&0 EA<A 6obett wilins

&C0@ 2earless tales Alfred

NG0@ my ambition 7arishGN0? 7arry 8otter ED 6owling

%()Fcreate table lssLrec*issLno number primary ey,issLdate date,memLno number*C+ references

membership*memLno+,booLno number*C+ references boo*booLno++

%()Fdesc issLrec

 5ame 5ull TypeXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

I%%L51 51T 5=)) 5=;6  

I%%L$AT; $AT;;L51 5=;6*C+

11DL51 5=;6*C+

%()Fselect from issLrec

I%%L51 I%%L$AT; ;L51 11DL51

Page 25: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 25/33

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

&@ 'C-EA5-'M C&&@ &C0@

N? 0N-$;"-'C C&&? NG0@00 'N-$;"-'C C&&' GN0?

C@ 'G-EA5-'M C&&0 U?0@

@C 'M-EA5-'M C&&& 0@&0

c )ist all the student names with their membership numbers

%()F select s.studname, m.memno from student s, membership m where m.studnoQs.studno

%T=$5A; ;51

------------- --------abhi!eet ?''?

arun ?''0

arvind ?''@ashish ?''&

ashwin ?''C

+ )ist all the issues for the current date with student and oo names

%()F select i.issno, s.studname, b.booname from issLrec I, membership m, student s, boo b0 where i.memnoQm.memno and m.studnoQs.studno and

i.issdateQtoLchar*sysdate+

I%%51%T=$5A; 11D5A;

------- ------------ ---------------

?@ arvind 8P%

e )ist the details of students who borrowed boo whose author is "E$AT;

%()F select from student where studno in*select studno from membership where memno in0 *select memno from issLrec where boono in*select boono from boo where authorQK"E$AT;K+++

%T=$51 %T=$5A;---------- -------------

ashwin

f 3ive a count of how many boos have been bought by each student

%()F select s.studno, count*i.boono+ from student s.membership m, boo b, 0 issLrec I where

s.studnoQm.studno and b.boonoQi.boono group by s.studno

%T=$51 "1=5T*I.11D51+

---------- -----------------------C'? C

C'0 C

C'@ CC'& C

C'C C

Page 26: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 26/33

! 3ive a list of boos taen by student with studLno as C

%()F select booname from boo where boono in *select boono from issLrec where

0 memno in*select memno from membership where

@ studno in*select studno from student where studnoQC+++

11D5A;

-------------

 5T

, )ist the boo details which are issued as of today

%()F delete from boo where boono in*select boono from issLrec where issdateQtoLchar*sysdate++

delete from boo where boono in *select boono from issLrec where issdateQtoLchar*sysdate++

Error" O7"er&e+@

;6616 at line ?:16A-'00U0: integrity constraint *%"1TT.%>%L"''N&'+ violated H child record found

i "reate a view which lists out the issLno, iss Ldate, studLname, boo name

 > "reate a view which lists the daily issues-date wise for the last one wee 

Page 27: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 27/33

E#p ; % Data7a"e Sc,ema for a <i+eo Li7rar: "cenario

"ustomer*custLno: integer,custLname: string+embership**emBno@ inte!er, cu"tBno@ inte!er+

"assette*ca""Bno@inte!er, cassLname:string, )anguage: %tring+

IssLreci""Bno@ inte!er, issLdate: date, memBno@ inte!er, ca""Bno@ inte!er+

For t,e a7o&e "c,ema) perform t,e fo--o'in! 

a+ "reate the tables with the appropriate integrity constraints

 b+ Insert around ?' records in each of the tablesc+ )ist all the customer names with their membership numbers

d+ )ist all the issues for the current date with the customer names and cassette names

%()Fcreate table customer*custLno number*C+ primary ey,custLname varchar0*0'++

%()Fdesc customer

<a-i+ Te"t Data@

%()Finsert into customer values*PcustLno,KPcustLnameK+%()Fselect from customer

"=%TL51 "=%TL5A;

XXXXXXXXXXXXXXXXXXXXXXXX.scott

 pandey

varshneynaidu

 bhimbra

%()Fcreate table membership*memLno number*C+ primary ey,custLno number*C+ references

customer*custLno++

%()Fdsec membership 5ame 5ull Type

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX...

;L51 51T 5=)) 5=;6*C+"=%TL51 5=;6*C+

%()Finsert into memship values*PmemLno,PcustLno+%()Fselect from memship

;L51 "=%TL51

XXXXXXXXXXXXXXXXXXXU0' C'

UN? C?

NUG C0N0' C@

U0N C&

%()Fcreate table cassette*cassLno number*C+ primary ey,"assLname varchar0*?C+,language varchar0*?C++

%()Fdesc cassette

Page 28: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 28/33

 5ame 5ull Type

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX..

"A%%L51 51T 5=)) 5=;6*C+"A%%L5A; <A6"7A60*?C+

)A53=A3; <A6"7A60*?C+

%()Finsert into cassette values*PcassLno,KPcassLnameK,KPlanguageK+

%()Fselect from cassette

"A%%L51 "A%%L5A; )A53=A3;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

? tagore telugu

0 the lion ing ;nglish@ anniyan tamil

& indra telugu

C lord of rings ;nglish

%()Fcreate table issuLrec*issLno number*C+ primary ey,issLdate date,memLno number*C+references

memship*memLno+,cassLno number*C+ references cassette*cassLno++

%()Fdesc issuLrec 5ame 5ull Type

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX...I%%L51 51T 5=)) 5=;6*C+

I%%L$AT; $AT;

;L51 5=;6*C+"A%%L51 5=;6*C+

%()Fselect from issuLrec

I%%L51 I%%L$AT; ;L51 "A%%L51

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX00 'G-EA5-'M U0' ?0@ ?'-EA5-'' UN? 0

0M ?'-EA5-'M NUG C

@ '?-EA5-'M N0' &@& @?-$;"-'C U0N @

c+ )ist all the customer names with their membership numbers%()Fselect c.custname,m.memno from customer? c,membership? m where c.custnoQm.custno

"=%T5A; ;51

XXXXXX.. XXXXXX.. 5ID7I) C?

<I<;D C0

%76A<A5 CN<A%I CG

%7I<A CM

d+ )ist all the issues for the current date with the customer names and cassette names

%()Fselect i.issno,c.custname,cc.cassettename from customer? c,membership? m,cassette cc,issrec? I whi.issdateQtoLchar*sysdate+ and c.custnoQm.custno and i.cassnoQcc.cassno and i.memnoQm.memno

Page 29: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 29/33

E#p% $= O7"er&ation of ne"te+ 3uer: an+ >oin 3uer:%

a corre-ate+ "u73uer: @8

In a %() database uery, a corre-ate+ "u7 3uer: *also nown as a ":nc,ronie+ "u73uer:+ is a subuery *a

nested inside another uery+ that uses values from outer uery. The subuery is evaluated once for each row proces

 by the outer uery.

;xample: find the list of all employees whose salary is above average for their departments.

%;);"T employeeLnumber, name

  261 employees A% ob

  47;6; salary F *

  %;);"T A<3*salary+  261 employees

  47;6; department Q ob.department+

In the above uery the outer uery is

 %;);"T employeeLnumber, name  261 employees A% ob

  47;6; salary F ...

and the inner uery *the correlated subuery+ is

 %;);"T A<3*salary+

  261 employees  47;6; department Q ob.department

"orrelated subueries may appear elsewhere besides the 47;6; clause for example, this uery uses a correl

subuery in the %;);"T clause to print the entire list of employees alongside the average salary for each employ

department. Again, because the subuery is correlated with a column of the outer uery, it must be re-executed for

row of the result.

 %;);"T

  employeeLnumber,

  name,  *%;);"T A<3*salary+

261 employees  47;6; department Q ob.department+ A% departmentLaverage

  261 employees A% ob

7 S(L Join"

%() Eoins are used to relate information in different tables. A Eoin condition is a part of the sl uery

retrieves rows from two or more tables. A %() Eoin condition is used in the %() 47;6; "lause of selupdate, delete statements.

T,e S:nta# for >oinin! t'o ta7-e" i"@

%;);"T col?, col0, col@...

261 tableLname?, tableLname0

47;6; tableLname?.col0 Q tableLname0.col?

If a sl !oin condition is omitted or if it is invalid the !oin operation will result in a "artesian product.

"artesian product returns a number of rows eual to the product of all rows in all the tables being !oined.

example, if the first table has 0' rows and the second table has ?' rows, the result will be 0' ?', or

rows. This uery taes a long time to execute.

+ata7a"e ta7-e pro+uct

Page 30: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 30/33

pro+uctBi+ pro+uctBname "upp-ierBname unitBprice

?'' "amera 5ion @''

?'? Television 1nida ?''

?'0 6efrigerator <ediocon ?C'

?'@ Ipod Apple GC

?'& obile 5oia C'

+ata7a"e ta7-e or+erBitem"

or+e

rBi+

pro+u

ctBi+

tota-B 

unit"

cu"to

mer

C?'' ?'& @'Infos

ys

C?'? ?'0 C%atyam

C?'0 ?'@ 0C4ipr 

o

C?'@ ?'? ?' T"%

%() Eoins can be classified into ;ui !oin and 5on ;ui !oin.

$ S(L E3ui >oin"

It is a simple sl !oin condition which uses the eual sign as the comparison operator. Two types of eui !

are %() 1uter !oin and %() Inner !oin.

An eui-!oin is further classified into two categories:a+ %() Inner Eoin

 b+ %() 1uter Eoin

a) S!L "nner #oin:All the rows returned by the sl uery satisfy the sl !oin condition specified.

For e#amp-e@ If you want to display the product information for each order the uery will be as given bel%ince you are retrieving the data from two tables, you need to identify the common column between these

tables, which is theproductLid.

The uery for this type of sl !oins would be lie,

%;);"T orderLid, productLname, unitLprice, supplierLname, totalLunits

261 product, orderLitems

47;6; orderLitems.productLid Q product.productLid

The columns must be referenced by the table name in the !oin condition, because productLid is a column

 both the tables and needs a way to be identified. This avoids ambiguity in using the columns in the %%;);"T statement.

The number of !oin conditions is *n-?+, if there are more than two tables !oined in a uery where /n/ is

number of tables involved. The rule must be true to avoid "artesian product.

4e can also use aliases to reference the column name, then the above uery would be lie,

Page 31: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 31/33

%;);"T o.orderLid, p.productLname, p.unitLprice, p.supplierLname, o.totalLunits

261 product p, orderLitems o

47;6; o.productLid Q p.productLid

b) S!L Outer #oin:

This sl !oin condition returns all rows from both tables which satisfy the !oin condition along with r

which do not satisfy the !oin condition from one of the tables. The sl outer !oin operator in 1racle is * O +

is used on one side of the !oin condition only.

The syntax differs for different 6$% implementation. 2ew of them represent the !oin conditions as left outer !oinW, Wsl right outer !oinW.

If you want to display all the product data along with order items data, with null values displayed for o

items if a product has no order item, the sl uery for outer !oin would be as shown below:

%;);"T p.productLid, p.productLname, o.orderLid, o.totalLunits261 orderLitems o, product p

47;6; o.productLid *O+ Q p.productLid

The output would be lie,

p

r

o

+

u

c

t

 B 

i

+

p

r

o

+

u

c

t

 B 

n

a

me

o

r

+

e

r

 B 

i

+

t

o

t

a

-

 B 

u

n

i

t

"

-

-

--

-

--

-

-

--

--

-

-

--

-

--

-

-

--

--

-

-

--

-

--

-

-

--

--

-

-

--

-

--

-

-

--

--

?'

'

"

a

me

a

?

'

T

e

C

?

?

'

Page 32: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 32/33

?

l

e

vi

s

io

n

'

@

?

'

0

ef 

ig

e

r a

t

or 

C?

'

?

C

?

'@

I p

od

C?

'0

0

C

?'

&

o b

i

le

C?

'

'

@

'

NOTE@If the *O+ operator is used in the left side of the !oin condition it is euivalent to left outer !oin. If u

on the right side of the !oin condition it is euivalent to right outer !oin.

 S!L Self #oin:

A %elf Eoin is a type of sl !oin which is used to !oin a table to itself, particularly when the table h216;I35 D;> that references its own 86IA6> D;>. It is necessary to ensure that the !oin statem

defines an alias for both copies of the table to avoid column ambiguity.

The below uery is an example of a self !oin,

%;);"T a.salesLpersonLid, a.name, a.managerLid, b.salesLpersonLid, b.name

261 salesLperson a, salesLperson b

47;6; a.managerLid Q b.salesLpersonLid

Page 33: ADBMS Lab Manual

7/21/2019 ADBMS Lab Manual

http://slidepdf.com/reader/full/adbms-lab-manual-56d97b04c84cf 33/33

. S(L Non e3ui >oin"

It is a sl !oin condition which maes use of some comparison operator other than the eual sign lie F, ,

Q

A 5on ;ui Eoin is a %() Eoin whose condition is established using all comparison operators except the e*Q+ operator. )ie FQ, Q, , F

For e#amp-e@ If you want to find the names of students who are not studying either ;conomics, the sl u

would be lie, *lets use studentLdetails table defined earlier.+

%;);"T firstLname, lastLname, sub!ect261 studentLdetails

47;6; sub!ect YQ /;conomics/

The output would be something lie,

fir

"t

 Bn

a

m

e

-a

"t

 B 

na

m

e

"u

7>

ec

t

---

---

---

----

----

--

--

----

-

---

---

---

----

Ana

 !al

i

ha

g

wat

at

hs

%h

e ar 

3

o

wda

aths

6a

hul

%

ha

r m

a

%c

ienc

e

%t

ep

he

n

2le

m

ing

%c

ie

nc

e