CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.

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)
Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating.
The Relational Model CMU SCS /615 C. Faloutsos – A. Pavlo Lecture #3 R & G, Chap. 3.
Database Management Systems 1 Raghu Ramakrishnan The Relational Model Chapter 3 Instructor: Mirsad Hadzikadic.
The Relational Model Class 2 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) (From ER to Relational)
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.
Murali Mani The Relational Model. Murali Mani Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still.
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.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
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
Data Models Amandeep Kaur Lecturer GPC Khunimajra Call at
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.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
CS3431-B111 The Relational Model Instructor: Mohamed Eltabakh
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.
ICS 421 Spring 2010 Relational Model & Normal Forms Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 1/19/20101Lipyeow.
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.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
COP Introduction to Database Structures
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 The slides for this text are organized into chapters. This lecture covers Chapter 3. Chapter 1: Introduction to Database Systems Chapter.
Instructor: Mohamed Eltabakh
The Relational Model Content based on Chapter 3
The Relational Model Content based on Chapter 3
Presentation transcript:

CS34311 The Relational Model

cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s IMS (hierarchical model) Recent competitions Object Oriented Model: ObjectStore, Oracle?

cs3431 Relational Query Languages (SQL Standard) Developed by IBM (system R) in the 1970s Need for a standard since it is used by many vendors (portable) SQL (Structured Query Language) Standards: SQL-86 SQL-89 (minor revision) SQL-92 (major revision) SQL-99 (major extensions in OO, current standard)

cs34314 Relational Model Structures: Relations (also called Tables) Attributes (also called Columns or Fields) Note: Every attribute is simple (not composite or multi-valued) Instance : a table with rows (tuples) and columns (attributes) #Rows = cardinality, #attributes = degree / arity.

cs34315 Relational Model Eg: Student Relation Are 2 relations equivalent? Think of a relation as a set of tuples i.e., all rows are distinct (not required by commercial database) sNumbersName 1Dave 2Greg Student sNumbersName 2Greg 1Dave Student Cardinality = 2 Arity/Degree = 2

cs34316 Relational Model Schema for a relation E.g.: Student (sNumber:INTEGER, sName:CHAR(20)) Schema for a database Schemas for all relations in the database Tuples (Rows) Set of rows in a relation are tuples of that relation Note: Attribute values may be null

cs34317 Attribute Types / Domain Set of allowed values for each column is called domain of column Domain specifies that values of column must be drawn from domain associated with column – domain constraint Column values are (normally) required to be atomic, i.e., indivisible The special value null is a member of every domain

cs34318 Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME

cs34319 SQL DDL DDL = Data Definition Language Create tables, columns of tables, types of columns, primary key constraints, unique constraints, foreign key constraints Drop tables, add/drop columns, add/drop constraints – primary key, unique, foreign key

cs Creating Tables CREATE TABLE (, …, [CONSTRAINT ] PRIMARY KEY (…), [CONSTRAINT ] UNIQUE (…), [CONSTRAINT ] FOREIGN KEY (…) REFERENCES (…) );

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

cs Dropping tables DROP TABLE

cs Adding/Dropping Columns ALTER TABLE ADD ; ALTER TABLE DROP COLUMN ;

cs Adding/Dropping Constraints ALTER TABLE ADD [CONSTRAINT ] … ALTER TABLE DROP CONSTRAINT

cs SQL DML: Basic DML = Data Manipulation Language Consists of Queries and Modification Modification: Inserting/Deleting values from table Insert a value into a table INSERT INTO VALUES (…) eg: INSERT INTO Student VALUES (1, ‘Dave’); Delete all values from a table DELETE FROM

cs3431 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.

cs Constraints in Relational Model Key constraints Foreign Key constraints (More constraints later)

cs Key Constraints A set of attributes is a key for a relation if: Unique: No two distinct tuples can have the same values in all key fields Minimal: A proper subset of the key attributes is not a key. INTUITION : Minimal subset of columns of the relation that uniquely identify the tuple.

cs Keys: Example sNumbersNameaddress 1Dave144FL 2Greg320FL Student Is a key ?

cs Primary Key Constraints If multiple keys, one of them is chosen to be the Primary Key. E.g.: PRIMARY KEY (Student) = Restriction: Primary key attributes cannot take null values

cs Candidate Keys (SQL: Unique) Keys that are not primary keys are Candidate Keys. Specified in SQL using UNIQUE E.g.: Student (sNumber, sName) PRIMARY KEY (Student) = CANDIDATE KEY (Student) = Note: Attribute of unique key may have null values !

cs More on Key Constraints Superkey K: if values for K are sufficient to identify a unique tuple of relation r(R) REMINDER: A set of attributes is a key if: No two distinct tuples can have the same values in all key fields A proper subset of the key attributes is not a key. Superkey: Contains some key in its attribute set A proper subset of a superkey may be a superkey again ?

cs Violation of key constraints A relation violates a primary key constraint if: There is a row with null values for any attribute of primary key, or, There are 2 rows with same values for all attributes of primary key A relation violates the unique constraint if: 2 rows in R have the same non-null values for any unique attribute R.a

cs Keys: Example sNumbersNameaddress 1Dave144FL 2Greg320FL Student Primary Key: Candidate key: Some superkeys: {,, }

cs3431 Primary & Candidate Keys in SQL Possibly many candidate keys (specified using UNIQUE ), one chosen as primary key. CREATE TABLE Student (sid CHAR(20), ssn INTEGER, cid CHAR(20), PRIMARY KEY (sid,cid) UNIQUE (cid )

cs3431 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.” “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!

cs3431 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

cs3431 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 (sid))  If all foreign key constraints are enforced, referential integrity is achieved, i.e., no dangling references.

cs Foreign Keys: More Examples Dept (dNumber, dName) Person (pNumber, pName, dept) PRIMARY KEY (Dept) = PRIMARY KEY (Person) = FOREIGN KEY Person (dept) REFERENCES Dept (dNumber) Persons working for Depts Person and his/her father Person (pNumber, pName, father) PRIMARY KEY (Person) = FOREIGN KEY Person (father) REFERENCES Person (pNumber)

cs Violation of Foreign Key Constraints Suppose we have: FOREIGN KEY R1 (S1) REFERENCES R2 (S2) This constraint is violated if Consider a row in R1 with non-null values for all attributes of S1 If there is no row in R2 which have these values for S2, then the FK constraint is violated.

cs3431 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

cs3431 Enforcing Referential Integrity Deletion: What if an Enrolled tuple is deleted? Enrolled (referencing relation) Students (referenced relation) Foreign Key Primary Key

cs3431 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’ (Not always applicable) Similar if primary key of Students tuple is updated. Enrolled (referencing relation) Students (referenced relation) Foreign Key Primary Key

cs3431 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.

cs3431 Referential Integrity in SQL SQL/99 supports all 4 options on deletes & 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 )

cs3431 Constraint Violation What if cyclic dependencies between two tables? Solution: 1. Group both insertions into one trasaction 2. Tell DBMS not to check the constraints until after the whole transaction has finished and is committed How: Follow declaration of constraint by “DEFERRABLE” Plus “INITIALLY DEFERRED” defer just before transaction commits Or plus “INITIALLY IMMEDIATE” check made immediately after each statement

cs3431 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 ? We can infer that an IC is true by looking at an instance ? No, NEVER ! 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.

cs Relational Model: Summary Structures Relations (Tables) Attributes (Columns, Fields) Constraints + Constraint Enforcement Domain Constraint Key Primary key, candidate key (unique) Super Key Foreign Key