15
1/17/1439 AH DataBase fundamentals (IS 220) بيانات قواعد أساسياتLecture 1: Ch1 -Principles of DataBases- File-Based Systems: Collection of application programs that perform services for the end users. (e.g: reports). - Each program defines and manages its own data. - Limitations (Disadvantages): 1. Separation and isolation of data 2. Duplication of Data 3. Data dependence 4. Incompatible file formats Solution? DATABASE Approach. - Data: know facts that can be recorded and have an implicit meaning. Data itself has no meaning without meatdata which describes data. - Information: the data you process in a manner that make it meaningful. So, Data is what you store in database. Information is what you retrieve from database. - Database: Shared collection of logically related data and a description of it, designed to meet the information needs of an organization. - Metadata (system catalog/data dictionary): the description of data, used to enable program-data independence. - Entity: a distinct object( person, place, thing…) in the organization that is to be represented in the database. - Attribute: is a property that describes some aspect of the object that we wish to record. - Relationship: is an association between entities. Database Management System (DBMS): a software system that enables users to create, maintain, and query the database. examples of DBMSs: Oracle, MS Access, MySQL Database Application: collection of data and programs that interacts with the database by issuing an appropriate request(typically an SQL statement) to the DBMS. Database System: The DBMS software together with the data itself. Sometimes, the applications are also included. (DB + DBMS + Application program) To manage large amounts of data Efficiently, reliably, securely and conveniently. Main Characteristics of the Database approach 1. Self-describing nature of a database system 2. Insulation between programs and data 3. Data Abstraction 4. Support of multiple views of the data 5. Sharing of data and multiuser transaction processing Transaction: executing program or process that includes one or more database accesses, such as reading or updating of DB record. View: Subset of the database. Allows each user to have his/her own view of database 1

DataBase fundamentals (IS 220) تانايب دعاوق تايساسأ · • Advantages of using the Database Approach ... Cardinality ratio (number of relationship an entity can

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: DataBase fundamentals (IS 220) تانايب دعاوق تايساسأ · • Advantages of using the Database Approach ... Cardinality ratio (number of relationship an entity can

1/17/1439 AH

DataBase fundamentals (IS 220) أساسيات قواعد بيانات

Lecture 1: Ch1 -Principles of DataBases-• File-Based Systems: Collection of application programs that

perform services for the end users. (e.g: reports). - Each program defines and manages its own data.- Limitations (Disadvantages): 1. Separation and isolation of data2. Duplication of Data3. Data dependence4. Incompatible file formatsSolution? DATABASE Approach. - Data: know facts that can be recorded and have an implicit

meaning. Data itself has no meaning without meatdata which describes data.

- Information: the data you process in a manner that make it meaningful. So, Data is what you store in database. Information is what you retrieve from database.

- Database: Shared collection of logically related data and a description of it, designed to meet the information needs of an organization.

- Metadata (system catalog/data dictionary): the description of data, used to enable program-data independence.

- Entity: a distinct object( person, place, thing…) in the organization that is to be represented in the database.

- Attribute: is a property that describes some aspect of the object that we wish to record.

- Relationship: is an association between entities.Database Management System (DBMS): a software system that enables users to create, maintain, and query the database.examples of DBMSs: Oracle, MS Access, MySQLDatabase Application: collection of data and programs that interacts with the database by issuing an appropriate request(typically an SQL statement) to the DBMS.Database System: The DBMS software together with the data itself. Sometimes, the applications are also included. (DB + DBMS + Application program)To manage large amounts of data Efficiently, reliably, securely and conveniently. Main Characteristics of the Database approach 1. Self-describing nature of a database system2. Insulation between programs and data3. Data Abstraction4. Support of multiple views of the data5. Sharing of data and multiuser transaction processingTransaction: executing program or process that includes one or more database accesses, such as reading or updating of DB record.View: Subset of the database. Allows each user to have his/her own view of database

�1

Page 2: DataBase fundamentals (IS 220) تانايب دعاوق تايساسأ · • Advantages of using the Database Approach ... Cardinality ratio (number of relationship an entity can

1/17/1439 AHComponents of DBMS environment: 1. Hardware2. Software3. Data4. Procedures5. People

• Role in the Database Environment - System Analyst: Determine the user requirements and

develop the system specification.- Database Designers: responsible for defining the

content, the structure, the constraints, and functions or transactions against the database.

- Application Programmer - Implement programs meet the end user needs. - Test, debug, document, and maintain transactions. - Database administrations - End-users > Categories: Naive(queries/modified data) -

Sophisticated. • Advantages of using the Database Approach - controlling redundancy in data storage and in development and maintenance

efforts.- Sharing of data among multiple users.- Restricting unauthorized access of data- Providing backup and recovery services- Providing multiple interfaces to different classes of users- Enforcing integrity constraints on the database• Disadvantages of using the Database Approach - Complexity - Cost of DBMS- Additional hardware costs- Performance - Higher impact of a failure

Lecture 2: Ch1 -DataBase Environment-• Data abstraction: generally refers to the suppression of details of data

organization and storage, and the highlighting of the essential features for an improved understanding of data.

- it enables different users to perceive data at their preferred level of detail.

• Schema(intention): description of database. < like metadata. + Change rarely.

- displays schema is called: Schema diagram. - We call each obj in the schema a schema construct. • Instance(database state/extension): the actual database at any

point of time. - Changes rapidly - When we initially load data into database, it said to move into

initial state of database.- Each write operation changes the current state to new state.• Goal of Three-level Architecture is to separate the user apps

from the physical database.

�2

Page 3: DataBase fundamentals (IS 220) تانايب دعاوق تايساسأ · • Advantages of using the Database Approach ... Cardinality ratio (number of relationship an entity can

1/17/1439 AH

1. The External or view Level - includes a number of external schemas or user views. (the ways users perceive

data)- describes the part of database the is relevant to a particular user.2. Conceptual Level - It has a conceptual schema(logical structure of entire database)- Describes what data is stored in database and relationships among

the data.- It concentrates on describing entities, data types, relationships, user

operations, and constraints.3. Internal Level - It has an internal schema(the way DBMS and OS perceive data)- Physical representation of the database on computer.- How the data stored in the database. • Mapping: the processes of transforming requests and results

between levels. • Reasons for separation: 1. Each user should able to access the data, but have a different

customized view of data.2. DBA should be able to change the DB storage structure without

affecting user’s view.3. The internal structure of database should be unaffected by

changes to the physical aspects of storage, such as change to new storage device.

• Data Independence: the ability to modify a schema definition in one level without affecting a schema definition in the next higher level.

- Three-level architecture provides Data Independence, which means that upper level are unaffected by changes to lower level.

• Kinds of data independence: - Logical Data independence- Refers to immunity of external schemas to changes in conceptual

schema.- Conceptual schema changes -addition/removal of entities- should not require

changes to external schema or rewrites of application programs.- Physical Data independence - Refers to immunity of conceptual schema to changes in the internal schema.- Internal schema changes -using different file organizations, storage structures/

devices- should not require change to conceptual or external schemas.• Data Model: collection of concepts that can be used to

describe the structure of a database. - Purpose > to represent data in an understandable way.• Database Design: - Conceptual database design > to build the conceptual

representation of the database, which includes identification of the important entities, relationships and attributes.

- Logical database design > to translate the conceptual representation to the logical structure of the database, which includes designing the relations.

- Physical database design > to decide how the logical

�3

The stages of database system development lifecycle

Page 4: DataBase fundamentals (IS 220) تانايب دعاوق تايساسأ · • Advantages of using the Database Approach ... Cardinality ratio (number of relationship an entity can

1/17/1439 AHstructure is to be physically implemented (as base relations) in the target database management system (DBMS).

- Conceptual data design: the process of constructing a model of the data in an enterprise, independent of all physical considerations.

- the conceptual data model includes ER and a data dictionary.- Logical data design: process off constructing a model of the data used in an

enterprise based on a specific data model -relational-, but independent of particular DBMS and other physical considerations.

- Physical data design: process of producing a description of the implantation of the database on secondary storage. < tailored to a specific DBMS.

• Classification or models of DBMSs: - First Generation: 1. Network data model > allowing a record to participate in multiple parent/

child relationships.- Allowing child records to have multiple parents (M:N).2. Hierarchical data model > each parent record can have many children,

but each record has only one parent (1:M). -Tree-like structure-- Disadvantages: 1. Required complex programs for even simple queries.2. Minimal data independence.3. No widely accepted theoretical foundations.- Second Generation: 1. Relational data model > computer database in which all data is stored in

relations(tables with rows and columns). each table is composed of records(Tuples) and each record is identified by a field(attribute with unique value).

- Advantages: - Modifying data will be efficient.- Data retrieval will be efficient too.- Self-documenting.- Changes to the database schema easy to make.- Third Generation: - Object-oriented data model > Response to increasing complexity of

DB applications.

Lecture 3: Ch12 -Data modeling using ER Model-• ER(Entity-Relationship) model: is a popular high-level conceptual

data model. - Based on real world objects called entities and relationships between them.- it can be directly translate to relations (tables).ERD(Entity-Relationship Diagram): is a graphical model for representing conceptual data model. using three elements: Entities, attributes, and relationships. 1. Entity: real thing with an independent existence. could be: - physical existence (car, student)- conceptual existence (company, course).2. Attribute: property describing an entity. eg: student name, ID, age…

�4

Page 5: DataBase fundamentals (IS 220) تانايب دعاوق تايساسأ · • Advantages of using the Database Approach ... Cardinality ratio (number of relationship an entity can

1/17/1439 AHTypes of Attributes:

• Keys of Entity: • Candidate key: is minimal set of attributes whose values uniquely

identify and entity. -Can’t be NULL- - there could be more than one; if so, we select one as primary key

and the other an alternate key.• Primary key: the selected key that uniquely identify and entity. -

indicated with underline-• we choose PK based on: - Attribute length - Number of attributes required- Certainty of uniqueness.• PK could be: - simple > single attribute- composite > multiple attributes.3. Relationship: association between entities. -represented by diamonds- - it could have attributes.• Degree of relationships: - Unary(recursive) between one entity- Binary between two entities- N-ary more than two. e.g.: 3 entities > ternary.• Role: purpose that each participating entity type plays in a relationship.• Types of constraints on relationships:

Multiplicity: number(range) of possible entities that may relate to a single association through a particular relationship. (min, max) - * = many -

- Strong entity: have a key attribute. (independent) - Weak entity: no key attribute. (depends on some other

entity) - -represented by double line- - weak entity in weak relationships is always mandatory.

Simple: single atomic value with an independent existence.

Single-valued Stored

Composite: multiple components. Multi-valued Derived: represents a derivable value from a related attribute.

�5

Cardinality ratio (number of relationship an entity can participate in)

Participation (all or only some entities participate in a relationship)

1:1 Mandatory(total): requires(every, should, must)represented by double lines

1:M - M:1 Optional(Partial): doesn’t required(not every, may, could)represented by single line

M:N

Page 6: DataBase fundamentals (IS 220) تانايب دعاوق تايساسأ · • Advantages of using the Database Approach ... Cardinality ratio (number of relationship an entity can

1/17/1439 AH

Lecture 4: Ch3 -Mapping ERD to Relations-

Logical (relational) model represents database as relations(tables). - Tuple: row of relation.- Degree of relation: number of attributes in it.- Cardinality: number of tuples in it.- Relational schema: describes a relation(R). Denoted by:

R(A1,A2,…,An).

• Relational Keys: - Candidate Key (CK)- Primary Key(PK)- Foreign Key(FK): attribute(s) in a relation that matches the

CK of some relation. (relationships between two relations). - Relations should have as few NULL values as possible, if

there are frequents NULL values, we can separate relations with PK. - NULL values: - Unknown value- Unavailable value- Not applicable attribute\

• Data redundancy: data field appear more than once in the database. - Disadvantages: weak maintaining of DB + waste of memory.

• Mapping next page >

�6

Page 7: DataBase fundamentals (IS 220) تانايب دعاوق تايساسأ · • Advantages of using the Database Approach ... Cardinality ratio (number of relationship an entity can

1/17/1439 AH

�7

Mapping strong entity type Mapping Multivalued attributes Mapping weak entities

1 relation with attributes + PK composite > only componentsDerived > omitted

2 relations: 1. all attributes except multivalued + PK2. PK of 1st relation as FK + multivalued

attribute

2 relations: 1. all attributes of 1st entity + PK2. all attributes of weak entity + Key + PK

of 1st relation as FK

Mapping Binary relationships M:N Mapping Binary relationships 1:M Mapping Binary relationships 1:1

3 relations: 1. all attributes of 1st entity + PK2. all attributes of 2nd entity + PK3. PKs of entities as FKs + Relationship

attribute

2 relations: 1. all attributes of (one side) entity + PK2. all attributes of (many side) entity + PK

of (one side) as FK + relationship attribute

- Mandatory/optional on both sides: 2 relations: 1. all attributes of 1st entity + PK2. all attributes of 2nd entity + PK of 1st as

FK + relationship attribute^ opposite is fine

- Mandatory on one side2 relations: 1. all attributes of optional side entity2. all attributes of mandatory side entity +

PK of optional side as FK + relationship attribute

Mapping Unary relationships 1:M Mapping Unary relationships M:N Mapping Unary relationships 1:1

1 relation: 1. PK + copy of PK as FK + relationship

attribute

2 relations: 1. all attributes of entity + PK2. PK as FK + copy of PK as FK +

relationship attribute

- Mandatory on both sides: < 1:M rules - Optional on both sides: < M:N rules - Mandatory on one side: either of two

methods 1:M/M:N

Mapping N-ary relationships

n relations: (n = number of entities) 1. all attributes of entity + PK2. PKs as FKs + relationship attribute

\

Page 8: DataBase fundamentals (IS 220) تانايب دعاوق تايساسأ · • Advantages of using the Database Approach ... Cardinality ratio (number of relationship an entity can

1/17/1439 AH

Lecture 5: Ch14-15 -FDs and Normalization-

Functional dependencies: describes the relationship between attributes in a relation.- Purpose: specify the set of integrity constraints that must hold

on a relation. Normalization: bottom-up approach to DB design that begins by examining the relationships between attributes. -series of tests- - Purpose:

Guarantees no redundancy due to FDs. Guarantees no update anomalies. • Normal forms:

UNF(unnormalized form): relation that contains one or more repeating groups.

�8

1NF 2NF 3NF

- All attributes depends on the key - intersection of each row & col

contains one & only one value.- Disallows: 1. Multivalued attributes 2. nested relations (repeating

group)

- All attributes depends on the whole key (Full functional dependency)

- Disallows: partial dependency

- All attributes depends on nothing but the key

- Disallows: Transitive functional dependency

UNF —> 1NF ? 2 relation: 1. all attributes except multivalued. 2. PK of 1st relation + multivalued -both becomes PK-

1NF —> 2NF ? - 1NF - remove partial dependency by

moving in a new relation with copy of determinants.

1NF —> 2NF ? - 1NF - 2NF - place transitive dependent

attributes in new relation with copy of determinants.

Page 9: DataBase fundamentals (IS 220) تانايب دعاوق تايساسأ · • Advantages of using the Database Approach ... Cardinality ratio (number of relationship an entity can

1/17/1439 AH

Lecture 6: Ch6 -SQL overview, oracle date type, DDL and constraint-

• Database Languages: 1. Data Definition Language(DDL): allows DBAs/users to describe and name

entities, attributes and relationships. (define a schema or modify it)2. Data Manipulation Language(DML): provides basic data manipulation operations

on data held in the database (insertion, modification, retrieval, deletion)Two types of DML:- Procedural DML > allows user to tell system how to manipulate data (OracleSQL)- Non-Procedural DML > allows user to state what data is needed (SQL query)3. Data Control Language(DCL): component of database language used to

create privileges to allow users access to the database.• SQL overview: - stands for Structured Query Language- standard for relational database management systems

such as: Oracle.- All SQL statements are instructions to the database- Easy to learn + case insensitive- non-procedural language

DDL: Referential integrity constraint: ON DELETE/UPDATE CASCADEON DELETE/UPDATE SET NULLON DELETE/UPDATE SET DEFAULT^ follows foreign key constraint.

�9

Table creation NOT NULL constraint *column level only*

UNIQUE constraint DEFAULT constraint

PRIMARY KEY constraint FOREIGN KEY constraint CHECK constraint

Page 10: DataBase fundamentals (IS 220) تانايب دعاوق تايساسأ · • Advantages of using the Database Approach ... Cardinality ratio (number of relationship an entity can

1/17/1439 AH

Lecture 7: Ch6 -SQL overview, oracle date type, DDL and constraint-ALTER table (add/delete/modify columns, add/remove constraint)

Lecture 8: Ch4,5-Relational Algebra, Data Manipulation in SQL, Simple SQL queries-

Relational Algebra: operations on relations(sets of records) to produce a relation as result. output from one operation can become input to another operation (nested relational algebra). 1. Unary operations: (works on one relation) < -examples only on unary- I. SelectionII. Projection1. Binary operations:(pair of relations) Cartesian product - Union - Join - Intersection - Set difference - Division.

• DML cont. in next page>

�10

add column delete column modify column add constraint

remove constraint enable/disable constraint drop table view table

Selection Projection

Select[cond](R) selects tuples out of relation R that meet specified condition to result in a new relation.

Project[attr-list](R) selects columns specified in the attributes list out of R to result in a new relation.

INSERT INTO -add rows-

SELECT (column)/(*) -only for outputs, no

change in the actual data-

UPDATE -update records in table-

DELETE -delete records with

rollback-

TRUNCATE -no rollback and empties

memory space-

Page 11: DataBase fundamentals (IS 220) تانايب دعاوق تايساسأ · • Advantages of using the Database Approach ... Cardinality ratio (number of relationship an entity can

1/17/1439 AHWHERE clause (condition) - Comparison (=,<,>,<>,<=,>=) , AND/OR/NOT - Range (BETWEEN/NOT BETWEEN) - Set membership (IN/NOT IN) - Pattern match (LIKE/NOT LIKE) > % 0 or more characters , _ single character - IS NULL/ IS NOT NULL

DISTINCT (removes duplicate values)

ORDER BY (ascending/descending order)

Lecture 9 is after 10 -sorry for the inconvenient-

Lecture 10: Ch5 -Data Manipulation in SQL, Advances SQL queries-

- Table and Column names can be prefixed with owner’s name. for example: table product owned by John: SELECT * FROM John.product;- Column names can be prefixed with table name: SELECT product.prodNo FROM product;• Alias: temporarily rename a table or column heading.

• Join: if two or more tables are needed at the same time to find all required data (joined together).

- it computes new table from joined ones.

�11

Page 12: DataBase fundamentals (IS 220) تانايب دعاوق تايساسأ · • Advantages of using the Database Approach ... Cardinality ratio (number of relationship an entity can

1/17/1439 AH- Types of JOIN: A. Inner join: return rows when there is at least one match in both

tables. B. Left join: return all rows from the left table even if there are no

matches in the right table. C. Right join: return all rows from the right table even if there are

no matches in the left table. D. Full outer join: retains rows that are unmatched in both the tables.Note: all outer joins, the displayed unmatched column filled with NULLs.

- Examples of JOIN >> -more examples in slides-

- Outer Joins in Oracle SQL: • put (+) on the potentially deficient side -where nulls

may be added-• it’s placed in the join condition next to the table that is allowed

to have NULL values. Note: table may be outer joined with only one other table.

- More examples:

• Nested Queries: 1. query results are tables, which can also be queried. - inner query referred to as subquery. 2. If query result is a single value, it can be treated as a value, and be compared

with other values.

�12

Simple join (two tables) more than two table

Page 13: DataBase fundamentals (IS 220) تانايب دعاوق تايساسأ · • Advantages of using the Database Approach ... Cardinality ratio (number of relationship an entity can

1/17/1439 AH

• Queries using EXISTS/ NOT EXISTS: - designed only for subqueries. - EXIST returns to there exist at least one row in

the result table returned by the subquery, it’s false if subquery returns an empty result table.

• Union: used to combine result-set of two or more SELECT statements and eliminates any duplicate rows from the result.

- SELECT statement with UNION must: 1. have the same number of columns. 2. columns must have same data type. 3. columns in each SELECT statement must be in the

same order. - to allow duplicate values, use UNION ALL.

• Minus: returns only unique rows returned by the first query not the second.

• Intersect: returns only those rows returned by the two queries.

�13

Page 14: DataBase fundamentals (IS 220) تانايب دعاوق تايساسأ · • Advantages of using the Database Approach ... Cardinality ratio (number of relationship an entity can

1/17/1439 AH

Lecture 9: Ch5 -Data Manipulation in SQL, Advances SQL queries-• Aggregate functions:

• Advanced queries -GROUP BY-: - used in conjunction with aggregate functions to

group the result-set. - use group by clause to get sub-totals. - if WHERE is used with GROUP BY, WHERE is

applied first, then groups are formed from rows satisfying condition.

• HAVING: - designed to be used with GROUP BY to restrict groups.

• Inserting data using queries: - you can insert a result of at query into table if you have

two tables that has same structure you can use it.

• SQL data loader: - for a table containing a large data set, INSERT

command is not efficient. - oracle provides a data loader utility SQLLoader. - data can be loaded from any text file inserted to

database. - but table must be created first.

�14

Count: return number of values(without nulls)

Sum: return total of numeric column

Avg : return average of numeric column

Min(smallest value), Max(larges value)

- we can use * for all table- we can use distinct to omit

duplicated values.

Page 15: DataBase fundamentals (IS 220) تانايب دعاوق تايساسأ · • Advantages of using the Database Approach ... Cardinality ratio (number of relationship an entity can

1/17/1439 AH

Lecture 11: Ch5 -Transaction control language, data control language-• Transaction control Language(TLC): used to control transactional

processing in database. - Commands: A. COMMIT: to save changes permanently in the database.B. ROLLBACK: to undo all changes of a transactions.C. SAVEPOINT: creates points within groups of transactions on which to

rollback.

• Data Control Language(DCL): component of the SQL language is used to create privileges to allow users access to, and manipulation of the database. two main commands:

الحمدلّله الذي بنعمته تتم الصالحات.“الّلهم عّلمنا ماينفعنا وانفعنا بما علمتنا وزدنا اللهم علًما”

�15

Savepoint Commit Rollback

- names and marks the current point in the processing of a transaction.

- if you want to make your update permanent.

- used to save changes invoked by a transaction to the database.

- erases any savepoints you marked since last commit.

- if you change your mind or need to correct a mistake.

- inverse of COMMIT statements.

- during current transaction.

Grant Revoke

- in order to log onto the database you must be given appropriate privileges.

- SQL command to grant privilege on a table:

- any combination is allowed.- you can issue this on any table you created.- example:

- remove a privilege from a user.- SQL command to revoke privilege on a table:

- example: