Entity-Relationship Diagrams and the Relational Model
Total Page:16
File Type:pdf, Size:1020Kb
Review Entity-Relationship Diagrams and the • Why use a DBMS? OS provides RAM Relational Model and disk CS 186, Fall 2007, Lecture 2 R & G, Chaps. 2&3 A relationship, I think, is like a shark, you know? It has to constantly move forward or it dies. And I think what we got on our hands is a dead shark. Woody Allen (from Annie Hall, 1979) Review Data Models • Why use a DBMS? OS provides RAM and disk • DBMS models real world • Data Model is link – Concurrency between user’s view of – Recovery the world and bits stored in computer – Abstraction, Data Independence • Many models exist Student (sid: string, name: string, login: string, age: integer, gpa:real) – Query Languages • We think in terms of.. – Relational Model (clean and – Efficiency (for most tasks) common) – Security – Entity-Relationship model (design) 10101 – Data Integrity – XML Model (exchange) 11101 Why Study the Relational Model? Steps in Database Design • Requirements Analysis • Most widely used model. – user needs; what must database do? • “Legacy systems” in older models • Conceptual Design – e.g., IBM’s IMS – high level description (often done w/ER model) • Object-oriented concepts merged in – Rails encourages you to work here – “Object-Relational” – two variants • Logical Design • Object model known to the DBMS – translate ER into DBMS data model • Object-Relational Mapping (ORM) outside the DBMS – Rails requires you to work here too – A la Rails • Schema Refinement • XML features in most relational systems – consistency, normalization – Can export XML interfaces • Physical Design - indexes, disk layout – Can provide XML storage/retrieval • Security Design - who accesses what, and how Conceptual Design name ER Model Basics ssn lot • What are the entities and relationships in the Employees enterprise? • Entity: Real-world object, distinguishable from • What information about these entities and other objects. An entity is described using a set of attributes. relationships should we store in the database? • Entity Set: A collection of similar entities. E.g., all employees. • What integrity constraints or business rules hold? – All entities in an entity set have the same set • A database `schema’ in the ER Model can be of attributes. (Until we consider hierarchies, anyway!) represented pictorially (ER diagrams). – Each entity set has a key (underlined). • Can map an ER diagram into a relational schema. – Each attribute has a domain. ER Model Basics (Contd.) ER Model Basics (Cont.) name since ssn lot name dname ssn lot did budget Employees since dname Employees Works_In Departments super- subor- did budget visor dinate Reports_To • Relationship: Association among two or more Departments Works_In entities. E.g., Attishoo works in Pharmacy department. •Same entity set can participate in different – relationships can have their own attributes. relationship sets, or in different “roles” in • Relationship Set: Collection of similar relationships. the same set. – An n-ary relationship set R relates n entity sets E1 ... En ; each relationship in R involves entities e1 ∈ E1, ..., en ∈ En name since dname ssn lot did budget …to be clear… Key Constraints Employees Manages Departments • Recall that each relationship has exactly An employee can one element of each Entity Set work in many Works_In – “1-M” is a constraint on the Relationship departments; a since dept can have Set, not each relationship many employees. • Think of 1-M-M ternary relationship In contrast, each dept has at most one manager, according to the key constraint Many-to- 1-to Many 1-to-1 on Manages. Many Participation Constraints Weak Entities • Does every employee work in a department? A weak entity can be identified uniquely only by • If so, this is a participation constraint considering the primary key of another (owner) – the participation of Employees in Works_In is said to be entity. total (vs. partial) – Owner entity set and weak entity set must – What if every department has an employee working in it? participate in a one-to-many relationship set (one • Basically means “at least one” owner, many weak entities). since name dname – Weak entity set must have total participation in ssn lot did budget this identifying relationship set. Employees Manages Departments name cost ssn lot pname age Works_In Means: “exactly one” Employees Policy Dependents since Weak entities have only a “partial key” (dashed underline) Binary vs. Ternary Relationships name Binary vs. Ternary Relationships (Contd.) ssn lot pname age Employees Covers Dependents If each policy is owned by just 1 employee: • Previous example illustrated a case when two binary relationships were better than one ternary Bad design Policies Key constraint on relationship. Policies would policyid cost mean policy can name pname age only cover 1 ssn lot • An example in the other direction: a ternary relation dependent! Dependents Contracts relates entity sets Parts, Departments and Employees Suppliers, and has descriptive attribute qty. No Purchaser • Think through all Beneficiary combination of binary relationships is an adequate the constraints in substitute. (With no new entity sets!) the 2nd diagram! Better design Policies policyid cost Summary so far Binary vs. Ternary Relationships (Contd.) qty • Entities and Entity Set (boxes) Parts Contract Departments • Relationships and Relationship sets (diamonds) VS. – binary Suppliers – n-ary Parts needs Departments • Key constraints (1-1,1-N, M-N, arrows) can-supply • Participation constraints (bold for Total) Suppliers deals-with • Weak entities - require strong entity for key – S “can-supply” P, D “needs” P, and D “deals-with” S does not imply that D has agreed to buy P from S. – How do we record qty? Administrivia Other Rails Resources • Blog online • Rails API: http://api.rubyonrails.org • Syllabus & HW calendar coming on-line • Online tutorials – Schedule and due dates may change (check – E.g. http://poignantguide.net/ruby frequently) – Screencasts: – Lecture notes are/will be posted http://www.rubyonrails.org/screencasts • HW 0 posted -- due Friday night! – Armando Fox’s daylong seminar: – Accts forms! http://webcast.berkeley.edu/event_details.php?we • Other textbooks bcastid=20854 – Korth/Silberschatz/Sudarshan – O’Neil and O’Neil • There are tons of support materials and fora – Garcia-Molina/Ullman/Widom on the web for RoR Relational Database: Definitions Ex: Instance of Students Relation • Relational database: a set of relations. • Relation: made up of 2 parts: sid name login age gpa – Schema : specifies name of relation, plus name 53666 Jones jones@cs 18 3.4 and type of each column. • E.g. Students(sid: string, name: string, login: string, age: 53688 Smith smith@eecs 18 3.2 integer, gpa: real) 53650 Smith smith@math 19 3.8 – Instance : a table, with rows and columns. • #rows = cardinality Cardinality = 3, arity = 5 , all rows distinct • #fields = degree / arity • • Can think of a relation as a set of rows or • Do all values in each column of a relation instance tuples. have to be distinct? – i.e., all rows are distinct SQL - A language for Relational DBs SQL Overview • SQL (a.k.a. “Sequel”), standard • CREATE TABLE <name> ( <field> <domain>, … ) language • INSERT INTO <name> (<field names>) • Data Definition Language (DDL) VALUES (<field values>) – create, modify, delete relations • DELETE FROM <name> – specify constraints WHERE <condition> – administer users, security, etc. • UPDATE <name> SET <field name> = <value> • Data Manipulation Language (DML) WHERE <condition> – Specify queries to find tuples that satisfy • SELECT <fields> criteria FROM <name> – add, modify, remove tuples WHERE <condition> Creating Relations in SQL Table Creation (continued) • Creates the Students relation. • Another example: the Enrolled table – Note: the type (domain) of each field is holds information about courses specified, and enforced by the DBMS students take. whenever tuples are added or modified. CREATE TABLE Enrolled CREATE TABLE Students (sid CHAR(20), (sid CHAR(20), cid CHAR(20), name CHAR(20), grade CHAR(2)) login CHAR(10), age INTEGER, gpa FLOAT) Adding and Deleting Tuples Keys • Can insert a single tuple using: • Keys are a way to associate tuples in INSERT INTO Students (sid, name, login, age, gpa) different relations VALUES (‘53688’, ‘Smith’, ‘smith@ee’, 18, 3.2) • Keys are one form of integrity constraint (IC) • Can delete all tuples satisfying some condition (e.g., name = Smith): Enrolled Students sid cid grade DELETE 53666 Carnatic101 C sid name login age gpa 53666 Jones jones@cs 18 3.4 FROM Students S 53666 Reggae203 B 53650 Topology112 A 53688 Smith smith@eecs 18 3.2 WHERE S.name = ‘Smith’ 53666 History105 B 53650 Smith smith@math 19 3.8 Powerful variants of these commands are available; more later! FOREIGN Key PRIMARY Key Primary Keys Primary and Candidate Keys in SQL • Possibly many candidate keys (specified using • A set of fields is a superkey if: UNIQUE), one of which is chosen as the primary key. – No two distinct tuples can have same values in all key fields • A set of fields is a key for a relation if : • Keys must be used carefully! – It is a superkey • “For a given student and course, there is a single grade.” – No subset of the fields is a superkey CREATE TABLE Enrolled • what if >1 key for a relation? CREATE TABLE Enrolled (sid CHAR(20) (sid CHAR(20) – One of the keys is chosen (by DBA) to be the primary key. cid CHAR(20), Other keys are called candidate keys. cid CHAR(20), vs. grade CHAR(2), grade CHAR(2), • E.g. PRIMARY KEY (sid,cid)) PRIMARY KEY (sid), – sid is a key for Students. UNIQUE (cid, grade)) – What about name? – The set {sid, gpa} is a superkey. “Students can take only one course, and no two students in a course receive the same grade.” Foreign Keys, Referential Integrity Foreign Keys in SQL • Foreign key: Set of fields in one relation • E.g. Only students listed in the Students relation that is used to `refer’ to a tuple in another should be allowed to enroll for courses.