Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 23

Introduction to

Databases

Prof. Himanshu Joshi


International Management Institute
himanshu@imi.edu

Information Technology for


Decision Making (ITDM)
Example of a Traditional Database
Application

Suppose we are building a system to store the


information about:
• students
• courses
• professors
• who takes what, who teaches what

Information Technology for


Decision Making (ITDM)
Can we do it without a DBMS ?
Sure we can! Start by storing the data in files:

students.txt courses.txt professors.txt

Now write programs to implement specific tasks

Information Technology for


Decision Making (ITDM)
Doing it without a DBMS...
• Enroll “Mary Johnson” in “CSE444”:

Write a program to do the following:


Read
Read ‘students.txt’
‘students.txt’
Read
Read ‘courses.txt’
‘courses.txt’
Find&update
Find&update the
the record
record “Mary
“Mary Johnson”
Johnson”
Find&update
Find&update the
the record
record “CSE444”
“CSE444”
Write
Write “students.txt”
“students.txt”
Write
Write “courses.txt”
“courses.txt”
Information Technology for
Decision Making (ITDM)
Problems without an DBMS...

• System crashes: Read ‘students.txt’


Read ‘students.txt’
Read ‘courses.txt’
Read ‘courses.txt’
Find&update
Find&updatethe
Find&update
therecord
the
record“Mary
record
“MaryJohnson”
Johnson”
“CSE444”
Find&update the record “CSE444”
CRASH !
Write
Write“students.txt”
“students.txt”
Write “courses.txt”
Write “courses.txt”

– What is the problem ?


• Large data sets (say 50GB)
– Why is this a problem ?
• Simultaneous access by many users
– Lock students.txt – what is the problem ?

Information Technology for


Decision Making (ITDM)
Database Transactions
• Enroll “Mary Johnson” in “CSE444”:

BEGIN
BEGINTRANSACTION;
TRANSACTION;
INSERT
INSERTINTO
INTOTakes
Takes
SELECT
SELECTStudents.SSN,
Students.SSN,Courses.CID
Courses.CID
FROM
FROMStudents,
Students,Courses
Courses
WHERE
WHEREStudents.name
Students.name==‘Mary
‘MaryJohnson’
Johnson’and
and
Courses.name
Courses.name==‘CSE444’
‘CSE444’
----More
Moreupdates
updateshere....
here....
IF
IFeverything-went-OK
everything-went-OK
THEN
THENCOMMIT;
COMMIT;
ELSE
ELSEROLLBACK
ROLLBACK

If system crashes, the transaction is still either committed or aborted


Information Technology for
Decision Making (ITDM)
Transactions
• A transaction = sequence of statements that
either all succeed, or all fail
• Transactions have the ACID properties:
A = atomicity (a transaction should be done or
undone completely )
C = consistency (a transaction should transform
a system from one consistent state to another
consistent state)
I = isolation (each transaction should happen
independently of other transactions )
D = durability (completed transactions should
remain permanent)

Information Technology for


Decision Making (ITDM)
Database Management System (DBMS)

• Collection of interrelated data


• Set of programs to access the data
• DBMS contains information about a particular
enterprise and provides an environment that is both
convenient and efficient to use.
• Database Applications:
– Banking: all transactions
– Airlines: reservations, schedules
– Universities: registration, grades
– Sales: customers, products, purchases
– Manufacturing: production, inventory, orders, supply chain
– Human resources: employee records, salaries, tax
deductions
• Databases touch all aspects of our lives

Information Technology for


Decision Making (ITDM)
Different parts of a database

• Field
• Record
• Queries
• Report
• Form

Information Technology for


Decision Making (ITDM)
Table Fields
• When assigning a name to any object in Access,
carefully select a name that will indicate what
data is stored there.
• Field properties include their data type, field
sizes, and an optional description of the field.
• Make sure the data type you select for a field is
appropriate for the kind of data to be stored in
that field.
• When selecting a field size, make sure the size
is big enough to hold the largest piece of data
that will be stored there.

Information Technology for


Decision Making (ITDM)
Access field types

Make certain the field type you select matches the data to be held in that field.

Information Technology for


Decision Making (ITDM)
Access field types
Additional Access field types.

Information Technology for


Decision Making (ITDM)
Designing a Database
• When designing a database, first try to think of
all the fields of data that needs to be stored.
• Next, group the fields into tables. Each table
will contain a group of related fields.
• You need to select a field in each table to become
the primary key for that table.
• When tables will be related to one another, you
need to include a common field in the two
tables that will be used to form the relationship.

Information Technology for


Decision Making (ITDM)
Primary key
• A primary key uniquely identifies each record in
the table.
• Primary keys can consist of more than one field.
– Primary keys with more than one field are called
composite keys

Information Technology for


Decision Making (ITDM)
Include a common field (Primary key)
• When one table needs to be related to another
table, you must include a common field.
• The common field will be the primary key in one
table.
• The common field is referred to as a foreign key
in the related table.
• The foreign key in a table can then be used as a
primary key to access the record in the related
table.
• The primary key is not mandatory, but it is a
good idea to assign one

Information Technology for


Decision Making (ITDM)
Add records to a table using Datasheet View
• Records are added to a table using Datasheet
View.
• As you enter records, they will be placed in the
order in which you enter them. However, when
you close the table and open it again, the records
will be ordered according to the primary key
order.
• The navigation bar at the bottom of the
Datasheet view will indicate how many records
are in the table and what the current record is
(i.e., the record on which your cursor currently
rests).

Information Technology for


Decision Making (ITDM)
Modify the structure of a table by deleting,
moving, and adding fields
• The structure of a table can be modified after it
has been created.
• To delete a field in Design View, right-click on
the field, and then click Delete Rows on the drop
down menu.
• To move a field, (you also do this in Design
View) click on the field you want to move, and
while holding your mouse button down, move the
field to the desired location.
• To add a field in Design View, right-click the
field where you want to insert the new field and
then click Insert Rows on the dropdown menu.

Information Technology for


Decision Making (ITDM)
Access is a Relational Database
• A database is almost always a collection of tables.
Access is a relational database management
system that allows you to form relationships
between the tables.
• When you form a relationship between tables,
you are joining the tables.
• Tables are joined on common fields between the
tables.
• When tables are joined, you can view data from
both tables as if the tables were one combined
table.

Information Technology for


Decision Making (ITDM)
A one-to-many relationship
• Tables can be joined in three ways; one-to-
one, one-to-many, and many-to-many.
• A one-to-many relationship exists when one
table has many records associated with a given
value but the related table has only one record
for that value.
– The table with one record is called the primary
table
– The table with many records is called the
related table
• Queries can be defined to use the relationship to
extract data from both tables in a single query.

Information Technology for


Decision Making (ITDM)
An example of a
one-to-many relationship

The Employer table is related to the Position table via the common field EmployerID.

The Employer table has


one record for EmployerID
value 10126. The Position
table has two records.
Information Technology for
Decision Making (ITDM)
Using referential integrity
• When dealing with related tables, you need to
decide if you want to enforce referential
integrity.
• Referential integrity allows you to maintain the
integrity between related tables.
• The rules associated with referential integrity
specify that when you add a record to a related
table, there must be a matching record in the
primary table.
• If you choose to enforce referential integrity, you
can ensure that you will not have orphaned
records (records that have no matching record in
the primary table).

Information Technology for


Decision Making (ITDM)
Use cascaded updates
and cascaded deletes
• In addition to referential integrity, you can also
tell Access to implement cascaded updates and
cascaded deletes.
• If you choose cascaded updates, making a
change in a field that is common to two related
tables will cause the update to be made in both
tables.
• Cascade deletes is similar. If you delete a field
that is common to two tables, the deletion will
take place in both tables.
• You should carefully consider whether you want
to implement these features, as they can have
dramatic effects on your data.

Information Technology for


Decision Making (ITDM)
Define one-to-one, one to many and many-
to-many and relationships between tables
• In a one-to-one relationship between tables, there is
exactly one record in the primary table that matches
exactly one record in the related table.
• In one to many relationship, each record in the primary
table is linked to multiple records, but each record in
related table is linked to only one record in primary table.
• A many-to-many relationship exists between tables
when the tables involved have multiple matches in each of
the tables. For example, if you have a table containing
student data and another table containing course data,
you could say that this is a (M:N) relationship because a
student can take many courses and a course can have
many students. Whenever there is a many-to-many
relationship, you must provide a third table that will “link”
the two tables together in a one-to-many relationship.

Information Technology for


Decision Making (ITDM)

You might also like