database management system notes
TRANSCRIPT
-
8/11/2019 Database Management System Notes
1/16
DATA BASE MANAGEMENT SYSTEM
SQL: -SQL is fourth generation non-procedural language. The SQL is further dividedinto three sub-languages for various purposes: -
1. DDL ( Data Definition Language)2. DML ( Data Manipulation Language)3. DCL ( Data Control Language)
1. Data Definition Language (DDL): - DDL provide folloing co!!ands forvarious purposes: -
a. Ceate: - Create reserved "e#ord is provided b# DDL to create tables$vies$ etc.
!. A"te: - %lter reserved "e#ord is provided b# DDL to !odif# e&istingstructure of the table.
#. Do$: - Drop reserved "e#ord is provided b# DDL to drop the e&istingstructure.
2. Data Mani$u"ation Language (DML): -DML provides co!!ands for variouspurposes as follos: -
a. %n&et: -'nsert reserved "e#ord is provided b# DML to insert records in thee&isting tables.
!. Se"e#t: -Select reserved "e#ord is provided b# DML to select data fro!
the data tables.
#. '$ate: - pdate reserved "e#ord is provided b# DML to update$ or!odif# e&isting data in the database.
. De"ete: -Delete reserved "e#ord is provided b# DML to delete e&istingrecord fro! the database.
3. Data Conto" Language (DCL):-DCL provide folloing co!!and to control: -
a. o""!a#*&.
!. Co++it.#. Sa,e ointetc.
Data Definition Language (DDL): -1. Ceate: - Data Definition Language provides reserved "e#ord #eate to
create tables$ vies$ database etc.Snta/: -
Ceate ta!"e 0Ta!"e na+e(
w.jntuworld.com
www.jntuworld.com
www.jw
-
8/11/2019 Database Management System Notes
2/16
Co"u+n1 na+e ata-t$e 0#on&taintCo"u+n2 na+e ata-t$e 0#on&taintCo"u+n3 na+e ata-t$e 0#on&taint
)
Note: -Constraint is optional$ and is restrictions.
Data T$e&: -
1. C4a (5it4): -This data t#pe is used to accept characters.Note: -
Ma&i!u! character alloed is **.
+# default if no idth specified default one (,) is ta"en intoconsideration.
't is a fi&ed length data t#pe.
Eg.Ceate ta!"e e+$"oee
(Na+e #4a (36)
)
2.7a#4a2 (5it4): -'t is a var#ing length data t#pe.Note: -Ma&i!u! characters can be stored.Eg.
Ceate ta!"e e+$"oee
(Na+e ,a#4a2 (36)
)
3.Nu+!e (8it4 e#i+a" o $oint): -'t provide support to all t#pes of nu!ericvalues.Note: -
Ma&i!u! digits allo.
+# default if no idth specified it is ta"en eight (/).
+# default if no point specified 0ero () is ta"en into consideration.
Eg.a.1u!ber (2.)
9ut$ut: - .
!.1u!ber (*.) or 1u!ber (*)3utput: - 44444
w.jntuworld.com
www.jntuworld.com
www.jw
-
8/11/2019 Database Management System Notes
3/16
Q. Create a table na!e student having folloing colu!n: -5!plo#ee no.1a!eSalar#
6ost%ns: -Create table e!plo#ee
(e!plo#ee7no varchar (8)$1a!e varchar (8)$Salar# nu!ber (*$ )$6ost varchar ()
)9
Q. Create a table na!e directors having folloing colu!n: -
Director idDirector first na!eDirector last na!eDirector cit#
%ns: -Create table directors
(Did varchar (,)$Dfna!e varchar ()$Dlna!e varchar (,*)$Dcit# varchar (8)
)9
Con&taint&: -1. 13T 1LL.2. 6ri!ar# e#.3. Chec" Constant.;. ;oreign e#. Note: - 'f 13T 1LL e&plicitl# added ith colu!n na!e default null is ta"en intoconsideration.
w.jntuworld.com
www.jntuworld.com
www.jw
-
8/11/2019 Database Management System Notes
4/16
Eg.Create table e!plo#ee
(5!plo#ee-no nu!ber ()$5!plo#ee-na!e varchar (8) 13T 1LL
)9
2.i+a ?e: - egno nu!ber (,*) constraint regnop" pri!ar# "e#$1a!e varchar (8) 13T 1LL$Class nu!ber (*) 13T 1LL$%ddress varchar (8)
)9
3. C4e#*: -
-
8/11/2019 Database Management System Notes
5/16
Snta/:-1. Co"u+n-na+e ata-t$e #4e#* (#o"u+n o$eato ,a"ue)
92. Co"u+n-na+e ata-t$e #on&taint 0e&ti#tion-na+e
C4e#* (#o"u+n o$eato ,a"ue) 9$eato u&e in SQL: -
1. e"ationa" 9$eato.2. Logi#a" 9$eato.3. S$e#ia" 9$eato.
a. %nan not in.b. Bet8eenan not !et8een.c. Li*ean not "i*e.
1. e"ationa" o$eato o #o+$ai&on o$eato: - This operator are used toco!pare to values folloing operator provided to co!pare values: -A. ? (@reater than).B. A (Less than).C. ?B (@reater than or eual to).D. AB (Less than or eual to).E. $ (A? ) (1ot eual to).@. B (5ual to)
e&ti#tion !a&e on e"ationa" o$eato: -Q.To create a table na!e e!plo#ee having folloing colu!n and restrictions: -
5!pno 6ri!ar# "e#.
5!pna!e 13T 1LL.6ost 13T 1LL.Salar# Chec".
%ns: -Create table e!plo#ee
(5!pno nu!ber (,*) constraint e!pnop" pri!ar#
"e#$5!pna!e varchar (8) 13T 1LL$6ost varchar (,) 13T 1LL$
Salar# nu!ber (*) constrain salar#ch"chec" (salar#?)
)9
2. Logi#a" 9$eato: - these operators are used to co!bine !ore than oneconditional e&pression. ;olloing logical operators provided b# SQL: -
a. AND: - 't returns true if both the condition !et true$ otherisefalse. 't is +inar# 3perator.
w.jntuworld.com
www.jntuworld.com
www.jw
-
8/11/2019 Database Management System Notes
6/16
!. 9:- 't returns true$ if either one of the condition !et true other-ise it returns false. 't is also a binar# operator.
#. N9T:- 't reserves the pulses that is true to false and vice-versa. 'tis also a binar# operator.
C4e#* #on&taint !a&e on Logi#a" o$eato: -Q.To create a table na!e e!plo#ee having folloing colu!n and restrictions: -5!pno pri!ar# "e# chec" !ust beteen 4 to 44445!pna!e 13T 1LL6ost progra!!er$ e&ecutive$ cler"Salar# !ust be greater than and less than *.
An&: -Create table e!plo#ee
(5!pno nu!ber (,*) constraint e!pnop" pri!ar#
"e# Chec" (5!pno?B4 and 5!pnoAB*)$5!pna!e varchar (8) 13T 1LL$6ost varchar (,*) constraint postch"
Chec" (postBEprogE or postBEe&ecutiveE orpostBEcler"E)$
Salar# nu!ber (4) constraint salch"Chec" (salar#?B and salar#AB*)
)9Q. Create a table !ovie as follos: -
Mid pri!ar# "e# !id !ust beteen M, to M4
Mna!e 13T 1LLCategor# %ction$ Thriller$ Dra!aCost !ust be either AB or ?B*.
An&: -Create table !ovies
(Mid varchar (,) constraint !idp"
Chec" (!id?BEM,E and !idABEM4E)$Mna!e varchar (,*) 13T 1LL$Categor# varchar (*) constraint catch"
Chec" (categor#BE%ctionE or categor#BEThrillerE or
categor#BEDra!aE)$Cost nu!ber (,*) constraint costch"
Chec" (costAB or cost?B*))9
3. S$e#ia" 9$eato: -
a. %N 9$eato: -This operator is used to restrict specified ithin parenthesis.Snta/: -
Co"u+n ata-t$e #on&taint 0e&ti#tion-na+e
w.jntuworld.com
www.jntuworld.com
www.jw
-
8/11/2019 Database Management System Notes
7/16
C4e#* (Co"u+n-na+e in (7a"ue1 7a"ue2 7a"ue3 ))>Note&: -a. 'f specified value are string t#pe it !ust enclosed ithin single uote (FF).!. 1o uote (FF) reuire to enclosed nu!ber value.#. G'1H operator acts inside as or t#pe.
C4e#* #on&taint !a&e on in an not in o$eato: -Q. Create a table na!e student having folloing colu!n and restrictions: -
>egno pri!ar# "e# A/ and ?Sna!e 13T 1LLClass ,$$8$Se& !ale$ fe!ale
An&: -Create table student
(
>egno nu!ber (,*) constraint regnop"pri!ar# "e#Chec" (regno? and regnoA/)$
Sna!e varchar (,*) 13T 1LL$Class varchar (,*) constraint classch"
Chec" (class in (,$ $ 8$ ))$Se& varchar (,*) constraint se&ch"
Chec" (se& in (F!ailE$ Efe!aleE)))9
!. N9T %N o$eato: -This operator is used to restrict values based on values
specified ithin parenthesis that is9 it accepts value that not !atched ith thevalues specified.
Snta/:-Co"u+n ata-t$e #on&taint 0e&ti#tion-na+e
C4e#* (#o"u+n not in (,a"ue1 ,a"ue2 ))Note: -
1. 'f specified values are of charIvarchar t#pe$ that is !ust enclosed ithinthe single uotes.
2. 1o uotes reuired for nu!eric values.3. 13T '1 operator internall# or"s as %1D operator.
C4e#* #on&taint !a&e on %N an N9T %N o$eato: -
Q. Create a table na!e directors having folloing colu!ns and restrictions: -Did pri!ar# "e# did !ust be ?BD, and ABD444Dfna!e 13T 1LLDlna!e !ust be @hai$ Jer!a$ or Shar!aDcit# not 6atna$ 6une$ +angalore
w.jntuworld.com
www.jntuworld.com
www.jw
-
8/11/2019 Database Management System Notes
8/16
An&: -Create table directors
(Did varchar (,*) constraint didp" pri!ar# "e#
Chec" (Did?BED,E and DidABED444E)$
Dfna!e varchar (8) 13T 1LL$Dlna!e varchar () constraint dlna!ech"Chec" (Dlna!e in (F@haiE$ FJer!aE$ FShar!aE))$
Dcit# varchar (8) constraint dcit#ch"Chec" (Dcit# not in (F6atnaE$ F6uneE$ F+angaloreE))
)9
#. BET5EEN an N9T BET5EEN o$eato: -+eteen operators is used in SQL 6LS to select values based on ranges
specified including initial and final li!it.Snta/: -
Co"u+n ata-t$e #4e#* (#o"u+n-na+e !et8een initia" an fina""i+it)13T +5Tegno pri!ar# "e# chec" ?B4 and AB4444S5M '$ or ''$ or '''
Sub, !ar"s ithin (-,)Sub !ar"s !ust be ?B and ABTotal !ust be either ? and A8
An&: -Create table result
(regno nu!ber (*) constraint regnop" pri!ar# "e#
Chec" (regno beteen ?B4 and AB4444)$S5M varchar (*) constraint se!ch"
Chec" (S5M in (F'E$ F''E$ F'''E))$Sub, nu!ber (8) constraint sub,ch"
Chec" (Sub, beteen and ,)$Sub nu!ber (8) constraint subch"
Chec" (Sub beteen and )$Total nu!ber (*) constraint totalch"
Chec" (Total beteen and 8))9. L%?E o$eato: -This operator in SQL is used to restrict value based on 6attern
!atching. 'n order to generate pattern SQL provide toild-card characters as follos.1. F: - 't !atches 0ero$ one$ or !ore than one character.
w.jntuworld.com
www.jntuworld.com
www.jw
-
8/11/2019 Database Management System Notes
9/16
Eg: -DF B? D$ Do$ Dabc$ are true.
doo,$ %, are false.EF? B? 5$ 5oo$ 5o are true.
5"$ 5o, are false
2. ('neoe): - 't !atches e&actl# one character (reuired)Eg: -D H D,$ D%+C are true.
D,$ d5ad are false.Snta/: -
Co"u+n ata-t$e #on&taint 0e&ti#tion-na+e #4e#* (#o"u+n"i*e $atten)C4e#* #on&taint !a&e on $atten ("i*e) +at#4ingQ.To create a table na!e directors having folloing colu!ns and restrictions: -
Did pri!ar# "e# chec" directorEs id !ust start ith FDEfolloed b#
an# seuence of characters but !ust end ithF"E.Dfna!e 13T 1LLDlna!e chec" Dlna!e !ust be either u!ar$ Shar!a$
Singh$ Jer!a.Dcit# chec" cit# of directors !ust start ith M$ $ and C
An&: -Create table directors
(Did nu!ber (/) constraint didp" pri!ar# "e#
Chec" (Did li"e (FDK"E))$
Dfna!e varchar (8) 13T 1LL$Dlna!e varchar (,*) constraint dlna!ech"Chec" (Dlna!e in (Fu!arE$ FShar!aE$ FSinghE$
FJer!aE))$Dcit# varchar (8) constraint dcit#ch"
Chec" (Dcit# FMKE or$ Dcit# FKE or$ Dcit# FC KE))9
e. N9T L%?E o$eato: -This operator in SQL is used to accept all values other thanthe specified one.Snta/: -
Co"u+n ata-t$e #on&taint 0e&ti#tion-na+e #4e#* (not "i*e
$atten)
Q.To create a table na!e !ovies having folloing colu!ns and restrictions: -Mid pri!ar# "e# chec" !ovie id !ust start ith three
charactersfor!ed b# an# characters but !ust end ith S.
Mna!e !ust be an# value e&cept !ovie na!e lastcharacters !ust not be $ M$ or 1.
w.jntuworld.com
www.jntuworld.com
www.jw
-
8/11/2019 Database Management System Notes
10/16
Categor# !ust not be thriller and suspense.Cost !ust lies ithin range to 8*.
An&: -Create table !ovies
(
Mid varchar (,) constraint !idp" pri!ar# "e#Chec" (Mid li"e F7 7 7 KSE)$Mna!e varchar (8) constraint !na!ech"
Chec" (Mna!e not li"e FKE$ FKME$ FK1E)$Categor# varchar (,*) constraint categor#ch"
Chec" (Categor# in (FSuspenseE$ FThrillerE))$Cost nu!ber (,) constraint costch"
Chec" (Cost beteen and 8*))9
Se#on +et4o fo #eating ta!"e: -Q.To create a table na!e !ovies having folloing colu!n and restrictions: -
Mid pri!ar# "e# !ust be ?BEM,E and !ust be ABEM4EMna!e 13T 1LLCategor# %ction$ Dra!a$ SuspenseCost !ust lies ithin range and *
An&: -Create table !ovies(
Mid varchar (,)$Mna!e varchar (8) 13T 1LL$Categor# varchar (,)$Cost nu!ber ()$
Constraint !idp" pri!ar# "e# (M'D)$Constraint !idch" chec" (Mid ?BEM,E and Mid ABEM4E)$Constraint catch" chec" (Categor# in (F%ctionE$ EDra!aE$ EThrillerE)$Constraint costch" chec" (Cost beteen and *)
)9
%dd !ore Questions
@oeign ?e o efeen#e&: -
-
8/11/2019 Database Management System Notes
11/16
@oeign *e 0Co"u+n efeen#e& 0Ma&te-Ta!"e(Co"u+n)
Q. (B @i&t +et4o)
%ns: -Create table !ovies(Mid varchar (,) constraint !idp" pri!ar# "e#
Chec" (!id li"e FM 7 7 7 7F)$Mna!e varchar (8) 13T 1LL$Categor# varchar () constraint catch"
Chec" (Categor# '1 (F%ctionE$ FDra!aE$ FSuspenseE))$Did varchar (,) constraint didf" >eferences directors (Did))9
B Se#on Met4o: -Create table !ovies(
Mid varchar (,)$Mna!e varchar (8) 13T 1LL$Categor# varchar ()$Did varchar (,)$Constraint Midp" pri!ar# "e# (Mid)$Constraint Midch" Chec" (Mid Li"e FM 7 7 7 7E)$Constraint Catch" Chec" (Categor# '1 (FDra!aE$ F%ctionE$
FSuspenseE)$
Constraint didf" ;oreign e# (Did) >eferences Directors (Did))9
Q.An&: -
Create Table directors(
Did Jarchar (,)$Dfna!e Jarchar () 13T 1LL$Dlna!e Jarchar (,)$Dcit# Jarchar (8)$
Constraint Didp" 6ri!ar# "e# (Did)$Constraint Didch" Chec" (Did Li"e FDKME)$Constraint Dcit#ch" Chec" (Dcit# '1 (FMu!baiE$ FChennaiE$ Fol"ataE))
)9
Create Table Movies(
Mid Jarchar (,)$Mna!e Jarchar (8) 13T 1LL$
w.jntuworld.com
www.jntuworld.com
www.jw
-
8/11/2019 Database Management System Notes
12/16
Categor# Jarchar ()$Cost 1u!ber (,)$Constraint Midp" 6ri!ar# e# (Mid)$Constraint Midch" Chec" (Mid FM 7 7 7 7E)$Constraint Catch" Chec" (Categor# 13T '1 (FSuspenseE$ FThrillerE))$
Constraint Costch" Chec" (Cost +eteen and *)$Constraint Didf" ;oreign e# (Did) >eferences Directors (Did)31 D5L5T5 C%SC%D5
)9
A"te Ta!"e: -A"te Co++an: - SQL provide alter co!!and to change or !odif# e&istingstructure of the table.Snta/: -
A"te Ta!"e 0Ta!"e Na+eAIMoifIDo$ 0State+entSQL provides three reserved "e# ords that is add$ drop$ and !odif# to do thefolloing obs: -
1. A: - Ais used to add ne colu!ns in the e&isting table.2. Moif: - Moifis used ith a alter co!!and to change the data-t#pe of
e&isting colu!n.3. Do$: - Do$ is used ith the alter co!!and to drop the e&isting constraint.
N9TE: - %n 9ACLE Ji Do$ #an a"&o !e u&e to o$ t4e e/i&ting #o"u+n.1. ADD: - %dd reserved "e# ord used ith alter table co!!and to add ne colu!nto e&isting table.Snta/:-a. To a &ing"e #o"u+n: -
A"te Ta!"e 0Ta!"e-na+e A Co"u+n-na+e ata-t$e0e&ti#tion
!. To a +u"ti$"e #o"u+n&: -
A"te Ta!"e 0Ta!"e-na+eA (Co"u+n1 Data-t$e 0e&ti#tion Co"u+n2 )
Eg: -
Que: -To add colu!n dfna!e in e&isting table directors.An&: - %lter Table Directors %dd dfna!e varchar (,) 13T 1LL9
Que: -To add to colu!ns Dlna!e$ Dcit# in the e&isting table.An&: -
%lter Table Directors%dd (
w.jntuworld.com
www.jntuworld.com
www.jw
-
8/11/2019 Database Management System Notes
13/16
Dlna!e varchar ()$ Dcit# varchar (,) constraint dcit#ch"Chec" (Dcit# '1 (FMu!baiE$ FChennaiE$ Fol"ataE)))9
To a ne8 e&ti#tion u&ing A"te ta!"e #o++an: -Snta/: -
a. To a &ing"e #on&taint: -
A"te Ta!"e 0Ta!"e-na+e A 0e&ti#tion
!. To a +u"ti$"e #on&taint& on t4e &a+e ta!"e at a ti+e
A"te Ta!"e 0Ta!"e-na+e A (0e&ti#tion1 0e&ti#tion2 )
Note: -
1. 'f table is e!pt# !eans can not e&ist an#$ e can add an# ne colu!n ithrestriction or$ ithout restriction.2. 'f table consists record$ e can add ne colu!n ithout restriction.
-
8/11/2019 Database Management System Notes
14/16
Q. 54at ae t4e e&&entia" #o+$onent& of DBMSKAn&: -The !aor co!ponents of a D+MS are as follos: -
1. DML e-#o+$i"e: - 't converts DML state!ents e!bedded in an applicationprogra! to nor!al procedure calls$ in the host language.
2. DDL Co+$i"e:- The DDL co!piler converts the Data Definition state!entsinto a table.3. Data!a&e Manage: - % database !anager is a progra! or !odule hich
provide the folloing functions: -a. The database !anager is responsible for the storing$ retrieving and
updating of data in the database.!.The database !anager is responsible fro securit#$ bac"up and recover#
of database in case of crash or failure and also for integrit# enforce!ent.;. Que o#e&&o: - The uer# processor selects the best possible plan or
strateg# for evaluating and responding to a user uer#.
-
8/11/2019 Database Management System Notes
15/16
The uer# processor uses the data dictionar# to find the structure of therelevant portion of the database and uses this infor!ation to e&ecute the uer#.
Q. E/$"ain t4e iffeen#e !et8een t4e 4&i#a" an Logi#a" Data%ne$enen#eK
An&: -
Q. Co+$ae an #onta&t Taitiona" fi"e 7& Data!a&e a$$oa#4.An&: -'n a traditional a#$ such as in C3+3L$ the infor!ation is used to "eep in a file and to!anipulate files$ nu!ber of application progra!s developed and added.
%s file oriented approach has li!itation regarding sharing of data$ the onl# solutionto this is to create ne files hich records the desired infor!ation hich can be!anipulated b# the application developed b# the progra!!ers$ hich depends onthe files$ folloing are the drabac"s of the file oriented approach: -a. Data eunan# an %n#on&i&ten#: - Since$ file are created to get desired
infor!ation over a long period the various files are li"el# to have the sa!einfor!ation !a# be duplicated in several files. 5g: - The address and 6honenu!ber of the student !a# appear in a Stuent @i"e and Ma*& @i"e. Thisredundanc# leads to data inconsistenc#9 if a particular student address ischanged in Stuent @i"e hich not be reflected in Ma*& @i"e due to li!itationof file oriented approach.
b. oga+ o Data De$enen#:- 'n traditional file approach if a ne data filedis to be added to a particular file$ all such progra!s that access that particularfile ould have to be changed to allo for this ne field$ hich ould have beenadded to that particular file recentl#.
This shos the li!itation of file oriented approach$ here file depends on
progra! and progra! depends on file.These li!itations !otivate to use D+MS rather than traditional file approach.
w.jntuworld.com
www.jntuworld.com
www.jw
-
8/11/2019 Database Management System Notes
16/16
OBJECT ORIENTED DATABASE MANAGEMENT SYSTEM; -
Co++on te+& of an 9!e#t oiente $oga++ing "anguage: -1. 9!e#t: -The ter! obect !eans a co!bination of data or code and progra! or!ethod that represents so!e real orld entit#. 5g: - Consider an e!plo#ee na!ed
Jina# 8 #ears old and earn salar# >s. ////I7 then Jina# !a# be represented inco!puter progra! as an obect.The code or data part of this obect ould be 1a!e: - Jina#$ %ge: - / and
Salar#: /. The progra! or !ethod part of the obect !a# be collections ofprogra!s or !ethod i.e.9 input$ displa#$ update salar#$ etc. 'n the Jina# obect thena!e$ age and salar# are called attributes hold descriptive properties of the obectJina#.2. C"a&&: -%ll obect that have the sa!e attributes for the data part and sa!e progra! or!ethod part are collectivel# called a class or t#pe.
The ter! class refers to the collections of all obects that have sa!e attributes
and !ethods.5g: - Consider a class or t#pe na!ed 5!plo#ee consist attributes or codes of datana!e$ age$ e!pno$ and salar# to hold data and !ethods or progra! i.e. input$change$ displa# etc.
Creating three obects of e!plo#ee t#pe na!ed Jina#$ %rti$ and >upa! holdsthe sa!e attributes and !ethods define the class or t#pe e!plo#ee.
En#a$&u"ation: - 't is the !echanis! b# hich the data or code part ofan obect is so encapsulated that the user canEt see inside of obect butcan use the obect b# calling the progra! or !ethod part of the obect.
%n4eitan#e: - The ter! inheritance is so!e ti!e called re-uses i.e.codes ones defined is re-usable. 'nheritance is a process b# hich a ne
class or t#pe can be inherits fro! e&isting class.% class !a# inherit fro! one or !ore e&isting classes and the inheritance
structure is called an inheritance hierarch# or class hierarch#.
Q. Bief" iu&& t4e a"tenati,e
w.jntuworld.com www.jw