COP 5725 Fall 2012
Database Management
Systems
University of Florida, CI SE Department Prof. Daisy Zhe Wang
The Relational Data Model
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 aboutdata. (e.g., translated from E/ R model) • Based on Set Theory: solid
Relational Database: Definitions
•
Relational database
:
a set ofrelations
•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).
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
Relational Query Languages
• A major strength of the relational model: supports simple, powerful
querying
of data with precise semanticsThe SQL Query Language
– SQL-89 (minor revision) – SQL-92 (major revision)
– SQL-99 (major extensions, current
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
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:
Creating (Declaring) a Relation
• Simplest form is:
CREATE TABLE < name> ( < list of elements>
);
• To delete a relation:
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.
Example: Create Table
CREATE TABLE Sells (
bar
CHAR(20),
beer
VARCHAR(20),
price
REAL
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),
Dates and Times
• DATE and TI ME are types in SQL. • The form of a date value is:
DATE ’yyyy-mm-dd’
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.
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)
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:
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!
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.
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
Declaring Key Constraints
• An attribute or list of attributes may be declared PRI MARY KEY or UNI QUE.
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)
Declaring Multi-attribute Keys
• A key declaration can also be another element in the list of elements of a CREATE TABLE
statement.
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
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.
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
Example: Default Values
CREATE TABLE Drinkers (
name CHAR(30) PRIMARY KEY,
addr CHAR(50)
DEFAULT ’123 Sesame St.’,
phone CHAR(16)
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)
Effect of Defaults --- (2)
• But what tuple appears in Drinkers?
name
addr
phone
Sally 123 Sesame St NULL
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),
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)
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
Enforcing Referential I ntegrity
• Consider Students and Enrolled;
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
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
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
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
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(
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
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,
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
Key Constraints on relationships
Translation to relational model?
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
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.
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
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,
Review: Weak Entities
• A
weak entity
can be identified uniquely only by considering the primary key of anotherTranslating Weak Entity Sets
• Weak entity set and identifying
relationship set are translated into a single table.
CREATE TABLE Dep_Policy (
Subclasses: Three Approaches
1. Object-oriented
: One relation per subset ofsubclasses, with all relevant attributes.
2. Use nulls
: One relation; entities have NULLin attributes that don’t belong to them.
3. E/ R style
: One relation for each subclass:– Key attribute(s).
Example
Beers
Ales isa
name manf
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.”
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.”
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.”
A Complicated Example
policyid cost Policies Purchaser
name
Employees
Binary vs. Ternary Relationships
(Contd.)
CREATE TABLE Dependents (
pname CHAR(20),
policyid cost Policies Purchaser
name
Employees
Views
• A
view
is just a relation, but we store adefinition
, 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
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
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.