Logical Database Design: Chapter-3
Logical Database Design: Chapter-3
Logical Database Design: Chapter-3
Introduction
The entire database and more particularly the modern database uses a very important data model called as the Relational Model. This model was proposed by Codd in the year 1970. During that time the popular models were only Network model and Hierarchical model.
Relation
The relational model represents the database as a collection of relations. Each relation resembles a simple table, having a set of rows and set of columns. Each relation consists of a relational schema and relational instance. The instance is nothing but a relation having a set of specific values for the records in the database at a particular time.
Example
Name
Prasad
RegNo
1BI99CS045
Addr
456, Koramangala
Phone
5567234
DBirth
23-Mar-67
GPA
8.9
Gaurav
1BI98CS012
2, J.C. Road
6623109
10-Dec-66
4.5
Ayswaria
1BI97CS044
6669456
04-Aug-65
9.0
BrName
VV Puram JC Road Jayanagar MG Road Gandhinagar
BrCity
Bangalore Mysore Bangalore Belgaum Belgaum
Assets
10034567 2390156 6784560 12445566 67888888
Relation - Definition
A relation R is a subset of the Cartesian product of the domains that define R R (dom(A1)) (dom(A2)) . . . (dom(An)) This actually gives the total number of tuples in the Cartesian product. R is represented as, R (A1, A2, . . ., An). The degree of a relation is the number of attributes that a relation has and the cardinality of the relation instance is the number of tuples in it.
Domain Constraints
The domain constraints specifies the value of each attribute A that must be an atomic value from the domain dom(A). Example: The type integer may hold all the integer values may be allowed. Oracle allows VARCHAR2, NUMBER, CHAR, INTEGER, DATE, LONG, RAW, LONG RAW, BLOB, CLOB, BFILE.
Key Constraints
A key constraint is a declaration to specify a minimal set of attributes to find a tuple uniquely in a relation. Candidate Key: Definition Let K be a set of attributes of the relation R. Then, K is a candidate key if and only if it satisfies the following two conditions: (1) Uniqueness: No legal values of R ever contain two distinct tuples with the same value. (2) Irreducibility: No proper subset of K has the uniqueness property.
Eg-1: {Sid, Name} is not a candidate key, because this set properly contains the key {Sid} Eg-2: {BranchName, BranchCity} is not a candidate key
Key Constraints
Super Key A superset of a candidate key is a super key. A super key has the uniqueness property but not necessarily the irreducible property. Eg: {Sid, Name} is a super key Primary Key It is customary to have one of the candidate keys as the primary key of the relation. It is better to choose a primary key with minimal set of attributes. Eg: {SSN}, {RegNo}, {PartNo}, etc. Can the subset {CFirstName, CLastName, CPhone} form a primary key?
Key Constraints
Foreign Key A foreign key is a set of attributes of a relation, say, R2 whose values are required to match values of some primary key of some other relation R1. The foreign key constraint is necessary between any two relations to maintain the data consistency.
Eg: Employees(SSN, Name, .., DNo) Departments(DNumber, DName) DNo in Employees relation is called as foreign key. What happens when insertion, deletion, or modification is done to the records with the enforced constraints?
Converting ER diagrams to relational schema Converting Entity into RELATION/Table Converting Attributes into Column or table Converting Relationships into Attributes or tables
ER to Relational mapping
Strong Entity to Table: For every strong entity E, create a table R that includes all the simple attributes of E.
Addr
Name Salary Sex BDate
SSN
Employees
Employees(SSN,Name,BDate,Addr,Sex,Salary)
Entity example
Address is a composite attribute Years of service is a derived attribute (calculated from DOJ & current date) Skill set is a multi-valued attribute Employee (E#, Name, Door_No, Street, City, Pincode, Date_Of_Joining) Emp_Skillset( E#, Skillset)
Weak entity types are converted into a table of their own, with the primary key of the strong entity acting as a foreign key in the table This foreign key along with the key of the weak entity form the composite primary key of this table
The Relational Schema Employee (E# ,.) Dependant (E#, D_Name, Address,REL)
Converting Relationships
The way relationships are represented depends on the cardinality and the degree of the relationship The possible cardinalities are: 1:1, 1:M, N:M The degrees are: Unary Binary Ternary
Unary 1:1
Consider employees who are also a couple The primary key field itself will become foreign key in the same table
Unary 1:N
The
primary key field itself will become foreign key in the same table Same as unary 1:1
Employee( E#, Name,,Manager)
PK FK
Unary M:N
There will be two resulting tables. One to represent the entity and another to represent the M:N relationship Employee( E#, Name,) Guarantor_of( Guarantor E#, Beneficiary E# )
Binary 1:1
Case 1:Combination of participation types The primary key of the partial participant(EMPLOYEE) will become the foreign key of the total participant. Employee( E#, Name,) Department (Dept#, Name,E#_Head)
Binary 1:1
1
1
Binary 1:N
The primary key of the relation on the 1 side of the relationship becomes a foreign key in the relation on the N side
FK Teacher (ID, Name, Telephone, ...)
Binary M:N
A new table is created to represent the relationship Contains two foreign keys one from each of the participants in the relationship The primary key of the new table is the combination of the two foreign keys Book (Acc#,Title) Employee (E#,Name,) Issue (Book#, E#)
Many-to-Many cardinality
Hours PNumbere r N
PName PLocation
Employees
Works_O n
Projects
Ternary relationship
Ternary relationship represented by a new table The new table contains three foreign keys one from each of the participating Entities The primary key of the new table is the combination of all three foreign keys Prescription (Doctor#, Patient #,Medicine_Name)
DLocation
1
2 3 2
Bangalore
Belgaum Bangalore Chennai
Departments
Handling Generalization:
Tables to be created are: AccN o Balanc e Account (Method-1) 1. Account(AccNo, Balance) 2. SB(AccNo, Int_Amt) 3. FD(AccNo, Dep_Amt)
ISA
SB
FD
RD
CA
Method-2:
Handling Aggregation:
RegNo StdName
Students
CourseTiitle CourseId Credits
PName SSN
Attends
Professors
Teaches
Courses
End of Chapter 3