Está en la página 1de 192

Introduction to RDBMS and SQL

Module 2: Introduction to RDBMS

Course Code #IDCGF002

Agenda
Data and Storage. Drawbacks with File Systems. Introduction to DBMS and its benefits. Multi-tier Architecture Data Models Hierarchical Model. Network Model. Relational Data Model. Relational Data Model concepts. Codds 12 rules.
2

Learning Objectives
Understand the purpose of storing data. Understand drawbacks of file systems. Understand the meaning of Database. Compare Databases with traditional methods. Understand the Multi-Tier Architecture. Understand Data Models. Understand Codds Rules.

Data and its storage


We require reliable data to work with and hence it becomes essential to store the data. We often have to store the data as it is crucial in business decisions. There are 2 ways to store data in computer:
Traditional Approach (File Systems). Database Management Systems (DBMS).

A File System is described by a very simple data storage model


In this, all the information is stored in a plain text file, one record per line. Each record is divided into fields using delimiters or are delimited using fixed column positions.
4

Drawbacks with File Systems


Drawbacks Data Dependency Description Data dependency means that the application program is dependant on the data. So any modifications made on the data affects the application program using it, forcing it to be rewritten. Redundancy is the repetition of data. Data duplication was allowed in the traditional approach. Data redundancy caused data inconsistency to occur in the data. Data Sharing was difficult or impossible with traditional systems. Data stored on the computer must be secured. It was difficult to enforce independent information security in traditional approach. Transaction is unit of work. If one is performing some work ( for e.g. inserting some records) other persons should not be able to use that particular data. There was no way to isolate independent units of work in traditional approach.
5

Redundancy

Sharing Security

Transaction Control

Introduction to DBMS
DBMS is Data Base Management System. It is a program that lets one or more computer users create and access data in a database. It allows data definition, storage, and management of data in the centralized pool that can be shared by many users.

Benefits of DBMS
Controls Data Concurrency
Databases allow multiple users to access the same database simultaneously.

Avoids Data Redundancies


A properly set-up database minimizes data redundancy. It will avoid data repetition in the databases.

Ensures Data Validity


Databases allow you to set up rules that ensure that data remains consistent when data is added or modified.

Ensures Data Sharing


Since it allows multiple users to access the data, sharing data becomes easier in database.

Ensures Data Security


Data is secured as different privileges and roles can be assigned to each user.
7

Application Programs using DBMS Services


Application 1 Application 2 Application 3 Application Programs

DBMS

File System

Storage
8

Multi-tier Architecture
Multi-tier architecture is also known as n-tier architecture.

It is a client-server architecture in which an application is executed by more than one distinct software agent. For example: An application that uses middleware to service data requests between a user and a database employs multi-tier architecture. The most widespread use of "multi-tier architecture" refers to three-tier architecture.
9

Three-tier Architecture
Presentation Tier
The top-most level of the application is the user interface. The main function of the interface is to translate tasks and results to something the user can understand.
> GET SALES TOTAL > GET SALES TOTAL 4 TOTAL SALES

Logic Tier
This layer coordinates the application, processes commands, makes logical decisions and evaluations, and performs calculations. It also moves and processes data between the two surrounding layers.

GET LIST OF ALL SALES MADE LAST YEAR

ADD ALL SALES TOGETHER

SALE 1 QUERY SALE 2 SALE 3 SALE 4

Data Tier
Here Information is stored and retrieved from a database or file system. The information is then passed back to the logic tier for processing, and then eventually back to the user.
Storage Database

10

Data Models
Data model is a model that describes in an abstract way how data is represented in a business organization, an information system or a database management system. Following are the different data models available
Type of Data Model Hierarchical Model Network Model Relational Model Description Well suited for data which are related hierarchically. Database model conceived as flexible way of representing objects and their relationships. Data is represented in the form of two-dimensional tables.

11

Hierarchical Data Model

12

Network Data Model

13

Relational Data Model


It is based on Relational algebra i. e. mathematical theory of relations. It was first described by E.F. Codd in 1970 and then modified by IBM. It presents data in form of tables. Rules of Relational Data Model It defines tables which contains Rows and Columns. It presents a set of rules to ensure the integrity of the database. In Relational model a table is also a relation. Row is known as a tuple. Column is known as an attribute. Dr. Codd modified his model by defining 12 rules (Codds Rules), that a DBMS must meet in order to be considered a relational database.
14

Basic Terms and Definitions


The term 'database' has many interpretations one definition is that database is a 'collection of persistent data. A relational database is one in which the data consists of a 'collection of tables related to each other through common values. A relational database management system (RDBMS) uses matching values in multiple tables to relate the information in one table with the information in the other table. The presentation of data as tables is a logical construct - it is independent of the way the data is physically stored on disk.
15

Basic Terms and Definitions (Contd )


The two most prominent characteristics of a relational database are Data stored in tables. Relationships between tables. A table (entity or relation) is a collection of rows and columns. A row (record or tuple) represents a collection of information about a separate item (e.g., a customer). A column (field or attribute) represents the characteristics of an item (e.g., the customer's name or phone number). A relationship (join) is a logical link between two tables.
16

Codds 12 rules
Rule No.
#1 #2

Rule
Information rule Guaranteed access Rule Systematic treatment of null values Dynamic on-line catalog based on the relational model

Description
Database must consist of tables related to each other and data should be stored in the form of tables only. The data can be accessed by specifying the table name and the columns that define the primary key. Primary key ensures that each value is unique and accessible. A Null is an unknown value and every database must have a provision for storing NULL values. In addition to user data, a relational database contains data about itself, there are 2 types of tables. User tables - that contain the 'working' data and system tables contain data about the database structure. Metadata that describes the structure of the database itself and includes object definitions (tables, indexes, stored procedures, etc.) and how they relate to each other. The collection of system tables is also referred to as the system catalog or data dictionary.

#3 #4

17

Codds 12 rules (Contd)


Rule No. #5 Rule Description

Comprehensive Data Sublanguage Rule

There must be a single language that handles all communication with the database management system. The language must support relational operations with respect to: data modification, data definition and administration. Database must allow for presenting data to the user in different combinations through views. Views are nothing but virtual tables which contains extraction of data from the source tables. Views allows to create customized snapshot of data to suit specific needs. If a view is a simple view then it can provide update and delete operations on views.

#6

View Updating Rule

18

Codds 12 rules (Contd)


Rule No.
#7

Rule
High-level insert, update and delete

Description
Rows are treated as sets for data manipulation operations. A relational database should support basic operations like subqueries, join, operators and set operations like union, intersection and minus. Set operations and relational operators are used to operate on 'relations' (tables) to produce other relations (tables).

#8

Physical Data Independence


Logical Data Independence Integrity Independence

Any changes in the data storage should not affect the application that accesses it.
Data is logically stored in tables and physically in files. For any change in the structure of tables and relationships, the application need not be re-created. In order to be considered relational, data integrity must be an internal function of the DBMS; not the application program. Data integrity means the consistency and accuracy of the data in the database (i.e., keeping the garbage out of the database).
19

#9

# 10

Codds 12 rules (Contd)


Rule No.
# 11

Rule
Distribution Independence

Description
Distributed databases should support the data manipulation language of SQL. A complex view can be created by implementing joins to extract data not only from tables on different servers (distributed queries) but also from different types of databases (heterogeneous queries). While extracting the data from the database, the user need not be aware whether the database is distributed or not. It is not possible to bypass the constraints defined on the table which means that the database integrity should never be violated.

# 12

Non subversion Rule

20

Key Points
Databases are an efficient way to store large amounts of data. Relational Databases contain tables and fields. It ensures that any data entered is valid and doesnt corrupt the database. Databases are part of a larger software application called a database management system (DBMS). A database to be called as RDBMS, should adhere to Codds Rules.

21

Questions and Comments What questions or comments do you have?

22

Normalization
Normalization is the process of efficiently organizing data in a database. It is a design technique that is widely used as a guide in designing relational databases.

Normalization is essentially a two step process


It puts data into tabular form by removing repeating groups.

It removes duplicated data from the relational tables. Normalization theory is based on the concepts of normal forms.
23

Normal Forms
A relational table is said to be in a normal form if it satisfies a certain set of constraints. They are special properties and constraints that a table schema should possess in order to achieve certain desired goals like minimizing redundancy of data. There are six normal forms that have been defined
First Normal Form (1NF) Third Normal Form (3NF) Fourth Normal Form (4NF) Second Normal Form (2NF) Boyce Codd Normal Form (BCNF) Fifth Normal Form (5NF)

The Third Normal Form is quite sufficient for most business database design purposes.
24

Example of Normalization
Normalize the following Data

item_ no

item_desc

price

manufacturer_id

manufacturer _name

distributor_ id

distributor _name

order_id

date_entered

Qty

Watches

3000

M1001

Aqua Marine

D1001

Marine World

10010 10011

12-May-07

20

Pens

1000

M1002

Magnum

D1002

Water World

10011 10012

14-May-07

10

Mobiles

4000

M1003

Coral

D1003

Alpha Company

10010 10012

16-May-07

15

25

First Normal Form(1 of 2)


A design is said to be in first normal form(1NF) when there are no repeating groups and all attributes are dependent on the primary key. Example:
In a table the Address field can be further divided into smaller segment like: House Number, Street Name1, City, PIN etc. So, in a table if we have a column called Address we can not consider it in first Normal Form.
26

First Normal Form(2 of 2)


A design is said to be in first normal form(1NF) when there are no repeating groups and all attributes are dependent on the primary key.

Example:
In a table the Address field can be further divided into smaller segment like: House Number, Street Name1, City, PIN etc. So, in a table if we have a column called Address we can not consider it in first Normal Form.
27

First Normal Form (Contd)

item _no

item_desc

price

manufacturer _id

manufacturer _name

distributor _id

distributo r_name Marine World Marine World Water World Water World Alpha Company

order_id 10010 10011 10011 10012 10010 10012

date_entered

Qty

1 1 2 2 3 3

Watches Watches Pens Pens Mobiles Mobiles

3000 3000 1000 1000 4000 4000

M1001 M1001 M1002 M1002 M1003 M1003

Aqua Marine Aqua Marine Magnum Magnum Coral Coral

D1001 D1001 D1002 D1002 D1003 D1003

12-May-07 12-May-07 14-May-07 14-May-07 16-May-07 16-May-07

20 20 10 10 15 15

Alpha Company

28

Functional Dependency(1 of 2)
A functional dependency exists when the value of one item is fully determined by another.
Example: given the relation Student(StudID, StudName, Marks), attribute StudName is functionally dependant on attribute StudID. This is written as StudID StudName. In other words attribute A is functionally dependent on B if and only if for each value of B, there is exactly one Value of A. Attribute B is called determinant. For any given value for attribute A, there is just one corresponding value of attribute B.
29

Functional Dependency(2 of 2)
A functional dependency exists when the value of one item is fully determined by another.
Example: given the relation Student(StudID, StudName, Marks), attribute StudName is functionally dependant on attribute StudID. This is written as StudID StudName. In other words attribute A is functionally dependent on B if and only if for each value of B, there is exactly one Value of A. Attribute B is called determinant. For any given value for attribute A, there is just one corresponding value of attribute B.
30

Example of Functional Dependency


Code Name City

E1
E2

Richard
Thomson

CA
Delhi

Given a particular value of Code there is precisely one corresponding value for Name Example: For code E1 there exactly one value of name Richard. Name is functionally dependent on Code.

31

Example of Functional Dependency


Airport Name National City Washington, DC Each airport name is unique and each airport can be in only one city. Therefore, City is functionally dependent on Airport. The value in the Airport field determines what the value will be in the City field and there can be only one value in the City field.

JFK
LaGuardia Logan Dulles

New York
New York Boston Washington, DC

This does not work in the reverse. As shown in the table, a city can have more than one airport, so Airport is not functionally dependent on City; the value in City does not necessarily determine what the value in Airport will be.
32

Full Functional Dependency


Given a relation R, attribute B of R is fully functionally dependent on attribute A of R, if it is functionally dependent on A and not functionally dependent on any subset of A (A must be composite)

Cname is fully functionally dependent on C#. Ccity and Cphone are also fully functionally dependent on C#. Qnt is fully functionally dependent on (C#, P#, Date composite key). Cname is not fully functionally dependent on (C#, P#, Date), it is only partially dependent on it (and similarly for Ccity and Cphone).
33

Second Normal Form


An entity type is in second normal form (2NF) if it is in 1NF and all its attributes are dependent on the primary key. In other words, there are no attributes that are dependent on either none of or only part of the primary key.
Items Item_id* Item_Desc Price Maufacturer_id Maufacturer_name Distributor_id Distributor_name
34

Orders Order_id* Item_id* Date_entered Qty

Transitive Dependency
A transitive dependency is a type of functional dependency in which the value in a non-key field is determined by the value in another non-key field and that field is not a candidate key.
The phone number is dependent on the manager, which is dependent on the project number (a transitive dependency) The ProjectMgr field is not a candidate key because the same person manages more than one project
35

Third Normal Form

An entity is in third normal form (3NF) if it is in 2NF and all of its attribute are dependent on only the primary key. In other words, all non-key attributes are mutually independent and are fully dependent on the primary key. To make sure that an entity is in 3rd Normal form, we need to remove any column which is depending on any other attribute of the table.

36

Example of 3NF
Example : Let us assume that we have the following structure for a table:

Column Name Book_ID Book_Name Publisher_ID Publisher_Name Publisher_Address ISBN_Number

Constraint Primary Key

37

Example of 3NF (Contd)


In the above example, Publisher_Address column is not depending on the Book_ID (which is the Primary Key), rather it depends on Publisher_Name. So, to make the entity in 3rd Normal Form, we need to restructure the table as follows:
Book Table Column Name Book_ID Book_Name Publisher_ID ISBN_Number Foreign Key Constraint Primary Key

Publisher Table Column Name Constraint Primary Key

We shall have another table which will hold the data about the Publishers:

Publisher_ID Publisher_Name

Publisher_Address
38

Example of 3NF
Example 1:
Manufacturers
Items Item_id* Item_Desc Price Maufacturer_id Distributor_id Distributors Distributor_id * Maufacturer_id * Maufacturer_name

Distributor_name

39

DENORMALIZATION
What is DENORMALIZATION? Denormalization is a technique which is the opposite of normalization. In order to speed up the database access, it suggests to move from higher level of normalization to lower normal forms of database modeling. NEED FOR DENORMALIZATION Consider 2 tables Orders and OrderItems which stores the order and items to be ordered respectively. As per your analysis, you have created a view called Order_Item_View. This is one of the most heavily used views in the database. To read data from Order and the OrderItems table will require a join statement and access to many physical pages. To write data will require several update statements. Moreover 90 percent of Orders have no more than 5 positions. To avoid joins, you can merge the two tables into 1 table. Data is normalized to avoid maximum redundancy. They are not optimized for minimum access time. In the case of denormalization process, time does not play a role. The only reason why denormalization is done is to enhance the performance.
40

Denormalization (Contd)

41

3: Database Design Techniques Activity


Input documents: Activity_1.0_Module3_Datab aseDesignTechniques.doc Output document: Activity_1.0_Module3_Datab aseDesignTechniquesOutput .doc

60 Minutes
42

3: Database Design Techniques Key Points Normalization is the process of efficiently organizing data in a database. Normalization is designed to logically address potential problems with information stored in a database. There are six normal forms. Typically a database will be in Third Normal Form.

43

3: Database Design Techniques Key Points (Contd) A design is said to be in first normal form(1NF) when there are no repeating groups and all attributes are dependent on the primary key. An entity type is in second normal form (2NF) if it is in 1NF and all its attributes are dependent on the primary key. A transitive dependency is a type of functional dependency in which the value in a non-key field is determined by the value in another non-key field and that field is not a candidate key.
44

3: Database Design Techniques Questions and Comments


What questions or comments do you have?

45

Introduction to RDBMS and SQL


Module 3: Database Design Techniques

Course Code #IDCGF002

Agenda
Normalization. Types of Normal forms. First Normal Form. Functional Dependency. Full Functional Dependence and Partial Functional Dependence. Second Normal Form. Transitive Dependency. Third Normal Form. Denormalization.
47

Learning Objectives
Understand Database design techniques. Understand the need for normalization. Understand different types of normalization. Understand Functional dependencies. Understand Denormalization.

48

Normalization
Normalization is the process of efficiently organizing data in a database. It is a design technique that is widely used as a guide in designing relational databases.

Normalization is essentially a two step process


It puts data into tabular form by removing repeating groups.

It removes duplicated data from the relational tables. Normalization theory is based on the concepts of normal forms.
49

Normal Forms
A relational table is said to be in a normal form if it satisfies a certain set of constraints. They are special properties and constraints that a table schema should possess in order to achieve certain desired goals like minimizing redundancy of data. There are six normal forms that have been defined
First Normal Form (1NF) Third Normal Form (3NF) Fourth Normal Form (4NF) Second Normal Form (2NF) Boyce Codd Normal Form (BCNF) Fifth Normal Form (5NF)

The Third Normal Form is quite sufficient for most business database design purposes.
50

Example of Normalization
Normalize the following Data

item_ no

item_desc

price

manufacturer_id

manufacturer _name

distributor_ id

distributor _name

order_id

date_entered

Qty

Watches

3000

M1001

Aqua Marine

D1001

Marine World

10010 10011

12-May-07

20

Pens

1000

M1002

Magnum

D1002

Water World

10011 10012

14-May-07

10

Mobiles

4000

M1003

Coral

D1003

Alpha Company

10010 10012

16-May-07

15

51

First Normal Form(1 of 2)


A design is said to be in first normal form(1NF) when there are no repeating groups and all attributes are dependent on the primary key. Example:
In a table the Address field can be further divided into smaller segment like: House Number, Street Name1, City, PIN etc. So, in a table if we have a column called Address we can not consider it in first Normal Form.
52

First Normal Form(2 of 2)


A design is said to be in first normal form(1NF) when there are no repeating groups and all attributes are dependent on the primary key.

Example:
In a table the Address field can be further divided into smaller segment like: House Number, Street Name1, City, PIN etc. So, in a table if we have a column called Address we can not consider it in first Normal Form.
53

First Normal Form (Contd)

item _no

item_desc

price

manufacturer _id

manufacturer _name

distributor _id

distributo r_name Marine World Marine World Water World Water World Alpha Company

order_id 10010 10011 10011 10012 10010 10012

date_entered

Qty

1 1 2 2 3 3

Watches Watches Pens Pens Mobiles Mobiles

3000 3000 1000 1000 4000 4000

M1001 M1001 M1002 M1002 M1003 M1003

Aqua Marine Aqua Marine Magnum Magnum Coral Coral

D1001 D1001 D1002 D1002 D1003 D1003

12-May-07 12-May-07 14-May-07 14-May-07 16-May-07 16-May-07

20 20 10 10 15 15

Alpha Company

54

Functional Dependency(1 of 2)
A functional dependency exists when the value of one item is fully determined by another.
Example: given the relation Student(StudID, StudName, Marks), attribute StudName is functionally dependant on attribute StudID. This is written as StudID StudName. In other words attribute A is functionally dependent on B if and only if for each value of B, there is exactly one Value of A. Attribute B is called determinant. For any given value for attribute A, there is just one corresponding value of attribute B.
55

Functional Dependency(2 of 2)
A functional dependency exists when the value of one item is fully determined by another.
Example: given the relation Student(StudID, StudName, Marks), attribute StudName is functionally dependant on attribute StudID. This is written as StudID StudName. In other words attribute A is functionally dependent on B if and only if for each value of B, there is exactly one Value of A. Attribute B is called determinant. For any given value for attribute A, there is just one corresponding value of attribute B.
56

Example of Functional Dependency


Code Name City

E1
E2

Richard
Thomson

CA
Delhi

Given a particular value of Code there is precisely one corresponding value for Name Example: For code E1 there exactly one value of name Richard. Name is functionally dependent on Code.

57

Example of Functional Dependency


Airport Name National City Washington, DC Each airport name is unique and each airport can be in only one city. Therefore, City is functionally dependent on Airport. The value in the Airport field determines what the value will be in the City field and there can be only one value in the City field.

JFK
LaGuardia Logan Dulles

New York
New York Boston Washington, DC

This does not work in the reverse. As shown in the table, a city can have more than one airport, so Airport is not functionally dependent on City; the value in City does not necessarily determine what the value in Airport will be.
58

Full Functional Dependency


Given a relation R, attribute B of R is fully functionally dependent on attribute A of R, if it is functionally dependent on A and not functionally dependent on any subset of A (A must be composite)

Cname is fully functionally dependent on C#. Ccity and Cphone are also fully functionally dependent on C#. Qnt is fully functionally dependent on (C#, P#, Date composite key). Cname is not fully functionally dependent on (C#, P#, Date), it is only partially dependent on it (and similarly for Ccity and Cphone).
59

Second Normal Form


An entity type is in second normal form (2NF) if it is in 1NF and all its attributes are dependent on the primary key. In other words, there are no attributes that are dependent on either none of or only part of the primary key.
Items Item_id* Item_Desc Price Maufacturer_id Maufacturer_name Distributor_id Distributor_name
60

Orders Order_id* Item_id* Date_entered Qty

Transitive Dependency
A transitive dependency is a type of functional dependency in which the value in a non-key field is determined by the value in another non-key field and that field is not a candidate key.
The phone number is dependent on the manager, which is dependent on the project number (a transitive dependency) The ProjectMgr field is not a candidate key because the same person manages more than one project
61

Third Normal Form

An entity is in third normal form (3NF) if it is in 2NF and all of its attribute are dependent on only the primary key. In other words, all non-key attributes are mutually independent and are fully dependent on the primary key. To make sure that an entity is in 3rd Normal form, we need to remove any column which is depending on any other attribute of the table.

62

Example of 3NF
Example : Let us assume that we have the following structure for a table:

Column Name Book_ID Book_Name Publisher_ID Publisher_Name Publisher_Address ISBN_Number

Constraint Primary Key

63

Example of 3NF (Contd)


In the above example, Publisher_Address column is not depending on the Book_ID (which is the Primary Key), rather it depends on Publisher_Name. So, to make the entity in 3rd Normal Form, we need to restructure the table as follows:
Book Table Column Name Book_ID Book_Name Publisher_ID ISBN_Number Foreign Key Constraint Primary Key

Publisher Table Column Name Constraint Primary Key

We shall have another table which will hold the data about the Publishers:

Publisher_ID Publisher_Name

Publisher_Address
64

Example of 3NF
Example 1:
Manufacturers
Items Item_id* Item_Desc Price Maufacturer_id Distributor_id Distributors Distributor_id * Maufacturer_id * Maufacturer_name

Distributor_name

65

DENORMALIZATION
What is DENORMALIZATION? Denormalization is a technique which is the opposite of normalization. In order to speed up the database access, it suggests to move from higher level of normalization to lower normal forms of database modeling. NEED FOR DENORMALIZATION Consider 2 tables Orders and OrderItems which stores the order and items to be ordered respectively. As per your analysis, you have created a view called Order_Item_View. This is one of the most heavily used views in the database. To read data from Order and the OrderItems table will require a join statement and access to many physical pages. To write data will require several update statements. Moreover 90 percent of Orders have no more than 5 positions. To avoid joins, you can merge the two tables into 1 table. Data is normalized to avoid maximum redundancy. They are not optimized for minimum access time. In the case of denormalization process, time does not play a role. The only reason why denormalization is done is to enhance the performance.
66

Denormalization (Contd)

67

3: Database Design Techniques Activity


Input documents: Activity_1.0_Module3_Datab aseDesignTechniques.doc Output document: Activity_1.0_Module3_Datab aseDesignTechniquesOutput .doc

60 Minutes
68

3: Database Design Techniques Key Points Normalization is the process of efficiently organizing data in a database. Normalization is designed to logically address potential problems with information stored in a database. There are six normal forms. Typically a database will be in Third Normal Form.

69

3: Database Design Techniques Key Points (Contd) A design is said to be in first normal form(1NF) when there are no repeating groups and all attributes are dependent on the primary key. An entity type is in second normal form (2NF) if it is in 1NF and all its attributes are dependent on the primary key. A transitive dependency is a type of functional dependency in which the value in a non-key field is determined by the value in another non-key field and that field is not a candidate key.
70

3: Database Design Techniques Questions and Comments


What questions or comments do you have?

71

Understanding RDBMS
Keith T. Weber GIS Director ISU-GIS Training and Research Center

Course Code #IDCGF002

RDBMS
Relational Database Management System

Databases

The I in GIS

Database software...
Light Duty

Medium Duty
Heavy Duty

Database software...
Light Duty Medium Duty Heavy Duty

IBM DB2 UDB


The GIS Centers heavy hitter- IBM DB2, Universal Database

Spreadsheets vs. Databases

Integrity! Structure

RDBMS Concepts and Terms

Independence

Physical Logical

Integrity
Important for consistency and transaction management. Types:
Domain Redundancy Constraint Entity Referential
Cascading or non-cascading

Key Fields

Unique Identifiers (?)


Primary key Foreign key

AKA- Relate fields.

RDBMS Structure

Database Tables

Database

Table1

Table2

Table3

Table Structure
Column 1 (Field 1) Row 1 (Record 1) Row 2 (Record 2) Value Column 2 (Field 2)

Data Value Types

Type Name Short Integer Long Integer Float Double Text (string) Date LOB (variant)

Storage Occupied/ data value 2 bytes 4 bytes 4 bytes 8 bytes 10 + max. length = bytes 8 bytes 22 + max. length = bytes

Valid Domain Range -35768 to 32767 -2147483648 to 2147483647 Any number from n-45 to n38 Any number from n-324 to n308 Any alphanumeric characters Jan 1, 100 to Dec. 31 9999 Any alphanumeric characters

Basic Steps in Database Design


Understand and document the business needs.
Problem statement Business object types Business relationships Business constraints

Create an ERM Data and process inventory Develop tuple types Tuple types to tables Integrity Populate the database

A Scenario...
Develop a GIS-Based Tourism database for Southeast Idaho.

Document the business needs


What problem or issue is this database going to address? This is a business statement

The Preliminary ERM


Symbolized.
Standard Representation Attribute Representation Entity Instance Representation

DINING DINING K K Restaurant Restaurant Number: DININ 126Number Name: Burger King Name G
Type of food: Fast Type of food

Relationships
Determine the relationships between your entity types. Add these to the ERM

Define the List


Database Dictionary The name of the Restaurant_Name restaurant Food_Type
Categories of food (e.g., 1 = Continental, 2 = Fast food, etc.) The average cost of all regular menu items.

Cost_Mean

Develop Tuple Types


Use your ERM with relationships Perform a Walk-through exercise
Simulate information is being added/used in your database.

Symbolize using Attribute Representation

Tuple Types to Tables


ENTITY TYPES TUPLE TYPES

TABLES

RELATIONSHIP TYPES

Normalization
First-Fifth Form Normal (1FN, 2FN,5FN) Academic Applied

1FN
All values are atomic Single cell contains single data value

2FN
Satisfy 1FN and Redundant data must be eliminated
How? Example: Puppy_ID, Trick_ID, Trick_Name

3FN
Satisfy 1NF and 2FN and No non-key attributes are dependent on other non-key attributes.
Example: Appointment_ID, Name, Date, Time, Species

After Normalization
New tuple types will be created. New tables will be planned. Many-many relationships will be handled using associative tables (bridge tables).

De-Normalization
What? Is this heresy?

Designing the Actual RDBMS


Visual modeling based upon your ERM and Tuple type model. Implementation of integrity rules based upon your business constraints.

Populate...
Questions and concerns to revisit
Null data Reporting discrepancies and variations Measuring or estimating methods Client utility/efficiency

The Last Step?

Questions?

Reading a Business Statement


IT4GIS Keith T. Weber, GISP GIS Director, ISU

Course Code #IDCGF002

Identify Candidate Classes


A candidate class may or may not remain a class throughout the design process A candidate class may or may not become a table Do not think about tables and relationship classes at this point

Think Object-Oriented
Classes are nouns A noun is a person, places, and things

And nowVerbs
Candidate methods are verbs
They show action They are behaviors

Methods
Identifying candidate methods allows us to better understand how the business operates and how the Enterprise uses GIS data. A method is a behaviora relationship between classes The candidate methods will describe an inheritance, aggregation, or dependency relationship

Questions?

Introduction to RDBMS and SQL


Module 7.1 Constraints

Course Code #IDCGF002

Objectives
At the end of this presentation, you should be able to: Understand the need of constraints Understand different types of constraints Understand how to add constraints in the schema design Understand how to remove constraints Understand how to add default value to a column

112

Agenda
Constraints Primary Key Constraint Foreign Key Constraint UNIQUE constraint CHECK constraint NOT NULL Constraint Default

113

Constraints
Constraint is a rule or set of rules that are applied to a column or combination of columns. It help in preventing data corruption. It help in preventing human errors while entering data into the database. Database constraints help to achieve data integrity without the need to write an external program.

114

Types of Constraints
Different Types of Constraints are as below Primary key Foreign key Check Unique Not null

Constraints can be created at the time of creating the table using CREATE TABLE or after creating the table using ALTER TABLE.
115

Primary Key Constraint


Primary key Constraints avoid the duplication of data. A table can have only one primary key constraint but can have several UNIQUE constraints. Example- Create the EMP_INFO table by adding a primary key constraint PK_EMPNO on the column EMPNO.
CREATE TABLE EMP_INFO (EMPNO NUMBER(4) CONSTRAINT PK_EMPNO PRIMARY KEY, ENAME VARCHAR2(20),DEPTNO NUMBER (2))

The above example creates the EMP_INFO table by adding a primary key constraint PK_EMPNO on the column EMPNO.
116

Primary Key Constraint (Contd)


Example- Create and modify the EMP_INFO1 table by adding a primary key constraint PK_EMPNO1 on the column EMPNO. Example CREATE TABLE EMP_INFO1 (EMPNO
The example creates and
modifies the EMP_INFO1 table by adding a primary key constraint PK_EMPNO1 on the column EMPNO. It also modifies the DEPT_INFO table with the primary key PK_DEPTNO on the deptno column.

NUMBER(4), ENAME VARCHAR2(20), DEPTNO NUMBER (2)) ALTER TABLE EMP_INFO1 ADD CONSTRAINT PK_EMPNO1 PRIMARY KEY (EmpNo); ALTER TABLE DEPT_INFO ADD CONSTRAINT PK_DEPTNO PRIMARY KEY (DEPTNO);
117

Foreign Key Constraint


When a column in one table refers to the Primary Key of another table, that column is referred as Foreign Key. It is also known as Referential Integrity and implemented through Foreign Key constraint. Example- create a table EMP_INFO2 to add a foreign key constraint: The example adds a CREATE TABLE EMP_INFO2 foreign key constraint (EMPNO NUMBER(4) FK_DEPTNO1 to the CONSTRAINT PK_EMPNO2 EMP_INFO2 table on the PRIMARY KEY, ENAME deptno column which VARCHAR2(20) , DEPTNO references DEPTNO -the CONSTRAINT FK_DEPTNO1 primary key of the REFERENCES DEPT_INFO DEPT_INFO Table. (DEPTNO))
118

Foreign Key Constraint (Contd)


Example- Modify the EMP_INFO1 table by adding a foreign key constraint FK_DEPTNO1 on the column DEPTNO:
Example :
ALTER TABLE EMP_INFO1 ADD CONSTRAINT FK_DEPTNO2 FOREIGN KEY(DEPTNO) REFERENCES DEPT_INFO(DEPTNO);

The above example adds a foreign key constraint FK_DEPTNO2 to the EMP_INFO1 table on the DEPTNO column which references the primary key of the DEPT_INFO Table.
119

Unique Constraint
Unique Key Constraints are used to enforce the uniqueness of a column. If a column has a Unique Constraint on it, duplicate values cannot be inserted on that column. Unique key allows Null value to be stored. Example: Create a table emp_sal to add the unique constraint. The example adds a CREATE TABLE EMP_SAL unique key (EMPNO NUMBER(4), constraint UK_ACTNO ENAME VARCHAR2(20), on the ACTNO ACTNO NUMBER (8) columns of the CONSTRAINT EMP_SAL table. UK_ACTNO UNIQUE, JOB VARCHAR2(20), HIREDATE DATE)
120

Unique Constraint
Example: Modify the table emp_sal to add the unique constraint.

ALTER TABLE EMP_SAL ADD CONSTRAINT UK_JOB_HDATE UNIQUE (job, hiredate);

The above example adds a unique key constraint UK_JOB_HDATE on the job and hiredate columns of the EMP_SAL table.

121

CHECK constraint
Check Constraints are the most elementary form of integrity constraints. Check constraint is also known as Domain Integrity. This powerful feature allows to establish range for data. Check Constraints are created by specifying a domain (Range) for a column. If we specify a domain constraint for a particular column, that column will contain only values specified in that domain.

122

Example Check Constraint


Example: Create a table emp_sal1 to add the unique constraint.
CREATE TABLE EMP_SAL1 (EMPNO NUMBER(4), ENAME VARCHAR2(20), ACTNO NUMBER(8),JOB VARCHAR2(20) CONSTRAINT CHK_JOB CHECK (job IN ('ANALYST','CLERK')), HIREDATE DATE)

The above example adds a check constraint CHK_JOB on the JOB column of the EMP_SAL1 table. ALTER TABLE EMP_SAL add CONSTRAINT CHK_JOB CHECK (job IN ('ANALYST', 'CLERK'))

The example adds a check constraint CHK_JOB on the job column of the EMP_SAL table.
123

NOT NULL Constraint


A NOT NULL constraint specifies that a column must not assume the NULL value. A NOT NULL constraint is always written as a column constraint.
Example : CREATE TABLE PRODUCTS (PRODUCT_NO NUMBER(2) CONSTRAINT NN_PROFNO NOT NULL, NAME VARCHAR2(40) CONSTRAINT NN_PROD_NAME NOT NULL)

The above example creates a table called products with two columns product_no and name. Both have a constraint NOT NULL.
124

Removing Constraints
A constraint can be removed as shown below Example : ALTER TABLE EMP_INFO DROP CONSTRAINT PK_EMPNO The above example removes a constraint named PK_EMPNO from EMP_INFO table.

125

Default
The default keyword is assigned to a column of a table at the time of creating a table or altering it. The value in the default keyword is applied when no value is inserted in the table using the INSERT statement. Example: Create a table products1 to create default

CREATE TABLE PRODUCTS1 (PRODUCT_NO NUMBER(2) NOT NULL, NAME VARCHAR2(40) NOT NULL, STATE VARCHAR2(2) DEFAULT 'MH') INSERT INTO PRODUCTS1 (PRODUCT_NO,NAME) VALUES(1, 'PENS')
The above example adds a default value of MH into the state field as no explicit value is provided while inserting.
126

7: Constraints

Activity
Input documents: Activity_4.0_Module7_Constr aints.doc Output : Activity_4.0_Module7_Constr aintsOutput.doc

45 Minutes
127

Key Points Constraint is a rule or set of rules that apply to a column or combination of columns Constraints ensure data integrity Important constraints are Primary Key Foreign Key CHECK UNIQUE NOT NULL
128

Questions/ Comments
What questions or comments do you have?

Introduction to RDBMS and SQL


Module 10: Views

Course Code #IDCGF002

Learning Objectives
Understand the concept of Views Understand why views are required in a database Understand how to create and work with the views

131

Agenda
Introduction to Views Creating Views Complex Views Read Only view View with Check Option

132

Introduction to Views
Views are virtual tables (tables which dont occupy memory) whose contents are taken or derived from other tables. Just like other tables, a view consists of rows with columns, and you can retrieve data from a view. Views can be used to join two tables in database and present the data as if the data were coming from a single table Views can be used as a security mechanism to restrict the data available to end users Views can also be used to aggregate data.
133

Example of a View
EmpNo 101 102 103 104 105 Ename Jack James Steve Peter Krik Empsal 10000 20000 30000 40000 50000 Empaddr jonvis kali Havens Lawrence cham Empcity London Paris Newyork Paris London

This example shows creating a view called EmpView from the EMP table. This view contains two columns EmpNo and Empname which are taken from EMP table. The actual data will be in the EMP table, which can be manipulated from the EmpView.
EmpNo 101 102 103 Empname jack james steve

134

Creating a VIEW
Syntax: CREATE VIEW viewname AS <select_statement> Example to create a view from Supplier table CREATE VIEW v_supp AS SELECT Supp_no,Supp_name,supp_city FROM Supplier; Example to create a view from Supplier table for City in MUM CREATE VIEW v_supp_City AS SELECT supp_no, supp_name, supp_city FROM Supplier S WHERE City = MUM;
135

Simple view
Simple View is also known as normal view. It contains data only from one table. It wont contain JOINS or GROUP By statements. DML operations can be performed on this view.

Example CREATE VIEW v_supp AS SELECT supp_no, supp_name FROM Supplier;

The example creates a simple view v_supp which actually selects supp_no and supp_name from Supplier Table.
136

Complex View
It will be accessed from multiple tables Normally a complex view will contain joins, sub queries and aggregate functions. It contains group functions to display values from two tables. DML operations cannot be performed through this view.

Example CREATE VIEW v_dept_emp(dname, job, maxSal, minSal, avgSal) AS SELECT dname, job, max(sal), min(sal), avg(sal)FROM dept d, emp e WHERE d.deptno = e.deptno GROUP BY dname, job;

The example creates a complex view v_dept_emp which selects the dname column, maximum sal, minimum sal and average sal. It uses three Aggregate functions and it joins two tables Dept and Emp. The column names of the view are mentioned in the create view statement.
137

Read Only View


With read only clause with the view definition does not allow any DML operation on that view. The view is read only (I.e the view can only be queried) Example

CREATE VIEW v_emp_job AS SELECT empno,ename,job FROM emp With Read only;
The example creates a simple view using the with read only clause which selects empno, ename and job from Emp Table . No DML operations can be carried through this view.
138

View With Check option


With Check option is an optional clause in the create view statements. It specifies the level of checking to be done while inserting or modifying data through view. If the view in read-only view then Check option cannot be used. It allows integrity constraints and data validation checks to be enforced on data before Insert or Update. CREATE VIEW v_empdept as select empno, ename, deptno from emp where deptno =30 With check option; The example uses With Check Option clause, which will restrict the insertion of a row that cannot be selected by the view. The user cannot insert a value other than 30 through this view.
139

Activity
Input documents:
Activity_7.0_Module10_Views.doc

Output documents:
Activity_7.0_Module10_ViewsOutp ut.doc

45 Minutes
140

Key Points
Views are virtual tables. Views provide security of data. Views conceal complexity of queries which contain complicated queries or sub queries. Normally a complex view will contain joins, subqueries and aggregate functions.

141

Questions/ Comments
What questions or comments do you have?

Comparative Terms

Terms Relation schema Relation Tuple Attribute Domain Notation Employee (emp_id,emp_name,sal) Student (studno,name,class) Department (Dept_no,Dept_name)

Oracle Table description Table Row Column Value set

143

Keys
Candidate Key. Primary Key. Super key. Foreign Key. Alternate Key.

144

Key definitions
Candidate Key: There may be a number of attributes or a combination of attributes which uniquely identify each instance of an entity. These are known as candidate keys. Primary Key: An attribute whose value can uniquely identify a complete record (one row of data) within an entity. Super key: If additional attributes are added to the primary key, the resulting combination would still uniquely identify an instance of the entity set. These augmented keys are called super keys. Composite Primary Key: A primary key that consists of two or more attribute within an entity.

145

Key definitions (continued)


Foreign Key: A copy of a primary key that exists in another entity (may exist in the same entity too) for the purpose of forming a relationship between the entities involved. (Discussed in detail later.) Alternate Key: The primary key is selected from the set of candidate keys. The remaining candidate keys are the alternate keys.

146

Foreign key
a set of attributes in a relation that exactly matches a (primary) key in another relation

the names of the attributes dont have to be the same but must be of the same domain a foreign key in a relation A matching a primary key in a relation B represents a
many: one relationship between A and B Employee (Emp_id, Emp_name, Sal, Dept_id) Department (Dept_id, Dept_name)

147

Referential integrity
Cascade: delete all matching foreign key tuples. Deletes those tuples matching in employee table. Restrict: cant delete primary key tuple Department whilst a foreign key tuple Employee matches Nullify: foreign key Employee.Dept_id set to null if the primary key Dept_ids are deleted.

148

Entity integrity and nulls


No part of a key can be null Attribute values Atomic Known domain Sometimes can be null

THREE categories of null values Not applicable Not known

Absent (not recorded)

149

Attribute constraints
This constraint is basically the declaration that a specified attribute is of specified type, the maximum value, minimum value, length of the field associated with them. Example: Emp Relation { Emp_no Integer(5) Emp_name Char(20) }

150

Database constraints (Oracle)


Not Null. Check. Primary Key. Foreign Key. Unique.

151

Relational Operators
Restrict orSelect: Only specific tuples from a relation satisfying a specific condition is returned. Project: All tuples in a specified relation and only specified attributes are returned. Product: It returns a relation containing all possible tuples that are a combination of two tuples, one from each of two specified relations. Union: All tuples appearing in either or both of two specified relations are returned. Intersect: All tuples appearing in both of two specified relations are returned. Difference: All tuples appearing in the first and not the second of two specified relations are returned. Join: All tuples that are a combination of two tuples, one from each of two specified relations, such that the two tuples contributing to any given combination have a common value for the common attributes of the two relations.

152

Select
To retrieve specific tuples/rows from a relation

Ord# 101
104

OrdDate 02-08-94
18-09-94

Cust# 002
002

153

Core Java | IBM Internal Use | Day 1 | 16-Jun-09

Project
To retrieve specific attributes/columns from a relation.

Descr Power Supply 101-Keyboard Mouse MS-DOS 6.0 MS-Word 6.0

Price 4000 2000 800 5000 8000

154

Core Java | IBM Internal Use | Day 1 | 16-Jun-09

Join
Example: ORD_AUG join CUSTOMERS (here, the common column is Cust#)

Ord# 101 102 103 104 105

OrdDate 02-08-94 11-08-94 21-08-94 28-08-94 30-08-94

Cust # 002 003 003 002 005

CustNam es Srinivasan Gupta Gupta Srinivasan Apte

City Madras Delhi Delhi Madras Bomba y

155

Core Java | IBM Internal Use | Day 1 | 16-Jun-09

Union
To retrieve tuples appearing in either or both the relations participating in the UNION.

Emp_no 1 3 4 6 7 10

E_name Jones Smith Lalonde Byron Evan Drew

12

Smith

AUB
156 Core Java | IBM Internal Use | Day 1 | 16-Jun-09

Intersect
To retrieve tuples present in both the relations participating in the intersect.

INTERSECT

Emp_no 3 4 10

E_name Smith Lalonde Drew

B
Core Java | IBM Internal Use | Day 1 | 16-Jun-09

157

Difference
To retrieve tuples appearing in the first relation participating in the DIFFERENCE but not the second.

Emp_no
1 7 12

E_name
Jones Evan Smith

DIFFERENCE

A-B
158 Core Java | IBM Internal Use | Day 1 | 16-Jun-09

Exercise
The remaining candidate keys left out after the selection of the primary key are called ______. The operator used to retrieve specific attributes from a relation is called the _________ operator. The constraint which disallows the occurrence of duplicate values for an attribute is known as the ____________ constraint. The primary key foreign key relationship sets ____________ integrity among two relations. When a primary key is formed from multiple attributes in a relation, it is called a _____________ key.

159

DAY 1

RDBMS Concepts And ANSI SQL (Using DB2)

Course Code #IDCGF002

Objective
Overview of Database Management and Architecture Slide No:3 Introduction to Relational databases Slide No:13 Entity Relationship Modeling Slide No:32 Normalization Slide No: 56 Exercises Slide No:79

161

Overview of Database Management and Architecture


What is Database? What is Database Management System Why Database? Different Database Models

162

What is Database?
A database is an organized collection of related data.
Data is defined as known facts or objects that have meaning in the user environment The term organized means that the data are structured so as to be easily stored, manipulated, and retrieved by users. The term related means that the data describe a domain of interest to a group of users and that those users can use the data to answer questions concerning the domain.

163

What is Database Management System?


A database management system is a general-purpose commercial software system used to define, create, maintain, and provide controlled access to the database. Ex. Of DBMS are DB2, IMS, ORACLE, INGRESS, SQL Server..

164

Why Database & Database Management System


File processing systems
File processing systems were developed in the early computer era so that computers could store, manipulate, and retrieve large files of data.

Limitations of FPS
Heavy dependency between Program and Data Data duplication Limited data sharing Lengthy development times No standards

165

Why Database & Database Management System Contd


Advantages of Database Approach
Program-Data Independence Minimal Data Redundancy Improved Data Consistency Improved Data Sharing Increased Productivity Enforcement of standards Improved Data Quality Improved Data Accessibility

166

Database Models
Models generally allow people to conceptualize an abstract idea more easily
Model airplanes ,Model homes

A database model is a way of explaining the logical layout of the data and the relationship of various parts to each other and the whole.
Network Model Hierarchical Model Relational Model

167

Hierarchical Database Models


Data in this type of database is structured hierarchically and is typically diagrammed as an inverted tree. A single table in the database acts as the Root and the relationships between the nodes are represented by parent/child. A parent can have many children and a child can have only one parent.

168

Hierarchical Database Models Contd


Let us consider departments, employees database. The structure would look like this.
COMPANY

Note:
1. Without department employee details cannot be entered. 2. Difficult to establish M-M relationship between TASK and EMPLOYEE
PROJECT

DEPARTMENT

MANAGER

EMPLOYEE

TASK
169

Hierarchical Database Models Contd..


Advantage: Data retrieval can be done very quickly and referential integrity is built in and automatically enforced. Disadvantage: A child record cannot be entered without a corresponding parent record. It cant support complex relationships (m-m). The most popular HDBMS was IBMs Information Management System (IMS), first introduced in 1968 for mainframes. It is still the choice of bank applications to support their high volume of transactions.

170

Network Database Model


Designed to eliminate the limitations in HDBMS. The structure of a network database is represented in terms of nodes and set structures. The implementation is done using pointers. Possible to design optimized databases with network systems. A node represents a collection of records, and a set structure establishes and represents a relationship in a network database.

171

Network Database Model Contd Network Database Model

Contd
COMPANY

DEPARTMENT EMPLOYEE TYPE MANAGER

EMPLOYEE PROJECT

ASSIGNMENT TASK
172

Network Database Model Contd Network Database Model


COMPANY

Contd
Note: 1. Managers can be part of both department and company 2. Assignment table allows multiple parents. 3. One disadvantage of HDBMS was multiple parents. That is answered. 4. So, Relationship between employee and task table is M-M

DEPARTMENT EMPLOYEE TYPE

MANAGER

EMPLOYEE PROJECT

ASSIGNMENT TASK

173

Network Database Model Contd


Advantage: It allows to create more complex queries than those created using HDBMS. In 1971 CODASYL published a standard for NDBMS. Disadvantage: The database structure and the program to create the applications are very dependent on each other. The popular NDBMSs are Cullinets IDMS, Adabs D BMS.

174

QUESTIONS?

175

Introduction to Relational databases


Relational Database Model Keys Relational Operators Constraints

176

Introduction to Relational databases


The dominant database model is the relational database model all current major DBMS products are based on it Created by IBM engineer E. F. Codd in 1970 It was based on Relational theory in mathematics

177

Introduction to Relational databases -Terms


Each relation is thought of as a table. Each row in the table represents a collection of related data values. Each row is called a tuple. A column header is called an attribute. The data type describing the types of values that can appear in each column is represented by domain. A domain is a set of atomic values from which the corresponding attribute draws the data values.

178

Exercises:
CASE STUDY I Prepare an E-R diagram for a real estate firm that lists property for sale. Also prepare a definition for each entity type, attribute, and relationship on your diagram. The following describe the organization. The firm has a number of sales offices in several states. Attributes of sales office include Office_number (identifier) and location. Each sales office is assigned one or more employees. Attributes of employee include Employee_ID(identifier), and Employee_Name. An employee must be assigned to only one sales office. For each sales office, there is always one employee assigned to manage that office. An employee may manage only the sales office to which she is assigned. The firm lists property for sale. Attributes of property include Property_ID (identifier) and Location, Components of Location include Address, City, State and Zip_code. Each unit of property must be listed with one (and only one) of the sales offices. A sales office may have any number of properties listed, or may have no properties listed. Each unit of property has one or more owners. Attributes of owners are Owner_ID (identifier) and Owner_Name. An owner may own one or more property. An attribute of the relationship between property and owner is percent_Owned.

179

Exercises
CASE STUDY II Draw an E-R diagram for the following situation. Projects Inc is an engineering firm with approximately 500 employees. A database is required to keep track of all employees, their skills, projects assigned, and departments worked in. Every employee has a unique number assigned by the firm, required to store his or her name and date of birth. If an employee is currently married to another employee of the same company the date of marriage and who is married to whom must be stored; however, no record of marriage is required if an employees spouse is not an employee of the company. Each employee is given a job title(for ex Engineer, Secretary..). An employee does only one type of job at any given time, and we only need to retain information for an employees current job. There are 11 different departments, each with a unique name. An employee can report to only one department. Each department has a phone number. To procure various kinds of equipments, each department deals with many vendors. A vendor typically supplies equipment to many departments. We are required to store the name and address of each vendor and the date of the last meeting between department and vendor. Many employees can work on project. An employee can work on many projects but can only be assigned to at most one project in a given city. For each city we are interested in its state and population. An employee can have many skills(preparing requisition, checking drawings) . But she or he may use only a given set of skills on a particular project. Employees use each skill that they possess in at least one project. Each skill is assigned a number, and we must store a short description of each skill. Projects are distinguished by project numbers, and we must store the estimated cost of each project.

180

Exercises
CASE STUDY III Draw E-R diagram for the following situation. Stillwater buys and sells antiques of all kinds(Furniture, Jewelry, clothing..) Each item is uniquely identified by an item number and is also characterized by a description asking price, condition, and open-ended comments. Stillwater works with many different individuals, called clients, who sell items to and buy items from the store. Some clients only sell items to Stillwater, some only buy items, and some others both sell and buy. A client is identified by a client number and is also described by a client name and client address. When Stillwater sells an item in stock to a client, the owners want to record the commission paid, the actual selling price, sales tax and date sold. When Stillwater buys and item from a client, the owners want to record the purchase cost, date purchased, and condition at time of purchase.

181

Exercises
CASE STUDY IV Draw an E-R diagram for the following situation. The HT School of Business operates international business programs in 10 locations throughout Europe. The School had its first class of 9000 graduates in 1965. The School keeps track of each graduates student number, name , country of birth, current country of citizenship, current address, and the name of each major the student completed(each student has one or two majors). In order to maintain strong ties to its alumni, the School holds various events around the world. Events have a title, date, location and type(Reception, dinner, seminar) . The school needs to keep track of which graduates have attended which events. For an attendance by a graduate at an event, a comment is recorded about information school officials learned from that graduate at that event. The school also keeps in contact with graduates by mail, e-mail, telephone and fax. As with events, the school records information learned from the graduate from each of these contacts. When a school official know that he or she will be meeting or talking to a graduate, a report is produced showing the latest information about the graduate and the information learned during the past two years from that graduate from all contacts and events the graduate attended.

182

Exercises
1. Consider the relation which has attributes that hold schedules of courses and sections at an university; R = { CourseNo, SecNo, OfferingDept, Credit-Hours, CouseLevel, InstructorNo, Semester, Year, Days_Hours, RoomNo, NoofStudents} Suppose the following functional dependencies hold on R {CourseNo} {OfferingDept, CreditHours, CourseLevel} {CourseNo, SecNo, Semester, Year} {Days_Hours, RoomNo, NoOfStudents, InstructorNo} {RoomNo, Days_Hours, Semester, Year} {InstructorNo, CourseNo, SecNo} How would you normalize this relation?

183

Exercises
2. Consider the following relations for an order-processing application database at ABC, Inc. ORDER(O#, Odate, Cust#, Total_amnount) ORDER-ITEM(O#, I#, Qty_ordered, Total_price, Discount%) Assume that each item has a different discount. The TOTAL_PRICE refers tone item, ODATE is the date on which the order was placed, and the TOTAL_AMOUNT, is the amount of the order. If we apply a natural join on the relations ORDER-ITEM and ORDER in this database, what does the resulting relation schema look like? What will be its key? Show the FDs in this resulting relation, Is it in 2NF? Is it in 3NF? Why or why not? (State assumptions, if you make any.)

184

Exercises
1. Consider the following relation: CAR_SALE(Car#, Date_sold, Salesman#, Commissin%, Discount_amt) Assume that a car may be sold by multiple salesmen, and hence {CAR#,SALESMAN#} is the primary key. Additional dependencies are Date_sold-> Discount_amt and Salesman#-> Commission% Based on the given primary key, is this relation in 1NF, 2NF, OR 3NF? Why or why not? How would you successively normalize it completely?

185

Exercises:
CASE STUDY I Prepare an E-R diagram for a real estate firm that lists property for sale. Also prepare a definition for each entity type, attribute, and relationship on your diagram. The following describe the organization. The firm has a number of sales offices in several states. Attributes of sales office include Office_number (identifier) and location. Each sales office is assigned one or more employees. Attributes of employee include Employee_ID(identifier), and Employee_Name. An employee must be assigned to only one sales office. For each sales office, there is always one employee assigned to manage that office. An employee may manage only the sales office to which she is assigned. The firm lists property for sale. Attributes of property include Property_ID (identifier) and Location, Components of Location include Address, City, State and Zip_code. Each unit of property must be listed with one (and only one) of the sales offices. A sales office may have any number of properties listed, or may have no properties listed. Each unit of property has one or more owners. Attributes of owners are Owner_ID (identifier) and Owner_Name. An owner may own one or more property. An attribute of the relationship between property and owner is percent_Owned.

186

Exercises
CASE STUDY II Draw an E-R diagram for the following situation. Projects Inc is an engineering firm with approximately 500 employees. A database is required to keep track of all employees, their skills, projects assigned, and departments worked in. Every employee has a unique number assigned by the firm, required to store his or her name and date of birth. If an employee is currently married to another employee of the same company the date of marriage and who is married to whom must be stored; however, no record of marriage is required if an employees spouse is not an employee of the company. Each employee is given a job title(for ex Engineer, Secretary..). An employee does only one type of job at any given time, and we only need to retain information for an employees current job. There are 11 different departments, each with a unique name. An employee can report to only one department. Each department has a phone number. To procure various kinds of equipments, each department deals with many vendors. A vendor typically supplies equipment to many departments. We are required to store the name and address of each vendor and the date of the last meeting between department and vendor. Many employees can work on project. An employee can work on many projects but can only be assigned to at most one project in a given city. For each city we are interested in its state and population. An employee can have many skills(preparing requisition, checking drawings) . But she or he may use only a given set of skills on a particular project. Employees use each skill that they possess in at least one project. Each skill is assigned a number, and we must store a short description of each skill. Projects are distinguished by project numbers, and we must store the estimated cost of each project.

187

Exercises
CASE STUDY III Draw E-R diagram for the following situation. Stillwater buys and sells antiques of all kinds(Furniture, Jewelry, clothing..) Each item is uniquely identified by an item number and is also characterized by a description asking price, condition, and open-ended comments. Stillwater works with many different individuals, called clients, who sell items to and buy items from the store. Some clients only sell items to Stillwater, some only buy items, and some others both sell and buy. A client is identified by a client number and is also described by a client name and client address. When Stillwater sells an item in stock to a client, the owners want to record the commission paid, the actual selling price, sales tax and date sold. When Stillwater buys and item from a client, the owners want to record the purchase cost, date purchased, and condition at time of purchase.

188

Exercises
CASE STUDY IV Draw an E-R diagram for the following situation. The HT School of Business operates international business programs in 10 locations throughout Europe. The School had its first class of 9000 graduates in 1965. The School keeps track of each graduates student number, name , country of birth, current country of citizenship, current address, and the name of each major the student completed(each student has one or two majors). In order to maintain strong ties to its alumni, the School holds various events around the world. Events have a title, date, location and type(Reception, dinner, seminar) . The school needs to keep track of which graduates have attended which events. For an attendance by a graduate at an event, a comment is recorded about information school officials learned from that graduate at that event. The school also keeps in contact with graduates by mail, e-mail, telephone and fax. As with events, the school records information learned from the graduate from each of these contacts. When a school official know that he or she will be meeting or talking to a graduate, a report is produced showing the latest information about the graduate and the information learned during the past two years from that graduate from all contacts and events the graduate attended.

189

Exercises
1. Consider the relation which has attributes that hold schedules of courses and sections at an university; R = { CourseNo, SecNo, OfferingDept, Credit-Hours, CouseLevel, InstructorNo, Semester, Year, Days_Hours, RoomNo, NoofStudents} Suppose the following functional dependencies hold on R {CourseNo} {OfferingDept, CreditHours, CourseLevel} {CourseNo, SecNo, Semester, Year} {Days_Hours, RoomNo, NoOfStudents, InstructorNo} {RoomNo, Days_Hours, Semester, Year} {InstructorNo, CourseNo, SecNo} How would you normalize this relation?

190

Exercises
2. Consider the following relations for an order-processing application database at ABC, Inc. ORDER(O#, Odate, Cust#, Total_amnount) ORDER-ITEM(O#, I#, Qty_ordered, Total_price, Discount%) Assume that each item has a different discount. The TOTAL_PRICE refers tone item, ODATE is the date on which the order was placed, and the TOTAL_AMOUNT, is the amount of the order. If we apply a natural join on the relations ORDER-ITEM and ORDER in this database, what does the resulting relation schema look like? What will be its key? Show the FDs in this resulting relation, Is it in 2NF? Is it in 3NF? Why or why not? (State assumptions, if you make any.)

191

Exercises
1. Consider the following relation: CAR_SALE(Car#, Date_sold, Salesman#, Commissin%, Discount_amt) Assume that a car may be sold by multiple salesmen, and hence {CAR#,SALESMAN#} is the primary key. Additional dependencies are Date_sold-> Discount_amt and Salesman#-> Commission% Based on the given primary key, is this relation in 1NF, 2NF, OR 3NF? Why or why not? How would you successively normalize it completely?

192

También podría gustarte