DOC PREVIEW
Berkeley COMPSCI 186 - ER & Relational - Digging Deeper

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

ER & Relational:Digging DeeperR &G - Chapters 2 & 3Databases Model the Real World• “Data Model” allows us to translate real worldthings into structures computers can store• Many models: Relational, E-R, O-O, XML,Network, Hierarchical, etc.• Relational– Rows & Columns– Keys & Foreign Keys to link Relationssid name login age gpa53666 Jones jones@cs 18 3.453688 Smith smith@eecs 18 3.253650 Smith smith@math 19 3.8sid cid grade53666 Carnatic101 C53666 Reggae203 B53650 Topology112 A53666 History105 BEnrolledStudentsAggregationUsed to model arelationshipinvolving arelationship set.Allows us to treat arelationship setas an entity setfor purposes ofparticipation in(other)relationships. Aggregation vs. ternary relationship? Monitors is a distinct relationship, with adescriptive attribute. Also, can say that each sponsorship ismonitored by at most one employee.untilEmployeesMonitorslotnamessnbudgetdidpidstarted_onpbudgetdnameDepartmentsProjectsSponsorssinceConceptual Design Using the ER Model• ER modeling can get tricky!• Design choices:– Should a concept be modeled as an entity or an attribute?– Should a concept be modeled as an entity or a relationship?– Identifying relationships: Binary or ternary? Aggregation?• Note constraints of the ER Model:– A lot of data semantics can (and should) be captured.– But some constraints cannot be captured in ER diagrams.• We’ll refine things in our logical (relational) designEntity vs. Attribute• Should address be an attribute of Employees or anentity (related to Employees)?• Depends upon how we want to use addressinformation, and the semantics of the data:• If we have several addresses per employee, addressmust be an entity (since attributes cannot be set-valued).• If the structure (city, street, etc.) is important, addressmust be modeled as an entity (since attribute valuesare atomic).• If the lifetime of the address differs from the entity,address must be modeled as an entity (since attributesare deleted with their entity).Entity vs. Attribute (Cont.)• Works_In2 does notallow an employee towork in a departmentfor two or more periods.– (why not?)• Similar to the problem ofwanting to record severaladdresses for anemployee: we want torecord several values ofthe descriptive attributesfor each instance of thisrelationship.nameEmployeesssnlotWorks_In2fromtodnamebudgetdidDepartmentsdnamebudgetdidnameDepartmentsssnlotEmployeesWorks_In3DurationfromtoEntity vs. RelationshipOK as long as amanager gets aseparatediscretionarybudget (dbudget)for each dept.What if manager’sdbudget covers allmanaged depts?(can repeat value,but suchredundancy isproblematic)Manages2namednamebudgetdidEmployees DepartmentsssnlotdbudgetsinceEmployeessincenamednamebudgetdidDepartmentsssnlotMgr_Apptsis_managerdbudgetapptnummanaged_byNow you try itTry this at home - Courses database:• Courses, Students, Teachers• Courses have ids, titles, credits, …• Courses have multiple sections that have time/rmand exactly one teacher• Must track students’ course schedules and transcriptsincluding grades, semester taken, etc.• Must track which classes a professor has taught• Database should work over multiple semestersThese things get pretty hairy!• Many E-R diagrams cover entire walls!• A modest example:A Cadastral E-R Diagramcadastral: showing or recording property boundaries, subdivision lines,buildings, and related detailsSource: US Dept. Interior Bureau of Land Management,Federal Geographic Data Committee Cadastral Subcommitteehttp://www.fairview-industries.com/standardmodule/cad-erd.htmConverting ER to Relational• Fairly analogous structure• But many simple concepts in ER are subtle tospecify in relationsLogical DB Design: ER to Relational• Entity sets to tables. CREATE TABLE Employees (ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn))Employeesssnnamelotssn name lot123-22-3666 Attishoo 48231-31-5368 Smiley 22131-24-3650 Smethurst 35Relationship Sets to Tables• In translating a many-to-many relationship set to arelation, attributes of therelation must include:1) Keys for eachparticipating entity set(as foreign keys). This setof attributes forms asuperkey for the relation.2) All descriptive attributes.CREATE TABLE Works_In( ssn CHAR(1), did INTEGER, since DATE, PRIMARY KEY (ssn, did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments)ssn did since123-22-3666 51 1/1/91123-22-3666 56 3/3/93231-31-5368 51 2/2/92Review: Key Constraints• Each dept has atmost onemanager,according to thekey constraint onManages.Translation to relational model?Many-to-Many1-to-1 1-to Many Many-to-1dnamebudgetdidsincelotnamessnManagesEmployeesDepartmentsTranslating ER with Key Constraints• Since each department has a unique manager, wecould instead combine Manages and Departments.CREATE TABLE Manages( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn)REFERENCES Employees, FOREIGN KEY (did)REFERENCES Departments)CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees)Vs.dnamebudgetdidsincelotnamessnManagesEmployeesDepartmentsReview: Participation Constraints• Does every department have a manager?– If so, this is a participation constraint: the participation ofDepartments in Manages is said to be total (vs. partial).• Every did value in Departments table must appear in arow of the Manages table (with a non-null ssn value!)lotnamednamebudgetdidsincenamednamebudgetdidsinceManagessinceDepartmentsEmployeesssnWorks_InParticipation Constraints in SQL• We can capture participation constraints involving one entityset in a binary relationship, but little else (without resorting toCHECK constraints which we’ll learn later).CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11) NOT NULL, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCESEmployees, ON DELETE NO ACTION)Review: Weak Entities• A weak entity can be identified uniquely only byconsidering the primary key of another (owner) entity.– Owner entity set and weak entity set must participate in aone-to-many relationship set (1 owner, many weakentities).– Weak entity set must have total participation in thisidentifying relationship set.lotnameagepnameDependentsEmployeesssnPolicycostTranslating Weak Entity Sets• Weak entity set and


View Full Document

Berkeley COMPSCI 186 - ER & Relational - Digging Deeper

Documents in this Course
Load more
Download ER & Relational - Digging Deeper
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view ER & Relational - Digging Deeper and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view ER & Relational - Digging Deeper 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?