Relationship Models: Entity - Relationship (E-R) Model
Relationship Models: Entity - Relationship (E-R) Model
Chapter 2
RELATIONSHIP MODELS
Entity Relationship (E-R) Model
The entity_relationship (E-R) model is based on a perception of a real world which consist of a set of basic objects called entities and relationships among these objects. The E-R model for data uses 3 features to describe the data: ENTITIES : which specify distinct real world items in an application. RELATIONSHIPS: which connect entities. ATTRIBUTES: which specify properties of entities and relationships.
A relationship set is a set of relationships of the same type. Formally, it is a mathematical relation on n 2 entity sets. If E1, E2, ,En are entity sets, then a relarionship set R is a subset of
V.Code
V.Addr
VENDOR
V.Code
Qty_Supply
Item_Code
SUPPLY
Date_of_Supply
Item_Code
ITEM
Item_Name
_________________________________________________________________________________
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University
Mapping Constraints
An E-R scheme may define certain constraints to which the contents of a database must conform. One important constraint is mapping cardinalities, which express the number of entities to which another entity can be associated via a relationship set. For a binary relationship set R between entity sets A and B, the mapping may be: One-to-one (1:1) : an entity in A is associated with the most one entity in B, and an entity in B is associated with at most one entity in A. One-to-many (1:M) : an entity in A is associated with any number of entities in B, and an entity in B, however, can be associated with at most one entity in A. Many-to-one (M:1) : an entity in A is associated with at most one entity in B, and an entity in B, however, can be associated with any number of entities in A. Many-to-many (M:M) : an entity in A is associated with any number of entities in B, and an entity in B, however, can be associated with any number of entities in A.
1: 1 Department 1: M Manager
M: M
Students
Keys
It is important to be able to specify how entities and relationships are distinguished. Conceptually, individual entities and relationships are distinct, but from a database perspective the difference among them must be expressed in terms of their attributes. The concept of a superkey allows us to make such distinction. A superkey is a set of one or more attributes which, taken collectively, allow us to identify uniquely an entity in the entity set. For example, the social_security attribute of the entity set customer is sufficient to distiguish one customer entity from another. Thus, the social_security is a superkey for the entity in the entity set customer. The customer_name attribute of the customer is not a superkey, as several people might have the same name. However, the combination of the social_security and the attribute customer_name is a superkey.
_________________________________________________________________________________
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University
However, if K is a superkey, then so is any superset of K. We often interested in superkeys for which no proper subset is a superkey. Such a minimal superkeys are called candidate keys. It is possible that several distinct sets of attributes could serve as a candidate key. For examples, both {social_security} and {customer_name, street} are candidate keys. We shall use the term primary key to denote a candidate key that is chosen by the database designer as the principal means of identifying entities within an entity set. It is possible that an entity set does not have sufficient attributes to form a primary key. Such an entity set is term a weak entity set. An entity set which has a primary key is term a strong entity set. In order for a weak entity set to be meaningful, it must be part of a one-to-many relationship set. The attributes of a relationship set R involving entity sets E1, E2,, En is the union of all the primary keys of these entity sets besides the descriptive attributes {a1,a2,,am} of the relationship set R it self. That is ; Primary_key(E1) Primary_key(E2) Primary_key(E1 ) {a1,a2,,am} For example, consider the relationship set CustAcct defined before, which involves the following two entity sets : customer : with primary key social_security, account : with the primary key account_number. Since the relationship set has the attribute date, the set attribute(CustAcct) consists of the three attributes social_security, account_number, and date.
street city
Social_security
name customer
CustAcct
account
Entity-Relationship Diagram
Consider the above diagram which consists of two entity sets, customer and account, related through a binary relationship set CustAcct. The attributes associated with customer are name, social_security, city, and street. The attributes associated with account are account_number, and balance. The relationship set CustAcct may be
_________________________________________________________________________________
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University
many-to-many, one-to-many, many-to-one, or one-to-one. To distinguish among these, a directed arc line () or an undirected line ( __ ) between the relationship set and the entity set is used. A directed line from the relationship to an entity set specifies that the entity set participates in either a one-to-one or a many-to-one relationship with the other entity sets. It can not participate in either a many-to-many or one-to-many relationship. An undirected line from the relationship set to an entity set specifies that the entity set participates in either a many-to-many or a many-to-one relationship with the other entity sets. If the relationship set CustAcct were one-to-many, from customer to account, then the line from CustAcct to customer would be directed with an arrow pointing to the customer entity set (Fig A). Similarly, if the relationship set CustAcct were many-toone from customer to account, then the line from CustAcct to account would have an arrow pointing to the account entity set (Fig B). The one-to-one relationship is shown in Fig C.
street city Account_number balance date
Social_security
name customer
CustAcct
account
street city
Social_security
name customer
CustAcct
account
street city
Social_security
name customer
CustAcct
account
_________________________________________________________________________________
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University
A database which conforms to an E-R diagram can be represented by a collection of tables. For each entity set and for each relationship set in the database, there is a unique table which is assigned the name of the corresponding entity set, or relationship set. Each table has a number of columns which , again, have unique names.
customer
name Salem Omer Ali Abdulla Ali Social_security 52-382 89-123 52-394 11-222 12-121 Street Main North Main North South City Abian Aden Aden Sanaa Sanaa
account
Account_number 259 630 401 199 210 118 117 183 Balance 1,000 2,000 1,200 2,200 22,500 62,000 3,000 100,000
CustAcct
Social_security 52-382 89-123 52-394 11-222 12-121 89-123 Account_number 259 630 401 199 118 177 Date 17 June 1994 17 May 1994 23 May 1994 19 June 1994 19 June 1994 26April1996
The function that an entity plays in a relationship is called its role. Roles are normally implicit and are not usually specified. However, they are useful when the meaning of a relationship needs clarification. Such is the case when the entity sets of a relationship set are not distinct. For example, the relationship set works-for might be modeled by ordered pairs of employee entities.
phone
employee-name
manager
employee Works-for ________________________ _________________________________________________________
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University
worker
DATABASE
Network Model
MODELS
A network database consists of a collection of records which are connected to one another through links. A record is in many respects similar to an entity in the E-R model. Each record is a collection of fields (attributes), each of which contains only one data value. A link is an association between precisely two records. Thus a link can be viewed as a binary form of relationship in the sense of the E-R model.
DATA-STRUCTURE DIAGRAMS
A data-structure diagram is a scheme representing the design of a network database. Such a diagram consists of two basic components: Boxes , which correspond to record types Lines , which correspond to links.
A data-structure diagram serves the same purpose as an E-R diagram; namely, it specifies the overall logical structure of the database. The figure shown below, illustrate an example of the E-R diagram and its corresponding data-structure diagram. The record type customer corresponds to the entity set customer. It include the fields name, street, and city . Similarly, account is the record type corresponding to the entity set account. It includes the two fields number and balance. Finally the relationship CustAcct has been replaced with the link CustAcct.
street city number balance
customer
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University
account CustAcct
The relationship CustAcct is many-to-many. If the relationship were one-tomany from customer to account, then the link would have an arrow pointing to customer record type. Similarly, if the relationship CustAcct were one-to-one, then the link would have two arrows, one pointing to account record type and one pointed to customer record type. A database corresponding to the above scheme may thus contain a number of customer records linked to a number of account records as illustrated in the figure shown below which is corresponding to the many-to-many relationship.
customer
account Main Aden 259 630 177 1000 2000 3000 1200
Ali Omer
Abdulla
North
Aden
North
Sanaa 401
Omer
North
Aden
630
2000
Abdulla
North
Sanaa
177
3000
If the relationship includes descriptive attributes, the tranformation from the E_R daigram to a data_structure diagram a new record type needs to be created and links need to be established as described below. Consider the E-R diagram shown below. To transform this diagram to a data-structure diagram we need to: 1. Replace entities customer and account with record types customer and account, respectively. 2. Create the following many-to-one links: CustDate from the date record type to the customer record type. AccDate from the date record type to the account record type.
_________________________________________________________________________________
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University
account
AccDate
Record Templates : a record for each record type . Currency Pointers : a set of pointers to various database records most recently accessed by the application. Currency pointers are of the following types: 1. Current of record type
_________________________________________________________________________________
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University
Status Flags : a set of variables used by the system, (e.g. DB_status flag which set to 0 if the most recent operation succeeded, otherwise, it is set to an error-code).
customer Omer North account 119 branch Arwa 10000 customer account branch CustAcc BrnAcc Run unit Aden 2000 Aden
Aden Abian Sanaa Aden Aden 117 245 119 1290 1200 2000
_________________________________________________________________________________
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University
find any < record type> using <record-field> find duplicate < record type> using <record-field> find first < record type> within <set-type> find next < record type> within <set-type> find owner within <set-type> find for update any < record type> using <record-field> find for update first < record type> within <set-type>
Example : Construct a DBTG query that prints the street name of Abdulla.
customer.name := Abdulla; find any customer using name; get customer; print (customer.street);
There may be several records with the specified value. The find any command locates the first of these. To locate other database records which match the <recordfield>, we use the command (find duplicate <record type> using <record-field>)
Example : Construct a DBTG query that prints the names of all the customers
who live in Aden. customer.city := Aden; find any customer using city; while DB_status = 0 do begin get customer; print (customer.name); find duplicate customer using city; end;
_________________________________________________________________________________
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University
The previous find commands locate any database record of type <record type>, we may concentrate on find that locate records in a particular DBTG-set. There are three different types of commands: find first .., find next , and find owner..
Example : Construct a DBTG query that prints the total balance of all the
accounts belonging to Omer. sum := 0; customer.name := Omer; find any customer using name; find first account within CustAcct; while DB_status = 0 do begin get account; sum := sum + account.balance; find next account within CustAcct; end; print (sum); The previous find commands locate records within a particular DBTG-set. There are many circumstances, where it may be necessary to locate the owner of a particular DBTG-set. This can be accomplished through the command (find owner within <set-type>).
Example : Construct a DBTG query that prints all the customers names of the
Arwa branch. branch.name := Arwa; find any branch using name; find first account within BrncAcct; while DB_status = 0 do begin find owner within CustAcct; get customer; print (customer.name); find next account within BrncAcct; end;
Example :
As a final example, consider the DBTG query that prints the names of all customers of the bank. Such a query cannot be formed easily since no one single set has all the customer records as its members. We can define a sigular set consisting of members of type customer as:
member is customer;
Then we can form the query as: find first customer within AllCust; while DB_status = 0 do begin get customer; print (customer.name); find next account within AllCust; end;
Example : Construct a DBTG query that prints the total number of accounts in
the Arwa branch with a balance greater than $10,000. Count := 0; branch.name := Arwa; find any branch using name; find first account within BrncAcct; while DB_status = 0 do begin get account; if account.balance > 10000 then count := count +1; find next account within BrncAcct; end; print(count);
Example : Construct a DBTG query for adding a new customer, Aziz, to the
database. customer.name := Aziz; customer.street := Old road; customer.city := Taiz; store customer;
_________________________________________________________________________________
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University
Modifying Records
In order to modify a record, we must find that record in the database, get it into the work_area memory and then change the desired fields in the template of the record. Then this modification can be reflected through the command (modify <record type>)
Deleting Records
In order to delete a record, the currency pointer of that type must point to the record in the database to be deleted. Then we can delete the record by (erase <record type>).
_________________________________________________________________________________
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University
It is possible to delete an entire set occurrence by finding the owner of the set , say, a record of type <record type>, and then execute (erase all <record type>). This will delete the owner of the set and all of its members.
Example : Construct a DBTG query to delete customer Amir and all of his
accounts. customer.name := Amir; find for update any customer using name; erase all customer;
In order to move a record from one set occurrence to another set occurrence, we need to find the selected record and the owner of the set to which that record is to be moved. Then we can move the record by : ( reconnect <record type> to <set type>).
Example : Construct a DBTG query for creating new account 401 which belongs
to Ali. account.number := 401; account.balance := 0; store account; customer.name := Ali; find any customer using name; connect account to CustAcct;
get account; find owner within CustAcct; disconnect account from CustAcct;
Example : Construct a DBTG query to move all accounts of Omer that are
currently at the Aden branch to the Taiz branch. customer.name := Omer; find any customer using name; find first account within CustAcct; while DB_status = 0 do begin find owner within BrncAcct; get branch; if branch.name = Aden then begin branch.name = Taiz; find any branch using name; reconnect account to BrncAcct; end; find next account within CustAcct; end;
Omer
89-123 Aden
117
1000
Ali
12-333
Aden
401
200
300
2000
_________________________________________________________________________________
Dr. Mohammed Fadle Abdulla 55-88 Sanaa Computer Sci & Engg. Department, Aden University
199
30000
Aziz
Rather than using multiple pointers , we can use a ring structure to represent the entire occurrence of the DBTG set-type. In a ring structure, the records of both the owner and the member types for a set occurrence are organized into a circular list. There is one circular list for each set occurrence. So, each record will contains exactly one pointer for each DBTG-set it is involve in, regardless of whether it is of the owner type or member type. Finally, two of the most pupolar network systems are Total and IDMS.
Omer
89-123 Aden
117
1000
Ali
12-333
Aden
401
200
300
2000
Aziz
55-88
Sanaa
199
30000
_________________________________________________________________________________
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University
Hierarchical Model
In the network model, the data is represented by collections of records and relationships among data are represented by links. This is true of the hierarchical model as well. The only difference is that in the hierarchical model, records are organized as collection of trees rather than as arbitrary graphs. A hierarchical database consists of a collection of records which are connected to one another through links. Each record is a collection of fields (attributes), each of which contains only one data value. A link is an association between precisely two records. Consider a database representing a customer-account relationship in a banking system. There are two record types, customer and account. A simple database is shown in the figure below. It shows that customer Omer has account 119, customer Ali has accounts 180 and 45, and customer Aziz has account 245.
Omer
North
Aden
Ali
South
Sanaa
Aziz
Road3
Taiz
119
2000
180
1250
45
120
245
4000
A DBMS belonging to the hierarchical data model uses tree structures to represent relationship among records. Here, the set of all customer and account records is organized in form of a rooted tree where the root of the tree is a dummy node. The hierarchical database (called as database tree) is a collection of such rooted trees, and hence forms a forest.
Consider the example of an institute which has a number of programs to offer. Each program has a number of courses. Each course has a number of students registered in it.
_________________________________________________________________________________
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University
Institute
Programs
Courses
Students
Institute
Program A Program B
Course A1
Course A2
Course B1
Course B2
Course B3
Student 1
Student 2
The content of a particular record may have to be replicated in several different locations. For example, an account may belong to several customers. The information pertaining to that account or the information to the various customers to which it may belong, will have to be replicated. Record replication has two major drawbacks: Data inconsistency may result when updating takes place. Waste of space is unavoidable.
Tree-Structure Diagrams
A tree-structure is the scheme for Hierarchical Database. Such a diagram consists of two basic components: Boxes , which correspond to record types, Lines, which correspond to links.
_________________________________________________________________________________
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University
A tree-structure diagram serves the same purpose as an entity-relationship diagram; namely, it specifies the overall logical structure of the database. It is similar to the data-structure diagram in the network model. The main difference is that the record types are organized in the form of a rooted tree. In a rooted tree, there can be no cycles, and the relationships formed in the tree must be such that only one-to-many or one-to-one relationships exist between a parent and a child. The database scheme is represented as a collection of tree-structure diagrams. For each such diagram, there exists one single instance of a database tree. Consider the entity-relationship diagram shown below, which consist of three entity sets customer, account and branch, related through the general relationship set CAB. This diagram specifies that a customer may have several accounts each located in a specific bank branch, and that an account may belong to several different customers. There are a number of different ways to transform this E-R diagram to a treestructure diagram. The most straightforward transformation is to create two treestructure diagrams as shown below.
assets name city street city branch name customer CAB account number balance
branch city
account balance
customer city
_________________________________________________________________________________
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University
An instance of the database corresponding to the above described scheme is illustrated below. It shows that Omer has account 200 in the arwa branch and the account 300 in the national bank, Ali has the accounts 256 and 119 in the arwa branch, and that Aziz has the same account 300 of Omer in the national branch and the account 160.
arwa
100000
aden
national
9999999
Sanaa
Omer
Road1 Ali
Omer aden
Road1
aden
256 200 55
300
300
10000
_________________________________________________________________________________
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University
Each application program executing in the system consists of a sequence of statements which access and manipulate database items as well as locally declared variables. For each such application program, the system maintains a program workarea, a buffer storage area which contains the following variables: Record templates, a record for each record type . Currency pointers, a set of pointers, one for each database tree, containing the address of the record in that particular tree most recently accessed. Status flag, a variable set by the system to indicate to the application program the outcome of the last database operation. We call this flag DB-status.
A B E F C G H D I J E B F C G
A D H I J
_________________________________________________________________________________
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University
Tree
Preorder Traversal
There are two different get commands for locating records in a database tree. The simplest command has the form : get first < record type> where <condition> The where clause is optional. The attached <condition> is a predicate that may involve any record type which is either an ancestor of <record type> or the <record type> itself. The get command locates the first record (in preorder) of the type <record type> in the database that satisfies the <condition> of the where clause. If the where clause is omitted, then the first record of type <record type> is located. Once such a record is found, the currency pointer is set to point to that record, and the content of that record is copied into the program work-area. If no record exists in the database, then the search is fials and the variable DB-status is set to error message.
Example : Construct the database query that prints the address of customer
Omer. get first customer where customer.name = Omer ; print (customer.street, customer.city);
Example : Construct the database query that prints an account belonging to Ali
with balance greater than $1000 (if one such exist). get first account where customer.name = Omer and account.balance >1000 ; if DB-status =0 then print (account.number);
There may be several similar records in the database that we wish to retrieve. The get first command locate one of these. In order to locate the other database records, the following command can be used: get next < record type> where <condition> This command locates the next record that satisfies <condition>. If the where clause is omitted, then the next record of the type <record type> is located.
_________________________________________________________________________________
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University
Example : Construct the database query that prints the account number of all
the account with a balance greater than $500 (if one such exist). get first account where account.balance >500 ; while DB-status =0 do begin print (account.number); get next account where account.balance > 500; end.
There are many circumstances where we wish to locate one record within a particular subtree. That is, we want to limit the search to one specific subtree rather than the entire database tree. The root of the subtree in question is the last record which was located with either the get first or get next command. The get command to locate a record within that subtree has the form get next within parent < record type> where <condition> which locates the next record (in preorder) which satisfies <condition> in the subtree whose root is the parent of current of <record type>. If the where clause is omitted, then the next record of type <record type> within the designed subtree is located.
Example : Construct the database query that prints the total balance of all
account belonging to Abdulla. Sum := 0; get first customer where customer.name = Abdulla; get next within parent account; while DB-status =0 do begin sum := sum + account.balance; get next within parent account; end; print (sum);
_________________________________________________________________________________
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University
Data Base Notes Chap ter_2 (25 / 26) ___________________________________________________________________________________ insert <record type> where <condition> If the where clause is included, the system searches the database tree for a record that satisfies the <condition> in the where clause. Once such a record, say X, is found the newly created record is inserted in the tree as the leftmost child of X. If the where clause is omitted, the record is inserted in the first position (in preorder) in the database tree.
Example :
Adding a new customer, Omer, to the Arwa branch. customer.name := Omer; customer.street := Road_1; customer.city := Aden; insert customer where branch.name = Arwa;
replace
The DL/I language requires that prior to modifying a record, the get command must have additional clause hold so that the system is aware of the fact that a record is to be modified.
Example :
Change the street address of Abdulla to Main_Road. Get hold first customer; where customer.name = Abdulla; customer.street := Main_Road; replace;
In order to delete a record of type <record type>, the currency pointer must be set to point to that record. Following this, we can delete that record by executing:
delete Example :
Delete account 561. Get hold first account; where account.number = 561 ; delete ;
A delete operation deletes not only the record in question but the entire sub_tree rooted by that record.
Virtual Records
We have seen that in the case of many-to-many relationships, record replication is necessary to preserve the tree-structure organization of the database. However, to overcome the drawbacks of this replications a virtual record is used. Such a record contains no data value, it does contain a logical pointer to a particular physical record. When a record is to be replicated in several database trees, we
_________________________________________________________________________________
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University
Data Base Notes Chap ter_2 (26 / 26) ___________________________________________________________________________________ keep a single copy of that record in one of the trees and replace every other record with a virtual record containing a pointer to that physical record.
leftmost next-sibling
leftmost
_________________________________________________________________________________
Dr. Mohammed Fadle Abdulla Computer Sci & Engg. Department, Aden University