You are on page 1of 12

Database Normalisation and Table structures

--------------------------------------------------------------------------------
Normalisation is the term used to describe how you break a file down into tables to create a database. There are 3 or 4 major steps
involved known as 1N !irst Normal orm"# $N !%econd Normal orm"# 3N !Third Normal orm" and &'N !&oyce-'odd Normal
orm". There are others but
they are rarely if ever used. ( database is said to be Normalised if it is in 3N !or ideally in &'N". These steps are descibed as
follows)
Note) *hen attribute is used we are speakin+ of a field in the table
1NF
To put a database in 1N
ensure that all attributes !columns" are atomic !which means that any sin+le field should only have a value for ,N- thin+". -.amples)
/n a database a table on 'ustomers would have an address attribute. The address is made up of 'ompany Name# (ddress 0ine1#
(ddress 0ine$# (ddress 0ine3# 'ity# 1ostcode. There are 2 values to this address and as such each should have it3s own field !column".
/f your company sold furniture a table on products could have a description attribute. /f for e.ample that attribute was 3&eech Desk 1$4w
. 56h . 64d3. /deally this would be broken down into a number attributes like 3'olour3# 3Type3# 3*idth3# 37ei+ht3 and 3Depth3. The reason for
this is it would allow you to seach the database for all Desks# for all pieces of &eech furniture# for all desks with a width of 1$4 etc.
'reate a separate table for each set of related data and /dentify each set of related data with a primary key
-.ample)
/n a +eneral /nvoicin+ table you would have a separate table for 'ustomers# ,rders# 1roducts# /nvoices and you would probably need
tables for ,rderDetails and /nvoiceDetails as well. -ach of these tables must have their own primary key. -ach of these tables e.cept
for customers would
have a forei+n key reference to the primary key of another table. !%ee 8elationships below"
Do not use multiple fields in a sin+le table to store similar data
-.ample)
!9nderlined fields are 1rimary :eys and /talicised fields are orei+n :eys"
/n a customer order you could have more than one product. That is the customer has ordered more than one item. /f you tried to put all
of this in one table as ;,rder/D# 'ustomer/D# ,rderDate# 1roduct1# 1roduct$# 1roduct3< what would happen if the customer ordered
more than 3 products. There would also be implications for =ueryin+ the kind or =uantiy of products ordered by a customer. Therefore
these product fields don3t belon+ in the order table which is why we would have an ,rderDetails table which would have a forei+n key
refernce to the ,rders table ;,rderDetails/D# ,rder/D# 1roduct/D# >uantity<. 9sin+ product/D as a forei+n key to the product table
means you don3t have to identify the product attributes here. This also allows you to enter a =uantity fi+ure for the product ordered.
8elationships)
(ll tables should have a 1 to 1 or 1 to many relationship. This means for e.ample that 1 customer can have 1 or many orders and 1
order can have 1 or many details.
Therefore ,rders table would have a forei+n key reference to the 'ustomer table primary key ;,rder/D# 'ustomer/D# ,rderDate< and
the ,rderDetails table would have a forei+n key reference to the ,rder table primary key ;,rderDetails/D# ,rder/D# 1roduct/D#
>uantity<. This table also
contains a forei+n key reference to the 1roducts table. (s a product is likely to be ordered more than once the is a many to 1
relationship between the ,rderDetails and the 1roducts table.
/f any tables have a many to many relationship this must be broken out usin+ a ?,/N table. or e.ample# 'ustomers can have many
%uppliers and %uppliers can supply to many 'ustomers. This is known as a many to many relationship. @ou would need to create a
?,/N table that would have aprimary key made up of a forei+n key reference to the 'ustomers table and a forei+n key reference to the
suppliers table. Therefore the %uppliers1er'ustomer table would be ;%upplier/D# 'ustomer/D<. Now the %uppliers table will have a 1 to
many relationship with the %uppliers1er'ustomer table and the 'ustomers table will also have a 1 to many relationship with the
%uppliers1er'ustomer table.
2NF
The database must meet all the re=uirements of the 1N.
/n addition# records should not depend on anythin+ other than a table3s primary key !a primary key can be made up of more than one
field# only if absolutely necessary like in a ?,/N table".
-.ample)
( customers address is needed by the 'ustomers table# but also by the ,rders# and /nvoices tables. /nstead of storin+ the customer3s
address as a separate entry in each of these tables# store it in one place# either in the 'ustomers table or in a separate (ddresses
table.
3NF
The third normal form re=uires that all columns in a relational table are dependent only upon the primary key. ( more formal definition
is)
( relational table is in third normal form !3N" if it is already in $N and every non-key column is non transitively dependent upon its
primary key. /n other words# all nonkey attributes are functionally dependent only upon the primary key. (ll 3N really means is that all
fields !attributes" should be dependent on the tables primary key. /f they are not they should be put in their own table. This means that
every attribute unless it is a primary or forei+n key must be D/8-'T0@ dependent on the
1
1rimary :ey of this table and not on some other column.
-.ample)
The 'ustomer table contains information such as address# city# postcode ima+ine it also contained a column called shippin+ cost. The
value of shippin+ cost chan+es in relation to which city the products are bein+ delivered to# and therefore is not directly dependent on
the customer even thou+h the cost mi+ht not chan+e per customer# but it is dependent on the city that the customer is in. Therefore we
would need to create another separate table to hold the information about cities and shippin+ costs.
BCNF
( relation is in &oyce-'odd Normal orm !&'N" if every determinant is a candidate key. &'N is very similar to 3N but deals with
dependencies within the primary keys. &'N in it3s simplist terms just says don3t have a primary key made up of more than one field
unless it is a join table to disperse a many to many relationship and only contains the two primary keys of the tables it is joinin+.
Aost relations that are in 3N are also in &'N. /t only happens that a relation which is in 3N is not in &'N when the primary key in
a table is made up of more than one field and the other columns are not dependent on both fields but only on one or the other.
( database is said to be normalised if it is in 3N andBor &'N
Normalisation is the process of usin+ a wide table with many columns and few rows and reconstructin+ that table into several subset
tables with fewer columns and more rows. The objective is to use stora+e space efficiently# eliminate redundant data# reduce or
eliminate inconsistent
data and ease future maintenance. The most important rule of normalisation is that the normalised data must be reconstructable into
the ori+inal unnormalised or flat spreadsheet-like view of the data.
Forms of Normalisation :- The forms of normalisation are called normal forms.
irst Normal orm - removal of repeatin+ +roups.
%econd Normal orm - no non-key attributes must depend on a portion of the primary key.
Third Normal orm - no attributes must depend on other non-key attributes.
( database must be in first normal form to be in second normal form and in second normal form to be in third normal form. There are
additional normal forms but these additional normal forms are rarely applied. /n fact it is sometimes best not to apply third and even
second normal forms in order to preserve processin+ speed and efficiency. The concept of a data warehouse is effectively a partially
denormalised database. /n fact the process of denormalisation into data warehouses e.hibits where relational database technolo+y
falls short of practicality. This is one possible area of implementation where object databases may become more widely used in the
future.
First Normal Form
&y definition# irst Normal orm eliminates repeatin+ +roups. *hat this means is that a separate table must be made for each set of
related attributes and +ive each table its own primary key. The best way to e.plain this is by e.ample. 0et us say we have a customer
database where customers make orders. /n a spreadsheet we would have the customer name and address repeated for every order.
(pplication of first normal form would divide the customers and orders into two separate entities or tables. The customer table would
contain customer details without order details and the order table would contain order details with only a reference to the appropriate
customer.
Second Normal Form
%econd Normal orm eliminates redundant data. Thus if an attribute relies on only part of a multi-valued key# that attribute must be
removed to a separate table. Aore specifically the %econd normal form re=uires that no non-key attributes are dependant upon a
portion of the primary key. ( primary key uni=uely identifies a instance within an entity. %econd normal form applies to tables where
constituents of the primary key effectively refer to repeatin+ +roups within the same table. These duplications should be removed to an
additional table. or instance# if a student takes a course then the course that the student is takin+ is uni=uely identifed by the student
and the course as the primary key. *ithin this entitiy we also have descriptions of the course. Therefore the course decriptions will
make up the contents of the additional entity and the descriptive details of the course would be removed from the table containin+ the
student details. The result would be two tables# one with student details and the other with course details.
Third Normal Form
Third Normal orm eliminates columns not dependant on a table3s primary key. This translates to the fact that if an attribute does not
contribute to the description of a key then it must be moved to another table. Third normal form re=uires that all columns in a table
contain data about the entity that is defined by the primary key. /n other words each entity should have attributes applicable to itself
only.
Fourth and Fifth Normal Forms
ourth and ifth Normal orms are rarely implemented.
Fourth Normal Form
ourth Normal orm eliminates multiple independant relationships. Thus no table can have more than a sin+le many-to-one or many-
to-many relationships which are not directly related.
Fifth Normal Form
ifth Normal orm re=uires isolation of lo+ically related many-to-many relationships.
2
The Truth About Normalisation
9sin+ normalisation e.tensively can seriously affect database performance. *hat is theoretically ele+ant and mathematically correct is
not always acceptable or practical in a commercial environment. Due to the fact that / have personally had many years e.perience
desi+nin+ and constructin+ relational databases / tend to i+nore the separate steps of first# second and third normal form. / usually
simply jump to a combination of all three by usin+ all three at once.
Software n!ineerin! life c"cle
#omain Anal"sis
,ften the first step in attemptin+ to desi+n a new piece of software# whether it be an addition to an e.istin+ software# a new application#
a
new subsystem or a whole new system# is# what is +enerally referred to as CDomain (nalysisC. (ssumin+ that the developers
!includin+ the analysts" are not sufficiently knowled+eable in the subject area of the new software# the first task is to investi+ate the so-
called CdomainC of the software. The more knowled+eable they are about the domain already# the less the work re=uired. (nother
objective of this work is to make the analysts who will later try to elicit and +ather the re=uirements from the area e.perts or
professionals# speak with them in the domain3s own terminolo+y and to better understand what is bein+ said by these people.
,therwise they will not be taken seriously. %o# this phase is an important prelude to e.tractin+ and +atherin+ the re=uirements. The
followin+ =uote captures the kind of situation an analyst who hasn3t done his homework well may face in speakin+ with a professional
from the domain) C/ know you believe you understood what you think / said# but / am not sure you realiDe what you heard is not what /
meant.CE1F
Software lements Anal"sis
The most important task in creatin+ a software product is e.tractin+ the re=uirements. 'ustomers typically know what they want# but
not what software should do# while incomplete# ambi+uous or contradictory re=uirements are reco+niDed by skilled and e.perienced
software en+ineers. re=uently demonstratin+ live code may help reduce the risk that the re=uirements are incorrect.
S$ecification
%pecification is the task of precisely describin+ the software to be written# possibly in a ri+orous way. /n practice# most successful
specifications are written to understand and fine-tune applications that were already well-developed# althou+h safety-critical software
systems are often carefully specified prior to application development. %pecifications are most important for e.ternal interfaces that
must remain stable.
Software architecture
The architecture of a software system refers to an abstract representation of that system. (rchitecture is concerned with makin+ sure
the
software system will meet the re=uirements of the product# as well as ensurin+ that future re=uirements can be addressed. The
architecture step also addresses interfaces between the software system and other software products# as well as the underlyin+
hardware or the host operatin+ system.
%m$lementation &or codin!'
8educin+ a desi+n to code may be the most obvious part of the software en+ineerin+ job# but it is not necessarily the lar+est portion.
Testin!
Testin+ of parts of software# especially where code by two different en+ineers must work to+ether# falls to the software en+ineer.
#ocumentation
(n important !and often overlooked" task is documentin+ the internal desi+n of software for the purpose of future maintenance and
enhancement. Documentation is most important for e.ternal interfaces. %oftware Trainin+ and %upport ( lar+e percenta+e of software
projects fail because the developers fail to realiDe that it doesn3t matter how much time and plannin+ a development team puts into
creatin+ software if nobody in an or+aniDation ends up usin+ it. 1eople are occasionally resistant to chan+e and avoid venturin+ into
an unfamiliar area so# as a part of the deployment phase# it is very important to have trainin+ classes for the most enthusiastic
software users !build e.citement and confidence"# shiftin+ the trainin+ towards the neutral users intermi.ed with the avid supporters#
and finally incorporate the rest of the or+aniDation into adoptin+ the new software. 9sers will have lots of =uestions and software
problems which leads to the ne.t phase of software.
(aintenance
Aaintainin+ and enhancin+ software to cope with newly discovered problems or new re=uirements can take far more time than the
initial development of the software. Not only may it be necessary to add code that does not fit the ori+inal desi+n but just determinin+
how software works at some point after it is completed may re=uire si+nificant effort by a software en+ineer. (bout G of all software
en+ineerin+ work is maintenance# but this statistic can be misleadin+. ( small part of that is fi.in+ bu+s. Aost maintenance is
e.tendin+ systems to do new thin+s# which in many ways can be considered new work. /n comparison# about G of all
civil en+ineerin+# architecture# and construction work is maintenance in a similar way.
3
)eferential inte!rit"
Definition) 8eferential inte+rity is a database concept that ensures that relationships between tables remain consistent. *hen one table
has a forei+n key to another table# the concept of referential inte+rity states that you may not add a record to the table that contains the
forei+n key unless there is a correspondin+ record in the linked table. /t also includes the techni=ues known as cascadin+ update and
cascadin+ delete# which ensure that chan+es made to the linked table are reflected in the primary table.
'onsider the situation where we have two tables) -mployees and Aana+ers. The -mployees table has a forei+n key attribute entitled
Aana+ed&y which points to the record for that employeeHs mana+er in the Aana+ers table. 8eferential inte+rity enforces the followin+
three rules)
1.*e may not add a record to the -mployees table unless the Aana+ed&y attribute points to a valid record in the Aana+ers table.
$./f the primary key for a record in the Aana+ers table chan+es# all correspondin+ records in the -mployees table must be modified
usin+ a cascadin+ update.
3. /f a record in the Aana+ers table is deleted# all correspondin+ records in the -mployees table must be deleted usin+ a cascadin+
delete.
Benefits of Stored *rocedures
*hy should you use stored proceduresG 0et3s take a look at the key benefits of this technolo+y)
*recom$iled e+ecution. %>0 %erver compiles each stored procedure once and then reutiliDes the e.ecution plan. This results in
tremendous performance boosts when stored procedures are called repeatedly.
)educed client,ser-er traffic. /f network bandwidth is a concern in your environment# you3ll be happy to learn that stored procedures
can reduce lon+ %>0 =ueries to a sin+le line that is transmitted over the wire.
fficient reuse of code and $ro!rammin! abstraction.
%tored procedures can be used by multiple users and client pro+rams. /f you utiliDe them in a planned manner# you3ll find the
development cycle takes less time.
nhanced securit" controls. @ou can +rant users permission to e.ecute a stored procedure independently of underlyin+ table
permissions.
Structure
%tored procedures are e.tremely similar to the constructs seen in other pro+rammin+ lan+ua+es. They accept data in the form of input
parameters that are specified at e.ecution time. These input parameters !if implemented" are utiliDed in the e.ecution of a series of
statements that produce some result. This result is returned to the callin+ environment throu+h the use of a recordset# output
parameters and a return code. That may sound like a mouthful# but you3ll find that stored procedures are actually =uite simple.
appropriate parameters at e.ecution time.
.hat command is used to create a table b" co$"in! the structure of another table/
'8-(T- T(&0- .. (% %-0-'T command
-.planation)
To copy only the structure# the *7-8- clause of the %-0-'T command should contain a (0%- statement as in the followin+.
'8-(T- T(&0- N-*T(&0- (% %-0-'T I 8,A -J/%T/NKT(&0- *7-8- 1L$M
/f the *7-8- condition is true# then all the rows or rows satisfyin+ the condition will be copied to the new table.
.hat is difference between T)0NCAT 1 #2T /
T89N'(T- commits after deletin+ entire table i.e.# cannot be rolled back. Database tri++ers do not fire on T89N'(T-
D-0-T- allows the filtered deletion. Deleted records can be rolled back or committed. Database tri++ers fire on D-0-T-
.hat is the sub-3uer"/
%ub-=uery is a =uery whose return values are used in filterin+ conditions of the main =uery.
.hat is correlated sub-3uer"/
'orrelated sub-=uery is a sub-=uery# which has reference to the main =uery.
/n the sub=ueries# %>0 evaluates the sub=uery once# substitutes the result of the sub=uery in the search condition# and evaluates the
outer-level %-0-'T based on the value of the search condition. @ou can also write a sub=uery that %>0 may need to re-evaluate as it
e.amines each new row !*7-8- clause" or +roup of rows !7(N/NK clause" in the outer-level %-0-'T. This is called a correlated
sub=uery
select cate+ory # articletitle # articlepubdate
from articles DD
where articlepubdate
L ! select ma.!articlepubdate"
from articles where cate+ory L DD.cate+ory "
+$lain 0N%4N5 (%N0S5 0N%4N A22 and %NT)SCT/
%NT)SCT - returns all distinct rows selected by both =ueries.
The /NT-8%-'T =uery allows you to return the results of $ or more CselectC =ueries. 7owever# it only returns the rows selected by all
=ueries. /f a record e.ists in one =uery and not in the other# it will be omitted from the /NT-8%-'T results.
-ach %>0 statement within the /NT-8%-'T =uery must have the same number of fields in the result sets with similar data types.
-.ample O1
The followin+ is an e.ample of an /NT-8%-'T =uery)
select supplierPid from suppliers
/NT-8%-'T
select supplierPid from ordersM
4
/n this e.ample# if a supplierPid appeared in both the suppliers and orders table# it would appear in your result set.
(%N0S - returns all distinct rows selected by the first =uery but not by the second.
The A/N9% operates on two %>0 statements. /t takes all the results from the first %>0 statement# and then subtract out the ones that
are present in the second %>0 statement to +et the final answer. /f the second %>0 statement includes results not present in the first
%>0 statement# such results are i+nored.
0N%4N - returns all distinct rows selected by either =uery
The purpose of the %>0 9N/,N command is to combine the results of two =ueries to+ether. /n this respect# 9N/,N is somewhat similar
to ?,/N in that they are both used to related information from multiple tables. ,ne restriction of 9N/,N is that all correspondin+
columns need to be of the same data type. (lso# when usin+ 9N/,N# only distinct values are selected !similar to %-0-'T D/%T/N'T".
0N%4N A22 - The purpose of the %>0 9N/,N (00 command is also to combine the results of two =ueries to+ether. The difference
between 9N/,N (00 and 9N/,N is that# while 9N/,N only selects distinct values# 9N/,N (00 selects all values.
4rder of S62 statement e+ecution
*here clause# Kroup &y clause# 7avin+ clause# ,rder &y clause Q %elect.
Transaction
Transaction is defined as all chan+es made to the database between successive commits.
Commit
'ommit is an event that attempts to make data in the database identical to the data in the form. /t involves writin+ or postin+ data to the
database and committin+ data to the database. orms check the validity of the data in fields and records durin+ a commit. Nalidity
check are uni=ueness# consistency and db restrictions.
*ostin!
1ostin+ is an event that writes /nserts# 9pdates Q Deletes in the forms to the database but not committin+ these transactions to the
database.
)ollbac7
8ollback causes work in the current transaction to be undone.
Sa-e$oint
%avepoint is a point within a particular transaction to which you may rollback without rollin+ back the entire transaction.
Set Transaction
%et Transaction is to establish properties for the current transaction.
2oc7in!
0ockin+ are mechanisms intended to prevent destructive interaction between users accessin+ data. 0ocks are used to achieve.
Consistenc"
'onsistency ) (ssures users that the data they are chan+in+ or viewin+ is not chan+ed until the are thro3 with it.
%nte!rit"
(ssures database data and structures reflects all chan+es made to them in the correct se=uence. 0ocks ensure data inte+rity and
ma.imum concurrent access to data. 'ommit statement releases all locks. Types of locks are +iven below.
Data 0ocks protects data i.e. Table or 8ow lock.
Dictionary 0ocks protects the structure of database object i.e. ensures table3s structure does not chan+e for the duration of the
transaction.
/nternal 0ocks Q 0atches protects the internal database structures. They are automatic.
-.clusive 0ock allows =ueries on locked table but no other activity is allowed.
%hare 0ock allows concurrent =ueries but prohibits updates to the locked tables.
8ow %hare allows concurrent access to the locked table but prohibits for a e.clusive table lock.
8ow -.clusive same as 8ow %hare but prohibits lockin+ in shared mode.
%hared 8ow -.clusive locks the whole table and allows users to look at rows in the table but prohibit others from lockin+ the table in
share or updatin+ them.
%hare 9pdate are synonymous with 8ow %hare.
#eadloc7
Deadlock is a uni=ue situation in a multi user system that causes two or more users to wait indefinitely for a locked resource. irst user
needs a resource locked by the second user and the second user needs a resource locked by the first user. To avoid dead locks# avoid
usin+ e.clusive table lock and if usin+# use it in the same se=uence and use 'ommit fre=uently to release locks.
(utatin! Table
Autatin+ Table is a table that is currently bein+ modified by an /nsert# 9pdate or Delete statement. 'onstrainin+ Table is a table that a
tri++erin+ statement mi+ht need to read either directly for a %>0 statement or indirectly for a declarative 8eferential /nte+rity
constraints. 1seudo 'olumns behaves like a column in a table but are not actually stored in the table. -.+. 'urrval# Ne.tval# 8owid#
8ownum# 0evel etc.
S6282oader
%>0I0oader is a product for movin+ data in e.ternal files into tables in an ,racle database. To load data from e.ternal files into an
,racle database# two types of input must be provided to %>0I0oader ) the data itself and the control file. The control file describes the
data to be loaded. /t describes the Names and format of the data files# %pecifications for loadin+ data and the Data to be loaded
!optional". /nvokin+ the loader s=lload usernameBpassword controlfilename RoptionsS.
The most im$ortant ##2 statements in S62 are:
'8-(T- T(&0- - creates a new database table
(0T-8 T(&0- - alters !chan+es" a database table
D8,1 T(&0- - deletes a database table
'8-(T- /ND-J - creates an inde. !search key"
D8,1 /ND-J - deletes an inde.
.hat is the use of CASCA# C4NST)A%NTS/
5
*hen this clause is used with the D8,1 command# a parent table can be dropped even when a child table e.ists.
*rimar" 9e":
( primary key is a field or combination of fields that uni=uely identify a record in a table# so that an individual record can be located
without confusion.
Forei!n 9e":
( forei+n key !sometimes called a referencin+ key" is a key used to link two tables to+ether. Typically you take the primary key field
from one table and insert it into the other table where it becomes a forei+n key !it remains a primary key in the ori+inal table".
( primary key is the field!s" !a primary key can be made up of more than one field" that uni=uely identifies each record# i.e. the primary
key is uni=ue for each record and the value is never duplicated in the same table# so in the above table the -mployee/D field would be
used. ( constraint is a rule that defines what data is valid for a +iven field. %o a primary key constraint is a rule that says that the
primary key fields cannot be null and cannot contain duplicate data.
constraint
1urpose
9se a constraint to define an inte+rity constraintTa rule that restricts the values in a database. ,racle Database lets you create si.
types of constraints and lets you declare them in two ways.
The si. types of inte+rity constraint are described briefly here and more fully in C%emantics C)
( N4T N022 constraint prohibits a database value from bein+ null.
( uni3ue constraint prohibits multiple rows from havin+ the same value in the same column or combination of columns but allows some
values to be null.
( $rimar" 7e" constraint combines a N,T N900 constraint and a uni=ue constraint in a sin+le declaration. That is# it prohibits multiple
rows from havin+ the same value in the same column or combination of columns and prohibits values from bein+ null.
( forei!n 7e" constraint re=uires values in one table to match values in another table.
( chec7 constraint re=uires a value in the database to comply with a specified condition.
( )F column by definition references an object in another object type or in a relational table. ( 8- constraint lets you further
describe the relationship between the 8- column and the object it references.
@ou can define constraints syntactically in two ways)
(s part of the definition of an individual column or attribute. This is called inline specification.
(s part of the table definition. This is called out-of-line specification.
N,T N900 constraints must be declared inline. (ll other constraints can be declared either inline or out of line.
N4T N022 Constraints
( N,T N900 constraint prohibits a column from containin+ nulls. The N900 keyword by itself does not actually define an inte+rity
constraint# but you can specify it to e.plicitly permit a column to contain nulls. @ou must define N,T N900 and N900 usin+ inline
specification. /f you specify neither N,T N900 nor N900# then the default is N900.
N,T N900 constraints are the only constraints you can specify inline on JA0Type and N(88(@ columns.
To satisfy a N,T N900 constraint# every row in the table must contain a value for the column.
Note)
,racle Database does not inde. table rows in which all key columns are null e.cept in the case of bitmap inde.es. Therefore# if you
want an inde. on all rows of a table# then you must either specify N,T N900 constraints for at least one of the inde. key columns or
create a bitmap inde..
)estrictions on N4T N022 Constraints
@ou cannot specify N900 or N,T N900 in a view constraint.
@ou cannot specify N900 or N,T N900 for an attribute of an object. /nstead# use a '7-': constraint with the /% EN,TF N900
condition.
0ni3ue Constraints
( uni=ue constraint desi+nates a column as a uni=ue key. ( composite uni=ue key desi+nates a combination of columns as the uni=ue
key. *hen you define a uni=ue constraint inline# you need only the 9N/>9- keyword. *hen you define a uni=ue constraint out of line#
you must also specify one or more columns. @ou must define a composite uni=ue key out of line.
To satisfy a uni=ue constraint# no two rows in the table can have the same value for the uni=ue key. 7owever# the uni=ue key made up
of a sin+le column can contain nulls. To satisfy a composite uni=ue key# no two rows in the table or view can have the same
combination of values in the key columns. (ny row that contains nulls in all key columns automatically satisfies the constraint. 7owever#
two rows that contain nulls for one or more key columns and the same combination of values for the other key columns violate the
constraint.
6
*hen you specify a uni=ue constraint on one or more columns# ,racle implicitly creates an inde. on the uni=ue key. /f you are definin+
uni=ueness for purposes of =uery performance# then ,racle recommends that you instead create the uni=ue inde. e.plicitly usin+ a
'8-(T- 9N/>9- /ND-J statement. %ee '8-(T- /ND-J for more information.
)estrictions on 0ni3ue Constraints
None of the columns in the uni=ue key can be of 0,&# 0,NK# 0,NK 8(*# N(88(@# N-%T-D T(&0-# ,&?-'T# 8-# T/A-%T(A1
*/T7 T/A- U,N-# or user-defined type. 7owever# the uni=ue key can contain a column of T/A-%T(A1 */T7 0,'(0 T/A- U,N-.
( composite uni=ue key cannot have more than 3$ columns.
@ou cannot desi+nate the same column or combination of columns as both a primary key and a uni=ue key.
@ou cannot specify a uni=ue key when creatin+ a subview in an inheritance hierarchy. The uni=ue key can be specified only for the top-
level !root" view.
*rimar" 9e" Constraints
( primary key constraint desi+nates a column as the primary key of a table or view. ( composite primary key desi+nates a combination
of columns as the primary key. *hen you define a primary key constraint inline# you need only the 18/A(8@ :-@ keywords. *hen you
define a primary key constraint out of line# you must also specify one or more columns. @ou must define a composite primary key out of
line.
( primary key constraint combines a N,T N900 and uni=ue constraint in one declaration. Therefore# to satisfy a primary key constraint)
No primary key value can appear in more than one row in the table.
No column that is part of the primary key can contain a null.
)estrictions on *rimar" 9e" Constraints
( table or view can have only one primary key.
None of the columns in the primary key can be 0,&# 0,NK# 0,NK 8(*# N(88(@# N-%T-D T(&0-# &/0-# 8-# T/A-%T(A1 */T7
T/A- U,N-# or user-defined type. 7owever# the primary key can contain a column of T/A-%T(A1 */T7 0,'(0 T/A- U,N-.
The siDe of the primary key cannot e.ceed appro.imately one database block.
( composite primary key cannot have more than 3$ columns.
@ou cannot desi+nate the same column or combination of columns as both a primary key and a uni=ue key.
@ou cannot specify a primary key when creatin+ a subview in an inheritance hierarchy. The primary key can be specified only for the
top-level !root" view.
Forei!n 9e" Constraints
( forei+n key constraint !also called a referential inte+rity constraint" desi+nates a column as the forei+n key and establishes a
relationship between that forei+n key and a specified primary or uni=ue key# called the referenced key. ( composite forei+n key
desi+nates a combination of columns as the forei+n key.
The table or view containin+ the forei+n key is called the child object# and the table or view containin+ the referenced key is called the
parent object. The forei+n key and the referenced key can be in the same table or view. /n this case# the parent and child tables are the
same. /f you identify only the parent table or view and omit the column name# then the forei+n key automatically references the primary
key of the parent table or view. The correspondin+ column or columns of the forei+n key and the referenced key must match in order
and datatype.
@ou can define a forei+n key constraint on a sin+le key column either inline or out of line. @ou must specify a composite forei+n key and
a forei+n key on an attribute out of line.
To satisfy a composite forei+n key constraint# the composite forei+n key must refer to a composite uni=ue key or a composite primary
key in the parent table or view# or the value of at least one of the columns of the forei+n key must be null.
@ou can desi+nate the same column or combination of columns as both a forei+n key and a primary or uni=ue key. @ou can also
desi+nate the same column or combination of columns as both a forei+n key and a cluster key.
@ou can define multiple forei+n keys in a table or view. (lso# a sin+le column can be part of more than one forei+n key.
8estrictions on orei+n :ey 'onstraints
None of the columns in the forei+n key can be of 0,&# 0,NK# 0,NK 8(*# N(88(@# N-%T-D T(&0-# &/0-# 8-# T/A-%T(A1
*/T7 T/A- U,N-# or user-defined type. 7owever# the primary key can contain a column of T/A-%T(A1 */T7 0,'(0 T/A- U,N-.
The referenced uni=ue or primary key constraint on the parent table or view must already be defined.
( composite forei+n key cannot have more than 3$ columns.
The child and parent tables must be on the same database. To enable referential inte+rity constraints across nodes of a distributed
database# you must use database tri++ers. %ee '8-(T- T8/KK-8 .
/f either the child or parent object is a view# then the constraint is subject to all restrictions on view constraints. %ee CNiew 'onstraintsC.
7
@ou cannot define a forei+n key constraint in a '8-(T- T(&0- statement that contains an (% sub=uery clause. /nstead# you must
create the table without the constraint and then add it later with an (0T-8 T(&0- statement.
)eferences:clause
orei+n key constraints use the referencesPclause synta.. *hen you specify a forei+n key constraint inline# you need only the
referencesPclause. *hen you specify a forei+n key constraint out of line# you must also specify the ,8-/KN :-@ keywords and one
or more columns.
4N #2T Clause
The ,N D-0-T- clause lets you determine how ,racle Database automatically maintains referential inte+rity if you remove a
referenced primary or uni=ue key value. /f you omit this clause# then ,racle does not allow you to delete referenced key values in the
parent table that have dependent rows in the child table.
%pecify '(%'(D- if you want ,racle to remove dependent forei+n key values.
%pecify %-T N900 if you want ,racle to convert dependent forei+n key values to N900.
8estriction on ,N D-0-T-
@ou cannot specify this clause for a view constraint.
Chec7 Constraints
( check constraint lets you specify a condition that each row in the table must satisfy. To satisfy the constraint# each row in the table
must make the condition either T89- or unknown !due to a null". *hen ,racle evaluates a check constraint condition for a particular
row# any column names in the condition refer to the column values in that row.
The synta. for inline and out-of-line specification of check constraints is the same. 7owever# inline specification can refer only to the
column !or the attributes of the column if it is an object column" currently bein+ defined# whereas out-of-line specification can refer to
multiple columns or attributes.
,racle does not verify that conditions of check constraints are not mutually e.clusive. Therefore# if you create multiple check constraints
for a column# desi+n them carefully so their purposes do not conflict. Do not assume any particular order of evaluation of the conditions.
)estrictions on Chec7 Constraints
@ou cannot specify a check constraint for a view. 7owever# you can define the view usin+ the */T7 '7-': ,1T/,N clause# which is
e=uivalent to specifyin+ a check constraint for the view.
The condition of a check constraint can refer to any column in the table# but it cannot refer to columns of other tables.
'onditions of check constraints cannot contain the followin+ constructs)
%ub=ueries and scalar sub=uery e.pressions
'alls to the functions that are not deterministic !'988-NTPD(T-# '988-NTPT/A-%T(A1# D&T/A-U,N-# 0,'(0T/A-%T(A1#
%-%%/,NT/A-U,N-# %@%D(T-# %@%T/A-%T(A1# 9/D# 9%-8# and 9%-8-NN"
'alls to user-defined functions
Dereferencin+ of 8- columns !for e.ample# usin+ the D-8- function"
Nested table columns or attributes
The pseudocolumns '988N(0# N-JTN(0# 0-N-0# or 8,*N9A
Date constants that are not fully specified
)F Constraints
8- constraints let you describe the relationship between a column of type 8- and the object it references.
ref:constraint
8- constraints use the refPconstraint synta.. @ou define a 8- constraint either inline or out of line. ,ut-of-line specification re=uires
you to specify the 8- column or attribute you are further describin+.
or refPcolumn# specify the name of a 8- column of an object or relational table.
or refPattribute# specify an embedded 8- attribute within an object column of a relational table.
&oth inline and out-of-line specification let you define a scope constraint# a rowid constraint# or a referential inte+rity constraint on a
8- column.
8
/f the scope table or referenced table of the 8- column has a primary-key-based object identifier# then the 8- column is a user-
defined 8- column.
SC4* )F Constraints
/n a table with a 8- column# each 8- value in the column can conceivably reference a row in a different object table. The %',1-
clause restricts the scope of references to a sin+le table# scopePtable. The values in the 8- column or attribute point to objects in
scopePtable# in which object instances of the same type as the 8- column are stored.
%pecify the %',1- clause to restrict the scope of references in the 8- column to a sin+le table. or you to specify this clause#
scopePtable must be in your own schema or you must have %-0-'T privile+es on scopePtable or %-0-'T (N@ T(&0- system
privile+es. @ou can specify only one scope table for each 8- column.
)estrictions on Sco$e Constraints
@ou cannot add a scope constraint to an e.istin+ column unless the table is empty.
@ou cannot specify a scope constraint for the 8- elements of a N(88(@ column.
@ou must specify this clause if you specify (% sub=uery and the sub=uery returns user-defined 8-s.
@ou cannot subse=uently drop a scope constraint from a 8- column.
)owid )F Constraints
%pecify */T7 8,*/D to store the rowid alon+ with the 8- value in refPcolumn or refPattribute. %torin+ the rowid with the 8- value
can improve the performance of dereferencin+ operations# but will also use more space. Default stora+e of 8- values is without
rowids.
8estrictions on 8owid 'onstraints
@ou cannot define a rowid constraint for the 8- elements of a N(88(@ column.
@ou cannot subse=uently drop a rowid constraint from a 8- column.
/f the 8- column or attribute is scoped# then this clause is i+nored and the rowid is not stored with the 8- value.
)eferential %nte!rit" Constraints on )F Columns
The referencesPclause of the refPconstraint synta. lets you define a forei+n key constraint on the 8- column. This clause also
implicitly restricts the scope of the 8- column or attribute to the referenced table. 7owever# whereas a forei+n key constraint on a non-
8- column references an actual column in the parent table# a forei+n key constraint on a 8- column references the implicit object
identifier !,/D" column of the parent table.
/f you do not specify a constraint name# then ,racle +enerates a system name for the constraint of the form %@%P'n.
/f you add a referential inte+rity constraint to an e.istin+ 8- column that is already scoped# then the referenced table must be the
same as the scope table of the 8- column. /f you later drop the referential inte+rity constraint# then the 8- column will remain
scoped to the referenced table.
(s is the case for forei+n key constraints on other types of columns# you can use the referencesPclause alone for inline declaration. or
out-of-line declaration you must also specify the ,8-/KN :-@ keywords plus one or more 8- columns or attributes.
)estrictions on Forei!n 9e" Constraints on )F Columns
,racle implicitly adds a scope constraint when you add a referential inte+rity constraint to an e.istin+ unscoped 8- column.
Therefore# all the restrictions that apply for scope constraints also apply in this case.
@ou cannot specify a column after the object name in the referencesPclause.
;4%NS
( join is used to combine rows from multiple tables. ( join is performed whenever two or more tables is listed in the 8,A clause of an
%>0 statement.
%nner ;oin &sim$le <oin'
/t is is the most common type of join. /nner joins return all rows from multiple tables where the join condition is met.
%-0-'T suppliers.supplierPid# suppliers.supplierPname# orders.orderPdate
8,A suppliers# orders
*7-8- suppliers.supplierPid L orders.supplierPidM
This %>0 statement would return all rows from the suppliers and orders tables where there is a matchin+ supplierPid value in both the
suppliers and orders tables.
*e have a table called suppliers with two fields !supplierPid and supplierP name". /t contains the followin+ data)
supplierPid supplierPname
9
14444 /&A
14441 7ewlett 1ackard
1444$ Aicrosoft
14443 Nvidia
*e have another table called orders with three fields !orderPid# supplierPid# and orderPdate". /t contains the followin+ data)
orderPid supplierPid orderPdate
6441$6 14444 $443B46B1$
6441$2 14441 $443B46B13
/f we run the %>0 statement below)
%-0-'T suppliers.supplierPid# suppliers.supplierPname# orders.orderPdate
8,A suppliers# orders
*7-8- suppliers.supplierPid L orders.supplierPidM
,ur result set would look like this)
supplierPid name orderPdate
14444 /&A $443B46B1$
14441 7ewlett 1ackard $443B46B13
4uter ;oin
(nother type of join is called an outer join. This type of join returns all rows from one table and only those rows from a secondary table
where the joined fields are e=ual !join condition is met".
select suppliers.supplierPid# suppliers.supplierPname# orders.orderPdate
from suppliers# orders
where suppliers.supplierPid L orders.supplierPid!V"M
This %>0 statement would return all rows from the suppliers table and only those rows from the orders table where the joined fields are
e=ual.
The !V" after the orders.supplierPid field indicates that# if a supplierPid value in the suppliers table does not e.ist in the orders table# all
fields in the orders table will display as RnullS in the result set.
The above %>0 statement could also be written as follows)
select suppliers.supplierPid# suppliers.supplierPname# orders.orderPdate
from suppliers# orders
where orders.supplierPid!V" L suppliers.supplierPid
*e have a table called suppliers with two fields !supplierPid and name".
/t contains the followin+ data)
supplierPid supplierPname
14444 /&A
14441 7ewlett 1ackard
1444$ Aicrosoft
14443 Nvidia
*e have a second table called orders with three fields !orderPid# supplierPid# and orderPdate".
/t contains the followin+ data)
orderPid supplierPid orderPdate
6441$6 14444 $443B46B1$
6441$2 14441 $443B46B13
/f we run the %>0 statement below)
select suppliers.supplierPid# suppliers.supplierPname# orders.orderPdate
from suppliers# orders
where suppliers.supplierPid L orders.supplierPid!V"M
,ur result set would look like this)
supplierPid supplierPname orderPdate
14444 /&A $443B46B1$
14441 7ewlett 1ackard $443B46B13
1444$ Aicrosoft RnullS
14443 Nvidia RnullS
Self ;oin
10
@ou can use a self-join to simplify nested %>0 =ueries where the inner and outer =ueries reference the same table. These joins allow
you to retrieve related records from the same table. The most common case where you3d use a self-join is when you have a table that
references itself# such as the employees table shown below)
id firstPname lastPname mana+er
----------- --------------- --------------- -----------
1 1at 'rystal N900
$ Dennis Ailler 1
3 ?acob %mith 1
4 (llen 7unter $
6 Aary 9nderwood 3
2 ?oy Needham 3
/n this table# the mana+er attribute simply references the employee /D of another employee in the same table.
or e.ample# Dennis Ailler reports to 1at 'rystal. 1at is apparently the president of this company# as she reports to no one.
%uppose you3re tasked with writin+ a %>0 =uery to retrieve a list of employees and their mana+ers. @ou can3t write a basic %>0
%-0-'T statement to retrieve this information# as you need to cross reference information contained in other records within the same
table. ortunately# you can use a self-join to solve this dilemma by joinin+ the table to itself.
=iew
/n database theory# a view is a virtual or lo+ical table composed of the result set of a =uery. 9nlike ordinary tables !base tables" in a
relational database# a view is not part of the physical schema) it is a dynamic# virtual table computed or collated from data in the
database. 'han+in+ the data in a table alters the data shown in the view.
Niews can provide advanta+es over tablesM
They can subset the data contained in a table
They can join and simplify multiple tables into a sin+le virtual table
Niews can act as a++re+ated tables# where a++re+ated data !sum# avera+e etc." are calculated and presented as part of the data
Niews can hide the comple.ity of data# for e.ample a view could appear as %ales$444 or %ales$441# transparently partitionin+ the
actual underlyin+ table
Niews do not incur any e.tra stora+e overhead
Dependin+ on the %>0 en+ine used# views can provide e.tra security.
0imit the e.posure to which a table or tables are e.posed to outer world
?ust like functions !in pro+rammin+" provide abstraction# views can be used to create abstraction. (lso# just like functions# views can be
nested# thus one view can a++re+ate data from other views. *ithout the use of views it would be much harder to normalise databases
above $nd normal form. Niews can make it easier to create lossless join decomposition.
8ows available throu+h a view are not sorted. ( view is a relational table# and the relational model states that a table is a set of rows.
%ince sets are not sorted - per definition - the rows in a view are not ordered either. Therefore# an ,8D-8 &@ clause in the view
definition is meanin+less and the %>0 standard !%>0)$443" does not allow this for the subselect in a '8-(T- N/-* statement.
Niews can be read-only or updatable. /f the database system is able to determine the reverse mappin+ from the view schema to the
schema of the underlyin+ base tables# then the view is updatable. /N%-8T# 91D(T-# and D-0-T- operations can be performed on
updatable views. 8ead-only views do not support such operations because the D&A% is not able to map the chan+es to the underlyin+
base tables.
%ome systems support the definition of /N%T-(D , tri++ers on views. This techni=ue allows the definition of lo+ic that shall be
e.ecuted instead of an insert# update# or delete operation on the views. Thus# data modifications on read-only views can be
implemented. 7owever# an /N%T-(D , tri++er does not chan+e the read-only or updatable property of the view itself.
T"$es of Functions
There are several basic types and cate+ories of functions in %>0. The basic types of functions are)
(++re+ate unctions
%calar functions
A!!re!ate functions
(++re+ate functions operate a+ainst a collection of values# but return a sin+le value.
unction Description
(NK!column" 8eturns the avera+e value of a column
&/N(8@P'7-':%9A
'7-':%9A
'7-':%9AP(KK
',9NT!column" 8eturns the number of rows !without a N900 value" of a column
',9NT!I" 8eturns the number of selected rows
',9NT!D/%T/N'T column" 8eturns the number of distinct results
/8%T!column" 8eturns the value of the first record in a specified field !not supported in %>0%erver$:"
11
0(%T!column" 8eturns the value of the last record in a specified field !not supported in %>0%erver$:"
A(J!column" 8eturns the hi+hest value of a column
A/N!column" 8eturns the lowest value of a column
%TD-N!column"
%TD-N1!column"
%9A!column" 8eturns the total sum of a column
N(8!column"
N(81!column"
Scalar functions
%calar functions operate a+ainst a sin+le value# and return a sin+le value based on the input value.
unction Description
9'(%-!c" 'onverts a field to upper case
0'(%-!c" 'onverts a field to lower case
A/D!c#startE#endF" -.tract characters from a te.t field
0-N!c" 8eturns the len+th of a te.t field
/N%T8!c#char" 8eturns the numeric position of a named character within a te.t field
0-T!c#numberPofPchar" 8eturn the left part of a te.t field re=uested
8/K7T!c#numberPofPchar" 8eturn the ri+ht part of a te.t field re=uested
8,9ND!c#decimals" 8ounds a numeric field to the number of decimals specified
A,D!.#y" 8eturns the remainder of a division operation
N,*!" 8eturns the current system date
,8A(T!c#format" 'han+es the way a field is displayed
D(T-D/ !d#date1#date$" 9sed to perform date calculations
>)40* B?...
K8,91 &@... was added to %>0 because a++re+ate functions !like %9A" return the a++re+ate of all column values every time they are
called# and without the K8,91 &@ function it was impossible to find the sum for each individual +roup of column values.
@A=%N>...
7(N/NK... was added to %>0 because the *7-8- keyword could not be used a+ainst a++re+ate functions !like %9A"# and without
7(N/NK... it would be impossible to test for result conditions.
The synta. for the 7(N/NK function is)
%-0-'T column#%9A!column" 8,A table
K8,91 &@ column
7(N/NK %9A!column" condition value
Tri!!er
( database tri++er is a stored procedure that is invoked automatically when a predefined event occurs.
Database tri++ers enable D&(3s !Data &ase (dministrators" to create additional relationships between separate databases.
or e.ample# the modification of a record in one database could tri++er the modification of a record in a second database
12

You might also like