Professional Documents
Culture Documents
Chapter-I
Introduction
The database is the heart of any system. If the design is wrong then the whole
application will be wrong, either in effectiveness or performance, or even both. No
amount of clever coding can compensate for a bad database design. Sometimes
when building an application we may encounter a problem, which can only be
solved effectively by changing the database rather than by changing the code.
The biggest problem we have encountered in all these years is where different teams
handle the database design and software development. The database designers build
something according to their rules, and they then expect the developers to write code
around this design. This approach is often fraught with disaster as the database
designers often have little or no development experience, so they have little or no
understanding of how the development language can use that design to achieve the
expected results.
What is a database?
This may seem a pretty fundamental question, but unless you know what a
database consists of you may find it difficult to build one that can be used effectively.
Here is a simple definition of a database:
Over the years there have been several different ways of constructing databases,
amongst which have been the following:
Like the Hierarchical Data Model the Network Data Model also consists of nodes
and branches, but a child may have multiple parents within the network structure.
Both hierarchical and network databases, suffered from the following deficiencies
(when compared with relational databases):
• Access to the database was not via SQL query strings, but by a specific set of
API's.
• It was not possible to provide a variable WHERE clause. The only selection
mechanism was to read entries from a child table for a specific entry on a related
parent table with any filtering being done within the application code.
• It was not possible to provide an ORDER BY clause. Data was presented in the
order in which it existed in the database. This mechanism could be tuned by
specifying sort criteria to be used when each record was inserted, but this had
several disadvantages:
• Only a single sort sequence could be defined for each path (link to a
parent), so all records retrieved on that path would be provided in that sequence.
• It could make inserts rather slow when attempting to insert into the
middle of a large collection, or where a table had multiple paths each with its own set
of sort criteria.
The Relation
Keys
1. A simple key contains a single attribute.
2. A composite key is a key that contains more than one attribute.
3. A candidate key is an attribute (or set of attributes) that uniquely
identifies a row. A candidate key must possess the following properties:
• Unique identification - For every row the value of the key must uniquely
identify that row.
• Non-redundancy - No attribute in the key can be discarded without
destroying the property of unique identification.
4. A primary key is the candidate key, which is selected as the principal
unique identifier. Every relation must contain a primary key. The primary key is
usually the key selected to identify a row when the database is physically
implemented. For example, a part number is selected instead of a part
description.
5. A superkey is any set of attributes that uniquely identifies a row. A
superkey differs from a candidate key in that it does not require the non-
redundancy property.
6. A foreign key is an attribute (or set of attributes) that appears as a non-key
attribute in one relation and as a primary key attribute in another relation.
• A many-to-many relationship can only be implemented by introducing an
intersection or link table, which then becomes the child in two one-to-
many relationships. The intersection table therefore has a foreign key for
each of its parents, and its primary key is a composite of both foreign
keys.
• A one-to-one relationship requires that the child table have no more than
one occurrence for each parent, which can only be enforced by letting the
foreign key also serve as the primary key.
7. A semantic or natural key is a key for which the possible values have an
obvious meaning to the user or the data. For example, a semantic primary key
for a COUNTRY entity might contain the value 'USA' for the occurrence
describing the United States of America. The value 'USA' has meaning to the
user.
8. A technical or surrogate or artificial key is a key for which the possible
values have no obvious meaning to the user or the data. These are used instead
of semantic keys for any of the following reasons:
• When the value in a semantic key is likely to be changed by the user, or
can have duplicates. For example, on a PERSON table it is unwise to use
PERSON_NAME as the key as it is possible to have more than one person
with the same name, or the name may change such as through marriage.
• When none of the existing attributes can be used to guarantee uniqueness.
In this case adding an attribute whose value is generated by the system, e.g
from a sequence of numbers, is the only way to provide a unique value.
Typical examples would be ORDER_ID and INVOICE_ID. The value
'12345' has no meaning to the user, as it conveys nothing about the entity
to which it relates.
9. A key functionally determines the other attributes in the row, thus it is
always a determinant.
10. Note that the term 'key' in most DBMS engines is implemented as an
index, which does not allow duplicate entries.
Relationships
One table (relation) may be linked with another in what is known as a relationship.
Relationships may be built into the database structure to facilitate the operation
of relational joins at runtime.
Relational Joins
The join operator is used to combine data from two or more relations (tables) in
order to satisfy a particular query. Two relations may be joined when they share at
least one common attribute. The join is implemented by considering each row in an
instance of each relation. A row in relation R1 is joined to a row in relation R2 when
the value of the common attribute(s) is equal in the two relations. The join of two
relations is often called a binary join.
The join of two relations creates a new relation. The notation 'R1 x R2' indicates the
join of relations R1 and R2. For example, consider the following:
Relation R1 Relation R1 x R2
Relation R2
A B C A B C D E
B D E
1 5 3 1 5 3 7 8
4 7 4
2 4 5 2 4 5 7 4
6 2 3
8 3 5 8 3 5 2 2
5 7 8
9 3 3 9 3 3 2 2
7 2 3
1 6 5 1 6 5 2 3
3 2 2
5 4 3 5 4 3 7 4
2 7 5 2 7 5 2 3
Note that the instances of relation R1 and R2 contain the same data values for
attribute B. Data normalization is concerned with decomposing a relation (e.g. R
(A,B,C,D,E) into smaller relations (e.g. R1 and R2). The data values for attribute B
in this context will be identical in R1 and R2. The instances of R1 and R2 are
projections of the instances of R (A,B,C,D,E) onto the attributes (A,B,C) and
(B,D,E) respectively. A projection will not eliminate data values - duplicate rows are
removed, but this will not remove a data value from any attribute.
The join of relations R1 and R2 is possible because B is a common attribute. The
result of the join is shown above. The row (2 4 5 7 4) was formed by joining the row
(2 4 5) from relation R1 to the row (4 7 4) from relation R2. The two rows were
joined since each contained the same value for the common attribute B. The row (2
4 5) was not joined to the row (6 2 3) since the values of the common attribute (4
and 6) are not the same. The relations joined in the preceding example shared
exactly one common attribute. However, relations may share multiple common
attributes. All of these common attributes must be used in creating a join. For
example, the instances of relations R1 and R2 in the following example are joined
using the common attributes B and C:
Relation R1 Relation R2
Relation R1 x R2
A B C B C D
A B C D
6 1 4 1 4 9
6 1 4 9
8 1 4 1 4 2
6 1 4 2
5 1 2 1 2 1
8 1 4 9
2 7 1 7 1 2
8 1 4 2
7 1 3
5 1 2 1
2 7 1 2
2 7 1 3
The row (6 1 4 9) was formed by joining the row (6 1 4) from relation R1 to the row
(1 4 9) from relation R2. The join was created since the common set of attributes (B
and C) contained identical values (1 and 4). The row (6 1 4) from R1 was not joined
to the row (1 2 1) from R2 since the common attributes did not share identical
values - (1 4) in R1 and (1 2) in R2. The join operation provides a method for
reconstructing a relation that was decomposed into two relations during the
normalization process. The join of two rows, however, can create a new row that
was not a member of the original relation. Thus invalid information can be created
during the join process.
Lossless Joins
A set of relations satisfies the lossless join property if the instances can be joined
without creating invalid data (i.e. new rows). The term lossless join may be
somewhat confusing. A join that is not lossless will contain extra, invalid rows. A
join that is lossless will not contain extra, invalid rows. To give an example of
incorrect information created by an invalid join let us take the following data
structure:
Assuming that only one section of a class is offered during a semester we can define
the following functional dependencies:
4. (COURSE) INSTRUCTOR
• R3 (COURSE, INSTRUCTOR)
R1
STUDENT HOUR COURSE
Smith 8:00 Math 1
Jones 8:00 English
Brown 8:00 English
Green 9:00 Algebra
R3
COURSE INSTRUCTOR
Math 1 Jenkins
English Goldman
Algebra Jenkins
R4
INSTRUCTOR HOUR ROOM
Jenkins 8:00 100
Goldman 8:00 200
Jenkins 9:00 400
Now suppose that a list of courses with their corresponding room numbers is
required. Relations R1 and R4 contain the necessary information and can be joined
using the attribute HOUR. The result of this join is:
R1 x R4
STUDENT COURSE INSTRUCTOR HOUR ROOM
Smith Math 1 Jenkins 8:00 100
Smith Math 1 Goldman 8:00 200
Jones English Jenkins 8:00 100
Jones English Goldman 8:00 200
Brown English Jenkins 8:00 100
Brown English Goldman 8:00 200
Green Algebra Jenkins 9:00 400
This join creates the following invalid information (denoted by the colored rows):
• Smith, Jones, and Brown take the same class at the same time from two
different instructors in two different rooms.
• Jenkins teaches Math 1 and Algebra simultaneously at both 8:00 and 9:00.
A correct sequence is to join R1 and R3 (using COURSE) and then join the resulting
relation with R4 (using both INSTRUCTOR and HOUR). The result would be:
R1 x R3
STUDENT COURSE INSTRUCTOR HOUR
Smith Math 1 Jenkins 8:00
Jones English Goldman 8:00
Brown English Goldman 8:00
Green Algebra Jenkins 9:00
(R1 x R3) x R4
STUDENT COURSE INSTRUCTOR HOUR ROOM
Smith Math 1 Jenkins 8:00 100
Jones English Goldman 8:00 200
Brown English Goldman 8:00 200
Green Algebra Jenkins 9:00 400
Extracting the COURSE and ROOM attributes (and eliminating the duplicate row
produced for the English course) would yield the desired result:
COURSE ROOM
Math 1 100
English 200
Algebra 400
The correct result is obtained since the sequence (R1 x r3) x R4 satisfies the lossless
join property.
A relational database is in 4th normal form when the lossless join property can be
used to answer unanticipated queries. However, the choice of joins must be
evaluated carefully. Many different sequences of joins will recreate an instance of a
relation. Some sequences are more desirable since they result in the creation of less
invalid data during the join operation.
R1 x R3
(R1 x R3) x R2 or
((R1 x R3) x R2) x
R4
2. That is, if we know the value of one (or several) data items, then we can
find the value of another (or several).
Modification Anomalies
Another method is to replace the arrowhead with a crow’s foot, as shown in figure 6:
Figure 6 - One-to-Many relationship using crow’s foot notation
The relating line can be enhanced to indicate cardinality, which defines the
relationship between the entities in terms of numbers. An entity may be optional
(zero or more) or it may be mandatory (one or more).
As well as using lines and circles the cardinality can be expressed using numbers, as
in:
We have now completed the logical data model, but before we can construct the
physical database there are several steps that must take place:
• Assign attributes (properties or values) to all the entities. After all, a table
without any columns will be of little use to anyone.
• Refine the model using a process known as 'normalisation'. This ensures that
each attribute is in the right place. During this process it may be necessary to
create new tables and new relationships.
Data Normalization
The guidelines for developing relations in 3rd Normal Form can be summarized as
follows:
• Check relations for dependencies of one non key attribute with another
non key attribute.
12. Combine relations with identical primary keys (3rd normal form) if
there are no transitive dependencies.
A table is in first normal form if all the key attributes have been defined
and it contains no repeating groups.
Taking the ORDER entity in figure 7 as an example we could end up with a set of
attributes like this:
ORDER
order_id customer_id product1 product2 product3
123 456 abc1 def1 ghi1
456 789 abc2
ORDER
order_id customer_id
123 456
456 789
In order to create a table that is in first normal form we must extract the repeating
groups and place them in a separate table, which we call ORDER_LINE.
ORDER_LINE
order_id product
123 abc1
123 def1
123 ghi1
456 abc2
Each row contains one product for one order, so this allows an order to contain any
number of products.
A table is in third normal form (3NF) if and only if it is in 2NF and every
non-key attribute is non transitively dependent on the primary key (i.e.
there are no transitive dependencies).
• The keys contain more than one attribute (they are composite keys).
Note that no two buildings on any of the university campuses have the same name,
thus ROOM/BLDG CAMPUS. As the determinant is not a candidate key this
table is NOT in Boyce-Codd normal form.This table should be decomposed into the
following relations:
R2(room/bldg, campus)
• (student#, course#)
• (student#, c_name)
The relation is in 3NF but not in BCNF because of the following dependencies:
• student# s_name
• course# c_name
This table is difficult to maintain since adding a new hobby requires multiple new
rows corresponding to each skill. This problem is created by the pair of multi-valued
dependencies EMPLOYEE# SKILLS and EMPLOYEE# HOBBIES. A much
better alternative would be to decompose INFO into two relations:
skills(employee#, skill)
hobbies(employee#, hobby)
5th (Projection-Join) Normal Form
... and there are no pairwise cyclical dependencies in the primary key
comprised of three or more attributes.
• Anomalies can occur in relations in 4NF if the primary key has three or more
fields.
• For any one you must know the other two (cyclical).
This is used to track buyers, what they buy, and from whom they buy.
The question is, what do you do if Claiborne starts to sell Jeans? How many records
must you create to record this fact?
The problem is there are pairwise cyclical dependencies in the primary key. That is,
in order to determine the item you must know the buyer and vendor, and to
determine the vendor you must know the buyer and the item, and finally to know the
buyer you must know the vendor and the item. The solution is to break this one table
into three tables; Buyer-Vendor, Buyer-Item, and Vendor-Item.
Unfortunately lots of relations are not in DKNF in the first place. For example,
suppose there's a constraint on R to the effect that R must contain at least ten tuples.
Then that constraint is certainly not a consequence of the domain and key
constraints that apply to R, and so R is not in DKNF. The sad fact is, not all relations
can be reduced to DKNF; nor do we know the answer to the question "Exactly when
can a relation be so reduced?"
Introduction to Database Design
Database design is a complex subject, no matter how easy some people think it is. This
session only scratches the surface, but it is a good scratch. A properly designed database
is a model of a business, or some "thing" in the real world. Like their physical model
counterparts, data models enable you to get answers about the facts that make up the
objects being modeled. It's the questions that need answers that determine which facts
need to be stored in the data model. In the relational model, data is organized in tables
that have the following characteristics: every record has the same number of facts; every
field contains the same type of facts in each record; there is only one entry for each fact;
no two records are exactly the same; the order of the records and fields is not important.
Why Design?
Accurate design is crucial to the operation of a reliable and efficient information system.
The design of a database has to do with the way data is stored and how that data is
related. The design process is performed after you determine exactly what information
needs to be stored and how it is to be retrieved. The more carefully you design, the better
the physical database meets users' needs. In the process of designing a complete system,
you must consider user needs from a variety of viewpoints.
The following section explains some common problems resulting from poor database
design. The problems can be grouped under two categories: redundant data and
modification anomalies.
Redundant Data
Modification Anomaly
Deletion Anomaly
Insertion Anomaly
As you have seen, database design plays a major role in the stability and the reliability of
your data. In this section, we show you the process of designing a database. To help
illustrate the design process, a database named Rags is created for a fictitious wholesale
clothing manufacturer called Unlimited Rags.
Although there are a number of rules that can be followed in designing a database
structure, the design process is as much an art as it is a science. Follow these rules when
at all possible, but not to the point where the database loses the functionality that is so
important to the user. Doing a paper design first has several advantages:
There is a great deal of information associated with every object. In this step, you should
list the facts about an object and then eliminate the facts that are not important to the
solution of the problem. The customer object, for example, can have many facts
associated with it: company name, address, city, founders, number of employees, stock
price. In this case, it is not important to keep information about the number of employees,
stock price, or founders. Unlimited Rags needs only the information it will use now and
possibly in the future.
Step 3: Turn the Objects and Facts into Tables and Columns
Objects automatically become tables, and facts become columns once the column
domains are determined. Recall that a domain is a set of values that a column can have.
Every column has a domain, which has both physical and logical properties. For example,
the column for employee last name is defined as TEXT 15. TEXT 15 is the physical
property of the column. Because of this definition, its domain is the set of all employee
last names with 15 characters or less. The following is a list of the preliminary tables,
columns, and domains for Unlimited Rags:
To determine the relationship among the objects, take each object and look at how that
object may be related to another. The relationships that are important are those that allow
you to model the database after the real-world situation that the database represents.
One-to-one relationships. For any given row in Table A, there is only one row in Table
B. For any given row in Table B, there is only one row in Table A.
One-to-many relationships. For any given row in Table A, there are many rows in Table
B. For any given row in Table B, there is only one row in Table A.
Many-to-many relationships. For any given row in Table A, there are many rows in
Table B. For any given row in Table B, there are many rows in Table A.
An effective method to find the type of relationship is to ask whether a specific record in
Table A can point to (is linked to) one or to many rows in Table B, and then reverse the
tables and ask the question again.
A key can be an account number, social security number, or any other numeric value or
combination of characters that are unique. A complex key is one that is derived from
more than one column. No other row in the table can have the value of the key column(s).
Other tables may share the same set of key information. If a company name is universally
unique, it is used as a unique row identifier. However, if there is any possibility another
company could have the same name, and then it is not unique and must not be employed
as a key column. Do not use any column as a key where the possibility exists for a
duplicate. A key column cannot contain null values. By definition, all key columns
should be indexed. Because text names are usually not unique and cannot be used in math
operations, it is useful to make key columns a sequential numeric value. In many cases, it
is easier to develop your own unique row identifier.
If you have been careful about designating key columns, you also have determined the
linking columns. Links provide a way to tie information (rows) in one table to another
table. If a table has a key column, that column can generally serve as the link. Tables are
linked together through their key columns. However, the placement of the key is
important, and where the link is placed depends on the type of relationship between the
tables.
To determine the placement of the links, you must first know the type of relationship
among the objects or tables. Once you know the type of relationship among tables, it is
much easier to determine where to place the linking column to tie two tables together.
Often the information we get from a database comes from more than one table. For
example, if we want to know who the parent of a particular dependent is, the name is
determined by using the value in empid to look up the correct row in the employee table.
The question of who the parent is can be answered only if there is a row in the employee
table with an empid value corresponding to that in the dependent table. To ensure the
integrity of the data in our database, our model should require, for example, that no row
could be added to the dependent table, unless there is already a corresponding row in the
employee table. This requirement is known as a relationship constraint. In this case, a
constraint must exist on the dependent table that ensures that the employee (parent)
exists. There are at least four methods to implement relationship constraints:
The next step in the design process is the evaluation of the design. In this step, you
should look for any design flaws that could cause the data to be unreliable, unstable, or
redundant.
Every table should be evaluated by asking the following questions:
1. Does each table have a single theme? It should. Each column should be a fact
about the key.
2. Does each table have a key column(s)? It should.
3. Are there any dependencies? Only logical consequences of the key should exist.
4. Are the domains unique among tables? Do not mix domains unless the column is
common between tables.
5. Are the restrictions domain or key?
6. Is the table easy to use?
CUSTID COMPANY
CADD1
CADD2
CCITY
CSTATE
CZIP
CAC
CTELPH
CONTACT
TITLE
The table does not have any dependencies that are not logical consequences of the key.
Given custid, a company and company address can be uniquely determined. Given a
company, we cannot determine any particular custid. Given a state, we cannot determine
any particular custid. Therefore, the customer table does not have any dependencies. The
column names are not used in any other tables except for custid, which is a foreign key in
other table. The restrictions are domain or key.
Once the database had been designed on paper, the next step is to implement the design
in Microsoft Access or Oracle or any other development tool. The following is a list of
the final tables, columns, and domains for Unlimited Rags, including linking columns:
Summary
By following the nine-step design process, the problems of data redundancy, changing
multiple occurrences of data, and deletion and insertion anomalies can be avoided. It is
well worth the time spent in the design process to ensure a reliable and flexible system.
Design to the point where redundancy is eliminated or controlled. As you design your
database, keep in mind the following list of common database errors to avoid: