You are on page 1of 9

Q.1] For the database system to be usable, it must retrieve data efficiently.

The need of
efficiency has led designers to use complex data structures to represent data in the database.
Developers hide this complexity from the database system users through several levels of
abstraction. Explain those levels of abstraction in detail with example.
A N S : Th e c o n c e p t o f a b s trac tio n is c ru c ia l in d e s ig nin g d a ta b a s e s y s tem s , a s it allo w s us e rs a n d
d e ve lo p ers t o w o rk w ith c o m p le x d a t a s t ru c t ures a n d o p era tio n s w it ho ut n e ed in g to u n d ers ta n d a ll th e
u n d erly in g d e ta ils . Ab s tra c tio n p ro v id es d ifferen t lev els o f in te ra c t io n a n d h id es c o m p lex ities , m a k in g
th e s y s tem m o re u s a b le a n d m a n ag ea b le. In th e c o n tex t o f d a ta b a s e s y s tem s , th e re a re typ ic ally th ree
lev els o f ab s tra c tio n : t h e p h ys ic a l le ve l, th e lo g ic al lev el, a n d th e v iew lev el. Le t's ex p lo re ea c h o f th es e
lev els w it h e xa m p le s :
1 . P h ys ic a l Lev el (In te rn a l L ev el): T h is is th e lo w es t le ve l o f a b s t ra c t io n a n d d ea ls w it h h o w d a ta is
s t o re d o n th e p h ys ic a l s to ra g e d e vic e s lik e h a rd d rive s . It in vo lv es d e ta ils s u c h a s d a ta s to rag e
fo rm a ts , ac c e s s m eth o d s , a n d in d e xin g te c h n iq u es .U s ers an d a p p lic a t io n d e ve lo p ers ra re ly
in te ra c t w ith th is lev el d irec tly.
E xa m p le : A t t h e p h ys ic a l le ve l, th e d at a b a s e m a n ag em e nt s ys te m (D B M S) m ig h t u s e te c h n iq u es lik e
c lus t erin g a n d ind ex in g t o s to re d a ta effic ien tly. I f yo u h a ve a ta b le " E m p lo y ees " , t he p h ys ic a l le ve l
w o u ld in vo lv e h o w th e ro w s o f d a ta a re s t o re d o n t h e d is k , a n d h o w in d e xe s a re u s ed to s p eed u p d a t a
ret riev a l.
2 . L o g ic a l Lev el (C o n c ep tu a l L ev el): T h e lo g ic a l le ve l a b s t ra c t s th e p h ys ic a l im p lem e nt a tio n d et ails a nd
fo c u s e s o n d e s c rib in g t he o ve ra ll s t ru c t u re o f th e d a t ab as e . It d e fin e s t h e s c h em a, w hic h in c lu d es
ta b le s , re lat io n s hip s , k eys , an d c o n s tra in ts . T h is lev el is m o re us e r-c en tric th a n t h e p h ys ic a l le ve l
a n d p ro v id es a w ay t o c rea te , m o d ify, a nd q ue ry t he d a ta b a s e u s ing h ig h lev el c o n s tru c ts .
E x a m p le: C o n s id er a d a ta b a s e fo r a lib ra ry. A t th e lo g ic al lev el, y o u w o u ld d e fin e ta b le s lik e " B o o k s " ,
" Au t ho rs ", a nd " C us t o m e rs ", a lo ng w ith th e ir at trib ut es a n d rela tio n s h ip s . Y o u w o uld a ls o s p ec ify
c o n s tra in ts , s uc h a s en s u rin g th a t a b o o k c a n 't b e c h ec k ed o u t b y m u lt ip le c u s to m ers s im u lta n eo us ly.

3 . V iew Le ve l (E x te rn a l L ev el): T h e v ie w le ve l is th e h ig h es t le ve l o f a b s trac tio n vis ib le t o en d -u s ers an d


a p p lic at io n p ro g ra m s . It a llo w s d ifferen t u s ers t o h av e d ifferen t c u s t o m ize d vie w s o f th e d at a b a s e.
U s e rs c a n d efin e an d in te ra c t w ith th e ir o w n p e rs p e c t iv e o f th e d at a w ith o u t k n o w in g a b o u t th e
u n d e rlyin g p h ys ic a l a n d lo g ic a l s truc tu re s .
E x a m p le: In th e lib ra ry d at a b a s e, d ifferen t v iew s c o u ld b e c rea t ed fo r lib ra ria n s , c us t o m e rs , a n d
a d m in is tra t o rs . A lib ra ria n 's view m ig h t s h o w info rm a tio n a b o u t
o ve rd u e b o o k s an d a v aila b le c o p ies . A c u s to m er's v iew m ig h t s h o w th eir b o rro w in g h is t o ry a n d c u rren t
c h e c k o u ts . T h es e v iew s a re t ailo red to t h e s p ec ific n ee d s o f ea c h g ro up w ith o u t e xp o s ing
u n ne c e s s ary c o m p lex it y .

Q.2] Explain the concept of candidate key and primary key, foreign key. Identify above listed key for
the following schema:
Person (driver_id, name, address, contactno)
Car(licence, model, year)
Owns (driver_id, licence)
AN S :
1 .C a nd id at e K e y:
A c a nd id at e k ey is a un iq u e id e nt ifie r fo r a tu p le (ro w ) w it h in a rela tio n (ta b le). It 's a s e t o f o n e o r m o re
a t trib u t es th a t, w h e n c o m b in ed , u niq u e ly id e n tify ea c h ro w in t he ta b le. T h ere c a n b e m u ltip le
c a nd id at e k ey s in a t a b le, b ut o n e o f th em is c h o s en a s th e p rim ary k ey.
2 . P rim a ry K ey :
Th e p rim a ry k ey is a c a n d id a te k e y th a t is c h o s en a s t h e m a in u n iq u e id en tifier fo r a ta b le . It en s u re s
t ha t e a c h ro w in t he ta b le is un iq u ely id e nt ifia b le a n d th a t n o tw o ro w s h a ve t h e s a m e v alu es fo r t h e
p rim a ry k ey a t trib u t es . A p rim a ry k e y als o e ns u res th a t n u ll v alu es a re no t a llo w e d in th e k e y
a tt rib u te s .
3 .F o reig n K ey :
A fo re ig n k e y is a n at trib ut e o r s e t o f a ttrib u tes in a t ab le th a t re fe rs to t h e p rim a ry k e y o f an o t h er
t ab le . It e s ta b lis he s a rela tio n s h ip b etw e en tw o ta b les , allo w in g d a ta to b e lin k ed b e tw ee n t h em .
T h e fo re ig n k e y en fo rc es re fe re nt ial int eg rity, en s u rin g th a t th e v a lu e s in th e fo reig n k ey c o lu m n (s )
m a t c h ex is t ing v a lu e s in th e p rim a ry k ey c o lu m n (s ) o f th e referen c e d ta b le o r a re n ull. In th e
s c he m a p ro v id ed :

● Ca n d id a te K ey s :
● C a n d id a te K ey fo r t he "P e rs o n " t a b le c o u ld b e " d riv er_id " s in c e it un iq u ely id e nt ifie s
ea c h p ers o n .
● C a nd id at e K e y fo r th e " C ar" ta b le c o u ld b e "lic e n c e" s in c e it u niq u e ly id e nt ifie s e a c h c a r.
● P rim a ry K ey s :
● P rim a ry K ey fo r th e "P ers o n " ta b le c o u ld b e "d rive r_ id ".
● P rim a ry K ey fo r th e "C a r" ta b le c o uld b e "lic en c e".
● Fo reig n K e y:
● In th e " O w n s " ta b le , "d rive r_ id " a n d "lic e nc e" a re fo re ig n k e ys
th a t refe ren c e th e " P ers o n "
an d " Ca r" ta b les res p e c tiv ely. T h es e fo reig n k e ys es t ab lis h a rela tio n s h ip b et w e en th e
"O w n s " t a b le a nd t h e o th e r tw o ta b les .
Q.3] Draw architecture of DBMS system and explain function of following components:
i) Storage manager
ii) Query Processor
AN S : DB M S A rc h itec tu re :
A typ ic al D B M S a rc h ite c t ure c a n b e d iv id ed in to s e ve ra l c o m p o ne n ts , w ith ea c h c o m p o n en t h a v in g
s p ec ific ro les a n d res p o n s ib ilitie s . H ere 's a s im p lified d e s c rip t io n :
1 . U s e r In te rfa c es : T his is th e fro n t -en d o f th e D B M S w h ere us e rs in tera c t w ith th e s ys t em . It
inc lud es int erfa c es fo r d a ta b a s e a d m in is tra to rs , a p p lic a tio n d ev elo p ers , a nd e n d -u s ers . Differen t
typ e s o f in te rfa c e s c o u ld b e c o m m a n d lin e in te rfa c es , g ra p h ic a l u s er in te rfa c es (G U I s ), o r
w eb -b a s ed in terfac es .
2 . Q u ery P ro c es s o r: Th is c o m p o n en t re c e iv es a n d p ro c es s e s us e r q u eries a nd req u es t s . It t ra n s la t es
h ig h -lev el q u erie s w rit te n in SQ L (St ru c t u red Q u ery L a ng ua g e ) in to lo w -le ve l o p era t io ns t ha t c a n
b e e xe c u te d b y th e s y s tem . T h e q u ery p ro c es s o r o p t im iz es q u e ries fo r e ffic ie nt ex ec u t io n.
3 . S to rag e M a n ag er: T his is a c ru c ia l c o m p o ne n t re s p o n s ib le fo r m a n a g ing t h e p h ys ic a l s t o ra g e o f
d at a . It in tera c ts d irec t ly w it h t he file s ys t em a n d m a na g e s ho w d a t a is s to red , re trie ve d , a n d
u p d at ed o n th e s t o ra g e d ev ic es .
4 . D a ta Dic tio n a ry M a n a g er: T h e d a ta d ic t io na ry s to res m et a d a ta ab o u t th e d a ta b a s e , in c lu d ing
in fo rm a tio n a b o u t t he s tru c tu re o f t ab le s , d a t a t yp es , c o n s tra in t s , in d ex es , an d m o re. I t's u s ed
b y v a rio u s c o m p o n en ts o f th e D B M S to u n d ers ta n d a nd m an a g e t h e d a ta b a s e's s c h em a .
5 . Tran s a c t io n M an a g e r: Th is c o m p o n en t en s u re s th e A C ID p ro p e rt ie s (A to m ic ity, C o n s is t en c y ,
Is o la t io n, D ura b ilit y) o f d a t ab as e tra n s a c t io ns . I t m a na g e s c o n c u rren t t ra n s a c tio n s , s c h ed u les
t he ir ex ec ut io n , a n d en s u res th a t d a t a rem a in s c o n s is ten t d es p ite m u lt ip le s im u lt a ne o u s
u s ers .
6 . C o n c u rren c y C o n t ro l M a n ag er: It 's res p o n s ib le fo r m a n ag in g c o n c u rren t a c c e s s to th e d at a b a s e b y
m u lt ip le u s ers o r tra n s a c t io ns . T h is c o m p o ne n t en s u res t ha t t ra n s a c tio n s d o n o t in te rfere w ith
ea c h o th e r, m ain ta in ing d at a in te g rity .
7 . B u ffer M a n a g er: T h e b u ffer m a n a g er ha n d les d a t a in m em o ry, k eep in g fre q u en tly a c c es s ed d at a
b lo c k s in a b u ffer c ac h e to im p ro v e d a ta re trie va l p erfo rm a n c e. I t d ec id es w h at d a ta to b rin g in to
m em o ry a n d w h a t d a t a t o w rit e b a c k t o d is k .
F u n c t io ns o f th e C o m p o n en ts :
i) St o ra g e M a na g e r: Th e S to ra g e M a n a g er p lay s a c ritic a l ro le in m a na g in g h o w d at a is s to red ,
ret riev ed , a n d o rg a n ize d o n p h ys ic a l s t o ra g e d ev ic es . I ts fun c tio n s in c lu d e :
● F ile O rg a niz a tio n : D e c id in g ho w d a t a files a re o rg a n iz ed o n d is k fo r e ffic ie nt s t o ra g e an d retriev al.
● D at a S to ra g e: M a n a g ing t h e a llo c a tio n an d d e a llo c a tio n o f s p ac e fo r d a t a o n d is k .
● Da t a R e trie va l: F e tc h in g d a ta fro m d is k in to m e m o ry w h en re q u es t ed b y t he Q u ery P ro c es s o r o r
o th er c o m p o n en ts .
● D at a U p d at e: W ritin g c h a ng es m a d e t o d a ta b a c k to d is k to m ain ta in c o n s is ten c y.
● In d ex ing : M a n a g in g d a ta in d e xe s to s p e ed u p d a ta re trie va l o p era tio n s . ● B u ffe r M a n a g em e n t:
M a na g in g th e b uffer c a c h e in m em o ry to o p tim iz e d a ta ac c e s s t im es .
ii) Q u ery P ro c es s o r: T h e Q ue ry P ro c es s o r is re s p o n s ib le fo r h a n d lin g u s er q u erie s a n d tran s la tin g
t he m int o ex ec ut a b le o p e ra tio n s . It s fu n c t io ns in c lu d e: ● Q u e ry P a rs in g : B rea k in g d o w n u s e r q u erie s
in to t he ir s yn ta c t ic a nd s e m a nt ic c o m p o n en ts .
● Q u e ry O p tim iz at io n : D ec id ing t h e m o s t e ffic ie n t w ay t o ex ec ut e a q u ery , c o n s id e rin g in d e xe s ,
jo in alg o rith m s , an d o t h er fa c t o rs .
● E x ec u t io nP la n G e n era t io n: C re at ing a p la n t h at o u tline s th e s eq ue n c e o f o p e ra tio n s n eed ed to
ex ec u t e th e q u e ry.
● A c c es s in g M et ad at a : C o n s u ltin g th e D a ta Dic tio n a ry to u n d e rs ta n d th e s t ru c t ure a n d
c o n s tra in t s o f t he d a ta b a s e.
● D a ta R etriev al a n d M a nip u la tio n : In it iat ing o p erat io n s t o re trie ve , in s ert , u p d a te , o r d elet e d a ta
b as e d o n th e q u ery.
● E rro r H a n d ling : D et ec tin g an d h a n d lin g e rro rs in t he q u ery , s u c h a s s y nt a x erro rs o r inc o rrec t
refe ren c es .

Q.4] Draw an ER diagram for the banking system. Assume the banking requirements are as given
below. • The bank is organized into branches. Each branch is located in a particular city.
• The bank offers two types of accounts: saving and current. Accounts can be held by more than one
customer and a customer can have more than one account.
• A loan originates at a particular branch and can be held by one or more customers
Identify the relationship among the entities along with the mapping cardinalities, keys in the E.R.
diagram. Construct appropriate tables for E-R diagram designed with above requirements
ANS:-

Q.5] Define DBMS. Explain advantages of DBMS over file system.


A N S: D B M S (Da t ab as e M a na g e m en t S ys t em ): A D a ta b a s e M a n a g em en t S ys te m (D B M S) is a s o ftw a re
s ys t em th a t a llo w s u s e rs to e ffic ie nt ly s to re, m an a g e , re trie ve , a n d m a nip u la te d at a in a s t ru c t u red
m a n n er. I t p ro vid e s a s et o f t o o ls a nd fu n c t io na lities t o c re at e, m a in t ain , a n d c o nt ro l d a ta b a s e s ,
en s u rin g d a ta c o ns is t en c y, s ec urity, an d e as e o f a c c es s .
Ad va n ta g e s o f D B M S o v er F ile S ys te m :
1 . D a ta In te g rit y an d S ec urity: D B M S en fo rc es d a ta int eg rity b y a p p lyin g ru les a n d c o n s tra in t s t o
e n s ure a c c ura te a n d re lia b le d a t a. I t a ls o o ffe rs s ec urity fea tu re s to c o nt ro l w h o c a n a c c es s a n d
m o d ify th e d a ta , p ro t ec tin g s e n s it ive in fo rm at io n .
2 . R ed uc ed D a ta R ed u n d a n c y: In a file s ys t em , th e s a m e d a t a m ig h t b e d u p lic a ted a c ro s s m u ltip le
file s , lea d in g to red u n d a n c y a n d inc o n s is ten c y.
D B M S c en traliz es d a ta s to ra g e, m inim iz ing red u n d a n c y a n d en s u rin g d a ta c o n s is te nc y.
3 . D a ta S ha rin g a n d C o lla b o ra tio n : DB M S a llo w s m u lt ip le u s ers a n d ap p lic a tio n s to a c c es s a nd
m a n ip u lat e th e d at a s im u lta n eo us ly. T h is p ro m o t es effic ien t c o llab o ra tio n a n d elim ina t es is s ue s
o f d a ta b e in g lo c k ed in in a c c e s s ib le file s .
4 . D a ta Q u e ryin g a nd R e p o rtin g : D B M S p ro v id es q u ery la n g u a g es (e. g ., S Q L) t h at allo w u s ers t o
re trie ve s p ec ific d a t a q u ic k ly. I t s u p p o rts c o m p lex q u e ries , ag g re g a tio n s , a n d re p o rt ing , m a k in g
d a ta a n alys is m o re p o w e rfu l a n d effic ien t.
5 . Da t a C en t ra liz a tio n a n d M a na g e m en t : D B M S c e n tra lize s d a ta m a n ag em e nt t a s k s , s u c h a s
b a c k up s , d a ta rec o ve ry, a n d d at a m ig rat io n . T his s im p lifies ad m inis t ra tiv e t a s k s a n d red u c e s
t he ris k o f d at a lo s s .

Q.6] Different components of database management systems like query processor, storage manager,
transaction manager etc. are functional for processing the query submitted by user. Explain the
functions of each component in view of getting query output

A N S: T he c o m p o n en ts o f a D a ta b a s e M an a g e m en t S ys te m (DB M S ) w o rk t o g e th er t o p ro c es s u s er
q u eries a nd p ro v id e q u ery o u tp u t . Let 's d elv e in to th e fu n c t io ns o f ea c h o f th es e c o m p o n en ts in v iew o f
g e ttin g q u ery o u tp u t :
1 . Q u e ry P ro c e s s o r: T he Q u ery P ro c e s s o r is res p o n s ib le fo r h an d lin g u s er q u eries a nd
t ra n s fo rm in g th e m in t o ex ec u t ab le ta s k s th a t re trie ve o r m a nip u la te d at a . Its fu nc tio n s
in c lu d e :
● Q u ery P a rs in g : B rea k ing d o w n t he us e r's q u e ry in to s yn ta c tic a n d s e m a n tic
c o m p o n en ts to u n d e rs ta n d its s tru c tu re a nd p urp o s e . ● Q u ery O p t im iza tio n : D ec id in g
th e m o s t effic ien t w a y to ex ec u te t he q ue ry. T h is in v o lve s c o n s id erin g fa c t o rs lik e
av a ila b le in d e xe s , jo in a lg o rith m s , a nd c o s t e s tim a te s .
● E x ec u t io n
P la n G e n era t io n: C re at ing a p la n t h at o u tline s th e s eq ue n c e o f o p e ra tio n s n eed ed
to ex ec u te t he q u ery . Th is p lan is o ften
re p re s en te d a s a q u ery ex ec ut io n t re e.
● D a ta R et riev a l a n d M a n ip u la t io n: In itia tin g th e a c t u al o p e ra tio n s to fet c h , in s ert, up d a te,
o r d ele te d a t a b a s e d o n t h e q u ery.
● E rro r H a n d lin g : D e tec tin g a nd h a n d ling e rro rs in t h e q u ery, s u c h a s s y nt a x erro rs o r
in c o rrec t refe ren c es .
2 . S to rag e M a n ag er: T he St o ra g e M an a g e r h an d le s t h e p h ys ic a l s to rag e o f d at a o n d is k a n d it s
effic ien t ret riev a l in to m e m o ry. Its fu n c tio n s in c lu d e: ● File O rg a n iza t io n: D ec id in g h o w d a ta
file s are o rg a niz ed o n d is k fo r o p t im al s to ra g e a n d re trie va l.
● D at a S to ra g e: M a n a g ing t h e a llo c a tio n an d d e a llo c a tio n o f s p ac e fo r d a ta o n d is k .
● B uffer M a na g e m en t : K eep in g fre q u en tly a c c es s e d d a ta b lo c k s in a b u ffe r c a c he in
m em o ry to im p ro v e d a ta a c c es s tim e s .
● Da t a R e trie va l an d U p d a t e: Fe tc hin g d a ta fro m d is k in to m e m o ry w h e n re q u es t ed a n d
w ritin g c h a ng es b a c k to d is k t o m a in t ain c o n s is te n c y. ● In d e xin g : M a na g in g d a ta in d ex es
to s p e ed u p d a ta re trie va l o p erat io n s .
3 . Tra ns a c tio n M a na g e r: Th e T ra n s a c t io n M a n a g er en s u res t he AC ID p ro p erties (A to m ic it y,
C o n s is te n c y, Is o la tio n , D u ra b ility) o f d a ta b a s e tran s a c t io n s . Its fu nc tio n s inc lud e:
● T ra n s ac tio n C o n tro l: M a n a g ing t h e s ta rt , c o m m it, a nd ro llb a c k o f t ra n s a c tio n s .
● C o n c u rren c y C o n t ro l: H an d lin g m u ltip le tra ns a c tio n s ex ec u t in g
s im u lta n eo u s ly to e ns u re d a ta c o n s is te n c y a n d is o la t io n.
● Is o la tio nLev els : D efinin g lev els o f is o la t io n t o c o n tro l ho w m u c h o n e t ra n s ac tio n c a n
s ee t he c h a ng es m a d e b y o th er t ra n s a c tio n s . ● D ea d lo c k D e tec tio n a nd R e s o lu tio n :
Id en t ifyin g a n d res o lv in g d ea d lo c k s t ha t o c c u r w h en tra ns a c tio n s are w a it ing fo r ea c h
o th er to re le a s e re s o u rc e s .
4 . D at a D ic t io n ary M a n ag er: T he Da t a D ic t io na ry M a na g e r s to res m e ta d a ta a b o u t th e d at a b a s e's
s tru c tu re a nd c o n s train ts . I ts fun c tio n s in c lu d e : ● M e ta d a ta S to rag e: St o rin g info rm a tio n a b o u t
ta b le s , c o lu m n s , in d e xe s , c o ns t ra in ts , a n d m o re.
● Sc h em a E n fo rc em e nt : E ns u rin g t ha t d at a e n tered in to t he d a ta b a s e c o n fo rm s to th e
s p ec ified s t ru c t u re a n d c o n s t ra int s .
● A c c es s C o n tro l: M a n a g in g p erm is s io ns a n d a c c e s s rig h ts fo r d ifferen t u s ers a n d ro le s .

Q.7] Explain with example what is physical data independence. Also explain its importance.
AN S : P h ys ic a l Da t a In d e p en d e n c e refers t o th e c o n c ep t in d a t ab as e m a na g e m en t s ys t em s (D B M S )
w h e re c ha n g e s t o th e p h ys ic al s to ra g e a n d o rg an iz at io n o f d a t a d o n o t a ffec t th e w a y u s ers a n d
ap p lic a tio n s in t era c t w it h t h e d a ta . In o th e r w o rd s , m o d ific a tio n s m a d e t o ho w d a t a is s to red o n th e
d is k (p h ys ic al lev el) s h o u ld n o t req u ire c h an g e s to th e w a y u s ers q ue ry o r m a nip u la te t h e d a ta (lo g ic a l
le ve l).
E x a m p le:
Le t's s a y yo u h av e a d a ta b a s e w ith a t a b le n am ed "E m p lo ye es ." A t t h e p h ys ic a l le ve l, th e d a t a is
s t o re d o n t h e d is k in s p e c ific files a n d d a ta b lo c k s . N o w , im ag in e t h at yo u ne ed t o reo rg a n ize t h e
d a t a s t o ra g e t o im p ro ve p erfo rm a n c e. Y o u m ig h t d e c id e t o m o v e s o m e d a ta to a d iffe ren t d is k
lo c at io n , c h a n g e t he s t o ra g e fo rm a t, o r im p le m en t n ew in d ex ing t ec hn iq u es .
W ith p h ys ic a l d a ta ind ep e nd en c e , yo u c a n m a k e th e s e c h a n g es to t h e s to ra g e s truc tu re w it ho ut
n ee d in g to m o d ify t he SQ L q u eries o r a p p lic a t io n c o d e t ha t
in te ra c t s w ith th e " E m p lo y ees " ta b le. U s ers an d a p p lic a t io ns w o u ld s till w rit e a n d ret riev e d a ta fro m t h e
" E m p lo ye es " t a b le u s in g t he s am e q u erie s as b e fo re , ev en th o u g h th e d a t a's p hy s ic a l a rra ng em e nt ha s
c ha n g e d .
Im p o rta n c e o f P hy s ic a l D a ta In d ep en d en c e: P h ys ic a l d a ta in d e p en d e n c e is im p o rt a nt fo r
s e ve ra l rea s o n s :
1 . F lex ib ility : It allo w s d a ta b a s e a d m in is trat o rs t o m a k e im p ro ve m en ts t o th e s ys te m 's p e rfo rm an c e,
s t o ra g e , a n d m a int en a n c e w ith o u t a ffec t ing t h e us e rs o r a p p lic at io n s . Th is fle xib ility m a k es it
e a s ie r to a d a p t th e d a ta b a s e a s te c h n o lo g y ev o lv es .
2 . R e d u c ed M ain te n an c e: Se p a ra tin g th e p h ys ic al a n d lo g ic a l le ve ls m e a ns th a t c ha n g e s t o th e
u n d erly in g s t o ra g e s t ru c t u re w o n 't n e c es s it at e m o d ific a tio n s to a p p lic at io n s o r q u erie s . Th is
re d u c e s t h e effo rt an d c o m p lex ity o f m a int en a n c e.
3 . E ffic ien c y : D a ta b a s e a d m in is trat o rs c an fine -tu n e t he p h ys ic a l s t o ra g e to o p t im iz e p e rfo rm a nc e
w ith o u t w o rryin g a b o u t b rea k ing e xis t in g fu nc tio n a lit y. T his is p a rtic ula rly c ru c ia l fo r la rg e
d a t ab as e s th a t n eed e ffic ie nt s to rag e m a n ag em e nt .
4 . D a ta b a s e E v o lu tio n : As t he d a ta b a s e g ro w s o ve r tim e , p h ys ic a l d a ta in d e p en d e n c e e na b le s
a d m in is tra t o rs to reo rg a n iz e a n d s c a le th e d at a b a s e s ys te m as n ee d ed , k ee p in g it effic ien t a n d
re s p o n s ive .
5 . D a ta S ec u rit y a nd P riv ac y: Ch a n g es m a d e to en h a n c e s e c u rity (lik e en c ryp t io n) c a n b e
im p le m en te d a t th e p h ys ic al lev el w it h o u t a ffe c tin g th e lo g ic al lev el w he re u s e rs int erac t w ith
t he d a ta .

Q.8] What is view and how to create it? Can you update view? If yes, how? If not, why not?

A N S : W h a t is a V ie w : A view in a d a ta b a s e is lik e a v irt ua l ta b le t ha t d o es n 't s t o re an y d a ta its e lf b u t


in s te a d d is p lay s d at a fro m o n e o r m o re e xis t in g t ab le s . It's a w a y to p re s en t a s p ec ific s u b s e t o f d a ta
o r a c u s to m iz ed p e rs p e c t iv e o f th e d at a t o u s ers w ith o u t c h a n g in g th e u n d erly in g t ab le s . V iew s a llo w
u s e rs to q u e ry a n d m a nip u la te d at a a s if t he y w ere w o rk in g w it h a re g u la r t a b le.
C re at ing a V ie w : T o c re a te a vie w , yo u d efin e a q u ery t h at s p ec ifies t he d a ta yo u w a n t th e v iew to
d is p la y. H ere's a s im p le ex a m p le u s in g a h yp o th et ic a l "S tu d e n ts " a n d "C o u rs e s " ta b le :
L et's s a y yo u h a ve tw o ta b les :
● St ud en ts (St u d en t_ ID , St ud en t_ N a m e, A g e)
● Co urs es (C o u rs e_ ID , Co urs e_ N a m e, In s truc to r)
U p d at ing a V ie w : Y es , yo u c a n up d a te a vie w , b u t th e re a re s o m e res tric tio n s . Y o u c a n p erfo rm
th e fo llo w ing o p erat io n s o n a vie w :
● In s ert ne w ro w s us in g th e v iew .
● U p d a te e xis tin g ro w s u s in g th e v ie w .
● De lete ro w s u s in g t he view .
H o w e ve r, th ere a re lim ita tio n s b a s ed o n t he c o m p le xit y o f t h e vie w a n d th e w a y it 's d efin ed . F o r
e xa m p le :
● If th e v iew inv o lv es m u ltip le ta b le s , yo u m ig h t o n ly b e a b le t o u p d a te ro w s fro m o n e ta b le .
● V iew s
w it h c o m p le x c a lc u la tio n s o r s u b q u e ries m ig h t n o t b e u p d a ta b le. ● S o m e D B M Ss
req uire th a t th e v iew 's d e fin itio n a d h ere s to s p e c ific c rit eria fo r u p d a te s t o b e a llo w ed .
W h y N o t A lw a ys U p d a tea b le :
V iew s m ig h t n o t a lw ay s b e u p d a te ab le d u e to t he c o m p le xit y o f t h e u nd erlyin g d a ta re la t io ns h ip s . T h e
DB M S n e ed s to e n s ure th a t u p d a t es m a d e t hro u g h th e vie w c an b e a c c u ra te ly tra n s la ted b ac k to th e
un d e rlyin g ta b les w ith o u t a m b ig u ity. W h en th e v iew 's d efin itio n is t o o c o m p lex o r inv o lv es m u ltip le
ta b les in in tric a te w a ys , t h e D B M S m ig h t p re ve n t u p d a te s t o m a int a in d a ta in teg rity.

Q.9] Defined stored procedure. Explain the creating and calling stored procedure with example.

AN S : St o re d P ro c ed ure:
A s to red p ro c e d u re is a s et o f p re-w rit ten S Q L s t a tem en ts th a t a re s to red in t h e d a ta b a s e a n d c a n b e
ex ec ut ed a s a s in g le u n it. T he y c a n p erfo rm s p e c ific ta s k s , p ro c e s s d at a , o r ret riev e in fo rm a tio n .
St o re d p ro c ed ures p ro vid e reu s a b ility, s e c u rit y, a n d a w a y to e n c a p s u lat e c o m p lex o p era tio n s .
Crea tin g a St o re d P ro c ed ure:
T o c rea te a s t o re d p ro c ed u re, y o u d efine a s e ries o f SQ L s t at em e n ts a nd g iv e it a na m e . H ere's a
s im p le ex am p le o f c re a tin g a s t o re d p ro c ed u re th a t re trie ve s in fo rm at io n a b o ut s tu d en t s :
CR E AT E P R O C E D U R E G e tS tu d en t In fo
AS
B E G IN
S E L E C T S tu d en t _N a m e , A g e F R O M St u d en ts ;
EN D ;
I n t his ex a m p le, th e s t o re d p ro c ed u re "G et St ud en tIn fo " d o es n 't t a k e a ny in p u t p a ra m e te rs . W he n
ex ec u t ed , it w ill ret riev e t he na m e s an d a g e s o f a ll s t u d en ts fro m t h e "S tu d e nt s " ta b le .
Ca lling a S to red P ro c e d u re :
Y o u c a n c a ll a s to red p ro c e d u re t o ex ec u t e its p re d efin ed s e t o f SQ L s ta t em e nt s . H ere 's h o w y o u
m ig h t c a ll th e "G et St u d en tI nfo " s to red p ro c e d u re: W h e n yo u run th is c o m m a nd , th e s t o re d p ro c ed ure
w ill ex ec ut e its S E LE C T s t a tem en t, an d t he res u lt (s tu d e n t n am es a nd a g e s ) w ill b e d is p la yed .
E x am p le Sc en a rio :
Im a g in e yo u h a ve a " Sa les " t a b le, a n d yo u w a nt to c rea te a s t o re d p ro c ed u re th a t c a lc u la tes t he to t al
s a le s fo r a s p ec ific p ro d u c t :
CR E AT E P R O C E D U R E Ca lc u la te To t a lS a les
@ P ro d u c t ID IN T
AS
B E G IN
S E L E C T S U M (S a leA m o u n t) A S T o ta lSa les
F R O M Sa les
W H E R E P ro d u c t_ ID = @ P ro d u c t ID ;
EN D ;
T o c a ll t h is s t o re d p ro c ed u re a nd g et t he to t al s a les fo r a s p ec ific p ro d u c t (e .g ., P ro d u c tI D = 1 0 1 ):
O U T P U T:
E X E C Ca lc u la te To t a lS a les @ P ro d uc tID = 1 0 1 ;

Q.10] Consider following schema. Student_fee_details (rollno, name, fee_deposited, date) Write a
trigger to preserve old values of student fee details before updating in the table.
A N S : I n S Q L , yo u c an c rea te a t rig g e r to p res e rv e o ld va lu es o f s tu d e nt fe e d e ta ils b e fo re u p d a t in g t he
ta b le. H e re's a n ex a m p le trig g er th a t d o e s t h at : C R E A T E TR IG G E R P re s erve Fe eH is to ry
O N S tu d en t _fe e_ d et a ils
AF T E R U P D A TE
AS
B E G IN
IN SE R T IN T O F ee U p d a te H is t o ry (ro lln o , n am e, o ld _fe e_ d ep o s ited ,
n ew _ fee _d ep o s it ed , up d a te_ d a t e)
S E L E C T i.ro lln o , i. na m e , d . fe e_ d e p o s ite d , i.fee _ d ep o s it ed , G E TD A TE () FR O M in s ert ed
i
I N N E R JO IN d e le te d d O N i.ro lln o = d .ro lln o ;
EN D ;
I n t his ex a m p le:
● P re s erve Fe eH is to ry is th e n a m e o f th e t rig g e r.
● St ud en t_ fee _d eta ils is t he na m e o f t h e ta b le yo u w a n t to t ra c k u p d a te s fo r. ● F eeU p d at eH is to ry
is a h yp o t he tic a l ta b le w h ere yo u s to re th e h is to ry o f fee u p d a te s (y o u n e ed to c re at e th is ta b le
s ep a ra t ely).
Th is trig g er fire s a fte r a n u p d at e o p e ra tio n (A FT E R U P D A TE ) o n th e S tu d e nt _ fe e_ d e ta ils t ab le . It in s erts
a n ew rec o rd in to t h e F eeU p d at eH is to ry ta b le w ith info rm a tio n a b o u t th e o ld a n d n ew fee v a lu e s , a lo n g
w ith th e d a t e o f th e u p d a te . Th e in s erted a n d d e le te d p s eu d o -ta b les a re u s e d to a c c es s th e n ew a n d
o ld v a lu e s a ffec ted b y th e u p d a te o p era tio n .

Q.11] Consider the following schemes


Supplier(SNO, Sname, Status, City)
Parts (PNO, Pname, Color, Weight, City)
Shipments(SNO,PNO,QTY)
Write SQL queries for the following:
i) Find shipment information (SNO, Sname, PNO, Pname, QTY) for those having quantity less than
157.
ii) List SNO, Sname, PNO, Pname for those suppliers who made shipments of parts whose quantity is
larger than the average quantity iii) Find aggregate quantity of PNO 1692 of color green for which
shipments made by supplier number who residing Mumbai

AN S :
i) F in d s h ip m en t in fo rm a t io n (S N O , Sn a m e, P N O , P na m e , Q TY ) fo r t ho s e h av in g q ua n tit y le s s th a n 1 5 7 :
sql
S E L E C T S .SN O , S .S n am e, S H .P N O , P .P na m e , S H .Q TY
F R O M Su p p lie r S
J O I N S h ip m en t s S H O N S .S N O = SH .S N O
J O I N P a rts P O N SH . P N O = P . P N O
W H E R E S H .Q TY < 1 5 7 ;
ii) L is t SN O , S n am e, P N O , P n a m e fo r t h o s e s u p p liers w h o m a d e s h ip m e n ts o f p arts w h o s e q u a n tity is
la rg e r th a n t h e a ve ra g e q u a n tity : s q l
S E L E C T S .SN O , S .S n am e, S H .P N O , P .P na m e
F R O M Su p p lie r S
J O I N S h ip m en t s S H O N S .S N O = SH .S N O
J O I N P a rts P O N SH . P N O = P . P N O
W H E R E S H .Q TY > (S E LE CT A V G (Q T Y ) F R O M Sh ip m e nt s );
iii) F ind a g g reg at e q u a n tity o f P N O 1 6 9 2 o f c o lo r g reen fo r w h ic h s hip m en ts m a d e b y s u p p lie r n um b er
w h o res id e s in M u m b a i:
sql
S E L E C T S U M (S H .Q TY ) A S A g g reg a t eQ ua n tit y
F R O M Sh ip m e n ts SH
J O I N S u p p lier S O N S H .S N O = S.S N O
J O I N P a rts P O N SH . P N O = P . P N O
W H E R E P .P N O = 1 6 9 2 A N D P .C o lo r = 'g ree n ' AN D S .C it y = 'M u m b a i'; P lea s e n o te th a t in t he s e
q u erie s , I a s s u m ed t ha t t h e re la t io ns h ip s b e tw ee n t he t a b les a re a s fo llo w s :
● Su p p lier: S N O is th e p rim a ry k e y.
● P a rt s : P N O is th e p rim ary k ey.
● Sh ip m e n ts : SN O an d P N O a re fo reig n k eys referrin g to th e S u p p lier a n d P a rt s ta b les
res p e c t iv ely.
Q .1 2 ] W h a t is an in d ex ? W h a t a re th e a d v an t ag es a nd d is a d v a n ta g es o f u s ing in d ex o n a
ta b le?
A N S: In d e x: A n in d ex in a d a ta b a s e is lik e an o rg a niz ed lis t o f p o in te rs th a t h elp s th e d a t ab as e
m a na g e m en t s y s tem (D B M S) q u ic k ly lo c a te s p ec ific ro w s in a t a b le. It's s im ila r to a n ind ex in a b o o k
th a t h elp s yo u fin d in fo rm a t io n fa s te r b y re ferring t o p a g e n u m b ers .
Ad va n ta g e s o f U s in g In d ex es :
1 . F a s ter D at a R etriev al: In d e xe s allo w th e DB M S t o q u ic k ly fin d th e ro w s th a t m a tc h c erta in
c o n d itio n s in a q ue ry. W ith o u t in d e xe s , t h e D B M S w o u ld n eed t o s c a n th e en tire ta b le, w h ic h c a n
b e s lo w fo r la rg e ta b le s .
2 . Im p ro v ed Q u ery P erfo rm a n c e : Q u erie s t h at in v o lve filte rin g , s o rtin g , o r jo in in g d a ta are m u c h
fa s ter w h e n in d ex es a re us e d . T he y h elp re d u c e t he a m o un t o f d a t a t he DB M S n e ed s to s e a rc h
th ro u g h .
3 . E ffic ien t D a ta M o d ific a tio n s : W h ile ind ex es s p ee d u p d a ta ret riev a l, th e y a ls o m a k e d a t a
m o d ific a tio n s (in s erts , up d a tes , d ele tes ) s lig h tly s lo w er. H o w e ve r, t h e o v era ll b en efit o f fa s ter
q ue ries u s u a lly o u t w e ig h s t h e s m a ll s lo w d o w n in m o d ific a t io ns .
4 . D at a I nt eg rity: In d ex es c an en fo rc e d a t a in te g rity b y en s u rin g th a t c e rta in c o lu m n s h a v e u niq u e
v alu es o r b y en fo rc ing c o n s train ts .
5 . O p t im ize d Jo in s : In d ex es o n c o lu m n s u s ed fo r jo in in g t ab le s c a n s ig n ific a nt ly
s p eed u p q u e ries th a t in vo lv e m u ltip le t a b les .
D is a d v an t ag es o f U s in g In d e xe s :

1 .In c rea s ed S to ra g e S p a c e: In d e xe s t a k e u p ad d itio n a l s t o ra g e s p ac e o n d is k . Fo r ve ry la rg e ta b les ,


t h is c an b e c o m e a c o n c ern .
2 .Slo w e r D at a M o d ific a tio n s : A s m e nt io n ed ea rlie r, w h ile ins e rt s , u p d a te s , a n d d elet es a re fa s te r
w ith o u t in d ex es , th ey c a n b e s lig h t ly s lo w e r w it h in d e xe s d ue t o th e e xt ra w o rk re q u ired t o m a in t ain
in d ex s truc tu re s .
3 .M a int en a n c e O ve rh ea d : In d e xe s n e ed to b e u p d a te d w h e ne ve r th e d at a c ha n g e s , w h ic h a d d s
o ve rh ea d t o d a ta m o d ific a tio n o p e ra tio n s . 4 .C o m p le xity : M a n a g in g in d e xe s an d d e c id in g w hic h
c o lu m n s to in d ex req uires ex p ertis e a n d c a re fu l c o n s id e ra tio n . P o o rly c h o s e n in d e xe s c a n a c t u ally
s lo w d o w n q u erie s .
5 . Se le c t iv ity Im p a c t: In d e xe s a re m o s t e ffe c t iv e o n c o lu m n s w ith hig h s elec tiv it y (c o lu m n s w it h
m a n y d is tin c t v a lue s ). In d ex es o n c o lu m n s w it h lo w s elec tiv it y m ig h t n o t p ro v id e a s m uc h
p e rfo rm an c e im p ro ve m en t.

Q.13] What is a trigger? How to create it? Discuss various types of triggers.

A N S: Trig g er:
A trig g er in a d a t ab as e is lik e a n a u to m a tic re s p o n s e t o an ev en t , s im ilar to a " trig g er" in t he rea l w o rld
t h at s ets o ff a n a c t io n w h en a s p ec ific c o n d itio n is m e t.
In a d a ta b a s e, a t rig g e r is a p red e fin ed s e t o f ins t ru c t io n s t h at are a u to m a t ic a lly ex ec u ted w h en
c ert a in ev en t s (lik e a n in s ert, up d a te, o r d e lete ) o c c u r o n a ta b le.

Crea tin g a Trig g er:


T o c rea te a trig g er, yo u s p e c ify th e e ve nt th a t s h o u ld t rig g e r th e a c tio n , d e fin e t he ac tio n its elf, a n d
a s s o c iat e it w ith a t ab le . H ere's a s im p le ex a m p le o f c re a tin g a t rig g e r th a t u p d a te s a
" La s tM o d ifie d " t im es ta m p w h en e ve r a ro w is u p d a te d in t he "E m p lo yee s " ta b le:
sql
CR E AT E T R IG G E R U p d a te La s tM o d ifie d
O N E m p lo yee s
AF T E R U P D A TE
AS
B E G IN
U P DA T E E m p lo y ees
S E T La s tM o d ifie d = G E T DA T E ()
W H E R E E m p lo y ee_ ID IN (SE LE C T E m p lo ye e_ ID FR O M in s ert ed ); E N D ;
In th is ex am p le, t h e trig g er is n a m ed " U p d a teL a s tM o d ified ." It 's s et t o fire a ft er a n up d a te o p era t io n
(A FT E R U P D A TE ) o n th e " E m p lo y ees " ta b le. T h e trig g er u p d a t es th e " La s tM o d ifie d " c o lu m n w ith th e
c u rren t d at e a n d tim e w h en ev er a ro w is u p d a te d .

T yp es o f T rig g e rs :
1 .B efo re Trig g ers : T h es e t rig g e rs fire b e fo re t h e ev en t o c c u rs (e. g ., b efo re an ins e rt , u p d a te , o r
d e lete o p era tio n ). Th ey c a n b e us e d to v a lid a t e d a ta o r m o d ify v a lu e s b efo re th ey a re ac tu a lly
c ha n g e d .
2 .A fter Trig g ers : T h es e t rig g e rs fire a fter th e ev en t h a s o c c u rre d . T he y a re o ft en u s e d fo r a ud it ing ,
lo g g ing , o r p erfo rm in g ad d itio n a l a c t io n s a fte r d a ta h a s b een m o d ified .
3 .I ns t ea d O f T rig g e rs : Th e s e trig g ers rep la c e th e s ta n d a rd a c tio n th a t w o u ld o c c u r d u e t o a n e ve nt .
T he y a re o ft en us e d to c us t o m iz e th e b e h av io r o f c e rt ain ac tio n s .

You might also like