<<

Why Study the ? • Most widely used model. The Relational Model – Vendors: IBM, Informix, Microsoft, Oracle, CS 186, Fall 2002, Lecture 4 Sybase, etc. ="tag">R & G, Chap. 3 • “Legacy systems” in older models – e.g., IBM’s IMS Mine eye hath play’d the painter and hath stell’d • Object-oriented concepts have recently Thy beauty’s form in of my heart. merged in – object-relational model Shakespeare, Sonnet XXIV • Informix, IBM DB2, Oracle 8i • Early work done in POSTGRES research project at Berkeley

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

SQL - A language for Relational DBs SQL Overview •CREATE TABLE ( , … )

• SQL (a.k.a. “Sequel”), standard language •INSERT INTO () • Definition Language (DDL) VALUES () – create, modify, relations •DELETE FROM – specify constraints WHERE – administer users, security, etc. • Data Manipulation Language (DML) •UPDATE SET = –Specify queries to find tuples that satisfy WHERE criteria – add, modify, remove tuples •SELECT FROM WHERE

1 Creating Relations in SQL Table Creation (continued) • Creates the Students relation. – Note: the type (domain) of each field is • Another example: the Enrolled table holds specified, and enforced by the DBMS about courses students take. whenever tuples are added or modified. CREATE TABLE Enrolled CREATE TABLE Students (sid CHAR(20), (sid CHAR(20), cid CHAR(20), name CHAR(20), grade CHAR(2)) login CHAR(10), age INTEGER, gpa FLOAT)

Adding and Deleting Tuples Keys

• Can a single using: • Keys are a way to associate tuples in INSERT INTO Students (sid, name, login, age, gpa) different relations VALUES (‘53688’, ‘Smith’, ‘smith@ee’, 18, 3.2) • Keys are one form of integrity constraint (IC) • Can delete all tuples satisfying some condition (e.g., name = Smith): Enrolled Students sid cid grade DELETE 53666 Carnatic101 C sid name login age gpa 53666 Jones jones@cs 18 3.4 FROM Students S 53666 Reggae203 B 53650 Topology112 A 53688 Smith smith@eecs 18 3.2 WHERE S.name = ‘Smith’ 53666 History105 B 53650 Smith smith@math 19 3.8

Powerful variants of these commands are available; more later! FORIEGN Key

Primary Keys Primary and Candidate Keys in SQL

• A set of fields is a if: • Possibly many candidate keys (specified using – No two distinct tuples can have same values in all key UNIQUE), one of which is chosen as the primary key. fields • Keys must be used carefully! • A set of fields is a key for a relation if : • “For a given student and course, there is a single grade.” –It is a superkey – No of the fields is a superkey CREATE TABLE Enrolled CREATE TABLE Enrolled (sid CHAR(20) • what if >1 key for a relation? (sid CHAR(20) cid CHAR(20), vs. cid CHAR(20), – one of the keys is chosen (by DBA) to be the primary grade CHAR(2), key. Other keys are called candidate keys. grade CHAR(2), PRIMARY KEY (sid,cid)) PRIMARY KEY (sid), • E.g. UNIQUE (cid, grade)) – sid is a key for Students. “Students can take only one course, and no two students – What about name? in a course receive the same grade.” –The set {sid, gpa} is a superkey.

2 Foreign Keys, Foreign Keys in SQL

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

Enforcing Referential Integrity Integrity Constraints (ICs)

• IC: condition that must be true for any • Consider Students and Enrolled; sid in Enrolled is a instance of the database; e.g., domain foreign key that references Students. constraints. • What should be done if an Enrolled tuple with a non- – ICs are specified when schema is defined. existent student id is inserted? (Reject it!) – ICs are checked when relations are • What should be done if a Students tuple is deleted? modified. – Also delete all Enrolled tuples that refer to it? • A legal instance of a relation is one that – Disallow deletion of a Students tuple that is referred to? satisfies all specified ICs. – Set sid in Enrolled tuples that refer to it to a default sid? – DBMS should not allow illegal instances. – (In SQL, also: Set sid in Enrolled tuples that refer to it to a special value , denoting `unknown’ or `inapplicable’.) • If the DBMS checks ICs, stored data is more faithful to real-world meaning. • Similar issues arise if primary key of Students tuple is updated. – Avoids data entry errors, too!

Where do ICs Come From? Logical DB Design: ER to Relational ssn name lot • ICs are based upon the semantics of the real-world • Entity sets to tables. that is being described in the database relations. 123-22-3666 Attishoo 48 name 231-31-5368 Smiley 22 • We can check a database instance to see if an IC is ssn lot violated, but we can NEVER infer that an IC is true 131-24-3650 Smethurst 35 by looking at an instance. Employees – 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. CREATE TABLE Employees • Key and foreign key ICs are the most common; (ssn CHAR(11), more general ICs supported too. name CHAR(20), lot INTEGER, PRIMARY KEY (ssn))

3 Relationship Sets to Tables Review: Key Constraints CREATE TABLE Works_In( • Each dept has at • In translating a many-to- ssn CHAR(1), most one since many relationship set to a did INTEGER, name dname manager, since DATE, ssn lot did budget relation, attributes of the according to the PRIMARY KEY (ssn, did), relation must include: key constraint on FOREIGN KEY (ssn) Manages. Employees Manages Departments 1) Keys for each REFERENCES Employees, participating entity set FOREIGN KEY (did) (as foreign keys). This REFERENCES Departments) set of attributes forms ssn did since a superkey for the Translation to relation. 123-22-3666 51 1/1/91 relational model? 123-22-3666 56 3/3/93 2) All descriptive 1-to-1 1-to Many Many-to-1 Many-to-Many attributes. 231-31-5368 51 2/2/92

Translating ER with Key Constraints Review: Participation Constraints since name dname • Does every department have a manager? ssn lot did budget – If so, this is a participation constraint: the participation of Employees Manages Departments Departments in Manages is said to be total (vs. partial). • Since each department has a unique manager, we •Every did value in Departments table must appear in a could instead combine Manages and Departments. of the Manages table (with a non-null ssn value!)

CREATE TABLE Manages( CREATE TABLE Dept_Mgr( since ssn CHAR(11), did INTEGER, name dname ssn lot did budget did INTEGER, dname CHAR(20), since DATE, Vs. budget REAL, Employees Manages Departments PRIMARY KEY (did), ssn CHAR(11),

FOREIGN KEY (ssn) since DATE, Works_In REFERENCES Employees, PRIMARY KEY (did), FOREIGN KEY (did) FOREIGN KEY (ssn) REFERENCES Departments) REFERENCES Employees) since

Participation Constraints in SQL Review: Weak Entities • We can capture participation constraints involving one • A weak entity can be identified uniquely only by 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 one-to-many relationship set (1 owner, many weak CREATE TABLE Dept_Mgr( entities). did INTEGER, dname CHAR(20), – Weak entity set must have total participation in this budget REAL, identifying relationship set. 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)

4 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  – When the owner entity is deleted, all owned weak As in C++, or other PLs, contractid entities must also be deleted. attributes are inherited. 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, • Overlap constraints: Can Joe be an Hourly_Emps as well as a cost REAL, Contract_Emps entity? (Allowed/disallowed) ssn CHAR(11) NOT NULL, • Covering constraints: Does every Employees entity also have PRIMARY KEY (pname, ssn), to be an Hourly_Emps or a Contract_Emps entity? (Yes/no) FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE CASCADE)

Translating ISA Hierarchies to Relations Relational Model: Summary • A tabular representation of data. • General approach: – 3 relations: Employees, Hourly_Emps and Contract_Emps. • Simple and intuitive, currently the most widely • Hourly_Emps: Every employee is recorded in used Employees. For hourly emps, extra info recorded in – Object-relational variant gaining ground Hourly_Emps (hourly_wages, hours_worked, ssn); must • Integrity constraints can be specified by the delete Hourly_Emps tuple if referenced Employees tuple is deleted). DBA, based on application semantics. DBMS checks for violations. • Queries involving all employees easy, those involving just Hourly_Emps require a to get some attributes. – Two important ICs: primary and foreign keys • Alternative: Just Hourly_Emps and Contract_Emps. – In addition, we always have domain – Hourly_Emps: ssn, name, lot, hourly_wages, constraints. hours_worked. • Mapping ER to Relational is (fairly) – Each employee must be in one of these two subclasses. straightforward. • Next: Powerful query languages exist.

5