<<

Why Study the ?

The Relational Model ™ Most widely used model. ƒ Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. ™ “Legacy systems” in older models Chapter 3 ƒ E.G., IBM’s IMS ™ Recent competitor: object-oriented model ƒ ObjectStore, Versant, Ontos ƒ A synthesis emerging: object-relational model • Informix Universal Server, UniSQL, O2, Oracle, DB2

Database Management Systems 3ed, ="tag">R. Ramakrishnan and J. Gehrke 1 Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2

Relational Database: Definitions Example Instance of Students

™ : a of relations sid name login age gpa ™ Relation: made up of 2 parts: 53666 Jones jones@cs 18 3.4 ƒ Instance : a , with rows and columns. #Rows = cardinality, #fields = degree / arity. 53688 Smith smith@eecs 18 3.2 ƒ Schema : specifies name of relation, plus name and 53650 Smith smith@math 19 3.8 type of each . • E.G. Students(sid: string, name: string, login: string, age: integer, gpa: real). ™ Cardinality = 3, degree = 5, all rows distinct ™ Can think of a relation as a set of rows or ™ Do all columns in a relation instance have to (i.e., all rows are distinct). be distinct?

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4

Relational Query Languages The SQL

™ A major strength of the relational model: ™ Developed by IBM (system R) in the 1970s supports simple, powerful querying of . ™ Need for a standard since it is used by many ™ Queries can be written intuitively, and the vendors DBMS is responsible for efficient evaluation. ™ Standards: ƒ The key: precise semantics for relational queries. ƒ SQL-86 ƒ Allows the optimizer to extensively re-order ƒ SQL-89 (minor revision) operations, and still ensure that the answer does ƒ SQL-92 (major revision) not change. ƒ SQL-99 (major extensions, current standard)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6 The SQL Query Language Querying Multiple Relations

™ What does the following query compute? ™ To find all 18 year old students, we can write: SELECT S.name, E.cid FROM Students S, Enrolled E SELECT * sid name login age gpa WHERE S.sid=E.sid AND E.grade=“A” FROM Students S 53666 Jones jones@cs 18 3.4 WHERE S.age=18 53688 Smith smith@ee 18 3.2 Given the following instance sid cid grade of Enrolled (is this possible if 53831 Carnatic101 C 53831 Reggae203 B •To find just names and logins, replace the first line: the DBMS ensures referential 53650 Topology112 A integrity?): SELECT S.name, S.login 53666 History105 B

we get: S.name E.cid Smith Topology112

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8

Creating Relations in SQL Destroying and Altering Relations

™ Creates the Students CREATE TABLE Students DROP TABLE Students relation. Observe that the (sid: CHAR(20), name: CHAR(20), type (domain) of each field ™ Destroys the relation Students. The schema login: CHAR(10), is specified, and enforced by and the tuples are deleted. age: INTEGER, the DBMS whenever tuples gpa: REAL) are added or modified. ALTER TABLE Students ™ As another example, the ADD COLUMN firstYear: integer Enrolled table holds CREATE TABLE Enrolled (sid: CHAR(20), ™ The schema of Students is altered by adding a information about courses cid: CHAR(20), new field; every in the current instance that students take. grade: CHAR(2)) is extended with a value in the new field.

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 9 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10

Adding and Deleting Tuples Integrity Constraints (ICs)

™ Can a single tuple using: ™ IC: condition that must be true for any instance of the database; e.g., domain constraints. INSERT INTO Students (sid, name, login, age, gpa) ƒ ICs are specified when schema is defined. VALUES (53688, ‘Smith’, ‘smith@ee’, 18, 3.2) ƒ ICs are checked when relations are modified. ™ Can all tuples satisfying some ™ A legal instance of a relation is one that satisfies condition (e.g., name = Smith): all specified ICs. ƒ DELETE DBMS should not allow illegal instances. FROM Students S ™ If the DBMS checks ICs, stored data is more WHERE S.name = ‘Smith’ faithful to real-world meaning. ƒ Avoids data entry errors, too! * Powerful variants of these commands are available; more later! Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 11 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12 Constraints Primary and Candidate Keys in SQL

™ Possibly many candidate keys (specified using ™ A set of fields is a key for a relation if : UNIQUE), one of which is chosen as the primary key. 1. No two distinct tuples can have same values in all ™ “For a given student and course, CREATE TABLE Enrolled key fields, and there is a single grade.” vs. (sid CHAR(20) 2. This is not true for any of the key. “Students can take only one cid CHAR(20), grade CHAR(2), ƒ Part 2 false? A . course, and receive a single grade for that course; further, no two PRIMARY KEY (sid,cid) ) ƒ If there’s >1 key for a relation, one of the keys is students in a course receive the CREATE TABLE Enrolled chosen (by DBA) to be the primary key. same grade.” (sid CHAR(20) ™ E.g., sid is a key for Students. (What about ™ Used carelessly, an IC can prevent cid CHAR(20), name?) The set {sid, gpa} is a superkey. the storage of database instances grade CHAR(2), that arise in practice! PRIMARY KEY (sid), UNIQUE (cid, grade) ) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14

Foreign Keys, Foreign Keys in SQL

™ Only students listed in the Students relation should ™ : Set of fields in one relation that is used be allowed to enroll for courses. to `refer’ to a tuple in another relation. (Must correspond to primary key of the second relation.) CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(2), Like a `logical pointer’. PRIMARY KEY (sid,cid), ™ E.g. sid is a foreign key referring to Students: FOREIGN KEY (sid) REFERENCES Students ) ƒ Enrolled(sid: string, cid: string, grade: string) Enrolled sid cid grade Students ƒ If all foreign key constraints are enforced, referential 53666 Carnatic101 C sid name login age gpa integrity is achieved, i.e., no dangling references. 53666 Reggae203 B 53666 Jones jones@cs 18 3.4 ƒ Can you name a w/o referential integrity? 53650 Topology112 A 53688 Smith smith@eecs 18 3.2 • Links in HTML! 53666 History105 B 53650 Smith smith@math 19 3.8

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 15 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 16

Enforcing Referential Integrity Referential Integrity in SQL

™ Consider Students and Enrolled; sid in Enrolled is a ™ SQL/92 and SQL:1999 foreign key that references Students. CREATE TABLE Enrolled support all 4 options on (sid CHAR(20), ™ What should be done if an Enrolled tuple with a deletes and updates. cid CHAR(20), non-existent student id is inserted? (Reject it!) ƒ Default is NO ACTION grade CHAR(2), ™ What should be done if a Students tuple is deleted? (delete/ is rejected) PRIMARY KEY (sid,cid), ƒ Also delete all Enrolled tuples that refer to it. ƒ CASCADE (also delete FOREIGN KEY (sid) REFERENCES Students ƒ Disallow deletion of a Students tuple that is referred to. all tuples that refer to ON DELETE CASCADE ƒ Set sid in Enrolled tuples that refer to it to a default sid. deleted tuple) ON UPDATE SET DEFAULT ) ƒ (In SQL, also: Set sid in Enrolled tuples that refer to it to a ƒ SET NULL / SET DEFAULT special value null, denoting `unknown’ or `inapplicable’.) (sets foreign key value ™ Similar if primary key of Students tuple is updated. of referencing tuple)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 17 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 18 Where do ICs Come From? Logical DB Design: ER to Relational

™ ICs are based upon the semantics of the real- world enterprise that is being described in the ™ Entity sets to tables: database relations. ™ We can check a database instance to see if an CREATE TABLE Employees IC is violated, but we can NEVER infer that name (ssn CHAR(11), an IC is true by looking at an instance. ssn lot name CHAR(20), lot INTEGER, ƒ An IC is a statement about all possible instances! Employees PRIMARY KEY (ssn)) ƒ From example, we know name is not a key, but the assertion that sid is a key is given to us. ™ Key and foreign key ICs are the most common; more general ICs supported too. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 19 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 20

Relationship Sets to Tables Review: Key Constraints

since ™ In translating a relationship name dname CREATE TABLE Works_In( ™ Each dept has at set to a relation, attributes of ssn lot did budget ssn CHAR(1), most one manager, the relation must include: did INTEGER, according to the ƒ Keys for each since DATE, key constraint on Employees Manages Departments participating entity set PRIMARY KEY (ssn, did), Manages. (as foreign keys). FOREIGN KEY (ssn) • This set of attributes REFERENCES Employees, forms a superkey for FOREIGN KEY (did) the relation. REFERENCES Departments) Translation to ƒ All descriptive attributes. relational model?

1-to-1 1-to Many Many-to-1 Many-to-Many

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 21 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 22

Translating ER Diagrams with Key Constraints Review: Participation Constraints

CREATE TABLE Manages( ™ Map relationship to a ssn CHAR(11), ™ Does every department have a manager? table: did INTEGER, ƒ If so, this is a participation constraint: the participation of ƒ Note that did is since DATE, PRIMARY KEY Departments in Manages is said to be total (vs. partial). the key now! (did), FOREIGN KEY (ssn) REFERENCES Employees, •Every did value in Departments table must appear in a ƒ Separate tables for FOREIGN KEY (did) REFERENCES Departments) Employees and of the Manages table (with a non-null ssn value!) Departments. since CREATE TABLE Dept_Mgr( name dname ™ Since each did INTEGER, ssn lot did budget department has a dname CHAR(20), unique manager, we budget REAL, Employees Manages Departments could instead ssn CHAR(11), since DATE, combine Manages PRIMARY KEY (did), Works_In and Departments. FOREIGN KEY (ssn) REFERENCES Employees)

since Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 23 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 24 Participation Constraints in SQL Review: Weak Entities

™ We can capture participation constraints involving ™ A weak entity can be identified uniquely only by one entity set in a binary relationship, but little else considering the primary key of another (owner) entity. (without resorting to CHECK constraints). ƒ Owner entity set and weak entity set must participate in a CREATE TABLE Dept_Mgr( one-to-many relationship set (1 owner, many weak entities). did INTEGER, ƒ Weak entity set must have total participation in this dname CHAR(20), identifying relationship set. budget REAL, ssn CHAR(11) NOT NULL, name cost since DATE, ssn lot pname age PRIMARY KEY (did),

FOREIGN KEY (ssn) REFERENCES Employees, Employees Policy Dependents ON DELETE NO ACTION)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 25 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 26

name ssn lot

Translating Weak Entity Sets Review: ISA Hierarchies Employees ™ Weak entity set and identifying relationship hourly_wages hours_worked set are translated into a single table. ISA ™ As in C++, or other PLs, contractid ƒ When the owner entity is deleted, all owned weak attributes are inherited. entities must also be deleted. Hourly_Emps Contract_Emps ™ If we declare A ISA B, every A CREATE TABLE Dep_Policy ( entity is also considered to be a B pname CHAR(20), entity. age INTEGER, cost REAL, ™ Overlap constraints: Can Joe be an Hourly_Emps as well as ssn CHAR(11) NOT NULL, a Contract_Emps entity? (Allowed/disallowed) PRIMARY KEY (pname, ssn), ™ Covering constraints: Does every Employees entity also have FOREIGN KEY (ssn) REFERENCES Employees, to be an Hourly_Emps or a Contract_Emps entity? (Yes/no) ON DELETE CASCADE)

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 27 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 28

Review: Binary vs. Ternary Translating ISA Hierarchies to Relations Relationships name ssn lot pname age ™ General approach: ƒ 3 relations: Employees, Hourly_Emps and Contract_Emps. Employees Covers Dependents • Hourly_Emps: Every employee is recorded in ™ What are the Bad design Policies Employees. For hourly emps, extra info recorded in additional Hourly_Emps (hourly_wages, hours_worked, ssn); must policyid cost delete Hourly_Emps tuple if referenced Employees constraints in name pname age tuple is deleted). the 2nd ssn lot diagram? Dependents • Queries involving all employees easy, those involving Employees just Hourly_Emps require a to get some attributes. Purchaser ™ Alternative: Just Hourly_Emps and Contract_Emps. Beneficiary ƒ Hourly_Emps: ssn, name, lot, hourly_wages, hours_worked. Better design ƒ Each employee must be in one of these two subclasses. Policies

policyid cost Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 29 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 30 Binary vs. Ternary Relationships (Contd.) Views CREATE TABLE Policies ( ™ The key policyid INTEGER, ™ constraints allow cost REAL, A is just a relation, but we store a us to combine ssn CHAR(11) NOT NULL, definition, rather than a set of tuples. Purchaser with PRIMARY KEY (policyid). Policies and CREATE VIEW YoungActiveStudents (name, grade) FOREIGN KEY (ssn) REFERENCES Employees, Beneficiary with AS SELECT S.name, E.grade Dependents. ON DELETE CASCADE) FROM Students S, Enrolled E ™ Participation CREATE TABLE Dependents ( WHERE S.sid = E.sid and S.age<21 constraints lead to pname CHAR(20), NOT NULL age INTEGER, ™ Views can be dropped using the DROP VIEW command. constraints. policyid INTEGER, ƒ ™ How to handle DROP TABLE if there’s a view on the table? What if Policies is PRIMARY KEY (pname, policyid). a weak entity set? • DROP TABLE command has options to let the user specify FOREIGN KEY (policyid) REFERENCES Policies, this. ON DELETE CASCADE) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 31 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 32

Views and Security Relational Model: Summary

™ Views can be used to present necessary ™ A tabular representation of data. information (or a summary), while hiding ™ Simple and intuitive, currently the most widely used. details in underlying relation(s). ™ Integrity constraints can be specified by the DBA, ƒ Given YoungStudents, but not Students or based on application semantics. DBMS checks for Enrolled, we can find students s who have are violations. enrolled, but not the cid’s of the courses they are ƒ Two important ICs: primary and foreign keys enrolled in. ƒ In addition, we always have domain constraints. ™ Powerful and natural query languages exist. ™ Rules to translate ER to relational model

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 33 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 34