COMP5138 Relational Database Management Systems. Today s Agenda

Similar documents
The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

Lecture 6. SQL, Logical DB Design

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

Outline. Data Modeling. Conceptual Design. ER Model Basics: Entities. ER Model Basics: Relationships. Ternary Relationships. Yanlei Diao UMass Amherst

EECS 647: Introduction to Database Systems

The Relational Model. Why Study the Relational Model?

There are five fields or columns, with names and types as shown above.

Review: Participation Constraints

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

Review Entity-Relationship Diagrams and the Relational Model. Data Models. Review. Why Study the Relational Model? Steps in Database Design

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

SQL NULL s, Constraints, Triggers

Winter Winter

The Entity-Relationship Model

Chapter 2: Entity-Relationship Model. Entity Sets. " Example: specific person, company, event, plant

Databases Model the Real World. The Entity- Relationship Model. Conceptual Design. Steps in Database Design. ER Model Basics. ER Model Basics (Contd.

Database Management Systems. Chapter 1

Overview. Introduction to Database Systems. Motivation... Motivation: how do we store lots of data?

Database Design Overview. Conceptual Design ER Model. Entities and Entity Sets. Entity Set Representation. Keys

Data Modeling. Database Systems: The Complete Book Ch ,

AVOIDANCE OF CYCLICAL REFERENCE OF FOREIGN KEYS IN DATA MODELING USING THE ENTITY-RELATIONSHIP MODEL

2. Conceptual Modeling using the Entity-Relationship Model

The Relational Data Model and Relational Database Constraints

Databases What the Specification Says

Databases and BigData

COMP 378 Database Systems Notes for Chapter 7 of Database System Concepts Database Design and the Entity-Relationship Model

Conceptual Design Using the Entity-Relationship (ER) Model

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 7

Lesson 8: Introduction to Databases E-R Data Modeling

ER modelling, Weak Entities, Class Hierarchies, Aggregation

Fundamentals of Database Design

LiTH, Tekniska högskolan vid Linköpings universitet 1(7) IDA, Institutionen för datavetenskap Juha Takkinen

The Relational Data Model: Structure

Database Design. Marta Jakubowska-Sobczak IT/ADC based on slides prepared by Paula Figueiredo, IT/DB

Relational model. Relational model - practice. Relational Database Definitions 9/27/11. Relational model. Relational Database: Terminology

Chapter 2: Entity-Relationship Model. E-R R Diagrams

Database Systems. Lecture 1: Introduction

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

Databasesystemer, forår 2005 IT Universitetet i København. Forelæsning 3: Business rules, constraints & triggers. 3. marts 2005

3. Relational Model and Relational Algebra

CSC 443 Data Base Management Systems. Basic SQL

Entity Relationship Diagram

Introduction to Computing. Lectured by: Dr. Pham Tran Vu

Tutorial on Relational Database Design

DATABASE MANAGEMENT SYSTEMS. Question Bank:

Entity-Relationship Model

CSC 742 Database Management Systems

IT2305 Database Systems I (Compulsory)

Chapter 6: Integrity Constraints

Introduction to SQL ( )

Introduction to normalization. Introduction to normalization

Chapter 1: Introduction

A. TRUE-FALSE: GROUP 2 PRACTICE EXAMPLES FOR THE REVIEW QUIZ:

Converting E-R Diagrams to Relational Model. Winter Lecture 17

IT2304: Database Systems 1 (DBS 1)

Bridge from Entity Relationship modeling to creating SQL databases, tables, & relations

SQL Tables, Keys, Views, Indexes

IV. The (Extended) Entity-Relationship Model

SQL Data Definition. Database Systems Lecture 5 Natasha Alechina

Foundations of Information Management

Database Design. Database Design I: The Entity-Relationship Model. Entity Type (con t) Chapter 4. Entity: an object that is involved in the enterprise

BCA. Database Management System

Chapter 1: Introduction. Database Management System (DBMS) University Database Example

Graham Kemp (telephone , room 6475 EDIT) The examiner will visit the exam room at 15:00 and 17:00.

OBJECTS AND DATABASES. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 21

Exercise 1: Relational Model

Lecture 12: Entity Relationship Modelling

In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina

Normalization. Functional Dependence. Normalization. Normalization. GIS Applications. Spring 2011

Section of DBMS Selection & Evaluation Questionnaire

Relational Databases

How To Create A Table In Sql (Ahem)

We know how to query a database using SQL. A set of tables and their schemas are given Data are properly loaded

DataBase Management Systems Lecture Notes

XV. The Entity-Relationship Model


How To Manage Data In A Database System

Foundations of Information Management

The Entity-Relationship Model

Database Design Process

MCQs~Databases~Relational Model and Normalization

CS 377 Database Systems. Database Design Theory and Normalization. Li Xiong Department of Mathematics and Computer Science Emory University

Chapter 3. Data Modeling Using the Entity-Relationship (ER) Model

Concepts of Database Management Seventh Edition. Chapter 6 Database Design 2: Design Method

TIM 50 - Business Information Systems

Database Management Systems

CIS 631 Database Management Systems Sample Final Exam

CSE 132A. Database Systems Principles

OVERVIEW 1.1 DATABASE MANAGEMENT SYSTEM (DBMS) DEFINITION:-

The core theory of relational databases. Bibliography

Database Design Process. Databases - Entity-Relationship Modelling. Requirements Analysis. Database Design

Limitations of E-R Designs. Relational Normalization Theory. Redundancy and Other Problems. Redundancy. Anomalies. Example

Relational Databases. Christopher Simpkins

Schema Mappings and Data Exchange

Fundamentals of Database System

CS411 Database Systems

2. Basic Relational Data Model

Relational Database Concepts

SUBQUERIES AND VIEWS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 6

Transcription:

COMP5138 Relational Database Management Systems Week 3: The Relational Data Model Today s Agenda Introduction to the Relational Model Logical Database Design Introduction to Views Textbook, Chapter 3 1

The Relational Data Model The model was first proposed by E.F.Codd (IBM) in 1970: "A Relational Model for Large Shared Data Banks," Communications of the ACM, June 1970. The above paper caused a major revolution in the field of database management and earned Ted Codd the coveted ACM Turing Award. The relational data model is based on the concept of a relation. A relation is a mathematical concept based on the ideas of sets. The strength of the relational approach to data management comes from the formal foundation provided by the theory of relations. By far the dominant data model today Being an IT professional and not knowing the relational data model is like practising medicine without a license. Chris Date 2

Definition of Relation Informal Definition: A relation is a named, two-dimensional table of data Table consists of rows (record) and columns (attribute or field) Attributes (Fields, Columns) Example: sid name students login age gpa Tuples (Records, Rows) 53666 53668 53650 Jones Smith Smith jones@cs smith@eecs smith@math 18 18 19 3.4 3.2 3.8 Some Remarks Not all tables qualify as a relation. Requirements: Every relation must have a unique name. Attributes (columns) in tables must have unique names. Every attribute value is atomic (not multivalued, not composite). The range of allowed values of an attribute is called its domain. Every row is unique (can t have two rows with exactly the same values for all their fields) The order of the columns is irrelevant. The order of the rows is irrelevant. 3

Formal Definition of Relation Formal Definition: Relation R Given sets D 1,D 2,, D n, a relation R is a subset of D 1 x D 2 x x D n. Thus, a relation is a set of n-tuples ( a 1, a 2,, a n ) where a i D i Example: If customer-name = {Jones, Smith, Curry, Lindsay} customer-street = {Main, North, Park} customer-city = {Sydney, Brisbane, Perth} then R = { (Jones, Main, Sydney), (Smith, North, Perth), (Curry, North, Brisbane), (Lindsay, Park, Sydney) } is a relation over customer-name x customer-street x customer-city Relation Schema and Instance a relation schema: specifies name of relation, plus name and data type of each attribute. A 1, A 2,, A n are attributes (or fields) R = (A 1, A 2,, A n ) is a relation schema e.g. Students(sid: string, name: string, login: string, age: integer, gpa: real) an relation instance: a set of tuples (table) for a schema D 1, D 2,, D n are the domains each attribute corresponds to one domain: dom(a i ) = D i, 1 <= i <= n R D 1 x D 2 x x D n #rows = cardinality #fields = degree (also arity) 4

Remarks Attribute Domains: NULL value The special value null is a member of every domain Represents an unknown value The 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 effects later The restriction of atomic attributes is also known as First Normal Form (1NF). Relational Keys Keys are special fields that serve two main purposes: Primary keys are unique, minimal identifiers of the relation in question. Examples include employee numbers, social security numbers, etc. This is how we can guarantee that all rows are unique. There may be several candidate keys to chose from If we just say key, we typically mean candidate key A set of attributes containing a key is called a superkey Foreign keys are identifiers that enable a dependent relation (on the many side of a relationship) to refer to its parent relation (on the one side of the relationship) Keys can be simple (a single field) or composite (more than one field) 5

Example: Relational Keys Primary key identifies each tuple of a relation. Composite Primary Key consisting of more than one attribute. students enroll courses sid name sid ucode semester ucode title credit_pts 31013 John 31013 I2005 2005S1 I2005 DB Intro 4 Foreign key is a (set of) attribute(s) in one relation that `refers to a tuple in another relation (like a `logical pointer ). Foreign Keys & Referential Integrity Foreign key : Set of attributes in a relation that is used to `refer to a tuple in a parent relation. Must refer to a candidate key of the parent relation Like a `logical pointer Referential Integrity: for each tuple in the referring relation whose foreign key value is α, there must be a tuple in the referred relation whose primary key value is also α e.g. sid is a foreign key referring to Students: Enrolled(sid: integer, ucode: string, semester: string) If all foreign key constraints are enforced, referential integrity is achieved, i.e., no dangling references 6

Relational Database Data Structure: A relational database is a set of relations (tables) with tuples (rows) and fields (columns) - a simple and consistent structure. The union of the corresponding relational schemata is the relational database schema. Data Manipulation: Powerful operators to manipulate the data stored in relations. Data Integrity: facilities to specify a variety of rules to maintain the integrity of data when it is manipulated. Creating and Deleting Relations using SQL Creation of tables (relations): CREATE TABLE name ( list-of-columns ) Example: Create the Students relation. Observe that the type (domain) of each field is specified, and enforced by the DBMS whenever tuples are added or modified. CREATE TABLE Students ( sid CHAR(20), name CHAR(20), login CHAR(10), age INTEGER, gpa REAL ); Deletion of tables (relations): DROP TABLE name the schema information and the tuples are deleted. Example: Destroy the Students relation DROP TABLE Students 7

Primary/Foreign Key Constraints in SQL Primary keys and foreign keys can be specified as part of the SQL CREATE TABLE statement: The PRIMARY KEY clause lists attributes that comprise the primary key. The FOREIGN KEY clause lists the attributes that comprise the foreign key and the name of the relation referenced by the foreign key. The UNIQUE clause lists attributes that comprise a candidate key. By default, a foreign key references the primary key attributes of the referenced table FOREIGN KEY (sid) REFERENCES students Reference columns in the referenced table can be explicitly specified but must be declared as primary or candidate keys FOREIGN KEY (lecturer) REFERENCES lecturers(empid) Tip: Name them using CONSTRAINT clauses CONSTRAINT StudentPK PRIMARY KEY (sid) Create Table Example Students Enroll Unit_of_studies sid name sid ucode semester ucode title credit_pts CREATE TABLE Students ( sid INTEGER,, CONSTRAINT Students_PK PRIMARY KEY (sid) ); CREATE TABLE UOS ( ucode CHAR(8),, CONSTRAINT Courses_PK PRIMARY KEY (ucode) ); CREATE TABLE Enroll ( sid INTEGER, ucode CHAR(8), semester VARCHAR, CONSTRAINT Enroll_FK1 FOREIGN KEY (sid) REFERENCES Students, CONSTRAINT Enroll_FK2 FOREIGN KEY (ucode) REFERENCES UOS, CONSTRAINT Enroll_PK PRIMARY KEY (sid,ucode) ); 8

Modifying Relations using SQL Insertion of new data into a table / relation Syntax: INSERT INTO table [ ( list-of-columns ) ] VALUES ( list-of-expression ) Example: INSERT INTO Students (sid, name) VALUES (53688, Smith ) Updating of tuples in a table / relation Syntax: UPDATE table SET column = expression {, column = expression} [ WHERE search_condition ] Example: UPDATE students SET gpa = gpa - 0.1 WHERE gpa >= 3.3 Deleting of tuples from a table / relation More details on Syntax: those in next chapter. DELETE FROM table [ WHERE search_condition ] Example: DELETE FROM Students WHERE name = Smith Today s Agenda Introduction to the Relational Model Logical Database Design: E-R To Relational Mapping Introduction to Views 9

Correspondence with E-R Model Relations (tables) correspond to entity sets and to many-to-many relationship sets. Rows correspond with entities and with many-tomany relationships. Columns correspond with attributes or one-to-many relationships. Note: The word relation (in relational database) is NOT the same as the word relationship (in E-R model). Transforming E-R Diagrams into Relations Mapping rules for Strong Entities Weak Entities Relationships One-to-many, Many-to-many, One-to-one Unary Relationships Ternary Relationships ISA Hierarchies We will concentrate in the lecture on typical examples 10

1. Mapping Regular Entities to Relations Each entity set becomes a relation Simple attributes E-R attributes map directly onto the relation Composite attributes Composite attributes are flattened out by creating a separate field for each component attribute => We use only their simple, component attributes Multi-valued Attribute Becomes a separate relation with a foreign key taken from the superior entity Example: Mapping Entity Sets Employees entity set with composite/multi-valued attributes street empid name city state address Employees skills zipcode Employees empid name street city state zipcode Skills empid skill PK-/FK reference between Employees table and table for multi-valued Skills attribute. 11

2. Mapping of Weak Entity Sets Weak Entity Sets become a separate relation with a foreign key taken from the superior entity primary key composed of: Partial key (discriminator) of weak entity Primary key of identifying relation (strong entity) Mapping of attributes of weak entity as shown before Example: Mapping of Weak Entity Weak entity set Dependents with composite partial key empiid name Employees Policy Dependents name birthdate given family Foreign key NOTE: the domain constraint for the foreign key of a weak entity should not allow NULL Employees Dependents empid name empid given family birthdate Composite primary key 12

3. Mapping of Relationship Sets Many-to-Many - Create a new relation with the primary keys of the two entities as its primary key One-to-Many - Primary key on the one side becomes a foreign key on the many side Participation Constraint: total side becomes NOT NULL One-to-One - Primary key on the mandatory side becomes a foreign key on the optional side Relationship attributes - become fields of either the dependent, respectively new relation Example: Mapping of Many-to-many Relationship Sets Many-to-many relationship between Students & courses sid ucode name birthdate Students enroll Courses (unit_of_study) title country semester grade credit_ points sid Students Enroll Courses name bdate cntry sid ucode semester grade ucode title credit_pts 13

Example: Mapping of One-to-many Relationships Key Constraint: One-to-many relationship set Participation Constraint: NOT NULL on foreign key ssn name Employees since WorksIn Departments did dname Employees Departments ssn name did since did dname Further Examples manager wife husband Employee manages Person Married_to Employees Persons ssn name manager name married_to 14

4. Mapping of ISA-Hierarchies Standard way (works always): Distinct relations for the superclass and for each subclass Superclass attributes (including key and possible relationships) go into superclass relation Subclass attributes go into each sub-relation; primary key of superclass relation also becomes primary key of subclass relation 1:1 relationship established between superclass and each subclass, with superclass as primary table I.e. primary keys of subclass relations become also a foreign key referencing the superclass relation Special case in presence of a total covering constraint: Distinct relations for each subclass with primary key of superclass All superclass attributes are included in each subclass relation No foreign-keys needed Example: Mapping of ISA-Hierarchy Table for superclass Primary key + common attributes Separate tables for subclasses Primary key of superclass (==foreign key) + specific attributes name street city Person ISA salary Credit_rating Employee Customer name Person street city since Employee name salary name Customer credit_rating 15

Today s Agenda Introduction to the Relational Model Logical Database Design Introduction to Views Relational Views A view is a virtual relation, but we store a definition, rather than a set of tuples. Mechanism to hide data from the view of certain users. Views can be used to present necessary information (or a summary), while hiding details in underlying relation(s). Given YoungStudents, but not Students or Enrolled, we can find students who have are enrolled, but not the cid s of the courses they are enrolled in. Syntax: CREATE VIEW v AS <query expression> Where <query expression> is any legal query expression the view name is represented by v 16

View Examples A view on the students showing their age. CREATE VIEW agestudents AS SELECT sid, name, extract(year from sysdate) - extract(year from birthdate) AS age FROM students A view on the courses showing number of students. CREATE VIEW coursestats AS SELECT c.ucode - c.title AS unit_of_study, COUNT(sid) AS nr_of_students FROM courses c, enrolled e WHERE c.ucode = e.ucode GROUP BY c.ucode, c.title Updates on Views Create a view of the enrolled relation, hiding the grade attribute CREATE VIEW enrolled_students AS SELECT sid AS student, uos_code FROM enrolled Add a new tuple to view enrolled_students INSERT INTO enrolled_students VALUES (200421567, COMP5138 ) This insertion means the insertion of the tuple (200421567, COMP5138, null, null) into the enrolled relation Updates on more complex views are difficult or impossible to translate, and hence are disallowed. Updatable Views: SQL-92 allows updates only on simple views (SELECT-FROM-WHERE without aggregates or distinct) defined on a single relation SQL:1999 allows more, but most implementations don t support this yet 17

Summary The Relational Model Relations, Attributes, Domains Primary and Foreign Keys Database creation using SQL Logical Database Design Transformation rules to map E-R diagrams to relations Introduction to Views Next Week Schema Refinement and Normal Forms Functional Dependencies Normal Forms Decomposition Algorithm Textbook Reading Chapter 19 (up to 19.6) 18