More Related Content
More from Angel G Diaz (18)
Lesson 01 - Introduction to SQL
- 2. Relational Database System
• Basic Database Terminology
• Relational Database Concepts
• Types of Relationships
• Types of Keys
• Integrity Rules
• Relational Languages
© 2007 Angel G. Diaz
- 3. Basic Terminology
Entity – a person, place, event, or item
Data – facts describing an entity
Attributes – characteristics of an entity
Entity Set – a collection of related entities
Database – a collection of entity sets
Relationship – interaction between entity sets
© 2007 Angel G. Diaz
- 4. Types of Relationships
One-to-one relationship (1:1)
Exists between two entity sets X and Y, if an entity in X has only
one matching entity in Y, and vice versa. For example,
Department and Manager.
One-to-many relationship (1:M)
Exists between two entity sets X and Y, if an entity in X has only
one matching entity in Y, but an entity in Y has many matching
entities in X. For example, Department and Employee.
Many-to-many relationship (M:N)
Exists between two entity sets X and Y, if an entity in X has only
many matching entity in Y, and vice versa. For example, Course
and Student.
© 2007 Angel G. Diaz
- 5. Database Management System
(DBMS)
•A software package such as Oracle or IBM DB2.
•Manages data and relationships in the database.
•Creates a Data Dictionary to store Metadata – data about data.
•Manages all day-to-day transactions.
•Provides user with data independence at application level.
•Transforms logical data requests to match physical data structures.
•Secures access through passwords, restricted user access, and encryption.
•Provides backup and recovery mechanisms.
•Provides export and import utilities.
•Allows sharing of data with locking capabilities.
© 2007 Angel G. Diaz
- 6. Relational Model
Relation – two-dimensional flat table
Tuple – a row or a record
Attribute – a column or a field
Degree – number of attributes in a table
Domain – all possible values of an attribute
Key – a minimal set of attributes used to
uniquely define a row/entity
© 2007 Angel G. Diaz
- 7. Types of Keys
Primary key
A single attribute used as a unique identifier
Composite key
Two or more attributes used as a unique identifier
Secondary key
A non-key attribute used in the search operation
Surrogate key
An added attribute used as a primary key
Foreign key
An attribute that references primary key of a table
© 2007 Angel G. Diaz
- 8. Integrity Rules
Entity integrity:
No primary key attribute may have a null value.
A primary key value must be unique and not null.
A null value is not known, not entered, not defied, or not
applicable.
A space or a zero is not a null value.
Referential integrity:
A foreign key value may be null.
A foreign key value must exist as a primary key value in
another table.
© 2007 Angel G. Diaz
- 9. Theoretical Relational Languages
E.F. Codd suggested two theoretical languages with his
proposed relational model in 1970.
1. Relational Algebra. A procedural language.
2. Relational Calculus. A non-procedural language.
Relational Operations.
Union Intersection Difference
Selection Projection Product
Join Assignment Division
© 2007 Angel G. Diaz
- 10. Data Modeling
• Database Modeling Techniques
• E-R Diagrams
• Dependency
• Redundancy Problem
• Normal forms
• Normalization
© 2007 Angel G. Diaz
- 11. Data Modeling
Modeling is simplified version of real-life complex
objects.
The Entity-Relationship (E-R) model is a very popular
modeling tool.
The E-R model provides:
•An excellent communication tool.
•A simple graphical representation of a database.
© 2007 Angel G. Diaz
- 12. E-R Diagram
•An entity (or entity set) is represented by a rectangle. The name of the entity is
written within the rectangle in a singular upper-case noun. For example,
EMPLOYEE.
•The relationship between two entity sets is represented in a diamond-shaped
box. The name of relationship is an active verb in lower-case. For example,
supervises.
•The types of relationship is called connectivity.
•The lower and upper limit of relationship is called cardinality.
•The optional relationship is shown with a small circle next to the optional
entity.
•The decomposition of a M:N relationship into two 1:M relationships involves a
third composite entity, which is shown with a diamond in a rectangle.
•The weak entities, which cannot exist by themselves, are shown with double-
lined rectangles.
© 2007 Angel G. Diaz
- 13. Types of Attributes
Simple Attribute.
Attribute that cannot be subdivided. For example, city, gender, or last name.
Composite Attribute.
Attribute that can be sub divided. For example, name or mailing address.
Single-valued Attribute.
Attribute with a single-value per entity. For example, first name or employee
number.
Multi-valued Attribute.
Attribute with multiple values per entity. For example, student’s courses.
© 2007 Angel G. Diaz
- 14. Table with Multi-valued Attributes
InvNo InvDate CustNo ItemNo Cust Item Item Qty
Name Name Price
1001 04/14/00 212 1 Starks Screw $2.25 5
3 Bolt $3.99 5
5 Washer $1.99 9
1002 04/17/00 225 1 Connors Screw $2.25 2
2 Nut $5.00 3
1003 04/17/00 239 1 Kapur Screw $2.25 7
2 Nut $5.00 1
1004 04/18/00 211 4 Garcia Hammer $9.99 5
© 2007 Angel G. Diaz
- 15. Dependencies
The primary key describes an entity.
The non-key attributes are functionally dependent on he primary
key attribute(s).
Types of Dependencies
Total – a non-key attribute is totally dependent on primary key
attributes.
Partial – a non-key attribute is dependent on part of the primary
key.
Transitive – a non-key attribute is dependent on another non-key
attribute.
© 2007 Angel G. Diaz
- 16. Redundancy
Redundancy means
• Repeated data entry.
• Repeated changes in many places.
• More storage space.
• Deletion anomaly – deletion of one entity results in
deletion of another entity.
• Insertion anomaly – Insertion of one entity depends on
information about another entity.
© 2007 Angel G. Diaz
- 17. Normal Forms
First Normal Form (1NF)
•Table’s primary key is defined.
•All non-key attributes show functional dependency on the primary key
components.
•Table does not contain multi-valued attributes.
Second Normal Form (2NF)
•All 1NF requirements are satisfied.
•There is no partial dependency.
Third Normal Form (3NF)
•All 2NF requirements are fulfilled.
•There is no transitive dependency.
© 2007 Angel G. Diaz
- 18. INVOICE Table in 1NF
InvNo InvDate CustNo ItemNo CustName ItemName ItemPrice Qty
1001 04/14/00 212 1 Starks Screw $2.25 5
1001 04/14/00 212 3 Starks Bolt $3.99 5
1001 04/14/00 212 5 Starks Washer $1.99 9
1002 04/17/00 225 1 Connors Screw $2.25 2
1002 04/17/00 225 2 Connors Nut $5.00 3
1003 04/17/00 239 1 Kapur Screw $2.25 7
1003 04/17/00 239 2 Kapur Nut $5.00 1
1004 04/18/00 211 4 Garcia Hammer $9.99 5
Attributes InvDate, CustNo and CustName are partially dependent on InvNo.
Attributes ItemName and ItemPrice are partially dependent on ItemNo.
Attribute Qty is totally dependent on InvNo and ItemNo.
Attribute CustName shows transitive dependency on CustNo.
© 2007 Angel G. Diaz
- 19. Normalization
• A process of decomposing a table into many tables.
• A process to increase table’s normal form.
• A process to reduce redundancy.
The higher the normal form is,
the lower the redundancy.
© 2007 Angel G. Diaz
- 20. 1NF to 2NF
(Removal of partial dependencies)
•Write each primary key component (with partial dependency on it)
on a separate line.
•Write composite key on the next line.
INVOICE table decomposed into 2NF:
INV (InvNo, InvDate, CustNo, CustName)
ITEM (ItemNo, ItemName, ItemPrice)
INVITEM (InvNo, ItemNo, Qty)
© 2007 Angel G. Diaz
- 21. 2NF to 3NF
(Removal of transitive dependencies)
•Move attributes with transitive dependency to a new table.
•Keep the primary key of the new table as the foreign key in the
existing table.
Database in 3NF:
INV (InvNo, InvDate, CustNo)
CUSTOMER (CustNo, CustName)
ITEM (ItemNo, ItemName, ItemPrice)
INVITEM (InvNo, ItemNo, Qty)
© 2007 Angel G. Diaz
- 22. Database Tools
• Personal Databases
• Client/Server Databases
• Oracle and its Tools
• Types of SQL Statements
© 2007 Angel G. Diaz
- 23. Personal Databases
• Usually stored on a client computer
• Primarily for single-user applications
• Stored on a server for multi-user or shared access
• A server is a computer that accepts and services
requests from clients. A server’s resources include hard
disk, programs, data, or printers.
• For example, Microsoft-Access and Visual Fox Pro
© 2007 Angel G. Diaz
- 24. Characteristics of a
Personal Database System
• Heavy traffic on network to transport applications and
databases between server and clients
• Heavy demand on Client computer
• Inferior system response time
• No automatic table locking, but optimistic locking
• Client failure may result in record locking and database
corruption
• Transaction log kept in client’s memory
© 2007 Angel G. Diaz
- 25. Client/Server Databases
• Usually stored on the server computer
• Primarily for multi-user applications
• Allows shared-access with locking
• An Oracle server accepts and services requests
from clients.
• The client computer does not contain database
and does not perform any processing
• For example, Oracle, DB2, SAP, etc.
© 2007 Angel G. Diaz
- 26. Characteristics of a
Client/Server Database System
• Low network traffic as only client requests and
results are transported
• Low demand on client as server processes requests
• Automatic locking
• If a client fails, other clients not affected
• Central Transaction Log kept on server
• If a batch transaction fails, entire transaction is rolled
back
© 2007 Angel G. Diaz
- 27. Oracle and its Tools
• A Client/Server database system
• A object-relational database system
• Oracle Corp is the second largest software company in the world
Tools
SQL*Plus – environment to write command-line SQL queries
PL/SQL – Oracle’s procedural language extension to SQL
Query Builder – graphical environment for SQL query building
Developer – Form Builder, Report Builder, Graphic Builder
Enterprise Manager – a tool to manage database and users. For example, Storage
Manager, Instance Manager and Security Manager
Oracle Web Application Server – to access Oracle databases through web pages
© 2007 Angel G. Diaz
- 28. Structured Query Language (SQL)
• Universal query language for relational databases
• Standardized and accepted by ANSI and ISO
• A non-procedural, fourth generation, high-level language
Types of SQL statements
Data Definition Language (DDL) – creates, changes, removes table’s structure
For example, CREATE, ALTER, DROP, RENAME, and TRUNCATE
Data Manipulation Language (DML) – inserts, changes and removes rows
For example, INSERT, UPDATE, and DELETE
Data Retrieval – retrieves data from database, For example, SELECT
Transaction Control Language (TCL) – manages logical transactions
For example, COMMIT, SAVEPOINT, and ROLLBACK
Data Control Language (DCL) – gives and removes rights to objects
For example, GRANT and REVOKE
© 2007 Angel G. Diaz