You are on page 1of 30

The Relational Model

CS 186, Spring 2007, Lecture 2 Cow book Section 1.5, Chapter 3 Mary Roth

Administrivia
Homework 0
Due next Tuesday, Jan 23 10 p.m. Submission instructions added to homework description Class account forms here if you need them

Discussion sections will meet today Questions?

Outline
What we learned last time
What is and what good is a DBMS anyway? Components of a DBMS

New stuff
A brief history of databases The relational data model

. and some reason for it to be organized in a particular way.Review: What is a database? A collection of data organized for rapid search and retrieval Data collection has some logical meaning.

000 transactions a day You d need a DBMS to: Help you find things fast Help you keep track of what s going on .000.000. Think big and lots of data 300.Review: What is a DBMS? A software system designed to manage a database.000 bank accounts Think mission critical 1.

Consistency you can t peek at the data til it is baked. it remains even if the database crashes . multiple users can operate on a database without conflicting Durability what s done is done.Review: ACID properties A DBMS ensures a database has ACID properties: Atomicity nothing is ever half baked. once a database operation completes. database changes aren t visible til they are commited Isolation concurrent operations have an explainable outcome. database changes either happen or they don t.

it has layers We re going to learn about these layers all semester We re going to build several layers in our homework projects Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB .Review: DBMS components A DBMS is like an ogre.

g.000 accounts! DB .000.000 simultaneous requests! ‡Makes efficient use of disk space -> Think 300. app to withdraw/deposit money or provide a history of the account ‡Figures out the best way to answer a question -> There is always nore than 1 way to skin a cat«! ‡Provides generic ways to combine data -> Do you want a list of customers and accounts or the total account balance of all customers? ‡Provides efficient ways to extract data -> Do you need 1 record or a bunch? ‡Makes efficient use of RAM -> Think 1.000.Review: DBMS componentsfor a specific task ‡Talks to DBMS to manage data Database application Query Optimization and Execution Relational Operators Access Methods Buffer Management Disk Space Management -> e.

2000 Query Optimization and Execution Relational Operators Access Methods Buffer Management Disk Space Management Customer accounts stored on disk .g.Review: How does a DBMS work? Database app Query in: e.g. ³Select min(account balance)´ Data out: e.

Embed database access in a program 4. look at data 3. Graphically compose queries. Embed database access in a web application Web browser Command line GUI JDBC/ODBC app App server JDBC/ODBC DBMS . etc. Enter queries.Review: Typical architecture for DB applications 1. By typing text 2.

Efficient data access DBMS is smart about how to retrieve data 3. not how it is stored 2. Centralized administration stored data on single server and let people specialize in managing it 5. Reduced application development time Derived from 1-5 . Data integrity and security DBMS won t let you corrupt data 4. Concurrent access Handles multiple users efficiently and recoverably 6. Data independence applications worry about what data they want.Summary: Benefits of a DBMS 1.

Minibase is a Java-based DBMS Database application Query Optimization and Execution Relational Operators Access Methods Buffer Management Disk Space Management ‡Homework 5 ‡Homework 3 (Pencil-work) ‡Homework 4 ‡Homework 2 ‡Homework 1 ‡Provided for you DB .

Intermission Get up and stretch Ask a quick question Get a drink of water .

but still important! 100.000 bank transactions a day move money through IMS system A bank manages over 300.000 online bank accounts on IMS One production IMS system has been running for over 8 years without down time or a crash .000.000.A brief history of databases Birth of the DBMS parallels adoption of computer over 1960s and 1970s 1960s: IBM introduced IMS 36 years old! Legacy technology.

A brief history of databases 1970: Ted Codd introduced the relational data model Revolutionary idea that spurred a flurry of DBMS activity at IBM (System R. DB2) at Universities like Berkeley (Ingres) at Oracle (it was born!!) Ted Codd won the Turing award in 1981 Larry Ellison became a gillionaire .

gpa:real) 1010111101 . name: string.So what s the big deal about the relational data model? What is the first benefit of a DBMS? Data independence A Data Model is key to data independence It s the link that provides an abstraction between user s view of the world and bits stored in computer Student (sid: string. login: string. age: integer.

. you often had to bring the whole system down Changes had to occur over scheduled system down time. Slow! Annoying! Expensive! The relational model changed all that.So what s the big deal about the relational data model? It is now the most widely used data model. the application had to change If you wanted to change the layout. Before 1970. there were other data models Network Hierarchical (IMS) But they didn t really provide data independence If the data layout changed.

Students(sid: string. login: string. e. #rows = cardinality #fields = degree / arity You can think of a relation as a set of rows or tuples.e. gpa: real) Instance : a table. name: string. age: integer. Relation: made up of 2 parts: Schema : specifies name of relation. with rows and columns. plus name and type of each column. (It s basically a spread sheet!) i.Relational Database: Definitions Relational database: a set of relations.g. all rows are distinct ..

all rows distinct ‡ Do all values in each column of a relation instance have to be distinct? .8 ‡ Cardinality = 3.Ex: Instance of Students Relation sid 53666 53688 53650 name login Jones jones@cs Smith smith@eecs Smith smith@math age 18 18 19 gpa 3. arity = 5 .4 3.2 3.

security.A language for Relational DBs SQL (a. modify. standard language Data Definition Language (DDL) create. etc. modify.SQL .k.a. Data Manipulation Language (DML) Specify queries to find tuples that satisfy criteria add. remove tuples . Sequel ). delete relations specify constraints administer users.

INSERT INTO <name> (<field names>) VALUES (<field values>) DELETE FROM <name> WHERE <condition> UPDATE <name> SET <field name> = <value> WHERE <condition> SELECT <fields> FROM <name> WHERE <condition> ) .SQL Overview CREATE TABLE <name> ( <field> <domain>.

gpa FLOAT) . name CHAR(20). and enforced by the DBMS whenever tuples are added or modified.Creating Relations in SQL Creates the Students relation. age INTEGER. Note: the type of each field is specified. login CHAR(10). CREATE TABLE Students (sid CHAR(20).

cid CHAR(20). grade CHAR(2)) .Table Creation (continued) Another example: the Enrolled table holds information about courses students take. CREATE TABLE Enrolled (sid CHAR(20).

name = µSmith¶ Powerful variants of these commands are available.2) Can delete all tuples satisfying some condition (e.g.µsmith@ee¶. name = Smith): DELETE FROM Students S WHERE S. more later! .gpa) VALUES (µ53688¶.Adding and Deleting Tuples Can insert a single tuple using: INSERT INTO Students (sid.3..age.µSmith¶.18.login.name.

4 3.8 FOREIGN Key PRIMARY Key .Keys Keys are a way to associate tuples in different relations Enrolled sid 53666 53666 53650 53666 cid grade Carnatic101 C Reggae203 B Topology112 A History105 B Students sid 53666 53688 53650 name login Jones jones@cs Smith smith@eecs Smith smith@math age 18 18 19 gpa 3.2 3.

Keys are the key to data independence! Big improvement over the hierarchical model Relationships are determined by field value. not physical pointers! .

Keys are the key to data independence! Let s enroll Smith in EECS in CS186 With hierarchical model 53688 Smith smith@eecs 18 3.2 CS186 A IMS requires A change to add a field for CS186 A change to Smith s record to have him point to the new field .

2 3.8 Relation model only requires A data change to add a new row to Enrolled table .Keys are the key to data independence! Let s enroll Smith in EECS in CS186 With relational model Enrolled sid 53666 53666 53650 53666 53688 cid grade Carnatic101 C Reggae203 B Topology112 A History105 B CS186 A Students sid 53666 53688 53650 name login Jones jones@cs Smith smith@eecs Smith smith@math age 18 18 19 gpa 3.4 3.

Let s return to our bank Can we apply a relational model to our bank spreadsheet? .

Exercises to test your understanding Write the DDL for our bank tables. . Write a SQL query (DML) that withdraws $300 from Frodo s account. Include primary and foreign key definitions Write a SQL query (DML) that returns the names and account balances for all customers that have an account balance > 2500.