190 likes | 304 Views
Relational Databases:. 2 - Dimensional tables/relations e.g: Student: Relational Operators Select - subset of rows from a table Project - subset of columns from a table Join - Create new table by linking two tables on common attributes Duplicate Rows not allowed ( Primary key )
E N D
Relational Databases: • 2 - Dimensional tables/relations e.g: Student: • Relational Operators • Select - subset of rows from a table • Project - subset of columns from a table • Join - Create new table by linking two tables on common attributes • Duplicate Rows not allowed ( Primary key ) PK : Attribute(s)that uniquely define each row Cannot have missing (Null) PK value St# Name Classfication
Student Department • Foreign Key • Defines relationship between entities e.g S# Dept# Sname DName Major Daddress Student: Major S# Name Dept#
Dept# DName DAddress Columns whose values point to PK values of another table are FKs Department: RULE: • FK values either reference existing PK values or they are NULL ( Referential Integrity Constraint)
Order# O-Date Prod# Prod_Desc Qty-Ordered 20 21 9/30/97 9/30/97 304 298 304 10 5 Normalization: • optimizes relational DB design • Removes potential problems/anomalies e.g Consider: Order Sales Rep Product Lists Stored in Serves Places Warehouse Customer Order
Repeating Group Order ( Order#, O-Date, (Prod#, ProdDesc, Qty)) First Normal Form: • No repeating groups Order ( Order#, O-Date, Prod#, ProdDesc, Qty) Potential Problems: • change a particular products description • can have inconsistent data • adding a product when an order does to exist for it • deletions Second Normal Form: • in 1NF • all non-key attributes are dependent on the entire PK 1NF
To convert 1NF to 2NF: • Separate table for each field in PK • Separate table for all combinations of PK fields, taken 2 at a time, 3 at a time etc. e.g. Order ( Order#, Odate) Product ( P#, Pdesc ) OrderLine ( Order#, P#, Qty-Ordered ) Consider Customer ( Cust#, Cname, CAddr, SalesRep#, SRName) • in 1NF ( no repeating groups) • in 2NF ( PK is a single field)
Problems • changing a SalesRep’s name • adding a SalesRep who does not yet have a customer • deletions Third Normal Form: • in 2NF • no non-key field is dependent on another non-key field To change to 3NF: • remove all fields that are dependent on another non-key field and place in new table with the other field as the key e.g. SalesRep ( SalesRep#, SRName ) Customer ( Cust#, CName, CAddr, SalesRep# (FK))
Boyce - Codd NF • 4NF • 5 NF
Example: Course and Instructor: Course ( Course #, CTitle, Inst#(FK) ) Student and Course: EnrollsIn (S-ID, Course#, Grade ) Student and Major: StudentMajor (S-ID, Major) Student and CampusOrg: BelongsTo (S-ID, Std-Org) Student(…),Instructor (….) S-ID SName HomeAddr Campus Addr Student Major Major Course Campus Org Std-Org Course# CTitle Inst# InstName Office Phone Instructor
Logical Data ModelPhysical Data Model Attribute Data Element, Field Object Class File, Table, Relation Object Instance Logical Record Identifier Primary Key Relationship Foreign Key O-R Model Database
Modeling relationship between Two entities: Rule1:For 1:1 (Cardinality) - place PK of one rec-type) in to rec-type for another Rule2: For 1:M or M:1 - place PK of rec-type for the entity on the 1 side into record-type for the entity on the M side Rule3:For M :M- create a new record (Associative Entity ) with PK composed of PKs of the two entities, and place ant attribute that is determined by both PKs into the new rec-type. Rule4:Discard rec-type with only one data element. This has already been included in other record-type by above rules
Ternary Relationships ( When relationship between at least two of the entities is M:M) Case1: All relationships are M:M • Form new rec-type with PK consisting of PKs of all 3 entities. Place any data element determined by all 3 PKs into new rec-type. Case 2: M:M and M:1 • Form new rec_type with PK consisting of PKs of the M:M entities. Place PK of entity on the 1 side of relationship into this new rec-type as a non-key element
S-ID S-Name S-Addr Ternary Relationship Example: Students may have more than one Major and for each major, a student is assigned an advisor Student ( S-ID,S-Name, S-Addr ) Advisor ( Ad-ID, Ad Name, Ad Phone) Major ( Major ) Std- Major- Advisor (S-ID, Major, Ad-ID ) Student M Has 1 Ad-ID Ad-Name Ad-Phone Advisor Major Major M
Primary Physician Patients PatientID P-Address P-DOB P-Hphone P-Wphone P-Sex P-Occupation Physician Phy Name PhySpeciality PhyPhone Phy-Pager Appointment App Date App Time Complaint Hospital HospName
Ex: Patient Records System: PATIENT (PatientID, P-Name, P-Addr, PDOB,P-HomePhone, P-WPhone,P-Sex,P-Occupation, PhyName(FK)) PHYSICIAN (PhyName, PhySpeciality, PhyPhone, PhyPager, HospitalName(FK)) HOSPITAL (HospitalName) APPOINTMENT (PatientID, PhyName, ApptDate, ApptTime, Complaint) ASSUMPTION: Patient does not have more than one appointment with same physician on the same day.
DATA MODELLING IN LIFECYCLE PHASES: System Planning • Study (Business Mission ) • No data modeling; but certain critical data entities may be identified • Definition Enterprise Analysis - Enterprise Data Model ( Objects) • Mgmt’s high level view of critical entities Business Area Analysis • More detail - attributes, relationships cardinalities (may entail changes to enterprise data model )
Systems Analysis • Preliminary Investigations • Application context data model • Study of current system • Focus on process modeling • Requirement Analysis • Expand and refine data model • Application essential Data Model ( Implementation independent) System Design Implementation Data Model (Files, DBs, normalization,etc)