Database Systems Instructor Name: Lecture-12.

Slides:



Advertisements
Similar presentations
Relational Database Design UNIT II 1. 2 Advantages of Using Database Systems Centralized control of a firm’s data Redundancy can be reduced (avoid keeping.
Advertisements

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
1 Logical Database Design and the Relational Model Modern Database Management.
Monash University Week 7 Data Modelling Relational Database Theory IMS1907 Database Systems.
Systems Development Life Cycle
© 2005 by Prentice Hall 1 Chapter 5: Logical Database Design and the Relational Model Modern Database Management 7 th Edition Jeffrey A. Hoffer, Mary B.
The Database Approach u Emphasizes the integration of data across the organization.
Logical Database Design and the Relational Model (part 1)
Introduction to Databases CIS 5.2. Where would you find info about yourself stored in a computer? College Physician’s office Library Grocery Store Dentist’s.
RELATIONSHIP  THE WAY TABLES ARE RELATED  A TABLE MUST PARTICIPATE IN AT LEAST ONE RELATIONSHIP  IN A BINARY RELATIONSHIP TWO ENTITIES PARTICIPATE 
1 © Prentice Hall, 2002 Chapter 5: Logical Database Design and the Relational Model Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B.
Database Design Concepts INFO1408 Term 2 week 1 Data validation and Referential integrity.
Michael F. Price College of Business Chapter 6: Logical database design and the relational model.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall 9.1.
MIS 385/MBA 664 Systems Implementation with DBMS/ Database Management Dave Salisbury ( )
Concepts and Terminology Introduction to Database.
MIS 301 Information Systems in Organizations Dave Salisbury ( )
Database Management COP4540, SCS, FIU Relational Model Chapter 7.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
Copyright 2006 Prentice-Hall, Inc. Essentials of Systems Analysis and Design Third Edition Joseph S. Valacich Joey F. George Jeffrey A. Hoffer Chapter.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
1 © Prentice Hall, 2002 Chapter 5: Logical Database Design and the Relational Model Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B.
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
SQL: Part 1 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
CS263 Lecture 5: Logical Database Design Can express the structure of a relation by a Tuple, a shorthand notation Name of the relation is followed (in.
The relational model A data model (in general) : Integrated collection of concepts for describing data (data requirements). Relational model was introduced.
1 ER Modeling BUAD/American University Mapping ER modeling to Relationships.
Chapter 10 Designing Databases. Objectives:  Define key database design terms.  Explain the role of database design in the IS development process. 
9-1 © Prentice Hall, 2007 Topic 9: Physical Database Design Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich,
1 © Prentice Hall, 2002 ITD1312 Database Principles Chapter 4B: Logical Design for Relational Systems -- Transforming ER Diagrams into Relations Modern.
Logical Database Design and the Relational Model.
Lecture 4: Logical Database Design and the Relational Model 1.
Logical Database Design and Relation Data Model Muhammad Nasir
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together.
IT 5433 LM3 Relational Data Model. Learning Objectives: List the 5 properties of relations List the properties of a candidate key, primary key and foreign.
AOIT Database Design Unit 3, Lesson 9 Data Integrity Copyright © 2009–2011 National Academy Foundation. All rights reserved.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Logical Design & the Relational Model
Getting started with Accurately Storing Data
CompSci 280 S Introduction to Software Development
Logical Database Design and the Rational Model
Fundamental of Database Systems
Chapter 5 Database Design
Chapter 4 Logical Database Design and the Relational Model
Chapter 4: Logical Database Design and the Relational Model
COP Introduction to Database Structures
Including Constraints
Chapter 5: Logical Database Design and the Relational Model
MIS 322 – Enterprise Business Process Analysis
Lecture 2 The Relational Model
Modern Systems Analysis and Design Third Edition
Example Question–Is this relation Well Structured? Student
Translation of ER-diagram into Relational Schema
Module 5: Implementing Data Integrity by Using Constraints
Chapter 9 Designing Databases
Chapter 3 The Relational Database Model
© 2011 Pearson Education, Inc. Publishing as Prentice Hall
Relational Database.
CHAPTER 4: LOGICAL DATABASE DESIGN AND THE RELATIONAL MODEL
Database Systems Instructor Name: Lecture-11.
Database Systems Instructor Name: Lecture-9.
Database Systems Instructor Name: Lecture-10.
Database Design: Relational Model
INTRODUCTION A Database system is basically a computer based record keeping system. The collection of data, usually referred to as the database, contains.
Presentation transcript:

Database Systems Instructor Name: Lecture-12

Contents Resolve Unary Relationship Resolve Ternary Relationship Resolve One-to-Many Unary Relationship Resolve Many-to-Many Unary Relationship Resolve Ternary Relationship Relational Model Integrity Constraints Well Structured Relations and Anomalies 2

Resolve Many-to-Many Unary Relationship Two Entities are created One for the Entity Type itself and other for Many-to-Many Relationship The primary key of the associative relation consists of two attributes, both taking their values from the primary key of the other relation Any non-key attribute of the relationship is included in the associative relation {Represent in ERD} 3

Resolve Many-to-Many Unary Relationship (a) Bill-of-materials relationships (M:N) (b) ITEM and COMPONENT relations 4

Resolve Ternary Relationship It is best to convert a ternary relationship to an associative entity in order to represent participation constraints more accurately. Firstly, we create a new associative relation. The default primary key of this relation consists of the three primary key attributes for the participating entities (sometimes additional attributes are required to form a unique primary key) These attributes then act in the role of foreign keys that reference the individual primary keys of the participating entity types . Any attributes of the associative entity type become attributes of the new relation 5

Resolve Ternary Relationship The following Fig. Represents a PATIENT receiving a TREATMENT from a PHYSICIAN The associative entity type PATIENT_Treatment has the attributes Date, Time and Results and values of these are recorded for each instance of patient treatment The primary key attributes Patient_ID, Physician_ID and Treatment_Code become foreign keys in PATIENT_TREATMENT – these are components of its primary key but do not uniquely identify a given treatment, since a patient may receive the same treatment from the same physician on more than one occasion 6

Ternary Relationship with Associative Entity 7

Resolve Ternary Relationship with Associative Entity 8

The Relational Model Was introduced in 1970 by Dr. E. F. Codd (of IBM) Commercial relational databases began to appear in the 1980s Today relational databases have become the dominant technology for database management 9

The Relational Model Data is represented in the form of tables, and the model has 3 components Data structure – data are organised in the form of tables with rows and columns Data manipulation – powerful operations (using the SQL language) are used to manipulate data stored in the relations Data integrity – facilities are included to specify business rules that maintain the integrity of data when they are manipulated 10

Relational Definitions A relation is a named, two-dimensional table of data Every relation has a unique name, and consists of a set of named columns and an arbitrary number of unnamed rows An attribute is a named column of a relation, and every attribute value is atomic. Every row is unique, and corresponds to a record that contains data attributes for a single entity. The order of the columns is irrelevant. The order of the rows is irrelevant. 11

Integrity Constraints These help maintain the accuracy and integrity of the data in the database Domain Constraints - a domain is the set of allowable values for an attribute. Domain definition usually consists of 4 components: domain name, meaning, data type, size (or length), allowable values/allowable range (if applicable) Entity Integrity ensures that every relation has a primary key, and that all the data values for that primary key are valid. No primary key attribute may be null. 12

Entity Integrity In some cases a particular attribute cannot be assigned a data value, e.g. when there is no applicable data value or the value is not known when other values are assigned In these situations we can assign a null value to an attribute (null signifies absence of a value) But still primary key values cannot be null – the entity integrity rule states that “no primary key attribute (or component of a primary key attribute) may be null 13

Referential Integrity A Referential Integrity constraint is a rule that maintains consistency among the rows of two relations – it states that any foreign key value (on the relation of the many side) MUST match a primary key value in the relation of the one side. (Or the foreign key can be null) 14

Referential Integrity How do you know if a foreign key is allowed to be null? In this example, as each ORDER must have a CUSTOMER the foreign key of Customer_ID cannot be null on the ORDER relation Whether a foreign key can be null must be specified as a property of the foreign key attribute when the database is designed 15

Referential Integrity what happens to order data if we choose to delete a customer who has submitted orders? We may want to see sales even though we do not care about the customer anymore. Three choices are possible: Restrict – don’t allow delete of “parent” side if related rows exist in “dependent” side, i.e. prohibit deletion of the customer until all associated orders are first deleted Cascade – automatically delete “dependent” side rows that correspond with the “parent” side row to be deleted, i.e. delete the associated orders, in which case we lose not only the customer but also the sales history Set-to-Null – set the foreign key in the dependent side to null if deleting from the parent side - an exception that says although an order must have a customer_ID value when the order is created, Customer_ID can become null later if the associated customer is deleted [not allowed for weak entities] 16

Well Structured Relations A well-structured relation contains minimal redundancy and allows users to insert, modify and delete the rows in a table without errors and inconsistencies Redundancies in a table (such as more than one entry for each EMPLOYEE) may result in errors and inconsistencies (anomalies) when the table is updated 3 Types of anomaly are possible, insertion, deletion and modification anomalies 17

Insertion Anomaly Insertion anomaly – looking at EMPLOYEE: A1 Fred Bloggs Info Sys Delphi A1 Fred Bloggs Info Sys VB Suppose that we want to add a new employee – the primary key for this relation is the combination of Emp_ID and Course_Title. Therefore, to insert a new row, the user must supply both these values (since primary keys cannot be null or nonexistent) This is an anomaly, since the user should be able to enter employee data without supplying course data 18

Insertion Anomaly Insertion anomaly – looking at EMPLOYEE: A1 Fred Bloggs Info Sys Delphi A1 Fred Bloggs Info Sys VB Suppose that we want to add a new employee – the primary key for this relation is the combination of Emp_ID and Course_Title. Therefore, to insert a new row, the user must supply both these values (since primary keys cannot be null or nonexistent) This is an anomaly, since the user should be able to enter employee data without supplying course data 19

Deletion and Modification Anomaly Suppose that the data for a particular employee are deleted from the table This results in losing the information that this employee completed a particular course This results in losing the information that this course was offered – deletion anomaly If employee A1 changes the department they work in, this must be recorded in both the rows of the table otherwise the data will be inconsistent – modification anomaly 20

Resolve Anomalies These anomalies indicate that EMPLOYEE is not a well-structured relation We should use normalisation theory (discussed later) to divide EMPLOYEE2 into 2 relations, one called EMPLOYEE1 and one called EMP_COURSE that keeps track of the course details 21

Database Systems - Lab What are Constraints? Types of Constraints Constraints are used to control, reference, verify (check) and restrict (allowable) Types of Constraints Column Level (inline) Table Level (out-of-line) 22

Constraint Column Level Constraints Not Null Unique Check Default Table Level Constraints Primary Key (In parent table) Foreign Key (In child table) 23

Creating Constraint Add Constraint at Table Level Creation Change and Drop Constraint using ALTER TABLE command Add Table Level Constraint Modify Table and Column Level Constraints Rename Table and Column Level Constraints Drop Table and Column Level Constraints Primary Key (In parent table) Foreign Key (In child table) 24

Creating Constraint On delete Cascade On Delete Set Null Enable/ Disable Constraints 25

Table with Constraints CREATE TABLE Show ( show_id NUMBER NOT NULL, category_id NUMBER NOT NULL, act_id NUMBER NOT NULL, venue_id NUMBER NOT NULL, showdate DATE NULL, CONSTRAINT XPKShow PRIMARY KEY (show_id), CONSTRAINT FKShow_Cat FOREIGN KEY (category_id) REFERENCES Category, CONSTRAINT FKShow_Venue FOREIGN KEY (venue_id) REFERENCES Venue, CONSTRAINT FKShow_Act FOREIGN KEY (act_id) REFERENCES Act ); 26

Table with Constraints CREATE TABLE Dept_tab ( Deptno NUMBER(3) CONSTRAINT Dept_pkey PRIMARY KEY, Dname VARCHAR2(15), Loc VARCHAR2(15), CONSTRAINT Dname_ukey UNIQUE (Dname, Loc), CONSTRAINT Loc_check1 CHECK (loc IN ('NEW YORK', 'BOSTON', 'CHICAGO'))); CREATE TABLE Emp_tab ( Empno NUMBER(5) CONSTRAINT Emp_pkey PRIMARY KEY, Ename VARCHAR2(15) NOT NULL, Job VARCHAR2(10), Mgr NUMBER(5) CONSTRAINT Mgr_fkey REFERENCES Emp_tab, Hiredate DATE, Sal NUMBER(7,2), Comm NUMBER(5,2), Deptno NUMBER(3) NOT NULL CONSTRAINT dept_fkey REFERENCES Dept_tab ON DELETE CASCADE); 27

???????????????? 28

Erroneous Relationship Receives or summarize the data, transfer, translate or calculate data 29