Outlines:-
1) Relational Data Model. 2) Relational Data Integrity Constraints. 3) Transforming ER-Diagrams into ( Logical Design ) Relational Schemas.
Attribute or Column
Data Manipulation
Once we build relations, we need a way to access and manipulate the data they contain. A Data Manipulation Language, such as Structured Query Language (SQL), supports and facilitates data access and manipulation. The following is an example of a SQL statement that updates the faculty relation. UPDATE TblFaculty SET Salary = Salary * 1.05 WHERE JoinDate < #1/1/2011# AND Salary < 70000
Relational Keys
Because no two records should be identical, we need to specify one or more attributes (defined as relational keys) that uniquely identify each record in a relation. We will now define some of the most important relational keys. Properties of a Relation 1- Each relation is uniquely identified by its name. 2- Each cell of a relation contains exactly one single (atomic) value. 3- Each record (all the attributes together) of a relation is unique. 4- Each attribute in a relation has a distinct name. 5- The values of an attribute are from the same domain. 6-The order of attributes is Irrelevant. Columns can be interchanged without changing the meaning of a relation. 7-The order of records is also Irrelevant. Rows can be interchanged without changing the meaning of a relation.
Alternate Keys
Because of the relational property stating that a relation cannot have duplicates, we should always be able to find a primary key for a relation. In the least desirable situation, we might have to use all of the attributes of a relation. The candidate keys that are not selected for the primary key are called the Alternate Keys, and a key that has more than one attribute is called a Composite Key. As we mentioned in Chapter 3, a relationship is not explicitly defined in the relational data model. Instead, we use common attributes to enforce the relationships between two relations. Therefore, the same attribute can appear in more than one series of relations.
10
Foreign Key
For example, the Department relation has the attribute DeptName, which also appears in the Student relation to indicate that students belong to departments, the primary relationship between these two relations. The Foreign Key is an attribute or a set of attributes for a relation that serves as a candidate key of some (possibly the same) relation.
We represent the Primary key in the relational data model by underlining the attribute(s) and a Foreign Key by
italicizing the attribute(s). Therefore, in the following example, SSN serves as the primary key and DeptName as the foreign key of STUDENT. DeptName serves as the primary key and CollegeID as the foreign key of DEPARTMENT.
STUDENT (SSN, Name, Email, DeptName) DEPARTMENT (DeptName,Address, Phone, CollegeID)
1 1
NULL
NULL simply means that the value is either unknown or is not applicable, and it allows us to handle incomplete or exceptional data. Many readers confuse NULL with the numeric value zero or with blank text; however, NULL only represents the absence of a value. For example, student fax numbers, occurs when there is no applicable data value for a particular attribute.
12
Domain Constraints
A Domain is the set of values that can be assigned to an attribute. The definition of a domain consists of the Domain Name, the Description, The Data Type, the Size, and other information related to Formatting and Ranges. For example, the attribute SSN has the following domain:
The domain constraint states that the values of an attribute must be from the same domain.
13
Entity Constraints
While assigning guidelines for the selection of the primary key, we mentioned that a candidate key that does not have NULL values is a good candidate for a primary key. Entity Constraints enforce this guideline as a constraint in the relational data model. Entity constraints ensure that every relation of a relational data model has a primary key and that the data values for the attributes of the primary key cannot be NULL. In the Student relation, for example, we cannot possibly enter a student record with no value for the SSN attribute. Because SSN is a primary key, it cannot contain a NULL value. However, suppose that we do have NULL values for primary key attributes. By definition, the primary key should still be able to uniquely distinguish records with NULL from the other records in the relation that do have values for the primary key.
14
15
Referential Constraints
The third integrity constraint pertains to foreign keys. Referential integrity constraints maintain consistency between two relations related to each other through the primary key-foreign key relationship. A Referential Integrity Constraint ensures that the foreign key values of a relation must match the primary key value of a (possibly the same) relation; otherwise, the value of a foreign key must be NULL. For example, consider the relationship between the Department and Student relations. The attribute DeptName is the foreign key in the Student relation and is the primary key in the Department relation. In keeping with the definition of a referential integrity constraint, the values for the DeptName attribute in the Student relation must match the values of the DeptName attribute in the Department relation, or they must be NULL.
16
Lets now see how the Referential Integrity Constraint maintains Data Consistency. We use the Department relation to store the information about all the universitys departments. When we add a new record to the Student relation, we should assign students to an existing department department relation). that is (from Assigning not in the a the Department
Department relation clearly results in data inconsistency. If we are not sure about the student's department, then constraints allow us to provisionally assign the NULL value to foreign key attributes.
17
18
19
Task 2: Transforming Composite Attributes When an entity type has Composite Attributes, we only have to make one change in the rules for the regular entity. We include simple attributes of a composite attribute in the relation.
20
We could add multiple values of a single record in a relation; however, this is prohibited by the properties of a relation. A relation has exactly one value for each of its cells. This feature demands a slightly different set of rules for multi-valued attributes: 1- Transform the regular entity as previously described; however, do not add a multi-valued attribute to the relation. 2- Create a new relation (one for each multi-valued attribute). The new relation gets two attributes: the primary key of the regular entity and the multi-valued attribute itself. The name of the new relation should be a logical name that reflects the meaning of the multi-valued attribute. 3- The primary key of this new relation is a Composite Key. Both of the attributes make up the primary key of this new relation.
21
Note : Database designers frequently use this decomposition technique, as it Reduces Data Redundancy and Eliminates Anomalies. For most of the remaining technique. transformations, we simply repeat the decomposition
22
Task 4: Transforming Unary Relationships. A) Transforming Unary (1-N) Relationships: do the following steps: 1- Transform the entity of a unary relationship as a regular entity described in Tasks 1-3. 2- Add a new attribute, the primary key of the same relation, as a foreign key. Draw an arrow that originates from the foreign key and points to the primary key. For example, Student Representative relationships:
N 1
23
B) Transforming Unary (M-N) Relationships: do the following steps: 1- Transform the entity of a unary relationship as a regular entity described in Tasks 1-3. 2- Create a new relation to represent the (M-N) relationship. Name the new relation to reflect the meaning of the relationship. 3- The new relation requires at least two attributes. The first attribute is the relations primary key, which represents the entity of a relationship. The second attribute represents the (M-N) relationship. The primary key of the entity relation migrates to the new relation. Therefore, in the case of a unary relationship, we get the same attribute twice as a foreign key in the new relation. 4- Draw referential integrity arrows for the foreign keys. Add the attributes on a (M-N) relationship for the new relation. 5- The primary key of a new relation is a composite key of foreign keys.
24
The Item is a regular entity with a unary (M-N) relationship, Contains, which represents the bill-of-material where items are assembled from other items. The relationship is many-to-many because an item can be assembled from one or more items, and, conversely, an item can be used to assemble one or more items.
N M
25
Task 5: Transforming Binary Relationships A) Transforming Binary (1-1) Relationships: 1-Create two relations, one for each entity in a binary relationship. 2-Transform each entity into a relation as a regular entity according to the process described in Tasks 1-3. 3-Include the primary key of one relation as a foreign key to the other relation. For one-to-one binary relationships, we can use minimum cardinalities to determine which relation should have a foreign key attribute. In such a pairing, the mandatory side migrates toward the optional side of the relationship. 4-Draw an arrow indicating the referential integrity constraint. 5-Any attributes of the relationship, as well as the foreign key, should migrate toward the optional side of a relationship.
26
B) Transforming Binary (1-M) Relationships: 1- Create two relations, one for each entity in a binary relationship. Transform each entity into a relation as a regular entity following the process described in Tasks 1-3. 2- Move the primary key of a relation on one side of the relationship toward the relation on the many sides of the relationship.
27
3- Draw an arrow indicating the existence of a referential integrity constraint. 4- Move any attributes on the relationship, along with the foreign key, toward the relation on the many sides of the relationship.
28
C) Transforming Binary (M-N) Relationships: 1- Create two relations, one for each entity in a binary relationship. Transform each entity into a relation as a regular entity following the process described in Tasks 1-3. 2- Create a third new relation to represent the many-to-many relationship. The name of the new relation should reflect the meaning of the relationship. 3- Move the primary key from both the relations (one for each entity in the relationship) to the new relation. Draw arrows for the referential integrity constraints. 4- The primary key of a new relation is a composite key with the foreign keys of relations. 5- Move any attributes of the relationship toward the third intermediate relation that represents the relationship.
29
30
Task 6: Transforming Ternary Relationships: 1- Create three relations, one for each entity in a ternary relationship. Transform each entity into a relation as a regular entity following the process described in Tasks 1-3. 2- Create a fourth relation that represents the ternary many-tomany relationship. The name of this new relation should reflect the meaning of the relationship. 3- Move the primary key from the first three relations (one for each entity in relationship) to the new relation. Draw arrows to signify referential integrity constraints 4- The primary key of a new relation is a composite key with its foreign keys. 5- Move any attributes on the relationship to this fourth relation.
31
32
Task 8: Transforming Weak Entities: In Chapter [ 3 ], we defined weak entities as those that cannot exist without another entity type. The weak entity depends on an entity called the identifying owner, or, simply, the owner. This relationship between a weak entity and its owner is called an identifying relationship. Weak entities have a partial key attribute or attributes, which, when combined with the primary key attribute(s) of its owner, provide the primary key for the weak entity. We can easily transform Weak Entities into a relational schema. To do so: 1- Create a relation for the owner entity type following the rules described in Tasks 1-3. 2- Create a new relation for a weak entity type and transform it to a regular entity.
33
3- Include the primary key of the owner relation as a foreign key in the weak entity relation. Draw an arrow indicating referential integrity constraint. 4- Make the primary key of a weak entity relation as the composite key formed by partial identifier attributes of a weak entity and a foreign key.
34
35
36