Docsity

Lecture Notes on Relational Model - Database Systems | CSCI 585, Study notes of Deductive Database Systems

Partial preview of the text

Download Lecture Notes on Relational Model - Database Systems | CSCI 585 and more Study notes Deductive Database Systems in PDF only on Docsity!

USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani 1/28/

The Relational Model

Excerpt from

Chapter 3, “Database Management Systems” 3ed, R. Ramakrishnan and J. Gehrke

USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani

Why Study the Relational Model?^ ™ 1/28/

Most widely used model.^ ƒ^ Vendors: IBM, Informix, Microsoft, Oracle,Sybase, etc. ™ Recent competitor: object-oriented model^ ƒ^ ObjectStore, Versant, Ontos^ ƒ^ A synthesis emerging:

object-relational model

  • Informix Universal Server, UniSQL, O2, Oracle, DB

USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani Example Instance of Students Relation 1/28/

sid^

name^

login^

age^

gpa

Jones^

jones@cs

Smith

smith@eecs

Smith

smith@math

™^ Cardinality = 3, degree = 5, all rows distinct ™^ Do all columns in a relation instance have tobe distinct?

USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani

The SQL Query Language ™ 1/28/

To find all 18 year old students, we can write:SELECT

FROM^

Students S WHERE

S.age= •To find just names and logins, replace the first line:SELECT

S.name, S.login

sid^ name

login

age gpa 53666 Jones

jones@cs

18 3.

53688 Smith

smith@ee

18 3.

USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani

Destroying and Altering Relations ™ Destroys the relation Students. The schemainformation 1/28/

and^ the tuples are deleted.

DROP TABLE

Students

™^ The schema of Students is altered by adding anew field; every tuple in the current instanceis extended with a

null^ value in the new field.

ALTER TABLE

StudentsADD COLUMN

firstYear: integer

USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani

Adding and Deleting Tuples ™ 1/28/

Can insert a single tuple using:INSERT INTO

Students (sid, name, login, age, gpa) VALUES

(53688, ‘Smith’, ‘smith@ee’, 18, 3.2)

™^ Can delete all tuples satisfying somecondition (e.g., name = Smith):

DELETEFROM^ Students SWHERE

S.name = ‘Smith’

*^ Powerful variants of these commands are available; more later!

USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani

Where do ICs Come From? ™ 1/28/

ICs are based upon the semantics of the real-world enterprise that is being described in thedatabase relations. ™ We can check a database instance to see if anIC is violated, but we can NEVER infer thatan IC is true by looking at an instance.^ ƒ^ An IC is a statement about

all possible

instances!

ƒ^ From example, we know

name^ is not a key, but the

assertion that

sid^ is a key is given to us.

USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani

Primary Key Constraints ™ 1/28/

A set of fields is a

key^ for a relation if :

  1. No two distinct tuples can have same values in allkey fields, and2. This is not true for any subset of the key. ƒ^ Part 2 false? A

superkey

ƒ^ If there’s >1 key for a relation, one of the keys ischosen (by DBA) to be the

primary key

™^ E.g.,

sid^ is a key for Students. (What about name ?) The set {

sid, gpa

} is a superkey.

USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani

Foreign Keys, Referential Integrity ™ Foreign key 1/28/

: Set of fields in one relation that is used

to refer’ to a tuple in another relation. (Mustcorrespond to primary key of the second relation.)Like alogical pointer’. ™ E.g.^ sid

is a foreign key referring to Students:

ƒ^ Enrolled(

sid : string,

cid : string,

grade : string)

ƒ^ If all foreign key constraints are enforced,

referential

integrity

is achieved, i.e., no dangling references. ƒ^ Can you name a data model w/o referential integrity?• Links in HTML!

USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani

Foreign Keys in SQL ™ Only students listed in the Students relation shouldbe allowed to enroll for courses.1/28/

CREATE TABLE

Enrolled (sid^ CHAR

(20), cid

CHAR(20)

, grade

CHAR^ (2),

PRIMARY KEY

(sid,cid), FOREIGN KEY

(sid)^ REFERENCES

Students ) sid^ name

login

age

gpa

53666 Jones

jones@cs

18

53688 Smith

smith@eecs

18

53650 Smith

smith@math

19

sid^

cid^

grade 53666 Carnatic

C 53666 Reggae

B 53650 Topology

A 53666 History

B Enrolled

Students

USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani

Referential Integrity in SQL ™ SQL/92 and SQL:1999support all 4 options ondeletes and updates. ƒ 1/28/

Default is

NO ACTION ( delete/update is rejected

ƒ^ CASCADE

(also delete all tuples that refer todeleted tuple) ƒ SET NULL

/^ SET DEFAULT

(sets foreign key valueof referencing tuple)

CREATE TABLE

Enrolled (sid^ CHAR

cid^ CHAR(20)

grade^ CHAR

PRIMARY KEY

(sid,cid), FOREIGN KEY

(sid) REFERENCES

Students ON DELETE CASCADEON UPDATE SET DEFAULT

USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani

Relational Model: Summary ™ 1/28/

A tabular representation of data. ™ Simple and intuitive, currently the most widely used. ™ Integrity constraints can be specified by the DBA,based on application semantics. DBMS checks forviolations.^ ƒ^ Two important ICs: primary and foreign keys^ ƒ^ In addition, we

always^ have domain constraints. ™^ Powerful and natural query languages exist. ™^ There are rules to translate ER to relational model