• Tidak ada hasil yang ditemukan

COP 5725 Fall 2012 Database Management Systems

N/A
N/A
Protected

Academic year: 2019

Membagikan "COP 5725 Fall 2012 Database Management Systems"

Copied!
61
0
0

Teks penuh

(1)

COP 5725 Fall 2012

Database Management

Systems

University of Florida, CI SE Department Prof. Daisy Zhe Wang

(2)

The Relational Data Model

(3)

Why Study the Relational Model?

• Most widely used model (Edgar F. Codd paper 1969, Turing award 1981)

– Vendors: I BM, I nformix, Microsoft, Oracle,

Sybase, etc.

Often

matches how we think about

data. (e.g., translated from E/ R model) • Based on Set Theory: solid

(4)

Relational Database: Definitions

Relational database

:

a set of

relations

Relation:

made up of 2 parts:

– I nstance : a table, with rows and columns.

cardinality vs. degree / arity.

– Schema : specifies name of relation, plus

name and type of each column.

• E.G. Students(sid: string, name: string, login: string, age: integer, gpa: real).

(5)

Example I nstance of Students

Relation

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

Cardinality = 3, degree = 5, all rows distinct

Do all columns in a relation instance have to

(6)

Relational Query Languages

• A major strength of the relational model: supports simple, powerful

querying

of data with precise semantics

(7)

The SQL Query Language

– SQL-89 (minor revision) – SQL-92 (major revision)

– SQL-99 (major extensions, current

(8)

The SQL Query Language

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

• To find just names and logins, replace the first line:

SELECT S.name, S.login ….

SELECT *

FROM Students S WHERE S.age=18

sid name login age gpa

(9)

Querying Multiple Relations

• What does the following query compute?

SELECT S.name, E.cid

FROM Students S, Enrolled E

WHERE S.sid=E.sid AND E.grade=“A”

S.name E.cid

Smith Topology112

sid cid grade

53831 Carnatic101 C

53831 Reggae203 B

53650 Topology112 A

53666 History105 B

Given the following instances of Enrolled and Students:

we get:

(10)

Creating (Declaring) a Relation

• Simplest form is:

CREATE TABLE < name> ( < list of elements>

);

• To delete a relation:

(11)

Elements of Table Declarations

• Most basic element: an attribute and its type.

• The most common types are:

– I NT or I NTEGER (synonyms). – REAL or FLOAT (synonyms).

– CHAR(n ) = fixed-length string of n characters.

(12)

Example: Create Table

CREATE TABLE Sells (

bar

CHAR(20),

beer

VARCHAR(20),

price

REAL

(13)

More Examples

CREATE TABLE Students

(sid: CHAR(20),

name: CHAR(20),

login: CHAR(10),

age: INTEGER,

gpa: REAL)

CREATE TABLE Enrolled

(sid: CHAR(20),

cid: CHAR(20),

(14)

Dates and Times

• DATE and TI ME are types in SQL. • The form of a date value is:

DATE ’yyyy-mm-dd’

(15)

Times as Values

• The form of a time value is:

TI ME ’hh: mm: ss’

with an optional decimal point and fractions of a second following.

(16)

Adding Attributes

• We may add a new attribute (“column”) to a relation schema by:

ALTER TABLE < name> ADD

< attribute declaration> ;

• Example:

ALTER TABLE Bars ADD phone CHAR(16)

(17)

Deleting Attributes

• Remove an attribute from a relation schema by:

ALTER TABLE < name>

DROP < attribute> ;

• Example: we don’t really need the license attribute for bars:

(18)

Adding and Deleting Tuples

• Can insert a single tuple using:

INSERT INTO Students (sid, name, login, age, gpa) VALUES (53688, ‘Smith’, ‘smith@ee’, 18, 3.2)

DELETE

FROM Students S

WHERE S.name = ‘Smith’

Powerful variants of these commands are available; more later!

(19)

I ntegrity Constraints (I Cs)

• I C: condition that must be true for any instance of the database; e.g., domain constraints.

– I Cs are specified when schema is defined. – I Cs are checked when relations are modified.

• A legal instance of a relation is one that satisfies all specified I Cs.

– DBMS should not allow illegal instances.

• stored data in DB is more faithful to real-world meaning.

(20)

Primary Key Constraints

• A set of fields is a

key

for a relation if :

1. No two distinct tuples can have same values in all key fields, and

(21)

Declaring Key Constraints

• An attribute or list of attributes may be declared PRI MARY KEY or UNI QUE.

(22)

Declaring Single-Attribute Keys

• Place PRI MARY KEY or UNI QUE after the type in the declaration of the attribute. • Example:

CREATE TABLE Beers (

name

CHAR(20) UNIQUE,

manf

CHAR(20)

(23)

Declaring Multi-attribute Keys

• A key declaration can also be another element in the list of elements of a CREATE TABLE

statement.

(24)

PRI MARY KEY Vs. UNI QUE

• Standard SQL requires these

distinctions:

1. There can be only one PRI MARY KEY for a relation, but several UNI QUE attributes. 2. No attribute of a PRI MARY KEY can ever

be NULL in any tuple. But attributes

(25)

PRI MARY KEY Vs. UNI QUE (I I )

• The SQL standard allows DBMS implementers to make their own

distinctions between PRI MARY KEY and UNI QUE.

(26)

Some Other Declarations for

Attributes

1. NOT NULL means that the value for this attribute may never be NULL.

2. DEFAULT < value> says that if there is no specific value known for this

(27)

Example: Default Values

CREATE TABLE Drinkers (

name CHAR(30) PRIMARY KEY,

addr CHAR(50)

DEFAULT ’123 Sesame St.’,

phone CHAR(16)

(28)

Effect of Defaults --- (1)

• Suppose we insert the fact that Sally is a drinker, but we know neither her

address nor her phone.

• An I NSERT with a partial list of

attributes makes the insertion possible:

INSERT INTO Drinkers(name)

(29)

Effect of Defaults --- (2)

• But what tuple appears in Drinkers?

name

addr

phone

Sally 123 Sesame St NULL

(30)

Make sense?

Be careful specifying keys…

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),

(31)

Foreign Keys, Referential I ntegrity

Foreign key

: Set of fields in one relation that is used to ` refer’ to a tuple in another relation.

Enrolled(sid: string, cid: string, grade: string)

(32)

Foreign Keys in SQL

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 3.4 53688 Smith smith@eecs 18 3.2 53650 Smith smith@math 19 3.8

(33)

Enforcing Referential I ntegrity

• Consider Students and Enrolled;

(34)

Enforcing Referential I ntegrity (I I )

• What should be done if a Students tuple is deleted?

– Delete all Enrolled tuples that refer to it.

– Disallow deletion of a Students tuple that is

referred to.

– Set sid in Enrolled tuples that refer to it to

a default sid or null

• What if primary key of Students tuple is

(35)

Referential I ntegrity in SQL

• SQL/ 92 and SQL: 1999 support 4 options on deletes and updates.

– NO ACTI ON (Default) – CASCADE

– SET NULL / SET DEFAULT

CREATE TABLE Enrolled

(sid CHAR(20), ON DELETE CASCADE

(36)

Where do I Cs Come From?

• Key and foreign key I Cs are the most common; more general I Cs supported too.

• From the semantics of the real-world

(37)

Logical DB Design: ER to Relational

• Entity sets to tables:

CREATE TABLE Employees

(ssn CHAR(11),

name CHAR(20),

lot INTEGER,

PRIMARY KEY (ssn)) Employees

(38)

Relationship Sets to Tables

• I n translating a relationship set to a relation, attributes of the relation must include:

– Keys for each

participating entity

set (as foreign keys).

• This set of attributes forms a superkey for the relation.

– All descriptive

attributes.

CREATE TABLE Works_In(

(39)

Combining Relations

Many-One Relationships

• OK to combine into one relation:

1. The relation for an entity-set E

2. The relations for many-one relationships of which E is the “many.”

• Example: Drinkers(name, addr) and

Favorite(drinker, beer) combine to

(40)

Another Example

CREATE TABLE Manages( ssn CHAR(11),

CREATE TABLE Dept_Mgr(

did INTEGER,

Employees Departments

CREATE TABLE Dept(

did INTEGER,

dname CHAR(20), budget REAL,

(41)

Combining Relations

Many-Many Relationships

• Combining Drinkers with Likes would be a mistake. I t leads to redundancy, as:

name addr beer

Sally 123 Maple Bud

Sally 123 Maple Miller

(42)

Key Constraints on relationships

Translation to relational model?

(43)

Weak Entity Sets Example

Logins At Hosts

name name

Hosts(hostName, location)

Logins(loginName, hostName, billTo) At(loginName, hostName)

billTo

At becomes part of Logins

(44)

Handling Weak Entity Sets

• Relation for a weak entity set must

include attributes for its complete key (including those belonging to other

entity sets), as well as its own, nonkey attributes.

(45)

Review: Participation Constraints

• Does every department have a manager?

– I f so, this is a participation constraint: the

participation of Departments in Manages is said to be total (vs. partial).

• Every did value in Departments table must appear in a row of the Manages table (with a non-null ssn value!)

lot

Manages Departments Employees

(46)

Participation Constraints in SQL

CREATE TABLE Dept_Mgr(

did INTEGER,

dname CHAR(20),

budget REAL,

ssn CHAR(11) NOT NULL,

since DATE,

PRIMARY KEY (did),

FOREIGN KEY (ssn) REFERENCES Employees,

(47)

Review: Weak Entities

• A

weak entity

can be identified uniquely only by considering the primary key of another

(48)

Translating Weak Entity Sets

• Weak entity set and identifying

relationship set are translated into a single table.

CREATE TABLE Dep_Policy (

(49)

Subclasses: Three Approaches

1. Object-oriented

: One relation per subset of

subclasses, with all relevant attributes.

2. Use nulls

: One relation; entities have NULL

in attributes that don’t belong to them.

3. E/ R style

: One relation for each subclass:

– Key attribute(s).

(50)

Example

Beers

Ales isa

name manf

(51)

Object-Oriented

name manf

Bud Anheuser-Busch

Beers

name manf color

Summerbrew Pete’s dark

Ales

Q1: “find the color of ales made by Pete’s.”

(52)

E/ R Style

name manf

Bud Anheuser-Busch Summerbrew Pete’s

Beers

name color

Summerbrew dark

Ales

Q1: “find the color of ales made by Pete’s.”

(53)

Using Nulls

name manf color

Bud Anheuser-Busch NULL Summerbrew Pete’s dark

Beers

Saves space unless there are lots of attributes that are Q1: “find the color of ales made by Pete’s.”

(54)

A Complicated Example

policyid cost Policies Purchaser

name

Employees

(55)

Binary vs. Ternary Relationships

(Contd.)

CREATE TABLE Dependents (

pname CHAR(20),

policyid cost Policies Purchaser

name

Employees

(56)

Views

• A

view

is just a relation, but we store a

definition

, rather than a set of tuples.

CREATE VIEW YoungActiveStudents (name, grade) AS SELECT S.name, E.grade

FROM Students S, Enrolled E

WHERE S.sid = E.sid and S.age<21

Views can be dropped using the DROP VIEW command.

How to handle DROP TABLE if there’s a view on the table?

• DROP TABLE command has options to let the user specify

(57)

Views and Security

• Views can be used to present necessary information (or a summary), while

hiding details in underlying relation(s).

– Given YoungStudents, but not Students or

Enrolled, we can find students s who have are enrolled, but not the cid’s of the

(58)

Relational Model: Summary

• A tabular representation of data.

• Simple and intuitive, currently the most widely used. • I ntegrity constraints can be specified by the DBA,

based on application semantics. DBMS checks for violations.

– Two important I Cs: primary and foreign keys – I n addition, we always have domain constraints.

(59)
(60)

Additional Exercise (I I )

(61)

Additional Exercise (I I I )

Referensi

Dokumen terkait

Data rata-rata dari hasil minat siswa antara kelas kontrol yaitu 8D dan kelas perlakuan yaitu 8E menunjukkan dari setiap indikator bahwa kelompok perlakuan

Adapun topik bahasan yang dipilih dalam Laporan Tugas Akhir ini adalah mengenai rangkaian mikrokontroler dan pemrograman bahasa C yang input kendalinya berasal dari water flow

ASUHAN KEPERAWATAN LANJUT USIA Pada Ny.P DENGAN DIAGNOSA MEDIS RHEUMATOID ARTHTRITIS DI UPT PELAYANAN.. SOSIAL LANJUT USIA PASURUAN BABAT

Rencana Terpadu dan Program Investasi Infrastruktur Jangka Menengah (RPI2-JM) Kabupaten Batu Bara merupakan dokumen rencana dan program pembangunan infrastruktur

untuk memenuhi persyaratan memperoleh gelar sarjana kependidikan Program Studi Pendidikan Matematika di Fakultas Keguruan dan Ilmu Pendidikan, Universitas

Skripsi ini disusun untuk memenuhi salah satu syarat dalam menempuh ujian akhir Program Studi S.1 Keperawatan Fakultas Ilmu Kesehatan Universitas Muhammadiyah

Bahan yang baik bagi pertumbuhan Acetobacter xylinum dan pembentukan nata. adalah ekstrak yeast

toxoplasmosis sehingga penulis memberi judul “ IDENTIFIKASI Toxoplasma gondii PADA KOPI LUWAK Di DESA KAYUMAS, KECAMATAN ARJASA,..