DOC PREVIEW
Berkeley COMPSCI 162 - Data and Queries in the Relational Model

This preview shows page 1-2-3-4 out of 12 pages.

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

Unformatted text preview:

4/6/11 1 Data and Queries in the Relational Model CS 162 Guest Lecture Mike Franklin April 6, 2011 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) Data Models – Describing Data • A Database design encodes some portion of the real world. • A Data Model is a set of concepts for thinking about this encoding. • Many models have been proposed. 1010111101 Student (sid: string, name: string, login: string, age: integer, gpa:real) • We will look at two related models: i) Entity-Relationship (graphical) ii) Relational (implementation) Steps in Database Design • Requirements Analysis  user needs; what must the database capture? • Conceptual Design  high level description (often done w/ER model) • Logical Design  translate ER into DBMS data model • Typically: “relational” model as implemented by SQL • Schema Refinement - consistency, normalization • Physical Design - indexes, disk layout • Security Design - who accesses what, and how Conceptual Design using ER • What are the entities and relationships? • What info about E’s & R’s should be in DB? • What integrity constraints (business rules) hold? • ER diagram is a representation of the `schema’ • Can map an ER diagram into a relational schema. • Conceptual design is where the SW/data engineering begins  Rails “models”4/6/11 2 ER Example An employee can work in many departments; a dept can have many employees. 1-to-1 Many-to-Many since Manages dname budget did Departments since Works_In lot name ssn Employees In contrast, each dept has at most one manager, according to the key constraint on Manages. 1-to-Many Many-to-1 Participation Constraints • Does every employee work in a department? • If so: a participation constraint  participation of Employees in Works_In is total (vs. partial)  What if every department has an employee working in it? • Basically means “at least one” lot name dname budget did since name dname budget did since Manages since Departments Employees ssn Works_In Implementation: The Relational Model • The E-R model is not directly implemented by most DBMSs. • Fairly easy to map an E-R design to a Relational Schema • The Relational Model is Ubiquitous  MySQL, PostgreSQL, Oracle, DB2, SQLServer, …  Note: some “Legacy systems” use older models • e.g., IBM’s IMS • Object-oriented concepts have been merged in • Early work: POSTGRES research project at Berkeley • Informix, IBM DB2, Oracle 8i • As has support for XML (semi-structured data)4/6/11 3 Relational Database: Definitions • Relational database: a set of relations • Relation: made up of 2 parts: Schema : specifies name of relation, plus name and type of each column Students(sid: string, name: string, login: string, age: integer, gpa: real) Instance : the actual data at a given time • #rows = cardinality • #fields = degree / arity Some Synonyms Formal Not-so-formal 1 Not-so-formal 2 Relation Tabl e Tup l e Row Record Attribute Column Field Domain Type Ex: Instance of Students Relation sid name login age gpa 536 6 6 Jones jones @c s 18 3.4 536 8 8 Smith smith@e e cs 18 3.2 536 5 0 Smith smith @m ath 19 3.8 • Cardinality = 3, arity = 5 , all rows distinct • Do all values in each column of a relation instance have to be distinct? SQL - A language for Relational DBs • Say: “ess-cue-ell” or “sequel”  But spelled “SQL” • Data Definition Language (DDL)  create, modify, delete relations  specify constraints  administer users, security, etc. • Data Manipulation Language (DML)  Specify queries to find tuples that satisfy criteria  add, modify, remove tuples4/6/11 4 Creating Relations in SQL • Create the Students relation: CREATE TABLE Students (sid CHAR(20), name CHAR(20), login CHAR(10), age INTEGER, gpa FLOAT) Table Creation (continued) • Another example: the Enrolled table holds information about courses students take. CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(2)) Constraints - Keys • Keys are a way to associate tuples in different relations • Keys are one form of integrity constraint (IC) sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 18 3.2 53650 Smith smith@math 19 3.8 sid cid grade 53666 Carnatic101 C 53666 Reggae203 B 53650 Topology112 A 53666 History105 B Enrolled Students PRIMARY Key FOREIGN Key Primary and Candidate Keys in SQL • Possibly many candidate keys (specified using UNIQUE), one of which is chosen as the primary key. • Keys must be used carefully! • “For a given student and course, there is a single grade.” “Students can take only one course, and no two students in a course receive the same grade.” CREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid)) CREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid), UNIQUE (cid, grade)) vs.4/6/11 5 Foreign Keys, Referential Integrity • Foreign key: a “logical pointer”  Set of fields in a tuple in one relation that `refer’ to a tuple in another relation.  Reference to primary key of the other relation. • All foreign key constraints enforced?  referential integrity!  i.e., no dangling references. Foreign Keys in SQL • E.g. Only students listed in the Students relation should be allowed to enroll for courses.  sid is a foreign key referring to Students: CREATE TABLE Enrolled (sid CHAR(20),cid CHAR(20),grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students); sid cid grade 53666 Carnatic101 C 53666 Reggae203 B 53650 Topology112 A 53666 History105 B Enrolled sid name login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 18 3.2 53650 Smith smith@math 19 3.8 Students 11111 English102 A Enforcing Referential Integrity • sid in Enrolled: foreign key referencing Students. • Scenarios:  Insert Enrolled tuple with non-existent student id?  Delete a


View Full Document

Berkeley COMPSCI 162 - Data and Queries in the Relational Model

Documents in this Course
Lecture 1

Lecture 1

12 pages

Nachos

Nachos

41 pages

Security

Security

39 pages

Load more
Download Data and Queries in the Relational Model
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 Data and Queries in the Relational Model 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 Data and Queries in the Relational Model 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?