Vous êtes sur la page 1sur 13

K. Bhardwaj DBMS BSc.

4th (PCSC-401 ) 2014



Guru Ghasidas Universit Pa!e 1

Data"ase Mana!e#ent Sste#
Introduction
Concept of Database
Database: It is a collection of interrelated
data files/tables.
Table: It is collection of similar records.
Record: It is collection of meaningful
attribute values.
Attribute: Property of an entity.
Entity: Areal world obect w!ic! s!ould be
distinguis!ed from ot!ers.
Table 1. Sample table(relation/entity set)
"id "name #ar$s
"% Ram &'
"( #o!an )'
"& "ita *'
") Ravan '

T!e table consists of rows and columns.
Rows are called records or tuple or entity.
+olumn is called attribute or field.
DBMS Database #anagement system is a
software pac$age w!ic! enables user to
create and maintain a database .
T!e D,#" is !ence a general-purpose
software system t!at facilitates t!e
process of defining. constructing and
manipulating databases for various
applications.
E.g. /racle. Ingress. "ybase. Dbase &0.
1o2base. 1o2pro. #s access. Database.
Datafle2. "34 "erver etc.
Data: Raw fact.
Information: 5seful data.
Purpose of Database system
Database "ystem6Database 0 D,#"
In early days. database applications ware built
on top of file system.
1ollowing are t!e drawbac$ of using file
system to store data w!ic! can be overcome
by database system.
Data redundancy and inconsistency.
Duplication of same information at
several places are possible.
All copies may not be updated
properly.
Difficulty in accessing data
#ay !ave to write a new application
program to satisfy an unusual
re7uest.
E.g. 1ind all students wit! same
mar$s.
could generate t!is data manually.
but a tedious ob.
Data Isolation
Data in different files.
Data in different formats.
Difficult to write new application
programs.
#ultiple users
8ant concurrency for faster response
time
9eed protection for concurrent
updates.
E.g. two customers depositing funds
in t!e same account at t!e same time.
"ecurity problems
Every user of t!e system s!ould be
able to access only t!e data t!ey are
permitted to see.
K. Bhardwaj DBMS BSc. 4th (PCSC-401 ) 2014

Guru Ghasidas Universit Pa!e $

Difficult to inforce t!is wit!
application programs.
Integrity problems
Data may be re7uired to satisfy
constraints.
E.g. 9o account balance s!ould be
below Rs *''.
Again difficult to enforce or to c!ange
constraints wit! t!e file processing
approac!.
Atomicity of updates
1ailures may leave database in an
inconsistent state wit! partial update
carried out.
E.g. Transfer of funds from one
account to anot!er s!ould eit!er
complete or not !appen at all.
Functionality of a database system
"pecifying t!e database structure
data definition language
#anipulation of database
3uery processing and 7uery
optimisation.
Integrity enforcement
integrity constraints
+oncurrent control
multiple user environment.
+ras! recovery
"ecurity and aut!ori:ation.
Types of DBMS
"everal criteria can be used to classify D,#"s.
1ollowing are t!e criteria and types of D,#"
according to t!em.
%. Data #odel
%.%. Relational model
%.(. /bect data model
%.&. /bect-relational model
%.). 9etwor$ model
%.*. ;ierarc!ical model
#any current D,#"s use t!e relational data
model or obect data model. #any legacy
applications still run on database systems
based on !ierarc!ical and networ$ data
models. Relational D,#"s are e2tending t!eir
models to incorporate obect based concepts
and ot!er capabilities. T!ese systems are
referred to as obect-relational systems.
(. 9umber of users
(.%. "ingle user systems
(.(. #ultiuser systems
&. 9umber of sites
&.%. +entrali:ed D,#"
Data is stored at a single computer site.
&.(. Distributed D,#"
can !ave t!e actual database and
D,#" software distributed over
many sites. connected by a computer
networ$.
&.&. 1ederated D,#"
Participating database and D,#"s
are !eterogeneous. It<s a
combination of centrali:ed and
distributed D,#".
). +ost
*. Type of access pat!
=. Purpose
=.%. >eneral-purpose D,#"
=.(. "pecial-purpose D,#"
E.g. D,#" for airline reservation system.
DBMS Arcitecture
Database systems are usually partitioned into
two or t!ree parts as in fig %.

Figure 1 two-tier and three-tier arhiteture
In a two-tier arc!itecture. t!e application is
partitioned into a component t!at resides at
K. Bhardwaj DBMS BSc. 4th (PCSC-401 ) 2014

Guru Ghasidas Universit Pa!e %

t!e client mac!ine. w!ic! invo$es database
system functionality at t!e server mac!ine
t!roug! 7uery language statements.
In a t!ree-tier arc!itecture. t!e client mac!ine
acts as merely a front end and does not any
direct database calls. instead . t!e client end
communicates wit! an application server.
usually t!roug! a forms interface. T!e
application server in turn communicates wit!
a database system to access data. T!e
business logic of t!e application. w!ic! says
w!at actions to carry out under w!at
conditions. is embedded in t!e application
server. instead of being distributed across
multiple client. T!ree-tier applications are
more appropriate for large applications and
for t!e applications t!at run on 8orld 8ide
8eb.
!ie"s of Data
A maor purpose of a database system is to
provide users wit! an abstract view of data.
T!at is t!e system !ides certain detail of !ow
t!e data are stored and maintained.

Figure ! three le"els o# data abstration or "iews
$hysial le"el% ;ow t!e data are actually
stored.
&ogial le"el% 8!at data are stored in
database and w!at relations!ip e2ist among
t!ose data. T!us. t!e logical level describes
t!e entire database in terms of small number
of relatively simple structures.
'iew le"el% describes only a part of entire
database. T!e system may provide many
views of t!e same database.
Data &nde'endenc
T!e ability to modify a sc!eme definition in
one level wit!out affecting a sc!eme
definition in a !ig!er level is called data
independence.
T!ere are two $inds of data independency
%. P!ysical data independence
T!e ability to modify t!e p!ysical
sc!eme wit!out causing logical
sc!ema to be modified.
#odifications at t!is level are usually
to improve performance.
(. 4ogical data independence
T!e ability to modify conceptual
sc!ema wit!out causing any
modification on view level.
5sually done w!en logical structure of
database is altered.
Data Models
T!ese are t!e model t!at determines t!e
logical structure of a database and
fundamentally determines in w!ic! manner
data can be stored. organi:ed. and
manipulated. #ost popular data model for
database design is relational model.
1ollowing are some common data models.
%. 1lat file model
(. 9etwor$ data model
&. ;ierarc!ical data model
). Entity Relations!ip model
*. Relational data model
=. /bect data model
?. /bect-Relational data model
Flat #ile model% T!e database is a collection of
flat files. Data are stored in t!ese files w!ere
didn<t t!in$ about relations!ips. T!ese
K. Bhardwaj DBMS BSc. 4th (PCSC-401 ) 2014

Guru Ghasidas Universit Pa!e 4

relations!ips ware present but t!ey were not
named or maintained.
(etwor) data model% T!e networ$ database
is a collection of set occurrences. T!e two
basic data structuring concept are records and
sets. A set occurrence will !ave one owner
record and many member records.
*ierarhial model% T!e !ierarc!ical database
is a collection of tree occurrences. T!e two
data structuring concepts are record and
parent c!ild relations!ip @P+RA. An occurrence
of t!e P+R type consists of one record of t!e
parent record-type and a number of record of
c!ild record type.
A !ierarc!ical database sc!ema consists of a
number of !ierarc!ical sc!emas. Eac!
!ierarc!y consists of a number of record type
and P+R types.
+ntity-,elationship model% T!is is ust a
model in w!ic! we represent entity sets and
t!eir relations!ips t!roug! grap!ical
diagrams. Entity-sets are same as table or
record type. 8e will see t!is model in detail in
ne2t unit w!ere we will come across different
notations and t!eir plotting.
,elational data model% T!e relational
database is a collection of tables. T!ese tables
will be related to eac! ot!er wit! t!e !elp of
foreign $eys.
-b.et data model% T!e database is a
collection of obects. T!e relations!ip among
t!e obects is maintained using t!e foreign
$ey as attribute of t!e class built for record
type. T!e obect data model !as an advantage
of being able to store data li$e picture. sound
files or videos etc.
-b.et ,elational data model% T!e relational
data model is e2tended to incorporate t!e
features of obect data model so t!at picture
or suc! unstructured data could be stored
wit! t!e facilities of relational environment.
Data dictionary
T!e data dictionary is considered to be a
special type of table. w!ic! can only be
accessed and updated by database system
itself @not a regular userA. A database system
consults t!e data dictionary before reading
modifying actual data.
T!e output of DD4 is placed in t!e data
dictionary. w!ic! contains metadata. w!ic! is
data about data. 1ollowing are t!e data w!ic!
must be present in t!e data dictionary.
9ame of t!e relation.
9ame of t!e attribute of eac! relation
Domains and lengt!s of attributes.
9ame of views defined on t!e
database. and definition of t!ose
views.
Integrity constraints.
9ame of aut!ori:ed users.
Aut!ori:ation and accounting
information about users.
Password or ot!er information used
to aut!enticate users.
9umber of tuples in eac! relation.
#et!od of storage in eac! relation.
#ay also note t!e storage information
@se7uential !as! or !eapA of relation and t!e
relation w!ere eac! relation is stored.
#ay also store following.
9ame of inde2.
9ame of t!e raltion being inde2ed.
Attribute on w!ic! t!e inde2ing is
done.
Type of inde2 formed.

K. Bhardwaj DBMS BSc. 4th (PCSC-401 ) 2014

Guru Ghasidas Universit Pa!e (

SQL (Structured Query Language)
#ntroduction
T!e !istory of "34 begins in an I,# laboratory
in "an Bose. +alifornia. w!ere "34 was
developed in t!e late %C?'s. T!e initials stand
for "tructured 3uery 4anguage. and t!e
language itself is often referred to as Dse7uel.D
It was originally developed for I,#Es D,(
product @a relational database management
system. or RD,#". t!at can still be boug!t
today for various platforms and
environmentsA. In fact. "34 ma$es an RD,#"
possible. "34 is a nonprocedural language. in
contrast to t!e procedural or t!ird generation
languages @&>4sA suc! as +/,/4 and + t!at
!ad been created up to t!at time.
Nonprocedural means what rat!er t!an how.
1or e2ample. "34 describes w!at data to
retrieve. delete. or insert. rat!er t!an !ow to
perform t!e operation.
Types of lan$ua$e
Data Definition Language (DDL) statements
are used to define t!e database structure or
sc!ema. "ome e2amples:
+REATE - to create obects in t!e
database.
A4TER F alters structure of t!e database.
DR/P - delete obects from t!e database
TR59+ATE - remove all records from a
table. including all spaces allocated for t!e
records are removed.
+/##E9T - add comments to t!e data
dictionary.
RE9A#E - rename an obect.
Data Manipulation Language (DML)
statements are used for managing data
wit!in sc!ema obects.
"ome e2amples:
"E4E+T - Retrieve data from t!e database
I9"ERT - Insert data into a table.
5PDATE - 5pdates e2isting data wit!in a
table.
DE4ETE - deletes all or selected records
from a table. t!e space for t!e records
remain.
#ER>E - 5P"ERT operation @insert or
updateA
+A44 - +all a P4/"34 or Bava subprogram.
EGP4AI9 P4A9 - e2plain access pat! to
data.
4/+H TA,4E - control concurrency.
Data Control Language (DCL) statements
are used to manage t!e users aut!ority.
"ome e2amples:
>RA9T - gives userEs access privileges to
database.
REI/HE - wit!draw access privileges given
wit! t!e >RA9T command.
Transaction Control (TCL) statements are
used to manage t!e c!anges made by
D#4 statements. It allows statements to
be grouped toget!er into logical
transactions.
"ome e2amples:
+/##IT - save wor$ done.
"AIEP/I9T - identify a point in a
transaction to w!ic! you can later roll
bac$.
R/44,A+H - restore database to original
since t!e last +/##IT.
"ET TRA9"A+TI/9 - +!ange transaction
options li$e isolation level and w!at
rollbac$ segment to use.
Creatin$ a table
"34 provides +REATE TA,4E command using
w!ic! we can define structure of a table. Eac!
table column definition is a single clause in
t!e create table synta2 w!ic! is separated
from eac! ot!er by a comma. 1inally. t!e "34
statement is terminated wit! a semi colon.
"ynta2:
+REATE TA,4E JTable9ameK
@
Jattribute%K JdatatypeK@Jsi:eKA.
Jattribute(K JdatatypeK@Jsi:eKA.
:
.
JattributenK JdatatypeK@Jsi:eKA
AL
K. Bhardwaj DBMS BSc. 4th (PCSC-401 ) 2014

Guru Ghasidas Universit Pa!e )

Rule for creating table:
A name can !ave ma2imum up to &'
c!aracters.
Alp!abets from A-M. a-: and number from
'-C are allowed
A name s!ould begin wit! an alp!abet.
T!e use of special c!aracters li$e N is
allowed and also recommended. @"pecial
c!aracters li$e O. P are allowed only in
/racleA.
"34 reserved words not allowed.
Data types in Oracle
1ollowing are some most popular data types.
5sing t!ese data types we can define t!e
domain of a attribute.
(umber (preision/ sale) T!e 9umber data
type is used to store numbers @fi2ed or
floating pointA. #a2imum si:e is &Q digits of
precision.
0har(si1e) T!is data type is used to store
c!aracter string values of fi2ed si:e. T!e si:e
in brac$ets determines t!e number of
c!aracters t!e cell can !old. If t!e inserted
string !as less t!an t!at c!ar t!en rest of t!e
entry is padded wit! space. T!e ma2imum t!is
data type can !old is (''',.
'arhar(si1e) T!is data type is used to store
variable lengt! alp!anumeric data. t!e
inserted values will not be padded by spaces.
T!e ma2imum t!is data type can !old is
)''',.
(har(si1e) T!is data type is similar to c!ar
e2cept t!e fact t!at it can store any natural
language c!aracter. It ta$es &, to store one
c!ar.
9varc!ar@si:eA T!is data type is similar to c!ar
e2cept t!e fact t!at it can store any natural
language c!aracter. It ta$es &, to store one
c!ar.
Date T!is data type is used to represent date
and time. T!e standard format is DD-###-
RR as in (&-BA9-%).
/t!er data types are available in oracle li$e
long. raw etc.
E2ample: +reate a table "tudent wit!
following structure.
"tudent@sid.sname.saddr.mar$sA.
"ol:
create table "tudent
@
sid varc!ar@*A PRI#ARR HER.
sname varc!ar@%*A.
saddr varc!ar@&'A.
mar$s number@&A
AL
T!is statement can be written in a single line.
T!is statement w!en e2ecuted will create a
table wit! name "tudent !aving four
attributes sid. sname. saddr and mar$s. T!e
$ey word PRR#ARR HER !as been used to
enforce a constraint w!ic! will not allow
entries in sid w!ic! are repeated .And at t!e
same time t!e entries cannot be left blan$.

Inserting values in table
T!e table created s!ould be loaded wit! data
to be manipulated latter.
"ynta2:
I9"ERT I9T/ JtablenameKS@list of attributeAT
values@v%.v(.v&.....vnAL
v
i
represents t!e values for corresponding
attributes.
E2ample:
insert into "tudent@sid.sname.saddr.mar$sA
values@Us%<.<ram<.<bilaspur<.='AL

T!e above command will operate in following
two stape.
%. +reates a new row@emptyA in t!e
database table
K. Bhardwaj DBMS BSc. 4th (PCSC-401 ) 2014

Guru Ghasidas Universit Pa!e *

(. 4oads t!e values passed @by t!e insert
statementA into t!e columns specified.
9ote: +!aracter values places wit!in t!e
insert into statement must be enclosed in
single 7uotes @UA.
If t!ere are less values being described t!an
t!ere are columns in t!e table t!en it is
mandatory to indicate bot! t!e table column
name and its corresponding value in t!e insert
into statement.
Retrieving data from table
T!e "E4E+T command is used to retrieve t!e
records selected from one or more tables.
1ollowing are t!e possibility of viewing
@retrievingA data from a table.
2ll rows and all olumns
Synta3% "elect V from JtableNnameKL
E2ample: "elect V from "tudentL
Result:
"id sname saddr mar$s
"% ram bilaspur ='
"( mo!an raipur )'
:
"n rames! puri Q'

/racle allows a meta c!aracter asteris$ @VA to
mean all attributes of t!e table.
Filtering table data
8!ile viewing data from a table. it is rare t!at
all t!e data from t!e table will be re7uired
eac! time. "34 provides a met!od of filtering
table data w!ic! are following.
All rows of selected columns.
"elected rows of all columns.
"elected rows of selected columns.
All rows of selected columns
T!e retrieval of specified columns can be
done using following synta2.
"E4E+T column%.column(.....column
$
form
Jtable nameKL
E2ample: Retrive t!e sid and mar$s of all t!e
student.
"E4E+T sid. mar$s from "tudentL
sid mar$s
"% ='
"( )'
:
"n Q'

"elected rows of all columns
If we !ave to retrieve selected records we will
!ave to specify selection condition. 1ollowing
is t!e synta2.
"E4E+T V from JtableNnameK 8;ERE
JconditionKL
E2ample: Retrieve t!e records of t!ose
students w!o !ave mar$s greater t!an *'.
"E4E+T V from "tudent 8;ERE mar$sK*'L
Result:
"id sname saddr mar$s
"% ram bilaspur ='
") gaab raipur ?'
:
"n rames! puri Q'
only t!ose records w!ic! !as mar$s greater
t!an *'.
+ondition is following format.
JAttributeNname K JoperatorK
JattributeNname/valueK
#eans condition will !ave a logical e2pression
w!ic! will evaluate eit!er to TR5E or 1A4"E.
T!e records for w!ic! t!e condition will
K. Bhardwaj DBMS BSc. 4th (PCSC-401 ) 2014

Guru Ghasidas Universit Pa!e +

evaluate to TR5E t!ose records will be
selected.
"elected rows of selected column
To view a specific set of rows and column we
will use following synta2.
"E4E+T J4ist of columnK 1rom JtableNnameK
8;ERE JconditionKL
E2ample: Retrieve t!e sid and mar$s of t!ose
students w!o !ave got more t!an *' mar$s.
"E4E+T sid. mar$s 1R/# "tudent 8;ERE
mar$sK*'L
Result:
"id mar$s
"% ='
") ?'
:
"n Q'
/nly t!e sid and mar$s of t!ose students w!o
!ave more t!an *' mar$s.
Eliminating Duplicate rows w!ile using a
select statement
"E4E+T DI"TI9+T Jattribute listK 1R/#
JtableNnameKL
DI"TI9+T is a $eyword used to eliminate t!e
duplicate rows.
e2ample: Retrieve t!e different mar$s given
to t!e students.
"E4E+T DI"TI9+T mar$s 1R/# "tudentL
Result:
mar$s
='
)'
&'
:
Q'

Sorting data in a table
/racle allows data from a table to be viewed
in sorted order. 1ollowing is t!e synta2.
"E4E+T V 1R/# JtableNnameK /RDER ,R
Jattribute%K.Jattribute(KSorderTL
E2ample: "elect V from "tudent order by
mar$s DE"+L
,y default order is A"+ w!ic! stands for
ascending order. 1or viewing data in
descending order t!e word DE"+ must be
mentioned after t!e column name.
Creating a table from a table
"ynta2: +REATE TA,4E
JtableNnameK@Jattribute%K.Jattribute(KA A"
"E4E+T Jattribute%K.Jattribute(K 1R/#
JtableNnameKL
E2ample: +reate table
c!!otastudent@sid.mar$sA A" select sid. mar$s
from studentL
8!en t!e above statement will be e2ecuted a
table named c!!otastudent will be created
wit! two attributes sid and mar$s. T!e data
types of attributes will be ta$en from t!e
student table. T!e result will a table named
c!!otastudent wit! two attributes and t!e
values of t!ose attributes for all t!e records
from table student.
To create a target table wit!out t!e records
from t!e source table @i.e. create t!e structure
onlyA. t!e select statement must !ave a w!ere
clause. T!e w!ere clause must specify a
condition t!at cannot be satisfied.
Inserting data into table from anoter table
"ynta2: Insert into JtableNnameK "elect
Jattribute%K. JattributenKfrom
JtableNnameKL
K. Bhardwaj DBMS BSc. 4th (PCSC-401 ) 2014

Guru Ghasidas Universit Pa!e ,

E2ample: Insert into +!!otastudent "elect sid.
mar$s from "tudentL
Insertion of a data set into a table from
anot!er table
"ynta2:
Insert into JtableNnameK select Jlist of
attributesK from JtableNnameK w!ere
JconditionKL
E2ample: Insert into +!!otastudent "elect sid.
mar$s from "tudent w!ere mar$sJ6)'L
Delete %perations
T!e DE4ETE command deletes rows from t!e
table t!at satisfies t!e condition provided by
its w!ere clause. and return t!e number of
record deleted.
9ote: If t!e DE4ETE statement is e2ecuted
wit!out w!ere clause t!en all t!e rows are
deleted.
Removal of all Ro!s
"ynta2: DE4ETE 1R/# JtableNnameKL
E2ample: Empty t!e "tudent table.
Delete #rom student4
Removal of Specified Ro!s
"ynta2: DE4ETE 1R/# JtableNnameK w!ere
JconditionKL
E2ample: Delete records of t!ose students
w!o !ave less t!an %' mar$s.
DE4ETE from student w!ere mar$sJ%'L
&pdatin$ te content of a table
T!e 5PDATE command is used to c!ange or
modify t!e content of e2isting records of a
table.
5pdating all rows
"ynta2: 5PDATE JtableNnameK "ET
Jattribute%K6Je2pression%K.
JattributenK6Je2pressionKL
E2ample: 5pdate t!e saddr of student by
c!anging its city name to bilaspur.
5PDATE "tudent "ET saddr6<bilaspur<L
"pdating a selected set of records in a table
"ynta2: 5PDATE Jtable nameK "ET
JattributeK6Je2pressionK 8;ERE
JconditionKL
E2ample: 5pdate t!e mar$s of t!ose students
w!ose mar$s is less t!an %' and set to '.
5pdate student set mar$s6' w!ere mar$sJ%'L
Modifying the structure of tables
T!e A4TER TA,4E command is used to modify
t!e structure of a table. 8it! A4TER TA,4E it
is possible to add or delete columns. or
c!ange t!e data type of e2isting columns.
A4TER TA,4E wor$s by ma$ing a temporary
copy of t!e original table. T!e alteration is
performed on t!e copy. t!em t!e original
table is deleted and t!e new one is renamed.
8!ile A4TER TA,4E is e2ecuting t!e original
table is still readable by users of /RA+4E.
Synta# for adding ne! column$
A4TER TA,4E JtableNnameK
ADD@J9ew+olumn9ameK Jdata type@si:eAK.
J9ew+olumn9ameK Jdata type@si:eAKAL
E2ample: Add a new column deptno to t!e
table "tudent w!ose data type s!ould be
varc!ar@&A.
A4TER TA,4E "tudent ADD@deptno
varc!ar@&AAL
Synta# for dropping a column from a table
A4TER TA,4E JtableNnameK DR/P +/45#9
Jcolumn9ameKL
E2ample: Drop t!e column saddr from t!e
table student.
A4TER TA,4E "tudent DR/P +/45#9 saddrL
K. Bhardwaj DBMS BSc. 4th (PCSC-401 ) 2014

Guru Ghasidas Universit Pa!e 10

"ynta2 for modifying e2isting columns
A4TER TA,4E JtableNnameK
#/DI1R@Jcolumn9ameK
J9E8DATATRPEK@Jnewsi:eKAAL
E2ample: Alter t!e "tudent table to allow t!e
sname field to !old ma2imum of )'
c!aracters.
A4TER TA,4E "tudent #/DI1R @sname
varc!ar@)'AAL
Restrictions on te %LT&R T%'L&
T!e following tas$s cannot be performed
w!en using t!e A4TER TA,4E command.
+!ange t!e name of a table.
+!ange t!e name of t!e column
Decrease t!e si:e of a column if table data
e2ists.
'enamin$ tables
/racle allows renaming of tables. T!e synta2
is as follows.
RE9A#E JtableNnameK T/
JnewNtableNnameKL
E2ample: +!ange t!e name of t!e table
"tudent to univstudent.
RE9A#E "tudent to univstudentL
Destroyin$ Tables
DR/P TA,4E statement wit! t!e table name
can destroy a specific table. If a table is
dropped all records !eld wit!in it are lost and
cannot be recovered.
"ynta2: DR/P TA,4E JtableNnameKL
E2ample: Remove t!e table c!!otastudent
along wit! t!e data !eld.
DR/P TA,4E c!!otastudentL

Truncating tables
TR59+ATE TA,4E empties a table completely.
It is e7uivalent to a DE4ETE statement t!at
deletes all rows. but t!ere are practical
differences .
TR59+ATE TA,4E differs from DE4ETE in t!e
following ways:
Truncate operation drop and re-create
t!e table. w!ic! is muc! faster t!an
deleting rows one by one.
Truncate operations are not transaction-
safe@i.e. an error will occur if an active
transaction or an active table loc$ e2istsA
T!e number of deleted rows are not
returned.
"ynta2: TR59+ATE TA,4E JtableNnameKL
E2ample: Truncate t!e table "tudent.
TR59+ATE TA,4E "tudentL
Displaying t!e table "tructure
To display information about t!e columns
defined in a table use t!e following "ynta2.
DE"+RI,E JtableNnameKL
T!is command displays t!e column names.
w!et!er 9544 values are allowed or notand
t!e data type wit! si:e.
E2ample: "!ow t!e structure of table "tudent.
DE"+RI,E "tudentL
9ote : In place of using DE"+RI,E t!e first four
c!aracters can be used to serve t!e same
purpose. 1or e2ample
DE"+ "tudentL

K. Bhardwaj DBMS BSc. 4th (PCSC-401 ) 2014

Guru Ghasidas Universit Pa!e 11

Data constraints
,usiness rules. w!ic! are being enforced on
data being stored in a table. are called
constraint. +onstraints super control t!e data
being entered into a table for permanent
storage.
/racle permits data constraints to be
attac!ed to table columns via "34 synta2 t!at
c!ec$s data for integrity prior storage. Even if
a single column of t!e record being inserted
into t!e table fails a constraint. t!e entire
record is reected and not stored in t!e table.
,ot! t!e +REATE TA,4E and A4TER TA,4E "34
commands can be used to attac! constraints
to a table column.
Types of data constraints
T!ere are two types of data constraints
namely I// constraints and business rule
constraints.
I(O Constraints
T!is data constraint determines t!e speed at
w!ic! data can be inserted or e2tracted from
a /racle table.
aA PRI#ARR HER
A primary $ey column in a table !as
special attributes:
It defines t!e column as mandatory
column @i.e. t!e column can not be
left blan$A.
T!e data present t!roug!out t!e
column must be uni7ue.
At column level
"ynta2:
Jcolumn nameK Jdata type@si:eAK
PRI#ARR HER

At table level
"ynta2:
PRI#ARR HER@column nameS.column
nameTA

bA 1/REI>9 HER
1oreign $ey represents relations!ip
between tables. A foreign $ey is a column
@or a group of columnsA w!ose values
w!ose values are derived from t!e
primary $ey or uni7ue $ey of some ot!er
table or same table.
At table level
"ynta2:
Jattribute nameK Jdata typeK
RE1ERE9+E" Jtable nameKS@JattributeKAT

At table level
"ynta2:
1oreign $ey
@Jcolumn9ameKS.J+olumn9ameKTA
References
JTable9ameKS@Jcolumn9ameKS.J+olumn
9ameKTAT
cA 59I35E
T!e 5ni7ue column constraint will not
allow duplicate values !owever 9544 is
allowed.
At column level
"ynta2:
Jcolumn9ameK Jdata type@si:eAK 59I35E

At table level
"ynta2:
5ni7ue
@Jcolumn9ameKS.Jcolumn9ameKTA
dA 9/T 9544
A 9544 value is different from a blan$ or
:ero. A 9544 is a un$nown or not e2isting
value.
At column level
"ynta2:
Jcolumn9ameK Jdata type@si:eAK 9/T
9544
At table level
9/T 9544 constraint annot be applied at
table level

K. Bhardwaj DBMS BSc. 4th (PCSC-401 ) 2014

Guru Ghasidas Universit Pa!e 1$

'usiness rule constraints
,usiness rule are determined by business
managers w!ic! will vary from system to
system
aA +;E+H
+!ec$ constraints are used to validate
business rules. T!e c!ec$ constraint will
!ave a logical e2pression t!at evaluates
eit!er to TR5E or 1A4"E.
A c!ec$ constraint ta$es subse7uently
longer time to e2ecute as compared to
ot!er above constraints.

"ynta2 at column level
J+olumn9ameK Jdata typeK@si:eA +;E+H
@Jlogical e2pressionKA

"ynta2 at table level
+;E+H@Jlogical e2pressionKA

(eneral Form of S)*)CT
statement
)"E4E+T SDI"TI9+TTJ4I"T /1 ATTRI,5TEK
%1R/# J4I"T /1 TA,4E 9A#EK
(S8;ERE JconditionKT
&S>R/5P ,R JattributenameKS;AII9>
JconditionKTT
*S/RDER ,RJattributeKSDE"+TTL

T!e numbers in front of t!e statement reflect
t!e order of e2ecution.
8e are mostly aware of all t!e above clauses
e2cept t!e >roup ,y clause.
(roup By clause
T!e >roup ,y clause is used to retrieve t!e
information group wise. T!e group by clause
ma$es one record for eac! group. 1rom eac!
group t!e attribute on w!ic! it !as been
grouped. can be proected as it is and t!e rest
attributes can be proected only wit! t!e !elp
of aggregate functions.
A$$re$ate Function
Aggregate functions are t!ose functions w!ic!
are used on group of values. E2amples of
aggregate functions are following.
#I9@JattributeKA
1inds t!e minimum value in an attribute.
#AG@JattributeKA
1inds t!e ma2imum value in an attribute.
"5#@SDistinctTJattributeKA
1inds t!e sum of all t!e values of attribute.
+/59T@SdistinctTJattributeKA
+ounts t!e number of records in t!e result or
t!e attribute.
AI>@SDistinctTJattributeKA
calculate t!e average of t!e values in t!e
attribute.
Signi#iane o# Distint.
8!en Distinct is used t!en t!e duplicate
values are considered only once.
+ested ,uery
8!en a 7uery is written inside anot!er 7uery
t!en it is called 9ested 7uery. Anyw!ere in
7uery we can !ave sub 7uery. T!e inner 7uery
is also called sub 7uery.
R "
A
%
(
%'
)
E2ample: Retrieve t!e value of attribute A
from relation R w!ic! is also present in
attribute , of relation ".
"elect R.A from R w!ere R.A 5( @"elect ".,
from "AL
,
%
*
%'
%%
K. Bhardwaj DBMS BSc. 4th (PCSC-401 ) 2014

Guru Ghasidas Universit Pa!e 1%

%perators used in nested -uery
1ollowing operators can be used in nested
7ueries.
#+. +%T #+
T!e I9 operator will ta$e a single value in 4;"
and a set of values in R;". T!e I9 operator
will evaluate to true only if T!e 4;" is present
in t!e R;" set of values. 9/T I9 is ust
opposite of I9 operator.
%P A+/
/P stands for operator. /perators can be 6.
JK. K. J. K6.J6. T!is operator also ta$es a
single value in 4;" and a set of value in R;".
T!e condition evaluates to true if t!e value on
t!e left !and side satisfies t!e operator
condition wit! any of t!e values in t!e set.
%P A**
/P is similar to above. T!is operator evaluates
to true if it satisfies t!e operator condition for
all t!e values of t!e set.
)0#STS. +%T )0#STS
T!is operator only a "34 7uery on R;". T!is
evaluates to true if t!e result of 7uery is
!aving at least one record. It evaluates to
1alse if t!ere is no record in t!e result of
7uery following t!e EGI"T operator. 9/T
EGI"T" is ust opposite of EGI"T".
Correlated +ested ,uery
A 7uery is said to be a correlated nested
7uery if t!e table listed in outer 7uery is also
used in inner 7uery.
E2ample:
"elect R.A from R w!ere EGI"T"@"elect ".,
from " w!ere R.A6".,AL
Substrin$ comparison
4IHE operator is used for substring
comparison.
E2ample: Retrieve t!e name of students
w!ose address starts from U,ilas<.
"ol: "E4E+T "9A#E 1R/# "T5DE9T 8;ERE
"ADDRE"" 4IHE <,ilasW<L
W is used for representing any number of
c!aracter.
N@underscoreA is used to represent a single
c!aracter.
Aritmetic operators
Arit!metic operators can be used in select
clause and also in w!ere clause. /perators are
0.-.V./ for numeric attribute and UXX< for
string.
E2ample : "elect name. %.%Vsalary from E#P.
B)T1))+ operator2
+an be used to mention a condition for a
range.
E2ample: "elect V from E#P w!ere salary
,ET8EE9 &'''' A9D )''''L
,ET8EE9 is always used wit! A9D operator.
#S operator
I" operator is used to compare 9544. 9544
cannot be compared using U6< operator.
E2ample: Retrieve t!e name of t!ose students
w!o don<t !ave a passport number.
"ol: "elect sname from student w!ere ppno 5S
nullL
ppno is a attribute of table student w!ic!
stores t!e passport number.
!ie"s in S,*
Iiew is a virtual table w!ic! is derived from
ot!er tables. It is based on some "34 7uery.
E2ample: +reate a view facultyNview on table
student w!ic! will !ave only attributes sid.
sname and mar$s from table student.
"ol: +reate view facultyNview A" select sid.
sname. mar$s from studentL

Vous aimerez peut-être aussi