Professional Documents
Culture Documents
Database Management Systems Handout
Database Management Systems Handout
Database Management Systems Handout
Table of contents
1. Introduction
2.1Conceptual level
2.2 Internal level
2.3 External level
2.4 Other Architectures
3. Database Models
pdfMachine
A pdf writer that produces quality PDF files with ease!
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
6. Normalization
pdfMachine
A pdf writer that produces quality PDF files with ease!
2
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Chapter 1
1. Introduction
1.1 Data, information, information System (Data Processing)
Data: is a collection of raw facts.
Information: is a processed data in the form that is meaningful to the user.
Information System (Data processing) is a system that:
Receives data and instruction
Processes the data as per the instruction
Produces output
Stores data/information for future use
1.2 Information System and Organization, database system
Information System doesn’t exist without organization. That is, organization of
data is necessary if data is voluminous.
Information System is a support system for the organizational activity to achieve a
certain goal.
A database system is basically a computerized record keeping system. Users of
the database can perform a variety of operations. Such as:
Adding new data to empty file
Adding new data to existing file
Retrieving data from existing file
Modifying data to existing file
Deleting data from existing file
Searching for target information
1.3 Alternative approaches of data handling
Reasons to study alternative approaches:
pdfMachine
A pdf writer that produces quality PDF files with ease!
3
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
II) If you want to convert these approaches to a database system, understanding how
these systems work will be extremely useful.
a) Manual
Disadvantages:
File based approaches were an early attempt to computerize the manual filing
system.
It is s a collection of application programs that performs services for the end
users.
Each program defines and manages its own data.
Each department has its own database
Programming languages were used.
Example of programming languages: C++, Cobol, Pascal
When data is isolated in separate files, it is difficult to access data that should be
available. This is because; there is no concept of relationship between files. Therefore, we
need to create a temporary file for the participating files.
pdfMachine
A pdf writer that produces quality PDF files with ease!
4
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
3. Data Dependence
pdfMachine
A pdf writer that produces quality PDF files with ease!
5
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
The organization of the database becomes necessary when the data is voluminous.
Otherwise, managing data will be very difficult.
E.g. A Manufacturing Company with product data
A Bank with account data
A Hospital with patients
A University with Students
A government with planning data
A DBMS is software that enables users to define, create, maintain and control access to
the database.
Example: Ms Access, FoxPro, SQL Server, MySQL, Oracle …
The data stored in the database can be thought of as several distinct data files.
In a multi-user environment, data can be shared.
pdfMachine
A pdf writer that produces quality PDF files with ease!
6
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
A database contains both the operational data & the meta-data (data about data).
(II) Hardware: - The processor, storage media and the memory of the computer system
(III) Software: The database management system allows the user to interact with the
data.
pdfMachine
A pdf writer that produces quality PDF files with ease!
7
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
e) Data redundancy is reduced by integrating the files so that multiple copies of the
same data will not be stored.
Database integrity refers to the validity and consistency of stored data. Integrity is
usually expressed in terms of constraints, which are consistency rules that the data
base is not permitted to violate.
J) Improved security
pdfMachine
A pdf writer that produces quality PDF files with ease!
8
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Disadvantages of DBMS:
Tasks of DBA:
pdfMachine
A pdf writer that produces quality PDF files with ease!
9
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Cost and benefit analysis is required and computerization must be carried out if the
benefit outweighs the cost.
A) Tangible cost
pdfMachine
A pdf writer that produces quality PDF files with ease!
10
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Chapter 2
Architecture of a database system
The objective of the three levels architecture is to separate each user’s view of the
database from the way the database is physically represented. That is, to provide users
with an abstract view of data by hiding certain details of how data is stored and
manipulated.
a) Internal Level
b) Conceptual Level
c) External Level
Conceptual
Schema
Internal
Schema
pdfMachine
A pdf writer that produces quality PDF files with ease!
11
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Note: Each organization has one physical and one conceptual schema and one or more
user views. This means, the three levels architecture defines one database with
multiple views.
For the system to be usable, it must retrieve data efficiently. This concern has led to the
design of complex data structure for the representation of data in the database. Since
many database systems users are not computer literates, developers hides the complexity
from users through several levels of abstraction, to simplify user’s interactions with the
system:
a) Internal Level
It is the one closest to physical storage. That is, it is concerned with the way the
data is physically stored.
It is the way the DBMS and the Operating system perceive the data.
It is concerned with how fields are represented, what physical sequence the stored
records are in, …
Physical schema contains the specifications for how data from a conceptual
schema are stored in a computer secondary memory.
It deals with assembly and similar language commands.
b) Conceptual/Logical level
It is the community view of the database as seen by the DBA.
It is a representation of the entire information in a particular enterprise.
This is the next-higher level of abstraction next to internal level that describes
what data are stored in the database, and what relationships exist among those
data.
It includes Entity-Relationship modeling, security and integrity constraints.
Although implementation of the simple structure at the logical level may involve
complex physical-level structures, the user of the logical level does not need to be
aware of this complexity.
The logical level of abstraction is used by database administrators, who must
decide what information should be kept in the database.
pdfMachine
A pdf writer that produces quality PDF files with ease!
12
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
c) External/View level
The highest level of abstraction describes only some part of the entire database.
It is concerned with how individual users view the database.
Despite the use of simpler structures at the logical level, some complexity
remains. Because of the large size of the database, many users of the database
system will not be concerned with all this information. Instead, such users need
to access only a part of the database. So that their interaction with the system is
simplified and the view level of abstraction is defined.
The system provides many views for the same database.
Analogies to the concepts of data types, variables, and values in programming languages
are useful here. Consider the supplier - record type definition. To declare such variables
in a C++ like language, we write
Supplier supplier1;
Database systems have several schemas, partitioned according to the levels of abstraction
that we discussed. At the lowest level is the physical schema; at the intermediate level is
the logical schema; and at the highest level is a subschema. In general, database systems
support one physical schema, one logical schema and several subschemas.
2.4 Mappings
The DBMS is responsible for mapping between the 3 types of schema.
The conceptual schema is related to the internal schema through a
Conceptual/Internal mapping.
Each External schema is related to the conceptual schema by the
External/Conceptual mapping.
Physical data Independence: is the ability to modify the physical schema without
causing application programs to be rewritten. Modifications at the physical level are
occasionally necessary to improve performance.
Logical data independence: is the ability to modify the logical schema without causing
application programs to be rewritten. Modifications at the logical level are necessary
whenever the logical structure of the database is altered (for example, when money-
market accounts are added to a banking system).
pdfMachine
A pdf writer that produces quality PDF files with ease!
14
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Chapter 3
Database Models
A model is a representation of real world objects and events and their associations.
Data model can be divided into four:
1) Hierarchical Model
2) Network Model
3) Relational Model
4) Object oriented Model
1) Hierarchical Model
Consists of an ordered set of trees in a parent child mode.
Records are represented by rectangles.
Allows a node to have only one parent.
It has two data structure concepts:
Records and PCR (parent child relationship).
1-1 or 1-M link is allowed.
Connection between child and its parent is called a Link.
pdfMachine
A pdf writer that produces quality PDF files with ease!
15
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
3) Relational Model
A relational system is a system in which the data in a database is perceived by the
user a table.
It is based on relational mathematics.
New table is generated from old tables by using data manipulation operations.
Most current DBMS technologies use relational model
Properties of relations
No duplication of records:
Mathematical sets also do not include duplicate elements.
Records are unordered:
Sets in maths are not ordered.
Fields are unordered:
All field values are atomic.
pdfMachine
A pdf writer that produces quality PDF files with ease!
16
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
pdfMachine
A pdf writer that produces quality PDF files with ease!
17
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Chapter 4
The Relational Model
Introduction
A relational model is a model that is perceived by the user as table.
History
The relational model was invented by E.F. (Ted) Codd as a general model of data, and
subsequently maintained and developed by Chris Date and Hugh Darwen.
Basic Structure
1. The following tables show deposit and customer tables for our banking example.
pdfMachine
A pdf writer that produces quality PDF files with ease!
18
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
We will use the terms relation and tuple in place of table and row from now on.
pdfMachine
A pdf writer that produces quality PDF files with ease!
19
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Database Scheme
pdfMachine
A pdf writer that produces quality PDF files with ease!
20
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
4. The relation schemes for the banking example used throughout the text are:
Note: some attributes appear in several relation schemes (e.g. bname, cname).
This is legal, and provides a way of relating tuples of distinct relations.
Keys
1. The notions of superkey, candidate key and primary key all apply to the
relational model.
2. For example, in Branch-scheme,
o {bname} is a superkey.
o {bname, bcity} is a superkey.
o {bname, bcity} is not a candidate key, as the superkey {bname} is
contained in it.
o {bname} is a candidate key.
o {bcity} is not a superkey, as branches may be in the same city.
o We will use {bname} as our primary key.
pdfMachine
A pdf writer that produces quality PDF files with ease!
21
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Query Languages
2. A complete query language also contains facilities to insert and delete tuples as
well as to modify parts of existing tuples.
Mathematical Foundation
The fundamental assumption of the relational model is that all data are represented as
mathematical n-ary relations, an n-ary relation being a subset of the Cartesian product of
n domains. In the mathematical model, reasoning about such data is done in two-valued
predicate logic, meaning there are two possible evaluations for each proposition: either
true or false (and in particular no third value such as unknown, or not applicable, either of
which are often associated with the concept of NULL). Data are operated upon by means
of a relational calculus or algebra, these being equivalent in expressive power.
The relational model of data permits the database designer to create a consistent, logical
representation of information. Consistency is achieved by including declared constraints
in the database design, which is usually referred to as the logical schema. The theory
includes a process of database normalization whereby a design with certain desirable
properties can be selected from a set of logically equivalent alternatives.
pdfMachine
A pdf writer that produces quality PDF files with ease!
22
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
The basic relational building block is the domain or data type, usually abbreviated
nowadays to type. A tuple is an unordered set of attribute values. An attribute is an
ordered pair of attribute name and type name. An attribute value is a specific valid value
for the type of the attribute. This can be either a scalar value or a more complex type.
A relation consists of a heading and a body. A heading is a set of attributes. A body (of
an n-ary relation) is a set of n-tuples. The heading of the relation is also the heading of
each of its tuples.
A relation is defined as a set of n-tuples. In both mathematics and the relational database
model, a set is an unordered collection of items, although some DBMSs impose an order
to their data. In mathematics, a tuple, has an order, and allows for duplication.
Interpretation
To fully appreciate the relational model of data it is essential to understand the intended
interpretation of a relation.
There is a one-to-one correspondence between the free variables of the predicate and the
attribute names of the relation heading. Each tuple of the relation body provides attribute
values to instantiate the predicate by substituting each of its free variables. The result is a
proposition that is deemed, on account of the appearance of the tuple in the relation body,
to be true. Contrariwise, every tuple whose heading conforms to that of the relation but
which does not appear in the body is deemed to be false.
pdfMachine
A pdf writer that produces quality PDF files with ease!
23
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Application to databases
A type in a typical relational database might be an int, a char, a boolean, and so on. A
type name would be the string "int", "char", "boolean", etc. An attribute is part of
declaring a table data structure and specifies a column name and the type of the value that
goes under it. A tuple is basically the same thing as a row, except in SQL DBMS, where
rows are ordered. Tuples should not be ordered. An attribute name might be "name" or
"age". An attribute value is a specific entry in a specific column and row, such as "John
Doe" or "35".
A relation is a specification for a table data structure along with the data in it. A heading
is defined by the declaration of the table data structure. A body is the data that goes into
the table data structure.
SQL standard
SQL, initially pushed as the standard language for relational databases, deviates from the
relational model in several places. The current ISO SQL standard doesn't mention the
relational model or use relational terms or concepts. However, it is possible to create a
database conforming to the relational model using SQL if one does not use certain SQL
features.
The following deviations from the relational model have been noted in SQL. Note that
few database servers implement the entire SQL standard and in particular do not allow
some of these deviations. Whereas NULL is nearly ubiquitous, for example, allowing
duplicate column names within a table or anonymous columns is uncommon.
Duplicate rows
The same row can appear more than once in an SQL table. The same tuple cannot appear
more than once in a relation.
pdfMachine
A pdf writer that produces quality PDF files with ease!
24
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Anonymous columns
A column in an SQL table can be unnamed and thus unable to be referenced in
expressions. The relational model requires every attribute to be named and reference-
able.
NULL
This special mark can appear instead of a value wherever a value can appear in SQL, in
particular in place of a column value in some row. It is used to indicate unknown values
of fields for the time being. The comparison NULL with something other than itself does
not yield false but instead yields unknown. It is because of this behavior in comparisons
that NULL is described as a mark rather than a value.
pdfMachine
A pdf writer that produces quality PDF files with ease!
25
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Chapter 5
The Entity-Relationship Model
Introduction
An entity-relationship model (ERM) is a model that provides a high-level description of a
conceptual data model. Data modeling provides a graphical notation for representing
such data models in the form of entity-relationship diagrams (ERD).
The whole purpose of ER modeling is to create an accurate reflection of the real world in
a database. The ER model doesn’t actually give us a database description. It gives us an
intermediate step from which it is easy to define a database. Let’s look at an example.
The E-R data model is based on a perception of a real world that consists of a set of basic
objects called entities, and of relationships among these objects. It was developed to
facilitate database design by allowing the specification of an enterprise schema, which
represents the overall logical structure of a database.
The E-R data model is one of several semantic data models; the semantic aspect of the
model lies in the attempt to represent the meaning of the data. The E-R model is
extremely useful in mapping the meanings and interactions of real-world enterprises onto
a conceptual scheme. Because of this utility, many database design tools draw on
concepts from the E-R model.
A data model in which information stored in the database is viewed as sets of entities and
sets of relationships among entities. There are three basic notions that the ER Model
employs: entity sets, relationships, and attributes.
pdfMachine
A pdf writer that produces quality PDF files with ease!
26
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Entity: something that exists and can be distinguished from other entities.
Examples:
Entity sets need not be disjoint. Example: a person entity could be in both the
customer and employee sets
Suppose you are presented with the following situation and are told to create a database
for it:
Every department within our company is in only one division. Each division has more
than one department in it. We don’t have an upper limit on the number of departments
that a division can have. For example, the New Business Development---the one
managed by Mackenzie---and Higher Education departments are both in the Marketing
division.
An entity type is a collection of entities that share a common definition. An entity is a
person, place, concept, or thing about which the business needs data.
For instance, Department is the name of one entity type. The Marketing division is an
instance of the Division entity type. Mackenzie is one instance of the Employee entity
type. Instances of entity types are referred to as entities. Person is an idea (entity type)
while Scott, Nancy, Lindsey, and Mackenzie are touchable (entities). Entity types provide
us with a means for making generalizations about entities.
For example, instead of saying “Every department within our company is in only one
division,” we could have gone down the list of all departments (that is, all entities with
entity type Department) and asserted that each one is, indeed, in one division.
In ER modeling we look for relationships among entity types because it is easier and
more concise to speak of relationships among general entity types rather than the
touchable entities themselves.
pdfMachine
A pdf writer that produces quality PDF files with ease!
27
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Determining the relationships among entity types is another important step in the process
of ER modeling.
The defining characteristic of a relationship is that several entity types are involved. So
something like a name or birth date would not be a relationship since only one entity is
involved.
Now we have identified three entity types (Employee, Department, Division) and two
relationships among these entity types (manages, contains).
ER models are usually represented graphically. The language we are going to use
represents entity types as rectangles and relationships as diamonds. Below is the
representation of the situation we are working with.
Notice that the “contains” relationship is drawn between the two entities that it is
associated with. Similarly for “manages” relationship. This (simplified) ER model tells us
that:
Consider the relationship between divisions and departments. We know that divisions
have multiple departments and departments can only be contained within one division.
Or, for every one division there can be many departments. In the language of ER
modeling this is called a 1: M (read: “one to many”) relationship.
5.1 Relationships
Relationships define which entity types are directly associated with which other entity
types. In the example in an earlier section, we saw that divisions are directly associated
with departments and departments are directly associated with employees. No direct
association between division and employee was given.
pdfMachine
A pdf writer that produces quality PDF files with ease!
28
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
This does not mean that there is no relationship between division and employee. In fact,
the ER diagram tells us that there is a relationship between the two:
Given any one division, there can be many employees managing departments within that
division.
An ER diagram should contain the minimum number of relationships necessary to reflect
the situation.
5.1.1 Cardinality
Once a relationship between entity types has been established, the analyst should
determine its cardinality.
A relationship’s cardinality defines the maximum number of entities of one type that can
be associated with an entity of another type.
For relationships between two entity types, there are three basic cardinalities. Each of the
following descriptions are given in terms of a relationship between entity type X and
entity type Y.
1:1 --- one-to- one --- One entity of type X can be associated with, at most, one entity of
type Y. One entity of type Y can be associated with, at most, one entity of type X.
1: M --- one-to-many --- One entity of type X can be associated with many entities of
type Y. One entity of type Y can be associated with, at most, one entity of type X.
M: M --- many-to-many --- One entity of type X can be associated with many entities of
type Y. One entity of type Y can be associated with many entities of type X.).
It would seem that at any particular time a patient can only have one primary care
physician and that any physician can have many patients (M:1). One physician can
perform many operations and one operation can be performed by many physicians (M:
M). One doctor can have specialties in many diseases and one disease can be the
specialty of many doctors (M: M). One needle can be injected into one patient and one
patient can have many needles injected into him/her (M: 1).
pdfMachine
A pdf writer that produces quality PDF files with ease!
29
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Entity types are things for which it is important that your company capture data. If it is
not important, it should not be in the database. In an accounting database you would
expect to find entity types for expenses, assets, liabilities, expenditures, deposits, etc. You
would not expect to find entity types for color of check, quality of dollar bills received,
etc. The database is supposed to reflect reality---but only the part of reality that is
important to the company.
5.3 Attributes
5.3.1 Basics
To this point we have focused on entity types and relationships among them. We have
mentioned, in passing, “facts” about entity types and “attributes” of entity types.
Attributes are the characteristics of an entity type that we are interested in. An attribute
is a descriptor whose values are associated with individual entities of a specific type.
The attribute value for any single entity can have only one value at a given time. This
value can change over time. An attribute of an employee might be salary. At any one
time if you asked for the salary level of a certain employee, then you should get one
answer. And if someone else asked the same question about that employee at the exact
same time, they would expect to get the same answer. Of course, if you asked this
question at a later time you might expect to get a different answer.
5.3.2 Identifier
Every entity type has an identifier. This identifier uniquely identifies a single (at least
one, and no more than one) entity. If you know the value of the identifier, then you know
exactly which entity you are dealing with. Further, the identifier’s value will never
change over time. Thus, if you know the identifier now, then you can be confident that at
any time in the future the identifier for that entity will not have changed.
Example: Social security number is a possible identifier for a person.
pdfMachine
A pdf writer that produces quality PDF files with ease!
30
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
5.4.1 Ternary
In the real world there are relationships other than those involving two things. For
example, suppose that we want to capture which employees use which skills on which
project. We might try to represent this data in a database as three binary relationships
between skills and project, project and employee, and employee and skill.
The “applies” relationship indicates which employee applies which skill. The used on
relationship indicates which skill is used on which project. The works on relationship
indicates which employee works on which project. But this is not enough to specify
which employee uses which skill on which project. Suppose you know the following:
Works-on
Lindsey and Mackenzie have worked on projects A and B.
Applies
Lindsey has used skills interface design and database design while Mackenzie
only used her database design skill.
Used-on
Both skills have been used on both projects.
pdfMachine
A pdf writer that produces quality PDF files with ease!
31
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Implementing ternary relationships does not mean that you have to get rid of the binary
relationships. You only get rid of the binary relationships if they capture a subset of the
information captured by the ternary relationship. If a binary relationship captures
information that differs from the ternary relationship, then the binary relationship should
be retained if the information is important to your company.
The used on relationship stays the same as in the previous ER diagram. The binary
relationships are different.
Have skill
An employee has a certain skill. This is different than used on because there are some
skills that an employee has that he or she may not have used on a particular project.
Needed
A project needs a particular skill. This is different than used on because there may
be some skills for which employees have not been assigned to the project yet.
Manages
pdfMachine
A pdf writer that produces quality PDF files with ease!
32
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
5.4.2 Recursive
The final, and possibly the most difficult, relationship is the recursive relationship. This
is a relationship that an entity has with itself. But it really doesn’t have to be difficult if
you think about it as you would any ordinary binary relationship. Let’s look at an
example.
A manager manages many employees and an employee has exactly one direct manager.
This is pretty straightforward. But, now, realize that a manager is really just another name
for an employee. So, replace managers with employees in this diagram.
Not everyone in the company has a manager. The president will not have a direct
manager. This is handled in the data in the table by indicating that the president’s
manager is the president.
A person can be a member of many clubs and a club can have many members. A natural
piece of information to store is the date the person joined the club. If the attribute is of the
person entity, then this would indicate when the person joined a club but we would not
know which club. If the attribute is of the club entity, then this would indicate (possibly)
when the club was founded or (possibly) when the most recent member joined the club
but we would not know the dates on which each person joined. The solution is to make
join date an attribute of the is member relationship.
pdfMachine
A pdf writer that produces quality PDF files with ease!
33
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
A person pays for zero or more insurance policies. An insurance policy is paid for
by exactly one person.
A person is insured by zero or more insurance policies. An insurance policy
insures one or more persons.
These are two distinct relationships. They mean two different things---that is why they
are represented as two separate relationships in the ER diagram.
pdfMachine
A pdf writer that produces quality PDF files with ease!
34
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Basic
These are values provided to the business. These are the types of attributes that we have
been discussing so far. Think of name, address, etc. These values cannot be deduced
from the values of other attributes.
Derived
This is a value that can be calculated from the value of other attributes in the database.
An example might be the age of an employee when the birth date is in the database.
These attributes should, generally, not be stored in the database but should be calculated
when needed.
6 Interpreting ER diagrams
pdfMachine
A pdf writer that produces quality PDF files with ease!
35
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Needed
A skill can be needed by many projects but might not be needed by any.
A project can need one or more skills.
Manages
An employee can manage many projects but might not manage any. (Or:
An employee can manage many projects. There are some employees who
don’t manage any projects.)
A project must be managed by an employee. (Or: A project is managed by
exactly one employee.) (Or: A project is managed by one and only one
employee.)
Has-skill
An employee may have many skills but might not have any.
A skill can be possessed by many employees. There are some skills that no
employees possess.
Used-on
The technique for an n-ary (in this case 3-ary, or ternary) relation is different than
for binary relations, but still straight-forward. Hold your hand on n-1 entity types
(in this case 2) and determine whether a 1 or an m goes on the remaining arm of
the relation. Below, in order, are the project, employee, and skill arms.
ER exercises
Question 1
What is the cardinality and existence of each of the following relationships in just
the direction given? State any assumptions you have to make.
1. Husband to wife
2. Student to degree
3. Child to parent
4. Player to team
5. Student to course
pdfMachine
A pdf writer that produces quality PDF files with ease!
36
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Question 2
For each of the following pairs of rules, identify two entity types and one
relationship. State the cardinality and existence of the relationship in each case. If
you don’t think enough information is available to define either of these, then
state an assumption that makes it clear. Draw the ER diagram.
Question 3
Draw an ER diagram for the following. Be sure to indicate the existence and cardinality
for each relationship.
1. A college runs many classes. Each class may be taught by several teachers, and a
teacher may teach several classes. A particular class always uses the same room.
Because classes may meet at different times or on different evenings, it is possible
for different classes to use the same room.
Question 4
Draw an ER diagram for each of the following situations. On the diagram be sure to
identify the cardinality, existence of each relationship.
1. A hospital patient has a patient history. Each patient has one or more history
records (we assume that the initial patient visit is always recorded as an instance
of the history). Each patient history record belongs to exactly one patient.
pdfMachine
A pdf writer that produces quality PDF files with ease!
37
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
CHAPTER 6
Normalization
Normalization is a formal process for deciding which attributes should be grouped
together in a relation.
It is a technique for analyzing tables based on primary keys or candidate keys.
It is the process of decomposing relations with anomalies to produce smaller,
well- structured relations.
If a relation is not normalized we can encounter the following problems.
(i) Information Redundancy
(ii) Anomalies:
Insertion anomalies
Deletion anomalies
Update anomalies
Normal Forms
Normalization is often done as a series of steps.
i.e UNF, 1st NF, 2nd NF 3rd NF, BCNF, ...
Each normal form involves a set of rules that can be tested against each table in
the system.
If a table violates the rules of some normal form, then we decompose the table in
to tables that individually meet the requirements of normalization.
Each higher form of normalization is based on the form prior to it.
pdfMachine
A pdf writer that produces quality PDF files with ease!
38
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
- Example1: consider the above (UNF) relation called "car owners"; its equivalent 1st
Normal form is the following table.
Employee
pdfMachine
A pdf writer that produces quality PDF files with ease!
39
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Table 1
emp_id emp_Name
Table 2
emp_id emp_tele
Table 3
emp_id fax
Note:
- No non-key attribute is functionally dependent on part of the primary key.
- A relation that is in 1st normal form will be in 2NF if any one of the following
conditions applies:
pdfMachine
A pdf writer that produces quality PDF files with ease!
40
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Functional Dependencies
The functional dependency in this statement implies that the date a course is
completed is completely determined by the identity of the employee & the title of
the course.
- Determinants: - The attribute on the left hand side of the arrow in a functional
dependency is called a determinant.
Eg: SSN, ISBN, (Empid, Course_title) are determinants.
- Fully functional dependency applies to composite determinants.
pdfMachine
A pdf writer that produces quality PDF files with ease!
41
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
- Therefore, if we have a table that violates the 2NF, then the composite PK can be
used as a guide for splitting the table.
T1
Stud_id Stud_tele
pdfMachine
A pdf writer that produces quality PDF files with ease!
42
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
And
T2
Stud_id Relative_id Relationship
Now, after the split, we need to check T1 and T2 obey the rules of normalization.
Hence, T1 and T2 are in 2NF.
Now, PK = empid
We have functional dependencies:
Empid → depid
Depid → depname
Or Depid → depbudjet
Therefore, the above table is not is 3NF. To normalize it, we can use the functional
dependencies:
Depid → depname
Depid → depbudjet
And
pdfMachine
A pdf writer that produces quality PDF files with ease!
43
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Empid → depid
So that the resulting tables are the following:
T1
empid empname empsal depid
And
T2
depid depname depbudjet
Description of BCNF:
Stud_Couse
pdfMachine
A pdf writer that produces quality PDF files with ease!
44
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Note: every relation in BCNF is also in 3NF, but not vice versa.
pdfMachine
A pdf writer that produces quality PDF files with ease!
45
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Chapter 7
Structured Query Language
Introduction
What is SQL?
SQL is a Standard
SQL is an ANSI standard computer language for accessing and manipulating database
systems. SQL statements are used to retrieve and update data in a database. SQL works
with database programs like MS Access, DB2, Informix, MS SQL Server, Oracle,
Sybase, etc.
There are many different versions of the SQL language, but to be in compliance with the
ANSI standard, they must support the same major keywords in a similar manner (such as
SELECT, UPDATE, DELETE, INSERT, WHERE, and others).
Note: Most of the SQL database programs also have their own proprietary extensions in
addition to the SQL standard!
Although we refer to the SQL language as a “query language” it contains many other
capabilities besides querying a database. It includes features for defining the structure of
the data, for modifying data in the database, and for specifying security constraints.
SQL (Structured Query Language) is THE standard DML for relational database
products. The query language is based on relational algebra, but borrows from tuple
relational calculus.
pdfMachine
A pdf writer that produces quality PDF files with ease!
46
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
SQL-89
SQL-92
SQL-92 defines some additional operations that are not part of SQL-89.
A database most often contains one or more tables. Each table is identified by a name
(e.g. "Customers" or "Orders"). Tables contain records (rows) with data.
The table above contains three records (one for each person) and four columns
(LastName, FirstName, Address, and City).
SQL Queries
With SQL, we can query a database and have a result set returned.
LastName
Hansen
Svendson
Pettersen
Note: Some database systems require a semicolon at the end of the SQL statement. We
don't use the semicolon in our tutorials.
pdfMachine
A pdf writer that produces quality PDF files with ease!
47
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
These query and update commands together form the Data Manipulation Language
(DML) part of SQL:
Syntax
SELECT column_name(s)
FROM table_name
Note: SQL statements are not case sensitive. SELECT is the same as select.
SQL SELECT Example
To select the content of columns named "LastName" and "FirstName", from the database
table called "Persons", use a SELECT statement like this:
SELECT LastName,FirstName FROM Persons
The result
LastName FirstName
Hansen Ola
Svendson Tove
Pettersen Kari
pdfMachine
A pdf writer that produces quality PDF files with ease!
48
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
To select all columns from the "Persons" table, use a * symbol instead of column names,
like this:
Result
Semicolon is the standard way to separate each SQL statement in database systems that
allow more than one SQL statement to be executed in the same call to the server.
Some SQL tutorials end each SQL statement with a semicolon. Is this necessary? We are
using MS Access and SQL Server 2000 and we do not have to put a semicolon after each
SQL statement, but some database programs force you to use it.
The SELECT statement returns information from table columns. But what if we only
want to select distinct elements?
With SQL, all we need to do is to add a DISTINCT keyword to the SELECT statement:
Syntax
SELECT DISTINCT column_name(s)
FROM table_name
pdfMachine
A pdf writer that produces quality PDF files with ease!
49
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
"Orders" table
Company OrderNumber
Sega 3412
Toyota 2312
Trio 4678
Toyota 6798
Result
Company
Sega
Toyota
Trio
Toyota
To select only DIFFERENT values from the column named "Company" we use a
SELECT DISTINCT statement like this:
Result:
Company
Sega
Toyota
Trio
pdfMachine
A pdf writer that produces quality PDF files with ease!
50
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Syntax
Operator Description
= Equal
<> Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
Note: In some versions of SQL the <> operator may be written as! =
"Persons" table
LastName FirstName Address City Year
Hansen Ola Timoteivn 10 Sandnes 1951
Svendson Tove Borgvn 23 Sandnes 1978
Svendson Stale Kaivn 18 Sandnes 1980
Pettersen Kari Storgt 20 Stavanger 1960
Result
LastName FirstName Address City Year
Hansen Ola Timoteivn 10 Sandnes 1951
Svendson Tove Borgvn 23 Sandnes 1978
Svendson Stale Kaivn 18 Sandnes 1980
pdfMachine
A pdf writer that produces quality PDF files with ease!
51
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Using Quotes
Note that we have used single quotes around the conditional values in the examples.
SQL uses single quotes around text values (most database systems will also accept
double quotes). Numeric values should not be enclosed in quotes.
This is correct:
SELECT * FROM Persons WHERE FirstName='Tove'
This is wrong:
SELECT * FROM Persons WHERE FirstName=Tove
Syntax
SELECT column FROM table
WHERE column LIKE pattern
A "%" sign can be used to define wildcards (missing letters in the pattern) both before
and after the pattern.
Using LIKE
The following SQL statement will return persons with first names that start with an 'O':
The following SQL statement will return persons with first names that end with an 'a':
pdfMachine
A pdf writer that produces quality PDF files with ease!
52
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
The following SQL statement will return persons with first names that contain the pattern
'la':
AND & OR
The AND operator displays a row if ALL conditions listed are true. The OR operator
displays a row if ANY of the conditions listed are true.
Example
Use AND to display each person with the first name equal to "Tove", and the last name
equal to "Svendson":
Result:
LastName FirstName Address City
Svendson Tove Borgvn 23 Sandnes
Example
Use OR to display each person with the first name equal to "Tove", or the last name equal
to "Svendson":
Result:
LastName FirstName Address City
Svendson Tove Borgvn 23 Sandnes
Svendson Stephen Kaivn 18 Sandnes
pdfMachine
A pdf writer that produces quality PDF files with ease!
53
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Example
You can also combine AND and OR (use parentheses to form complex expressions):
Result:
LastName FirstName Address City
Svendson Tove Borgvn 23 Sandnes
Svendson Stephen Kaivn 18 Sandnes
The BETWEEN ... AND operator selects a range of data between two values. These
values can be numbers, text, or dates.
Example 1
To display the persons alphabetically between (and including) "Hansen" and exclusive
"Pettersen", use the following SQL:
Result:
pdfMachine
A pdf writer that produces quality PDF files with ease!
54
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Example 2
To display the persons outside the range used in the previous example, use the NOT
operator:
IN
The IN operator may be used if you know the exact value you want to return for at least
one of the columns.
Example 1
To display the persons with LastName equal to "Hansen" or "Pettersen", use the
following SQL:
With SQL, aliases can be used for column names and table names.
pdfMachine
A pdf writer that produces quality PDF files with ease!
55
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Family Name
Hansen Ola
Svendson Tove
Pettersen Kari
pdfMachine
A pdf writer that produces quality PDF files with ease!
56
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Table Employees:
LastName FirstName
Hansen Ola
Svendson Tove
Pettersen Kari
Orders:
Company OrderNumber
Sega 3412
ABC Shop 5678
W3Schools 2312
W3Schools 6798
Example
Result:
Company OrderNumber
ABC Shop 5678
Sega 3412
W3Schools 6798
W3Schools 2312
pdfMachine
A pdf writer that produces quality PDF files with ease!
57
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Example
Result:
Company OrderNumber
ABC Shop 5678
Sega 3412
W3Schools 2312
W3Schools 6798
Example
Example
pdfMachine
A pdf writer that produces quality PDF files with ease!
58
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Function Syntax
Types of Functions
There are several basic types and categories of functions in SQL. The basic types of
functions are:
Aggregate Functions
Scalar functions
Aggregate functions
Aggregate functions operate against a collection of values, but return a single value.
Note: If used among many other expressions in the item list of a SELECT statement, the
SELECT must have a GROUP BY clause!!
pdfMachine
A pdf writer that produces quality PDF files with ease!
59
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Scalar functions
Scalar functions operate against a single value, and return a single value based on the
input value.
GROUP BY...
GROUP BY... was added to SQL because aggregate functions (like SUM) return the
aggregate of all column values every time they are called, and without the GROUP BY
function it was impossible to find the sum for each individual group of column values.
GROUP BY Example
Company Amount
W3Schools 5500
IBM 4500
W3Schools 7100
And This SQL:
SELECT Company, SUM (Amount) FROM Sales
Company SUM(Amount)
W3Schools 17100
IBM 17100
W3Schools 17100
The above code is invalid because the column returned is not part of an aggregate. A
GROUP BY clause will solve this problem:
pdfMachine
A pdf writer that produces quality PDF files with ease!
60
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Company SUM(Amount)
W3Schools 12600
IBM 4500
HAVING...
HAVING... was added to SQL because the WHERE keyword could not be used against
aggregate functions (like SUM), and without HAVING... it would be impossible to test
for result conditions.
Company Amount
W3Schools 5500
IBM 4500
W3Schools 7100
This SQL:
Company SUM(Amount)
W3Schools 12600
The SELECT INTO statement is most often used to create backup copies of tables or for
archiving records.
Syntax
SELECT column_name(s) INTO newtable [IN externaldatabase]
FROM source
pdfMachine
A pdf writer that produces quality PDF files with ease!
61
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Syntax
INSERT INTO table_name
VALUES (value1, value2,....)
You can also specify the columns for which you want to insert data:
pdfMachine
A pdf writer that produces quality PDF files with ease!
62
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Syntax
UPDATE table_name
SET column_name = new_value
WHERE column_name = some_value
Person:
Result:
We want to change the address and add the name of the city:
UPDATE Person
SET Address = 'Stien 12', City = 'Stavanger'
WHERE LastName = 'Rasmussen'
pdfMachine
A pdf writer that produces quality PDF files with ease!
63
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Result:
Syntax
DELETE FROM table_name
WHERE column_name = some_value
Person:
Delete a Row
Result
It is possible to delete all rows in a table without deleting the table. This means that the
table structure, attributes, and indexes will be intact:
pdfMachine
A pdf writer that produces quality PDF files with ease!
64
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Tables in a database can be related to each other with keys. A primary key is a column
with a unique value for each row. Each primary key value must be unique within the
table. The purpose is to bind data together, across tables, without repeating all of the data
in every table.
In the "Employees" table below, the "Employee_ID" column is the primary key, meaning
that no two rows can have the same Employee_ID. The Employee_ID distinguishes two
persons even if they have the same name.
Employees:
Employee_ID Name
01 Hansen, Ola
02 Svendson, Tove
03 Svendson, Stephen
04 Pettersen, Kari
Orders:
We can select data from two tables by referring to two tables, like this:
Example
pdfMachine
A pdf writer that produces quality PDF files with ease!
65
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Result
Name Product
Hansen, Ola Printer
Svendson, Stephen Table
Svendson, Stephen Chair
Example
SELECT Employees.Name
FROM Employees, Orders
WHERE Employees.Employee_ID=Orders.Employee_ID
AND Orders.Product='Printer'
Result
Name
Hansen, Ola
Using Joins
OR we can select data from two tables with the JOIN keyword, like this:
Syntax
pdfMachine
A pdf writer that produces quality PDF files with ease!
66
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
The INNER JOIN returns all rows from both tables where there is a match. If there are
rows in Employees that do not have matches in Orders, those rows will not be listed.
Result
Name Product
Hansen, Ola Printer
Svendson, Stephen Table
Svendson, Stephen Chair
Syntax
The LEFT JOIN returns all the rows from the first table (Employees), even if there are no
matches in the second table (Orders). If there are rows in Employees that do not have
matches in Orders, those rows also will be listed.
Result
Name Product
Hansen, Ola Printer
Svendson, Tove
Svendson, Stephen Table
Svendson, Stephen Chair
Pettersen, Kari
pdfMachine
A pdf writer that produces quality PDF files with ease!
67
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
The RIGHT JOIN returns all the rows from the second table (Orders), even if there are no
matches in the first table (Employees). If there had been any rows in Orders that did not
have matches in Employees, those rows also would have been listed.
Result
Name Product
Hansen, Ola Printer
Svendson, Stephen Table
Svendson, Stephen Chair
Example
Who ordered a printer?
SELECT Employees.Name
FROM Employees
INNER JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID
WHERE Orders.Product = 'Printer'
Result
Name
Hansen, Ola
Create a Database
To create a database:
CREATE DATABASE database_name
Create a Table
pdfMachine
A pdf writer that produces quality PDF files with ease!
68
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Example
This example demonstrates how you can create a table named "Person", with four
columns. The column names will be "LastName", "FirstName", "Address", and "Age":
This example demonstrates how you can specify a maximum length for some columns:
The data type specifies what type of data the column can hold. The table below contains
the most common data types in SQL:
pdfMachine
A pdf writer that produces quality PDF files with ease!
69
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
ALTER TABLE
The ALTER TABLE statement is used to add or drop columns in an existing table.
ALTER TABLE table_name
ADD column_name datatype
ALTER TABLE table_name
DROP COLUMN column_name
Some database systems don't allow the dropping of a column in a database table
Note:
(DROP COLUMN column_name).
Person:
Example
Result:
Example
Result:
pdfMachine
A pdf writer that produces quality PDF files with ease!
70
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
To delete a table (the table structure attributes, and indexes will also be deleted):
To delete a database:
Views
A view is a virtual table based on the result-set of a SELECT statement. In SQL, a VIEW
is a virtual table based on the result-set of a SELECT statement.
A view contains rows and columns, just like a real table. The fields in a view are fields
from one or more real tables in the database. You can add SQL functions, WHERE, and
JOIN statements to a view and present the data as if the data were coming from a single
table.
Note: The database design and structure will NOT be affected by the functions, where, or
join statements in a view.
Syntax
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
Note: The database does not store the view data! The database engine recreates the data,
using the view's SELECT statement, every time a user queries a view.
Using Views
A view could be used from inside a query, a stored procedure, or from inside another
view. By adding functions, joins, etc., to a view, it allows you to present exactly the data
you want to the user.
The sample database Northwind has some views installed by default. The view "Current
Product List" lists all active products (products that are not discontinued) from the
Products table. The view is created with the following SQL:
pdfMachine
A pdf writer that produces quality PDF files with ease!
71
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!
Intermediate Database Management
Another view from the Northwind sample database selects every product in the Products
table that has a unit price that is higher than the average unit price:
Another example view from the Northwind database calculates the total sale for each
category in 1997. Note that this view selects its data from another view called "Product
Sales for 1997":
pdfMachine
A pdf writer that produces quality PDF files with ease!
72
Produce quality PDF files in seconds and preserve the integrity of your original documents. Compatible across
nearly all Windows platforms, simply open the document you want to convert, click “print”, select the
“Broadgun pdfMachine printer” and that’s it! Get yours now!