Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
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.
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.
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
12
13
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
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
18
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
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
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
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
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.
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
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
item _no
item_desc
price
manufacturer _id
manufacturer _name
distributor _id
distributo r_name Marine World Marine World Water World Water World Alpha Company
date_entered
Qty
1 1 2 2 3 3
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
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
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
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
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
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:
37
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
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
45
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.
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
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
item _no
item_desc
price
manufacturer _id
manufacturer _name
distributor _id
distributo r_name Marine World Marine World Water World Water World Alpha Company
date_entered
Qty
1 1 2 2 3 3
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
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
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
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
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
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:
63
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
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
71
Understanding RDBMS
Keith T. Weber GIS Director ISU-GIS Training and Research Center
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
Integrity! Structure
Independence
Physical Logical
Integrity
Important for consistency and transaction management. Types:
Domain Redundancy Constraint Entity Referential
Cascading or non-cascading
Key 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)
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
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.
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
Cost_Mean
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?
Populate...
Questions and concerns to revisit
Null data Reporting discrepancies and variations Measuring or estimating methods Client utility/efficiency
Questions?
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?
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
The above example creates the EMP_INFO table by adding a primary key constraint PK_EMPNO on the column EMPNO.
116
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
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.
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
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
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?
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.
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
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
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)
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
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
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
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
Project
To retrieve specific attributes/columns from a relation.
154
Join
Example: ORD_AUG join CUSTOMERS (here, the common column is Cust#)
155
Union
To retrieve tuples appearing in either or both the relations participating in the UNION.
Emp_no 1 3 4 6 7 10
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
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
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
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
164
Limitations of FPS
Heavy dependency between Program and Data Data duplication Limited data sharing Lengthy development times No standards
165
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
168
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
170
171
Contd
COMPANY
EMPLOYEE PROJECT
ASSIGNMENT TASK
172
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
MANAGER
EMPLOYEE PROJECT
ASSIGNMENT TASK
173
174
QUESTIONS?
175
176
177
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