The Relational Model Class 2 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) (From ER to Relational)

Slides:



Advertisements
Similar presentations
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Advertisements

Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke. Edited by Keith Shomper, The Relational Model Chapter 3.
CSC 411/511: DBMS Design 1 1 Dr. Nan WangCSC411_L3_Relational Model 1 The Relational Model (Chapter 3)
Database Management Systems 1 Raghu Ramakrishnan The Relational Model Chapter 3 Instructor: Mirsad Hadzikadic.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
D ATABASE S YSTEMS I T HE R ELATIONAL D ATA M ODEL.
The Relational Model Ramakrishnan & Gehrke Chapter 3.
The Relational Model CS 186, Fall 2006, Lecture 2 R & G, Chap. 3.
The Relational Model CS 186, Spring 2006, Lecture 2 R & G, Chap. 1 & 3.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
The Relational Model Ramakrishnan & Gehrke, Chap. 3.
The Relational Model 198:541 Rutgers University. Why Study the Relational Model?  Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle,
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
1 Data Modeling Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 The Relational Model Chapter 3. 2 Objectives  Representing data using the relational model.  Expressing integrity constraints on data.  Creating,
The Relational Model These slides are based on the slides of your text book.
The Relational Model Chapter 3
Relational Data Model, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 The Relational Model Chapter 3.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3 Modified by Donghui Zhang.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model  Vendors: IBM, Informix, Microsoft, Oracle, Sybase  Recent.
 Relational database: a set of relations.  Relation: made up of 2 parts: › Instance : a table, with rows and columns. #rows = cardinality, #fields =
1 The Relational Model Instructor: Mohamed Eltabakh
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1.1 CAS CS 460/660 Relational Model. 1.2 Review E/R Model: Entities, relationships, attributes Cardinalities: 1:1, 1:n, m:1, m:n Keys: superkeys, candidate.
Relational Data Model Ch. 7.1 – 7.3 John Ortiz Lecture 3Relational Data Model2 Why Study Relational Model?  Most widely used model.  Vendors: IBM,
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Levels of Abstraction in DBMS data Many views, single conceptual (logical) schema and physical schema. – Views describe how users see the data (possibly.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
The Relational Model Content based on Chapter 3 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 3 The Relational Model.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
The Relational Model Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY.
Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems 1 Raghu Ramakrishnan The Relational Model Chapter 3 Instructor: Jianping Fan.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Database Management Systems 1 Raghu Ramakrishnan The Relational Model Chapter 3 Instructor: Xin Zhang.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Multi-billion dollar industry, $15+ bill in  Vendors:
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
COP Introduction to Database Structures
CS 186, Fall 2006, Lecture 2 R & G, Chap. 3
Instructor: Mohamed Eltabakh
The Relational Model Content based on Chapter 3
The Relational Model Chapter 3
The Relational Model Relational Data Model
The Relational Model Chapter 3
The Relational Model The slides for this text are organized into chapters. This lecture covers Chapter 3. Chapter 1: Introduction to Database Systems Chapter.
The Relational Model Chapter 3
Instructor: Mohamed Eltabakh
The Relational Model Content based on Chapter 3
The Relational Model Content based on Chapter 3
Presentation transcript:

The Relational Model Class 2 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) (From ER to Relational)

2  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Why Study the Relational Model?  Most widely used model in Commercial DBMSs:  Vendors: IBM, Informix, Microsoft, Oracle, Sybase…  “Legacy systems” in older models  E.G., IBM’s IMS  Competitor  Object-oriented model e.g. Informix Universal Server, UniSQL, O2, Oracle, DB2  XML Model e.g. XML to Relational ( IBM, Oracle, SQL-Server, Rainbow, SilkRoute, XPERANTO) Native XML Engine ( Timber, Natix, etc.)

3  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts:  Instance : a table, with rows (tuples) and columns (attributes). #Rows = cardinality, #attributes = 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).  Think of a relation as a set of rows or tuples i.e., all rows are distinct (not required by commercial database)

4  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Example Instance of Students Relation  Cardinality = ?, degree = ?  Cardinality = 3, degree = 5.  All rows are distinct  Do all columns in a relation instance have to be distinct?

5  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Attribute Types / Domain  Each column of a relation has a name  Set of allowed values for each column is called domain of column  Domain specifies that values of the column must be drawn from the domain associated with the column – domain constraint  Column values are (normally) required to be atomic, i.e., indivisible  The special value null is a member of every domain  Null value causes complications in the definition of many operations  We shall ignore the effect of null values in our main presentation and consider their effect later

6  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Relations are Unordered  Order of tuples is irrelevant (tuples may be stored in an arbitrary order)  E.g., account relation with unordered tuples

7  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Database  A database consists of multiple relations  Information about an enterprise is broken up into parts, with each relation storing one part of the information E.g.: account : stores information about accounts depositor : stores information about which customer owns which account customer : stores information about customers  Storing all information as a single relation such as bank ( account-number, balance, customer-name,..) results in:  repetition of information (e.g., two customers own an account)  need for null values (e.g., represent a customer without an account)  Normalization theory (Chapter 7) deals with how to efficiently design relational schemas.

8  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Relational Query Languages (SQL)  Developed by IBM (system R) in the 1970s  Need for a standard since it is used by many vendors  Standards:  SQL-86  SQL-89 (minor revision)  SQL-92 (major revision)  SQL-99 (major extensions, current standard)

9  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Relational Query Languages (SQL)  A major strength of the relational model: supports simple, powerful querying of data.  Queries can be written intuitively, and the DBMS is responsible for efficient evaluation.  The key: precise semantics for relational queries.  Allows the optimizer to extensively re-order operations, and still ensure that the answer does not change.  SQL = DDL + DML + …… (Chap 5)

10  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational DDL ---- Creating Relations  Creates Students relation. Observe that type (domain) of each field is specified, and enforced by DBMS whenever tuples are added or modified.  As another example, Enrolled table holds information about courses that students take. 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), grade: CHAR (2))

11  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational DDL --- Destroying and Altering Relations  Destroys the relation Students. The schema information and the tuples are deleted. DROP TABLE Students  The schema of Students is altered by adding a new field; every tuple in the current instance is extended with a null value in the new field. ALTER TABLE Students ADD COLUMN firstYear: integer

12  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational DML --- Query single relation  To find all 18 year old students, we can write: SELECT * FROM Students S WHERE S.age=18 To find just names and logins, replace the first line: SELECT S.name, S.login

13  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational DML --- 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” Given the following instances of Enrolled and Students: we get:

14  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational DML --- Adding and Deleting Tuples  Can insert a single tuple using: INSERT INTO Students (sid, name, login, age, gpa) VALUES (53688, ‘Smith’, 18, 3.2)  Can delete all tuples satisfying some condition (e.g., name = Smith): DELETE FROM Students S WHERE S.name = ‘Smith’ * Powerful variants of these commands are available; more later!

15  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Integrity Constraints (ICs)  IC: condition that must be true for any instance of the database ICs are specified when schema is defined. ICs are checked when relations are modified.  A legal instance of a relation is one that satisfies all specified ICs. DBMS should not allow illegal instances.

16  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Integrity Constraints (ICs)  IC include: Fundamental constraints : Key Foreign Key, Domain Constraints General constraints: table constraints (single table) assertions (several tables)

17  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Key Constraint  Two rules for Key constraints:  Two distinct tuples in a legal instance cannot have identical values in all columns of keys (unique)  No subset of the set of fields in a key is a unique identifier for a tuple (maximal)  Example: “No two students can have the same student Id “ “No two students can have the same student Id and name” CORE IDEA : Minimal subset of columns of the relation that uniquely identify the tuple.

18  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Keys  Let K  R  K is a superkey of R if values for K are sufficient to identify a unique tuple of relation r(R)  Example: { customer-name, customer-street } and { customer-name } are both superkeys of relation Customer. NO two customers can possibly have the same name.  Set of all fields is a super key  K is a candidate key if K is minimal  Example: { customer-name } is a candidate key for Customer. - superkey - no subset of it is a superkey.

19  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Primary and Candidate Keys in SQL  Possibly many candidate keys (specified using UNIQUE ), one of which is chosen as the primary key. 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), UNIQUE (cid, grade) )

20  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Primary and Candidate Keys in SQL CREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid) ) “For a given student and course, there is a single grade.” vs. “Students can take only one course, and receive a single grade for that course; further, no two students in a course receive the same grade.” CREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid), UNIQUE (cid, grade) ) Used carelessly, an IC can prevent the storage of database instances that arise in practice!

21  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Foreign Keys, Referential Integrity  Foreign key : Set of fields in one relation that is used to "refer" to a tuple in another relation. -Like a `logical pointer’.  Foreign key :  FK in referencing relation must match PK of referenced relation.  Match = same number of columns, compatible data types (column names can be different). Enrolled (referencing relation) Students (referenced relation) Foreign Key Primary Key

22  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Foreign Keys in SQL  Only students listed in Students relation should be allowed to enroll for courses. CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students )  If all foreign key constraints are enforced, referential integrity is achieved, i.e., no dangling references.

23  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Enforcing Referential Integrity  Consider Students and Enrolled; sid in Enrolled is a foreign key that references Students.  Insertion: What if a new Student tuple is inserted?  Insertion: What should be done if an Enrolled tuple with a non-existent student id is inserted?  Reject it Enrolled (referencing relation) Students (referenced relation) Foreign Key Primary Key

24  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Enforcing Referential Integrity Deletion: What if an Enrolled tuple is deleted? Enrolled (referencing relation) Students (referenced relation) Foreign Key Primary Key

25  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Enforcing Referential Integrity Deletion: What if a Students tuple is deleted?  Cascading -- Also delete all Enrolled tuples that refer to it.  No Action -- Disallow deletion of a Students tuple that is referred to.  Set Default -- Set sid in Enrolled tuples that refer to it to a default sid.  Set Null -- Set sid in Enrolled tuples that refer to it to a special value null, denoting `unknown’ or `inapplicable’. (Not always applicable)  Similar if primary key of Students tuple is updated. Enrolled (referencing relation) Students (referenced relation) Foreign Key Primary Key

26  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Enforcing Referential Integrity  Consider Students and Enrolled; sid in Enrolled is a foreign key that references Students.  Insertion: What should be done if an Enrolled tuple with a non-existent student id is inserted? ( Reject it! )  Deletion: What should be done if a Students tuple is deleted?  Cascading -- Also delete all Enrolled tuples that refer to it.  No Action -- Disallow deletion of a Students tuple that is referred to.  Set Default -- Set sid in Enrolled tuples that refer to it to a default sid.  Set Null -- Set sid in Enrolled tuples that refer to it to a special value null, denoting `unknown’ or `inapplicable’. (Not always applicable)  Similar if primary key of Students tuple is updated.

27  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Referential Integrity in SQL  SQL/92 and SQL/99 support all 4 options on deletes and updates:  Default is NO ACTION ( delete/update is rejected )  CASCADE (also delete all tuples that refer to deleted tuple)  SET NULL / SET DEFAULT (sets foreign key value of referencing tuple) CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students ON DELETE CASCADE ON UPDATE SET DEFAULT )

28  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Where do ICs Come From?  ICs are based upon semantics of real-world enterprise being described in database relations.  We can check a database instance to see if an IC is violated,  but we can NEVER infer that an 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.  Key and foreign key ICs are the most common; but more general ICs supported too in some systems.

29  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Views  A view is just a relation, but we store a definition, rather than a set of tuples. CREATE VIEW YoungActiveStudents (name, sid, course) AS SELECT S.name, S.sid, E.cid FROM Students S, Enrolled E WHERE S.sid = E.sid  Views can be dropped using DROP VIEW command.  How to handle DROP TABLE if there’s a view on the table? DROP TABLE { RESTRICT | CASCADE }

30  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Views and Security  Views can be used to present necessary information (or a summary), while hiding details in underlying relation(s).  Given YoungStudents view only (not Students or Enrolled table)  User can find students who have enrolled, but not the grades of the courses they got.

31  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Relational Model Terminology  Schema / Data Model  Relation /Relation Schema  Instance / Database  Schema Definition Language  Constraint Specification Language

32  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Relational Model: Summary  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 for violations.  Two important ICs: primary and foreign keys  In addition, we always have domain constraints.  Powerful and ‘natural’ query languages exist.