Sie sind auf Seite 1von 68

Introduction to Databases

Engr. Abdur Rehman


Lecturer,
SE Department, University of Malakand
2 What is a database
Database is an organized collection of data.
Some examples you may encounter in your daily life are:
 A telephone directory
 T.V. Guide
 Airline reservation system
 NADRA record
 Motor vehicle registration records
 Papers in your filing cabinet
 Files on your computer hard drive
 Hospital patients
 Literature (published articles in a certain field)
 Student Information System
3 Why do we need a database?

 Keep records of our:


 Clients
 Staff
 Data
 Files
 Activities
 sales

 Develop reports;
 Perform research
4 Types of Databases
Non-relational databases
 Non-relational databases place information in field
categories that we create so that information is available
for sorting and disseminating the way we need it.
 The data in a non-relational database, however, is limited
to that program and cannot be extracted and applied
to a number of other software programs, or other
database files.
 Example: a spread sheet

Relational databases
 In relational databases, fields can be used in a number of
ways (and can be of variable length), provided that they
are linked in tables.
 It is developed based on a database model that
provides for logical connections among files (known as
tables) by including identifying data from one table in
another table
5 A Simple Relational Database Example
6
DataBase Management System
(DBMS)

• DBMS – provides storage, retrieval, analysis, sorting, and


printing of information in a database.
• Allows a user to deal with data in logical terms, without
having to understand the computer's physical view.
DBMS is:
• A collection of program independent, interrelated data
• A set of programs to access the data
• An environment that is both convenient and efficient to
use.
7 Selecting a Database Management
System

Database management systems (or DBMS)


can be divided into two categories –
 Desktop databases are oriented toward
single-user applications and reside on
standard personal computers (hence the
term desktop).
 Server databases contain mechanisms to
ensure the reliability and consistency of
data and are geared toward multi-user
applications.
8 Database Options
Software package for defining and managing
a database.
Consumer Enterprise RDMS
 Flat Files  Oracle
 Microsoft Excel  IBM/DB2
- Limit of 65,536 Rows  MS SQL-server
 Microsoft Access  Sybase
 FileMaker Pro  Informix
 MySQL (Open Source)  Lotus Notes
 Postgres (Open Source)  MySQL (Open Source)
 Postgres (Open Source)
9 Distributed and Web Database

Distributed Database
A single logical database that is
spread physically across
computers in multiple locations
that are connected by a data
communications link
Web Database
Data is accessible through Internet
Have different underlying
database models
Databases and the Web

10
11 Database Terminology
Logical data view –
How humans see things

Physical data view –


How things are stored in a computer

Database Management System (DBMS) –


Allows a user to deal with data in logical terms, without
having to understand the computer's physical view.
12 Some Definitions
 Entity/File: Something of importance to the
organization
 Entities that the organization wants to store data
about typically becomes a database table
 A group or collection of similar records, like Student File,
American History 1850-1866 file, Basic Food Group Nutrition
File
 A record book: index of data records, like address
lists, inventory lists, or groupings of other unique
records that are combined into one list.
 A field/Attribute: one category of information,
i.e., Name, Address, Semester Grade, Academic
topic
 Characteristics of an entity
13 Terminology cont’d
 A record: one piece of data, i.e., one student's
information, a recipe, a test question
 Each row represents an instance of an entity
 Each column represents an attribute of an entity
 A layout: a design for a database that contains
field names and possibly graphics.
 Relationship --> Definitions linking two or more
tables
 Relationships between entities are represented
by values stored in the columns of the
corresponding tables (keys)
 Key --> The attribute used to define a required item
14 Relational DB Terminology
Fields
(Columns, Attributes)

Num Name Credits


CS208 CS Fundamentals 3 Records
MT360 Calculus 4 (Rows, Tuples)
CS320 C Programming 3
Courses

File (Table) = Relation Relationships between


Rows (Tuples) = Records objects are defined by
Columns (Attributes) = Fields common attributes
15 Primary Keys
 In a Relational DB, each table
 Must have a primary key (unique identifier)
 Must have no duplicate rows
 Used in a relational database to relate tables together
 Must be unique and a field that doesn’t change
 A primary key is
 A data attribute (column), or a combination of
attributes, that uniquely identifies each record in the
table.
 A simple key consists of a single attribute
 A composite key consists of two or more attributes
 If there is no easy natural choice, you can add a column
containing a unique identifier (PK)
16 Primary Key
• To ensure that each record is unique in each table, we can set one
field to be a Primary Key field.
• A Primary Key is a field that will contain no duplicates and no blank
values.
17 Other Types of Keys
 Secondary Key
A column that is used to aid in the retrieval
of information from a table.
A secondary key is not required to have
unique values in each of its rows,
 Foreign Key
Foreign Keys link to data in other tables
A column used to retrieve information from
one table (i.e., is a secondary key) that is
also the primary key in another table.
Foreign keys are a major tool in Relational
DBs.
Entity Relationships
 Used to connect tables
 Field(s) that have the same value in the related tables
 Describe an association between two or more entities
 One-to-one (1:1) entity relationships (not common)
 e.g. each store has a single manager
 One-to-many (O:M) entity relationships (most common)
 e.g. a supplier supplies more than one product to a company
 Many-to-many (M:M) entity relationships (requires a third
table to tie the tables together)
 e.g. an order can contain multiple products and a product
can appear on multiple orders

18
19 Data entry considerations
 Define “must” enter fields – no record is
complete unless: such and such is
entered;
 Make data entry fool proof. Example:
Grade level can be entered as a
number (8 or 8th or eight). By using a
pull-down menu with the correct data
format these mistakes can be avoided.
Data Entry – additional considerations

 Barcode scanners
 USB or
 Wireless attached to a
Palm or Pocket PC
 Pocket PC
 WiFi 802.11g, Bluetooth
 Wireless networks (real-
time on demand
systems)

20
21 Input Data with Forms
 A friendlier view of the database
 Used for data input, menus, display and printing
 Can perform Calculations and Combine fields
 Present one record to the user
 Often used to change or view data
22 Reports
 Output of information in a printed report
 Reports can be tailored to the needs of the data-
user, making the information they extract much
more useful
 Allows you to group and summarize data
 Can perform Calculations and Combine fields
 Cannot Edit Data
 Can Make Labels
23 Database Models
 Flat files ’60
 Two dimensional
 Hierarchical ‘60
 Set up in a tree structure
 Network ‘70
 Records are stored with links to other records
 Relational ‘80
 Most popular and a very powerful tool
 Not just for storing data but also for accessing it.
 Object oriented ‘90
 Web enabled ‘90
24
25 Flat File Databases
 Flat-file DBs are like the DBs you can construct in a
single spreadsheet page
 All the information in the DB is in one file consisting of
one array of rows and columns.
 Useful for simple data storage needs
 Hard to manage large data needs
 Can waste disk space

 For example:

SSN Surname First Name(s) Telephone Number


123-45-6789 Doe John X. 303-555-1234
987-65-4321 Smith Martin 720-111-2222
567-89-0123 Johnson Billy Bob 303-444-5555
26 Network Databases
Permits links among all components
(i.e. elements can be linked to
other elements anywhere in the
database, not just those directly
above and below)
The interconnected design allows
for access via multiple pathways
Can be extremely difficult to
manage
The World-Wide Web is a very large
example of a network database.
27 Network Model

Department Students
DeptNum Name IDNum Name Course

Courses Professors
CourseNum Name Section IDNum Name

- Added data paths across the tree


(instead of just up and down)
- Reduced time required to access data,
but increased overhead space requirements
28 Relational Databases
 A relational database is a set of one or more tables that
together embody information about a set of related
concepts and entities.
 The tables are connected (related) via fields within the
table that are shared by a pair of tables.
 Tables are related by a common field
 Called a relationship or join
 Can help organize data

 Allows for data to be broken down into smaller, logical, and


manageable units
 One can still use a single query to retrieve data spread over
multiple tables
 This is achieved thru keys.
 Most common form of database
 Maintaining data is easier
 No wasted disk space
29 Relational Model

Department Professors
DeptNum Name IDNum Name DeptNum Course
… … … … … …
… … … … … …
… … … … … …

Courses
Students
Num ProfID Section IDNum Name Course
… … … … … …
… … … … … …
… … … … … …

Based on tables of objects (the data), rather than


specific paths (ways to access the data)
What is a Relational Database
30
Management System (RDMS)?

A relational database is a collection of tables


from which data can be accessed in many
different ways without having to reorganize the
database tables.

DON’T DUPLICATE DATA

 That is, once relationships are created, tables can


“talk” to each other. We can link (relate) the
tables to find:
 Which schools are in a system?
 Which students are in which class?
 Which classes are filling up?
Relationships
31
32 Relational Database Rules

Each row is unique (distinct)


Each column name is unique within a
table
It is permissible to have the same
name for a column in two different
tables in the same database.
To distinguish between them we use a
qualified name:
Pet.Name vs Family.Name
33 Relational Databases

 In a Relational DB, the information content of


a table does not depend on either

The order of the rows; or


The order of the columns
 In other words, the rows and columns of a
table can be rearranged at will without
affecting the table's information content
34 DBMS ARCHITECTURE

The logical DBMS architecture

The physical DBMS architecture


35 DBMS ARCHITECTURE

The logical DBMS architecture

The logical architecture deals with the way data is


stored and presented to users.
The physical DBMS architecture
36 DBMS ARCHITECTURE

The logical DBMS architecture


The physical architecture is concerned with the
s/w components that make up a DBMS.

The physical DBMS architecture


37 Three Level Architecture of DBMS

 A major purpose of a database system is to


provide users with an abstract view of the
data. That is, the system hides certain
details of how the data is stored and
maintained.

 External or View Level

 Conceptual Level

 Internal or Physical Level


38 Three Level Architecture of DBMS

External View 1 View 2 View 3


Level

Conceptual
Level

Physical
Level
39 Three Level Architecture of DBMS

continue…
Sales Officer Inventory Controller

View 1 View 2
External Item_Name Item_Name
Level Price Stock

Conceptual
Item_Number Character (6)
Conceptual
Item_Name Character(30)
Level Price Numeric(5,2)
Stock Numeric(4)

Physical
Stored_Item Length=50
Physical Item # Type = Byte(6), offset = 0, Index = Ix
Name Type = Byte(30), offset = 6
Level Price Type = Byte(8), offset = 36
Stock Type = Byte(4), offset = 44
40 External or View Level

 Highest level of abstraction of database

 Closest to the users

 Concerned with the way in which data is viewed by


individual users.

 Most of the users need only a part of the database


relevant to them.

 The system provides many views for the same


database.

 Users – application programmers or end-users


41 Conceptual Level

 This level of abstraction describes what data are actually


stored in the database.

 It also describes the relationships existing among data.

 At this level, the database is described logically in terms of


simple data-structures.

 The users of this level are not concerned with how these
logical data structures will be implemented at the physical
level, rather they just are concerned about what information
is to be kept in the database.
42 Conceptual Level

continue…

 The conceptual view is a representation of


the entire information content of the
database in a form that is some what
abstract in comparison with the way in which
the data is physically stored.
43 Internal or Physical Level

 Lowest level of abstraction.

 The internal level is closest to physical storage.

 It describes how the data are actually stored on the


storage medium.

 At this level, complex low-level data structures are


described in detail.

 Internal view –not only defines the various types of


stored record but also specifies what indexes exists,
how files are represented, etc.
44 Creating a Student Database

 Student ID  Home Phone


 First Name  Work Phone
 Middle Initial  Cell Phone
 Last Name  Course ID
 Home Address
 Course Name
 School Address
 Course Instructor
 Street Address
 City
 Province
45 Database Design
Steps in database design
• Requirement analysis
• What does the user want?

• Conceptual database design


• Defining the entities and attributes, and the
relationships between these --> the ER model

• Physical database design


• Implementation of the conceptual design using a
database management system
46 Database Normalization

Optimize the Tables by:

 Storing each piece of data once and


only once (i.e. Eliminate redundant
data).

 Ensuring data dependencies make sense


(only storing related data within a
particular table).

 Maintaining data integrity.


47 Queries
 A means of asking questions (querying) of your data
 Queries are the information retrieval requests you make to
the database
 Your queries are all about the information you are trying to
gather
 Can look across a number of Tables and other Queries
 Can perform Calculations and Combine fields
48 Query Languages

Query languages allow non-programmers to


question the DBMS

Structured Query Language (SQL) –


the only standard structured query language
49 Structured Query Language (SQL)
 Pronounced either "S, Q, L" or "sequel"
 ANSI (American National Standards Institute)
standard computer language for accessing and
manipulating database systems.
 Used especially for query and retrieval
 Includes commands for defining Relational DBs,
conducting transactions, storing data, etc.
 Each Relational DBMS also has additional features
unique to it, because SQL does not handle all the
practical details involved in using a Relational DB
 SQL is essentially divided into two sublanguages
 Data Manipulation Language (DML)
 Data Definition Language (DDL)
50 Select

The select clause directs the DBMS to


choose a subset of fields from one or
more tables.
For example,
SELECT last_name, first_name
chooses all last names and first names and will place
them in this order in the result set.
51 From clause

The from clause directs the query


toward one or more tables.
 from Student (directs the query to use the student
table)
Used with the SELECT clause:
SELECT last_name, first_name
FROM Student
chooses the last and first name of all rows in
Student table.
 Note: As long as the column names within
the tables used in the from clause are
unique, qualified names are not required.
52 Where clause

A where clause specifies a selection


criterion that we will use to limit our
choice of records.
A where clause follows the name of
the table.
Format:
SELECT field1,…, fieldN
FROM tablename
WHERE (boolean expression)
53 Boolean Expressions

Boolean expressions used in where


clauses typically involve a field
compared to another field or a field
compared to a value.
For example:
WHERE GPA > 3.50
WHERE last_name > “M”
WHERE amount_owed >= amount_paid
Comparison Operators

= is equal to
> is greater than
< is less than
>= is greater than or equal to
<= is less than or equal to
not is not equal to

54
55 Simple SQL Query

Textbooks
Title Price Category Publisher
Java Intro 109.99 Computers Prentice Hall
Calculus 129.99 Math Kaufman
Advanced C 115.99 Computers Tech Inc
Philosophy 83.99 Lib Arts Prentice Hall

SELECT *
FROM Textbooks
WHERE category=‘Computers’
Title Price Category Publisher
Java Intro 109.99 Computers Prentice Hall
“selection”
Advanced C 115.99 Computers Tech Inc
56 Simple SQL Query
Textbooks
Title Price Category Publisher
Java Intro 109.99 Computers Prentice Hall
Calculus 129.99 Math Kaufman
Advanced C 115.99 Computers Tech Inc
Philosophy 83.99 Lib Arts Prentice Hall

SELECT Title, Price, Publisher


FROM Textbooks
WHERE Price < 110

Title Price Publisher


“selection” and Java Intro 109.99 Prentice Hall
“projection” Philosophy 83.99 Prentice Hall
57 Data Manipulation Language
Syntax for executing queries, updating,
inserting, and deleting records.

SELECT - extracts data from one or


more table
INSERT INTO - inserts new data into a
table
UPDATE - updates data in a table
DELETE FROM - deletes data from a
table
58 DML Example

SELECT *
FROM Student
WHERE
Student.GPA > 3.0 AND
Student.Address = “Hostel”
59 Data Definition Language
Syntax for creating ,editing, deleting:
Databases
Tables
Indexes
Constraints
Users
Privileges
60 DDL Examples
CREATE DATABASE Inventory;

CREATE TABLE employee(


Name varchar(100)
E_ID int(10)
Address varchar(200)
Appointment date(8)
);

DROP TABLE Student;


61 Working with a Database

Creating tables
List the necessary fields
Steps to define a field
Descriptively name the field
Specify the field type
Determine the field size
62 Working with a Database

Field types
Describes the type of data stored
Most DBMS use the same types
Text fields store letters and numbers
Numeric field store numbers
Date and time field
Logical field stores yes or no
Binary field stores images or sounds
Counter field generates sequential
numbers
Memo fields store large amounts of data
63 Working with a Database

Entering data into a table


Users type data into a field
Data must be entered
accurately
Constraints help to verify data
Forms are typically used for data
entry
64 Working with a Database

Viewing records
Datasheet view shows all records
Filters can limit the records shown
Display only records matching a criteria
Forms allow viewing one record
65 Working with a Database

Sorting records
Order records based on a field
Multiple sub sorts resolve ‘ties’
Several types of sorts
Alphabetic
Numeric
Chronological
Ascending
Descending
66 Working with a Database

Querying a database
Statement that describes desired
data
List of fields can be modified
Uses of querying
Find data
Calculate values per record
Delete records
Most important DBMS skill
67 Working with a Database

Query languages
All DBMS use a query language
Structured Query Language
(SQL)
Most common query language
Select FirstName, LastName, Phone
From tblPhoneNumbers
Where LastName=“Norton”;
68 Working with a Database

Generating reports
Printed information extracted from
a database
Can calculate data
Calculate data per row
Calculate for entire table
Pictures and formatting can be included

Das könnte Ihnen auch gefallen