Anda di halaman 1dari 64

www.agogueconsultancy.

com

Session 1

Oracle Concepts

The Role of Data

to Identify

to Explain

to Plan

to Administer

to Monitor to Decide

to Analyze

to Implement

How can we store the data?

Text Rich text

Graphics
Document

Spreadsheet
Table Database

WHAT IS DATABASE?

Data

Raw facts like numbers, text, names, etc.

Anything meaningless.
Meaningful arrangement of data.

Information

Some sensible data.


Collection of data that is stored in organized way to form information. It may or may not have structure.

Database

Disadvantages of file system

Data Redundancy Data Inconsistency Difficulty in Accessing Data Data Isolation Integrity Problems

Atomicity Problem
Concurrent Access anomalies

Security Problems

What is a DBMS??
Database
A003 Allen A007 Shane

A004 Brian Retrieve Data


Insert Data

A008

Ricky
Update Data Delete Data

DBMS

Benefits of DBMS

The amount of redundancy in the stored data can be reduced No more inconsistencies in data The stored data can be shared Standards can be set and followed Data integrity can be maintained Security of data can be implemented

www.agogueconsultancy.com

Database Models
- Flat file model
- Hierarchical model - Network model - Relational model

RDBMS
Relational Database RDBMS A suite of S/W programs to create, maintain DB

Applications for interacting with data in DB Create Applications

Users of RDBMS
Database Administrators

End Users

Application Programmers

Relational features of SQL Server


Information representation Unique definition of rows Systematic treatment of Null values
www.agogueconsultancy.com

Guaranteed access High level update, insert and delete

The E-R Model(1)


It is a conceptual model of the database Here, a system is composed of related entities

Tables identified Database Designer End User

Components of E-R Model


Entity Attribute s A description

A Property

Relationship

Entity Relationship Diagram

Entity Attribute

Relationship

Entity Sets customer and loan


customer-id customer- customer- customername street city loan- amount number

6/1/11

Attributes

Entity is a set of attributes Attribute types:


Simple and composite attributes. Single-valued and multi-valued attributes E.g. multivalued attribute: phone-numbers Derived attributes Can be computed from other attributes E.g. age, given date of birth Example: customer = (customer-id, customer-name, customer-street, customer-city) loan = (loan-number, amount)

6/1/11

Relationship Sets

Association among several entities.


Example: Hayes borrower A-102

customer entity relationship set account entity

Relationship set : A mathematical relation among n 2 entities, each taken from entity sets
{(e1, e2, en) | e1 E1, e2 E2, , en En}
where (e1, e2, , en) is a relationship

Example:

(Hayes, A-102) depositor

6/1/11

Relationship Set borrower

6/1/11

Relationship Sets

An attribute can also be property of a relationship set.

6/1/11

Mapping Cardinalities

Express the number of entities to which another entity can be associated via a relationship set.

Most useful in describing binary relationship sets.


For a binary relationship set the mapping cardinality must be one of the following types:

One to one
One to many Many to one

Many to many

6/1/11

Mapping Cardinalities

One to one

One to many

6/1/11

Note: Some elements in A and B may not be mapped to any elements in the other set

Mapping Cardinalities

Many to one
6/1/11

Many to many

Note: Some elements in A and B may not be mapped to any elements in the other set

Mapping Cardinalities affect ER Design


Can make access-date an attribute of account, instead of a

relationship attribute, if each account can have only one customer


I.e., the relationship from account to customer is many to one,

or equivalently, customer to account is one to many

6/1/11

E-R Diagrams

Rectangles represent entity sets.


Diamonds represent relationship sets. Lines link attributes to entity sets and entity sets to relationship sets. Ellipses represent attributes Double ellipses represent multivalued attributes. Dashed ellipses denote derived attributes. 6/1/11 Underline indicates primary key attributes (will study later)

E-R Diagram With Composite, Multivalued, and Derived Attributes

6/1/11

Relationship Sets with Attributes

6/1/11

Roles

Entity sets of a relationship need not be distinct


The labels manager and worker are called roles; they specify how employee entities interact via the works-for relationship set. Roles are indicated in E-R diagrams by labeling the lines that connect diamonds to rectangles. Role labels are optional, and are used to clarify semantics of the relationship

6/1/11

Cardinality Constraints

(), signifying one,

undirected line (), signifying many,


E.g.: One-to-one relationship:

A customer is associated with at most one loan via the relationship borrower A loan is associated with at most one customer via borrower

6/1/11

Types of Relationships

One to One 1:1 One to Many 1: N Many to Many N:M 1


Place s

1
Ha s 1 C_Code

Customer

N
Contain s

Order

Item

One-To-Many Relationship

6/1/11

Many-To-One Relationships

In a many-to-one relationship a loan is associated with several (including 0) customers via borrower, a customer is associated with at most one loan via borrower

6/1/11

Many-To-Many Relationship

A customer is associated with several (possibly 0) loans via borrower

A loan is associated with several (possibly 0) customers via borrower

6/1/11

Participation of an Entity Set in a Relationship Set


Total participation E.g. participation of loan in borrower is total every loan must have a customer associated to it via borrower Partial participation: E.g. participation of customer in borrower is partial

6/1/11

Alternative Notation for Cardinality Limits


Cardinality limits can also express participation constraints

6/1/11

Keys

Super key
candidate key primary key.

Unique key
Foreign key

6/1/11

Weak Entity Sets

We depict a weak entity set by double rectangles. We underline the discriminator of a weak entity set with a dashed line. payment-number discriminator of the payment entity set Primary key for payment (loan-number, payment-number)

6/1/11

More Weak Entity Set Examples

In a university, a course is a strong entity and a course-offering can be modeled as a weak entity The discriminator of course-offering would be semester (including year) and section-number (if there is more than one section) If we model course-offering as a strong entity we would model course-number as an attribute. Then the relationship with course would be implicit in the coursenumber attribute

6/1/11

Summary of Symbols Used in E-R Notation

6/1/11

Alternative E-R Notations

6/1/11

Question

Draw an ERD to show the relationship between the Orders and Item entities

ERD and Database Design


Entities Tables

Identifies
Database Designer Attributes Fields Relations
Relationships

Creating Tables - Normalization

Points to Note
Well defined relationship between tables Unnecessary or redundant data kept away.

Group related data


www.agogueconsultancy.com

Achieved through a technique called Normalization

First Normal Form


Rule 1: Eliminate Repeating Groups
First Normal Form Puppy Puppy Number(pk) Puppy Name Kennel Code Kennel Name Kennel Location Trick Puppy Number(fk) Un-Normalised Data Items for Puppies
Puppy number Puppy name Kennel Code Kennel Name Kennel location Trick ID 1...n Trick Name 1...n Trick Where Learned 1...n Skill Level 1...n

Trick Id(pk)
Trick Name Trick Where Learned Skill Level

Second Normal Form


Rule 2: Eliminate Redundant Data
Second Normal Form
Puppy Puppy Tricks Tricks

Puppy Number Puppy Name Kennel Code Kennel Name Kennel Location

Puppy Number Trick ID Trick ID Trick Name Trick Where Learned Skill Level

No Update or Delete Anomalies

Third Normal Form


Rule 3: Eliminate Columns Not Dependent On A Key
Third Normal Form

Puppies Puppy Number Puppy Name Kennel Code


Kennel Kennel Code Kennel Name Kennel Location

Puppy Tricks Puppy Number Trick ID Trick Where Learned Skill Level
Trick Trick ID Trick Name

Characteristics of a normalized database


Each table must have a key field

All fields must contain small data There must be no repeating fields
Each table must contain information about a single entity

Each field in a table must depend on the key field


All non-key fields must be mutually independent

When to use Normalization??


When data is large and scattered
There is no defined group of data Data is too complicated The first step to build the database application

Denormalisation
Normalization has some drawbacks. Joins with multiple tables affects the performance of the database. The CPU time required to solve such queries will be very large. In such cases, storing a few fields redundantly can be considered to increase the performance of the database. Such a database is said to be a

www.agogueconsultancy.com

Denormalised database.

Features of tables
Each table must have unique name Each row in the table must be unique and describe one set of related information about the subject of the table. Each column or field describes a single characteristic of an entity. Column name must be unique within the table. The order of rows or columns is not significant.

www.agogueconsultancy.com

Design Oracle Tables


Decide
What tables are needed Type of data in each table Restrictions on data

Keys
Default values Indexes
www.agogueconsultancy.com

Null values

Creating Tables

Modifying Tables
Add Rows
INSERT [INTO] <Table_name> VALUES <values>

Update Rows
UPDATE <Table_name> SET <Column_name = Value> WHERE <Search_Condition>

Delete Rows
DELETE FROM <Table_name> WHERE <Search_Condition>

Viewing the Tables


Syntax: To view the information on a table using a stored procedure. Sp_help <Table_name> Syntax: To view the data in the table.

SELECT <Select_list> FROM <Table_Name>

Removing tables
Syntax :
DROP TABLE <table name>

Example :
DROP TABLE passenger

Joins
joins are used to query data from two or more tables.

It is based on a relationship between certain columns in these tables.


Tables in a database are often related to each other with keys.

A primary key is a column (or a combination of columns) 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.

Types of Joins
Self Join Cross Join Inner Join Outer Join

Left join Right join

Self Join
Join on the same tabel

SQL INNER JOIN Syntax


SELECT t1.column_name3,t2.column_name4 FROM table_name1 t1, table_name1 t2 where table_name1.column_name1=table_name1.column_name2

Cross Join
Provides cartesion product

SQL INNER JOIN Syntax


Select t1.columnname(s),t2.columnname(s) From Table_name1 t1 CROSS JOIN Table_name2 t2

Inner Join

The INNER JOIN keyword return rows when there is at least one match in both tables.

SQL INNER JOIN Syntax

SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON table_name1.column_name=table_name2.column_name

LEFT JOIN

The LEFT JOIN keyword returns all rows from the left table, even if there are no matches in the right table. SQL LEFT JOIN Syntax

SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name

RIGHT JOIN Keyword

The RIGHT JOIN keyword returns all the rows from the right table, even if there are no matches in the left table.

SQL RIGHT JOIN Syntax

SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2 ON table_name1.column_name=table_name2.column_name

What is View?

VIEWS are data objects a virtual table based on the result-set of an query statement contains rows and columns functions, WHERE, and JOIN statements can be used in views

www.agogueconsultancy.com

View Syntax
CREATE VIEW view_name AS

SELECT column_name(s) FROM table_name WHERE condition

Ex:
CREATE OR REPLACE VIEW person_view AS

SELECT first_name AS FNAME, last_name LNAME, socsecno FROM person;

www.agogueconsultancy.com

Create Single Table View With WHERE Clause


CREATE OR REPLACE VIEW <view_name> AS

SELECT <column_name> FROM <table_name> WHERE <column_name> = <value or condition>;

Ex:
CREATE OR REPLACE VIEW person_two_titles_view AS

SELECT first_name, last_name, socsecno FROM person WHERE title_1 IS NOT NULL AND title_2 IS NOT NULL;

www.agogueconsultancy.com

SELECT * FROM person_two_titles_view;

Anda mungkin juga menyukai