12
CSC 280: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012

CSC 280: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012

Embed Size (px)

Citation preview

Page 1: CSC 280: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012

CSC 280: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMSDr. Adam Anthony

Fall 2012

Page 2: CSC 280: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012

Lecture 2

Introduction to the Relational Model Views of Data Definition of a Relation Schemas and Keys

Page 3: CSC 280: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012

Different Views of Data

Disk (physical) level: Where does each bit go on the drive, and how

do we retrieve it when we need it? Logical level:

What is the picture in our head of how the data is arranged?

May or may not differ from reality of the disk level

View level: That portion of the logical level that we can see Sometimes a refined version of the logical level

Page 4: CSC 280: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012

What is a Relation?

UNLEARN: In a database context, a relation is NOT a link between two different entities (e.g. a family tree relation) Though we can represent such data

RE-LEARN: A relation is actually a theoretical collection of potential entities with common attributes Imagine the data to describe a single instructor Imagine a relation for instructor as being a data

table, but you don’t know what instructors are listed in that table yet Any instructor who ever exists MIGHT be in the table

Page 5: CSC 280: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012

A Relation Instance

Given a Relational Definition: Instructor(ID, name, dept_name, salary)

We create a data file with actual data:

IMPORTANT: OPERATIONS DEFINED ON RELATIONS WILL BE INDEPENDENT OF ANY ACTUAL DATA

Page 6: CSC 280: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012

In-Class Exercise

Same groups as last time: Use the relation schema definition format:

relation-name(attribute1,attribute2,…)

To define at least three relations you might see in a music database

Page 7: CSC 280: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012

Terminology

Relation: a data table with defined column types Tuple: a single row in a relation table Attribute: a single column in a relation or tuple Relation Instance: a data table with specific data

entries already in it Order-Independent: the rows in a relation are not

ordered; if the tuples are the same but in different order, the relations are equal.

Domain: indicates the kind of values allowed for an attribute (integer range, characters, date/time, etc) Should be atomic, or, single-valued

Page 8: CSC 280: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012

Keys

Let K R K is a superkey of R if values for K are sufficient to

identify a unique tuple of each possible relation r(R) Example: {ID} and {ID,name} are both superkeys of

instructor.

Superkey K is a candidate key if K is minimalExample: {ID} is a candidate key for Instructor

One of the candidate keys is selected to be the primary key. which one?

Page 9: CSC 280: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012

Find the Keys

Determine good primary keys for part of the university schema (underline your choice): Instructor(ID,name,dept_name,salary) Department(dept_name,building,budget) Student(ID,name,dept_name,tot_cred) Advisor(s_id,i_id) Takes(ID,course_id,sec_id,semester,year,grade) Classroom(building,room_number,capacity) Time_slot(time_slot_id,day,start_time,end_time)

Page 10: CSC 280: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012

Foreign Keys

Foreign key constraint: Value in one relation must appear in another Referencing relation Referenced relation

Page 11: CSC 280: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012

Textbook University Schema Diagram

Page 12: CSC 280: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012

In-Class Activity

Draw a schema diagram for the music database from earlier. Underline primary keys, and use arrows for foreign keys.