1 The Relational Model Instructor: Mohamed Eltabakh

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.
The Relational Model. Introduction Introduced by Ted Codd at IBM Research in 1970 The relational model represents data in the form of table. Main concept.
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.
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 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.
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
Relational Data Model, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 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 =
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
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
Advanced SQL: Triggers & Assertions
The Relational Model Content based on Chapter 3 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
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.
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 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.
SQL-1 Week 8-9.
Database Systems (資料庫系統)
SQL: Structured Query Language
Instructor: Mohamed Eltabakh
SQL: Structured Query Language
The Relational Model Content based on Chapter 3
The Relational Model Content based on Chapter 3
Presentation transcript:

1 The Relational Model Instructor: Mohamed Eltabakh

2 Relational Model Another model for describing your data and application requirements Currently the most widely used model Vendors: Oracle, Microsoft, IBM Recent competitions Object Oriented Model: ObjectStore, Oracle XML Databases : native and extensions What about ER model ER model is the first step in the design ER model is then translated (mapped) to relational model

Design and Build Phases 3 ER Model & ERD Phase 1Phase 2Phase 3 Relational Model Build the database

4 Relational Model Structure: Relations (also called Tables) Attributes (also called Columns or Fields)

5 Attributes in Relational Model Attribute Name Each attribute of a relation has a unique name within the relation Attribute Domain The set of allowed values for each attribute is called the domain of the attribute E.g., integer, real, date, string {“Male”, “Female”}, etc. Atomic Attributes Attribute values are required to be atomic (primitive or derived) Note: multivalued attributes are not atomic Note: composite attributes are not atomic We will see how to handle non-atomic attributes Special Null Value The special value null is a member of every domain The null value means the values does not exist (unknown)

Relational Schema Schema is the structure of the relation Attributes A1, A2,..., An of relation R R=(A1,A2,...,An ) is the relations schema of R Relational Schema of “customer” is customer = (customer_name, customer_street, customer_city) customer (customer_name, customer_street, customer_city) 6

Relational Instance Instance is the current schema plus the current tuples in the relation The current instance of “customer” has four tuples (rows or records) Instances are frequently changing E.g., inserting new tuples, deleting or updating existing tuples Schemas may also change but not frequently 7

Arity & Cardinality of Relations Arity (Degree) of a relation Number of attributes of that relation Cardinality of a relation Number of tuples in that relation 8 Arity = 3 Cardinality = 4

9 Relations are Unordered Order of tuples is not important Order of columns is not important Think of a relation as a set (or bag) of tuples not as a list of tuples Most current DBMSs allow bags sNumbersName 1Dave 2Greg Student sNumbersName 2Greg 1Dave Student These relations are equivalent sNameSNumber Greg2 Dave1 Student

Keys of Relations Super Keys Subset of attributes that uniquely identify each tuple Candidate Keys Minimal super key Primary Keys Choose any of the candidate keys Represented by “underline” under the selected attributes Set vs. Bag If a relation has a primary key, then it is a set not a bag 10 customer (customer_name, customer_street, customer_city) Assuming the customer name is unique

To create these relations (tables) We use SQL 11

Relational Query Languages (SQL Standard) Developed by IBM (system R) in 1970s Standard as 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) 12

SQL Language Data Definition Language (DDL) Create tables, specifying the columns and their types of columns, the primary keys, etc. Drop tables, add/drop columns, add/drop constraints – primary key, unique, etc. Data Manipulation Language (DML) Update, Insert, Delete tuples Query the data 13

14 SQL DDL DDL allows the specification of: The schema for each relation The type and domain of values associated with each attribute Integrity constraints Will see many types of these constraints The set of indices to be maintained for each relation Security and authorization information for each relation The physical storage structure of each relation on disk

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

DDL ---- Creating Relations Create “Students” relation CREATE TABLE Students (sid: CHAR(20), name: CHAR(20), login: CHAR(10), age: INTEGER, gpa: REAL ); CREATE TABLE Enrolled (sid: CHAR(20), cid: Varchar2(20), enrollDate: date, grade: CHAR (2)) Observe that type (domain) of each field is specified Types are enforced by DBMS whenever tuples are added or modified Create “Enrolled” relation CREATE TABLE Courses (cid: Varchar2(20), name: Varchar2(50), maxCredits : integer, graduateFlag: char(1) ); Create “Courses” relation 16

Domain Types in SQL String Data: char(n): Fixed length character string, with user-specified length n varchar2(n): Variable length character strings, with user-specified maximum length n Numbers: int: Integer Real: floating point number Number(p,s): Number with precision p and scale s Boolean: Make it char(1) [Y/N or T/F], or number(1) [1/0] Date & Time: Date: Date field with certain format Timestamp: Date plus time with a certain format 17 customer (name: varchar2(100), DoB: Date, Phone: Char(10), …)

18 Dropping & Altering Tables Dropping (Deleting) a table DROP TABLE ; Altering a table to add a column ALTER TABLE ADD ; The initial value of the column is Null Altering a table to drop a column ALTER TABLE DROP COLUMN ; Some DBMSs do not support dropping columns

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. Examples: Student ID must be unique The combination of first + middle + last names must be unique The grade must be one of ( A, B, C, D) The gender must be (M, F) Account balance must be >= 0 19

20 IC: Key Constraints Two types of key constraints can be defined Primary Key Unique (The other candidate keys) Each relation may have only one primary key but, possibly many unique keys Primary key attributes do not accept Null, Unique attributes accept Null Create “Students” relation CREATE TABLE Students (sid: CHAR(20) Primary Key, name: CHAR(20), login: CHAR(10) Unique, age: INTEGER, gpa: REAL ); CREATE TABLE Courses (cid: Varchar2(20) Primary Key, name: varchar2(50), maxCredits : integer, graduateFlag: char(1) ); Create “Courses” relation

CREATE TABLE Students (sid: CHAR(20), name: CHAR(20), login: CHAR(10), age: INTEGER, gpa: REAL, Constraint pk_sid Primary Key (sid), Constraint u_login Unique (login)); IC: Key Constraints (Cont’d) Another way to define Keys 21 Create “Students” relation CREATE TABLE Enrolled (sid: CHAR(20), cid: Varchar(20), enrollDate: date, grade: CHAR (2), Constraint pk_cols Primary Key (sid, cid, enrollDate)); Create “Enrolled” relation A relation can have only one primary key, but many unique keys That is how you define constraints on multiple columns

Violations of Key Constraints DBMS prevents this from happening Example customer (name: varchar2(50), Phone: Char(10), address: varchar2(100)); (“Mike”, “ ”, “NY”); Inserted (“Mike”, “ ”, “CA”); Raises an error (duplication) (null, “ ”, “CA”); Raises an error (null value) 22

23 Violation of key constraints Primary key violation if: There is a row with null values for any attribute of primary key, OR Two rows with same values for all attributes of primary key Unique key violation if: Two rows have the same non-null values in the unique attributes Null values are not equal to each other DBMS will prevent this from happening

24 Adding/Dropping Constraints Can be done after creating tables Adding a constraint ALTER TABLE ADD CONSTRAINT … Dropping a constraint ALTER TABLE DROP CONSTRAINT

CREATE TABLE Students (sid: CHAR(20), name: CHAR(20), login: CHAR(10), age: INTEGER, gpa: REAL); Example 25 Create “Students” relation with no constraints Alter table Students add constraint StudentPrimaryKey Primary Key (sid); Alter table Students add constraint StudentUnique Unique (login); Then add constraints…

Constraints Limits the Data 26 CREATE TABLE Enrolled (sid: CHAR(20), cid: Varchar2(20), enrollDate: date, grade: CHAR (2), Constraint pk Primary Key (sid, cid)); Create “Enrolled” relation A student can take a course only once. CREATE TABLE Enrolled (sid: CHAR(20), cid: Varchar2(20), enrollDate: date, grade: CHAR (2), Constraint pk Primary Key (sid, cid, enrollDate)); Create “Enrolled” relation A student can take a course many times on different dates.

Not Null & Default Constraint Types 27 Create “Students” relation CREATE TABLE Students (sid: CHAR(20), name: CHAR(20) NOT NULL, login: CHAR(10), age: INTEGER, gpa: REAL Default 0, Constraint pk Primary Key (sid), Constraint u1 Unique (login)); CREATE TABLE Courses (cid: Varchar2(20), name: varchar2(50), maxCredits : integer, graduateFlag: char(1) Default ‘Y’ ); Create “Courses” relation NOT NULL & DEFAULT constraints

Domain Values Constraint 28 CREATE TABLE Enrolled (sid: CHAR(20), cid: Varchar2(20), enrollDate: date, grade: CHAR (2), Constraint pk Primary Key (sid, cid, enrollDate), Constraint gradeVal check (grade in (‘A+’, ‘A-’, ‘B+’, ‘B-’)) ); Create “Enrolled” relation Domain constraint

IC: Foreign Key Constraints (Referential Integrity ) 29 Create “Students” relation CREATE TABLE Students (sid: CHAR(20), name: CHAR(20), login: CHAR(10), age: INTEGER, gpa: REAL ); CREATE TABLE Courses (cid: Varchar2(20), name: varchar2(50), maxCredits : integer, graduateFlag: char(1) ); Create “Courses” relation CREATE TABLE Enrolled (sid: CHAR(20), cid: Varchar2(20), enrollDate: date, grade: CHAR (2)); Create “Enrolled” relation Foreign key

Foreign Keys, Referential Integrity Foreign key : Set of fields in one relation that "refer" to a tuple in another relation (like a 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) The relationship is many-to-one from the “referencing” to the “referenced” Enrolled (referencing relation) Students (referenced relation) Foreign Key Primary Key 30

Foreign Keys in SQL 31 Create “Students” relation CREATE TABLE Students (sid: CHAR(20) Primary Key, name: CHAR(20), login: CHAR(10), age: INTEGER, gpa: REAL ); CREATE TABLE Courses (cid: Varchar2(20) Primary Key, name: varchar2(50), maxCredits : integer, graduateFlag: char(1) ); Create “Courses” relation CREATE TABLE Enrolled (sid: CHAR(20) Foreign Key References Students (sid), cid: Varchar2(20), enrollDate: date, grade: CHAR (2), Constraint fk_cid Foreign Key (cid) References Courses (cid)); Create “Enrolled” relation Two ways to define the FK constrain while creating a table Alter Table Enrolled Add Constraints fk_cid Foreign Key cid References Courses(cid)); That is a third way

Foreign Keys in SQL 32 Create “Students” relation CREATE TABLE Students (sid: CHAR(20) Primary Key, name: CHAR(20), login: CHAR(10), age: INTEGER, gpa: REAL ); CREATE TABLE Courses (cid: Varchar2(20) Primary Key, name: varchar2(50), maxCredits : integer, graduateFlag: char(1) ); Create “Courses” relation CREATE TABLE Enrolled (sid: CHAR(20) Foreign Key References Students (sid), cid: Varchar2(20), enrollDate: date, grade: CHAR (2), Constraint fk_cid Foreign Key (cid) References Courses (cid)); Create “Enrolled” relation Alter Table Enrolled Add Constraints fk_cid Foreign Key cid References Courses(cid)); sid & cid have to be already defined as primary keys

33 Violation of Foreign Key Constraints DBMS will prevent foreign key values that does not exist in the primary key column(s) Enrolled (referencing relation) Students (referenced relation) Foreign Key Primary Key Will be rejected

Enforcing Referential Integrity Deletion: What if an Enrolled tuple is deleted? No problem. It is allowed 34

Enforcing Referential Integrity (Cont’d) Deletion: What if a Student tuple is deleted? Cascading -- Also delete all Enrolled tuples that refer to it No Action -- Disallow deletion of a Students tuple since there are dependent tuples 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) Same rules apply when updating Students.sid The two most common actions are: Cascading or No Action 35

Referential Integrity in SQL SQL/99 supports all 4 options on deletes & updates: CREATE TABLE Enrolled (sid: CHAR(20) Foreign Key References Students (sid), cid: Varchar2(20), enrollDate: date, grade: CHAR (2), Constraints fk_cid Foreign Key cid References Courses (cid) ON DELETE CASCADE ON UPDATE NO ACTION); 36

Cyclic Dependencies What if cyclic dependencies between two tables exists? Table R has a foreign key to Table S (Say R.a references S.b) Table S has a foreign key to Table R (Say S.c references R.d) At insertion time: When insert into R  will be rejected because S.b does not yet exits When insert into S  will be rejected because R.d does not yet exits Solution (Many) – One option: Disable the constraint on one table, say S Insert the tuple into S, then insert into R Enable the constraint on S again Another solution Group the insertions in one transaction Begin Transaction  End Transaction Alter Table S Disable Constraint ; Alter Table S Enable Constraint ; Constraints are checked at the end of the transaction 37

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 ? In typical cases, DBMS should make sure all constraints are enforced Given an instance, can we tell what are the ICs? No, NEVER ! An IC is a statement about all possible instances! They need to be checked from the schema definition not from a given instance 38

39 Relational Model: Summary Structure Relations (Tables) Attributes (Columns, Fields) Constraints + Constraint Enforcement Domain, Default, and Not Null Constraint Key Constraint Primary key, candidate key (unique) Foreign Key Constraint SQL DDL commands

Inserting, Updating, Deleting Data This is performed using Data Manipulation Language of SQL (DML) Insertion Insert into Students values (‘1111’, …); Deletion Delete from Students; Delete from Students Where sid = ‘1111’; Update Update Students Set GPA = GPA + 0.4; Update Students Set GPA = GPA Where sid = ‘1111’; 40

Exercise For Quiz 1 application (airline database), assume we use the following design… What are the relations (tables) that you can build? 41