Week 2: Directed Reading Relational Database Design Nick Rossiter

Slides:



Advertisements
Similar presentations
Rev Monday, January 13, Foundations, Technology, Skills Tools.
Advertisements

SAP-Customizing SAP-Customizing.

Chapter 10: Designing Databases
Please, select a question: How does a personal account work? How to apply to a job offer? How to send a spontaneous application? How to recover your password?
8/25/ Click on Member Billing Icon 2 8/25/2014 Recent Updates to the Program 3.
Database Modelling Lecture 1: Introduction to Databases Akhtar Ali 25 August
MSc IT UFCE8K-15-M Data Management Prakash Chatterjee Room 2Q18
10/12/20141Chem-160. Covalent Bonds 10/12/20142Chem-160.
The Relational Model and Relational Algebra Nothing is so practical as a good theory Kurt Lewin, 1945.
The Relational Model Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
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.
Candidate Keys Objectives of the Lecture : To consider Integrity Constraints; To consider the principles of Candidate Keys; To consider Candidate Keys.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
THE RELATIONAL DATABASE MODEL & THE DATABASE DEVELOPMENT PROCESS Fact of the Week: According to a Gartner study in ‘06, Microsoft SQL server had the highest.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 8.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Basic (Flat) Relational Model.
Fundamentals, Design, and Implementation, 9/e Chapter 5 Database Design.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Database Design Concepts INFO1408 Term 2 week 1 Data validation and Referential integrity.
Concepts of Database Management Seventh Edition
1 Chapter 2 Reviewing Tables and Queries. 2 Chapter Objectives Identify the steps required to develop an Access application Specify the characteristics.
Relational Database Management System A type of database in which records are stored in relational form is called relational database management system.
Introduction To Databases IDIA 618 Fall 2014 Bridget M. Blodgett.
Objectives of the Lecture :
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
Module Title? DBMS E-R Model to Relational Model.
Chapter 4 The Relational Model.
Retrievals & Projections Objectives of the Lecture : To consider retrieval actions from a DB; To consider using relational algebra for defining relations;
Week 6 Lecture Normalization
DAY 15: ACCESS CHAPTER 2 Larry Reaves October 7,
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
The Relational Model1 Nulls A dilemma. The Relational Model2 The Real World Situation A common situation “Date of birth not given” “Present whereabouts.
PHP meets MySQL.
CODD’s 12 RULES OF RELATIONAL DATABASE
CG084&085 / / 1 The Relational Data Model Properties of Relations Keys and Constraints.
Fundamentals of Relational Database Yong Choi School of Business CSUB, Bakersfield.
Module 3: The Relational Model.  Overview Terminology Relational Data Structure Mathematical Relations Database Relations Relational Keys Relational.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
Ad Hoc Constraints Objectives of the Lecture : To consider Ad Hoc Constraints in principle; To consider Ad Hoc Constraints in SQL; To consider other aspects.
The Data in a Relation To consider atomic data in relations; To consider data types in a relation; To consider missing data & NULLs in relations. Objectives.
9/7/2012ISC329 Isabelle Bichindaritz1 The Relational Database Model.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Relational Data Model Ch. 7.1 – 7.3 John Ortiz Lecture 3Relational Data Model2 Why Study Relational Model?  Most widely used model.  Vendors: IBM,
M1G Introduction to Database Development 2. Creating a Database.
Concepts of Database Management Sixth Edition Chapter 6 Database Design 2: Design Method.
Entity-Relationship (ER) Modelling ER modelling - Identify entities - Identify relationships - Construct ER diagram - Collect attributes for entities &
Slide Chapter 5 The Relational Data Model and Relational Database Constraints.
The University of Akron Dept of Business Technology Computer Information Systems The Relational Model: Concepts 2440: 180 Database Concepts Instructor:
Chapter 9 Logical Database Design : Mapping ER Model To Tables.
Topics Related to Attribute Values Objectives of the Lecture : To consider sorting relations by attribute values. To consider Triggers and their use for.
1 CS 430 Database Theory Winter 2005 Lecture 4: Relational Model.
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Information Access Mgt09/12/971 Entity-Relationship Design Information Level Design.
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 5 – September 4 th,
The relational model A data model (in general) : Integrated collection of concepts for describing data (data requirements). Relational model was introduced.
Logical Database Design and Relation Data Model Muhammad Nasir
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
1 The Relational Data Model David J. Stucki. Relational Model Concepts 2 Fundamental concept: the relation  The Relational Model represents an entire.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1 Agenda TMA02 M876 Block 4. 2 Model of database development data requirements conceptual data model logical schema schema and database establishing requirements.
Logical Database Design and the Rational Model
Chapter 5 Database Design
COS 346 Day 8.
Relational Database Design
Database Design: Relational Model
IST 318 Database Administration
Presentation transcript:

Week 2: Directed Reading Relational Database Design Nick Rossiter Database Modelling Week 2: Directed Reading Relational Database Design Nick Rossiter April 6, 2017

Learning Objectives To consider atomic data in relations To consider data types in a relation To consider missing data & NULLs in relations To consider Integrity Constraints To consider the principles of Candidate Keys To consider Candidate Keys in SQL April 6, 2017

The Information Principle A relation contains only data values, i.e. facts. Each attribute-in-a-tuple contains just a single data value. In particular there are no pointers or OIDs. Pointers point to variables because they reference the storage location of the variable) and thus are different in nature to data values. The reason for this principle is to keep relations simple. Following work by EF Codd (1970) April 6, 2017

Efficiency and Human Productivity Handling of relations is a simple abstraction: of what is going on at the physical level of a computer queries that used to take many hours to write for earlier types of DB can be written in a few minutes in a relational language. It would be foolish to throw away this productivity Use computer power to increase human productivity. April 6, 2017

Use of Pointers Pointers are often advocated as a means to make DBs more efficient. But their place is in the implementation of the relational model that the user uses, not in the model itself where it complicates things. It is important to distinguish between the relational model and its implementation. Object-based DBMS provide pointers at the logical level. April 6, 2017

Atomic Data Since it is now known that an atom consists of component parts – a nucleus of protons and neutrons with electrons orbiting round it - perhaps atom is no longer the best word to describe this concept. However until the 20th century, an atom was regarded as the smallest, indivisible particle of matter possible The tradition of using atom/atomic to describe this concept has remained. April 6, 2017

Definition of Atomic Value Definition: An atomic value is a single, indivisible value, not a composite value or a collection of values. The data in one attribute of one tuple must be atomic. April 6, 2017

Example of Atomic Data Example: In this EMPLOYEE relation there is only one value in each attribute-in-a-tuple. April 6, 2017

Atomicity Requirement Maintains the inherent simplicity of relations, and is of great practical benefit. Occasionally it is not obvious whether a value is atomic or not. For example, is a date atomic? On the one hand, it represents a particular day in the calendar, and thus should be atomic. On the other hand, it has 3 components, day-of-the-month, month and year. Another example is a person’s full name should this be atomic ? or split into its component parts, (set of) forename(s) and surname? April 6, 2017

Designer’s choice The DB designer must decide based on what precisely the data means in the context of the database and how it will be used. Typically a date will be regarded as atomic, through a date type predefined in the database standard SQL.   Names are more diverse and are usually handled through definitions to suit the application. April 6, 2017

Non-Atomic Data Example: A “relation” containing non-atomic data. April 6, 2017

Multiple Atomic Values The attributes ‘Component’ and ‘’Quantity’ clearly contain multiple atomic values. Although not shown in the above example, it is possible that some tuples could contain single values of ‘Component’ and ‘Quantity’. Traditional prohibition against repeating values has weakened in the last few years. allowing relations and other “container types” such as arrays, records, etc to be stored as the single atomic value in an attribute. April 6, 2017

Data Types Definition: A data type has a set of permissible values. Each attribute value must be drawn from the set of permissible values of the data type specified for that attribute. More than one attribute in a relation may draw its values from the same data type. April 6, 2017

Example of Type Assignment Every attribute must have a data type, just as every variable in a program must have a type. April 6, 2017

Example of Using Data Types April 6, 2017

Comments on Example All the attribute values in the top tuple are drawn from their respective data types Would not be acceptable otherwise. Only 1 out of 4 values in the bottom tuple are drawn from their respective data types; so that tuple cannot be stored in the relation. If even one attribute has a value that is not drawn from its type’s set of permissible values that tuple cannot be in the relation. April 6, 2017

Data Types -- Operators Definition: A Data Type has a set of permissible operators. Examples :  Number : +, -, /, x, <, > apply to all possible numbers Text : length( ), sub( ), concat( ) apply to all possible texts April 6, 2017

Data Type Implementation A data type needs logical representations for its values and operators. These are what the user uses. They become part of the logical model. A data type needs physical representations for its values and executable code for each of its operators. These form the implementation of the logical model.  We need Strong Typing. Object classes as described in traditional object-oriented programming meet precisely these criteria. Thus object classes are data types. April 6, 2017

Built-in Data Types Commonly needed data types are Built-In, i.e. provided with the DBMS. Examples: Number, Text, (and usually Date ). Text appears as char, varchar in SQL Also have data types for multimedia (blob, clob in SQL) Can also add new data types (later in module) April 6, 2017

Underlying/Specific Data Types When specifying an attribute‘s type, there are typically 2 stages : Specify the general kind of data required, e.g. numbers, text, dates. This is the Underlying Type. Specify that subset of it that the attribute values must be limited to, e.g. only the integer numbers 1 .. 1,000. This is the Specific Type. It uses the same operators as the underlying type. April 6, 2017

Example of Specific Type Note that data types have sets of values that are permitted to be used. Specific Type allows a subset of those in Underlying Type April 6, 2017

Ways to Define a Mathematical Set Set enumeration the values in the set are individually specified; useful for comparatively small sets of values. Set comprehension a constraint or rule applied to any potential member of the set to see if it is in fact a member of the set Both these methods can be used to constrain the underlying type to get the specific type. April 6, 2017

CHECK constraint Some underlying types have parameters which can be used to constrain them to the specific type. SQL has a Check constraint option, allows the enumeration of the specific type within the underlying type and more ad hoc constraints on the underlying type April 6, 2017

SQL Data Types SQL data types are often based on their physical storage. April 6, 2017

Explanation of Types The “2” in Varchar2(x) indicates that it is Oracle’s second version of the variable length text type. It has nothing to do with the number of characters in the text. It is the standard Oracle variable length text type now. The text type is often known as the character string type. SQL attributes/columns are assigned their types when the relation/table is created with the Create Table statement. The SQL Create Table statement is the statement used to specify all aspects of a table. the Alter Table statement can be used to add, delete and modify any aspect of a table already created with a Create Table. April 6, 2017

SQL : Example The SQL Create Table statement creates a table type with the required columns of specified names and specific types (which will use the operators of the underlying types), and creates the table variable EMPLOYEE of that type. April 6, 2017

Missing Data In the real world, data is not always available to put in the DB. Reasons for this include the value being : unknown  not available  not applicable  not yet known  undefined  to be announced  does not exist  not supplied, etc  One paper recorded 42 different reasons! April 6, 2017

Solutions to Missing Data Use a special value to represent missing data. Example: ‘N/A’, ‘T.B.A.’ It gives the reason why the data is missing. The special value must have the same type as the data that is missing, so it can be stored with the data that is known. So cannot use ‘N/A’ in numeric field April 6, 2017

Use of NULL Null requires special support from the DBMS. SQL DBMSs provide this support. But which reason does it represent ? In reality, no-one yet has come up with a good way of handling missing data that is generally accepted . These options are the main ones used in practice, but there is considerable controversy about them. April 6, 2017

NULL terminology We might say : “The attribute ‘Sal’ in a tuple has a null value.” This is wrong ! Null is the absence of a value, so it can’t be a value. “The attribute ‘Sal’ in a tuple contains a null.” This is better, but not very precise. “The attribute ‘Sal’ in a tuple is null.” This is accurately stated. ‘Sal’ is null does not mean that there is no such value or that the salary is £0. April 6, 2017

2-Valued Logic Normal Boolean logic only has the truth values true and false. The result (or value) of any comparison will be true or false. Thus, the Boolean logic operators AND, OR and NOT can be used to combine comparisons, forming an expression. Example:- ( X = 3 AND Y < X ) OR NOT ( Y > 0 )  The value of this expression will be true or false, and can be derived if the values of X and Y are known. April 6, 2017

Truth Table The rules which the logic operators apply to their operands to yield a Boolean result are represented by truth tables In the tables, T and F stand for true and false respectively. The 3 operators yield commonsense results. AND yields false unless both its operands are true, OR yields true unless both operands are false, and NOT reverses the truth value of its operand. April 6, 2017

3-valued Logic With NULL, results of queries can be maybe. For example, for X = Y, if both NULL, result is unclear. We still want to use logical expressions. Therefore AND, OR and NOT must be extended to cope with maybe. This logic offends Gödel’s theorem on decidability. April 6, 2017

3-valued Logic Truth Tables In the tables, M stands for maybe. April 6, 2017

SQL : Not NULL Constraint Sometimes we want to prevent a column from ever holding NULLs. To achieve this in SQL, add a “Not NULL” constraint to the definition of the column. April 6, 2017

Advantages of NOT NULL Because 3-valued logic is more complex than 2-valued it sometimes gives unexpected results It is desirable to avoid having missing data if at all possible. It would be better to stick to 2-valued logic.  Thus we often specify a “not NULL” constraint for a column to ensure that it always contains a data value in every row. April 6, 2017

Integrity Constraints Definition: An Integrity Constraint is a constraint on the values that a given DB relation is permitted to hold. April 6, 2017

Purpose To try to ensure that the relations in the DB only hold data that is true, accurate and up-to-date. A constraint is a validation check that the DBMS automatically applies when a relation’s value is altered. The requirement that a data type be assigned to every attribute is itself an integrity constraint. However, more integrity constraints are possible. April 6, 2017

Applying Constraints in SQL In SQL, integrity constraints are usually applied when the relation is created using the Create Table statement. But can also be enforced in the Alter Table statement April 6, 2017

Categories of Integrity Constraints Attribute type constraints: already considered. Candidate Key constraints: these apply to an individual relation.  Referential Integrity constraints: these correlate two relations. Ad hoc constraints: these apply to one or more relations.  This is not the only possible categorisation of integrity constraints but it is a convenient and practical one. April 6, 2017

Candidate Keys There are no duplicate tuples in a relation, because it is a set of tuples. So every tuple must be unique. Often, indeed typically, the values of only one attribute, or a small number of attributes, in a relation are sufficient to make each tuple in it unique. Whether it requires one attribute, several attributes, or all the attributes in a relation to make each tuple unique, that set of 1 or more attribute(s) is called a Candidate Key. April 6, 2017

Candidate Keys for ID The candidate key attribute(s) can also be considered as uniquely identifying each tuple in the relation. A relation may contain two or more candidate keys. Often a candidate key consists of just one attribute. For example passport number. Nevertheless it is quite normal for a candidate key to consist of 2 or more attributes For example student number + module number in marks Occasionally in an extreme case all of a relation’s attributes form the candidate key. If there is only one key, it is still called the Candidate Key. April 6, 2017

Properties of a Candidate Key Uniqueness: No two distinct tuples may contain the same key value. Irreducibility: No attribute can be removed from the set forming the key, and the remaining attributes still possess the uniqueness property. Underlining attribute(s) is a common way of specifying a candidate key. April 6, 2017

Benefits of these two Properties Candidate keys provide (the only) guaranteed way to find a particular tuple. Checks on the uniqueness of tuples can be limited to the candidate key attribute(s), giving greater efficiency. April 6, 2017

Candidate Keys: Example (1) April 6, 2017

Rationale for Candidate Keys Relation EMPLOYEE : Employee names are not guaranteed to be unique, Many employees may be on the same salary. So none of the attributes apart from ‘ENo’, either on their own or taken in combination, is sufficient to form a candidate key for EMPLOYEE. Relation CAR : A car’s registration number is guaranteed to be unique, and so can be a candidate key. As ‘Owner’ is an employee number which the company will ensure is unique, that can also be a candidate key April 6, 2017

Candidate Keys : Example (2) Re-consider CAR. Let us change the assumptions. The company now has a “share and park” scheme whereby a group of employees can share a car to work; a group may use several of the members’ cars. Now neither ‘RegNo’ or ‘ENo’ on its own is sufficient to identify a tuple in CAR. However, both together will identify any tuple. Therefore, they jointly become the only candidate key. April 6, 2017

2-attribute candidate key The ‘ENo’ attribute need not represent the owner of the car in question; s/he need not even own a car. We simply need to know which cars each employee may come to work in, or alternatively which employees may be allowed to come to work in each car. It is essential to be able to distinguish: two 1-attribute candidate keys and one 2-attribute candidate key. April 6, 2017

Candidate Keys in SQL SQL has Primary and Alternate Keys. If there is only one candidate key, it becomes the primary key; there are no alternate keys.   If there is more than one candidate key, choose one as the primary key; the rest become the alternate keys. April 6, 2017

Making the choice Any candidate key can become the primary key. So choose one that makes the most practical sense. Usually the shortest - easiest for the user, most efficient for the computer. While SQL defines a Primary Key using the phrase Primary Key, it defines an Alternate Key using the word Unique!  SQL does not make specifying at least one candidate key mandatory ! April 6, 2017

Define a Primary Key So bags can occur. To insist on a set Therefore ensure every SQL table has at least a primary key. April 6, 2017

SQL Key Requirements SQL requires that a primary key never be null. However, it will let an alternate key be null, thereby permitting duplicate null keys, a contradiction in terms. To prevent this, a “not NULL” constraint can be added to an alternate key. It is desirable to specify any other candidate keys that happen to exist as alternate keys.   April 6, 2017

Entity Integrity This is an additional constraint for relational DBs that allow nulls.   Definition: Entity Integrity requires that no attribute in a primary key can ever be null. April 6, 2017

Rationale Each tuple represents an entity in the real world Each entity must be identifiable by definition Primary keys serve as identifiers of tuples Therefore a primary key can never be partly or wholly null, to ensure that it does identify each tuple. Note entity integrity does not apply to alternate keys which can be null, either wholly or in part. April 6, 2017

Constraint Names Most relational DBMSs give integrity constraints a name when they store the constraint in the DB. Thus a candidate key constraint would receive a name. If the user does not supply a name when assigning the constraint, a unique default name is created for it by the DBMS. April 6, 2017

Use of naming Constraints The naming of integrity constraints can be of great practical use : It helps users find out about what integrity constraints have been assigned to relations. It allows more meaningful error messages to be provided to the user if there is an attempt to break an integrity constraint. April 6, 2017

Constraints in Oracle Oracle allows user to provide integrity constraint names, and generate default names if the user does not provide them. Oracle default names are not very user-friendly. Therefore assign your own names to integrity constraints. Constraint names must be unique within the entire user’s DB, not just within a single relation. April 6, 2017

Specifying Candidate Keys in SQL Keys (primary and alternate) can be assigned in two places : in the same sub-statement in which an attribute is assigned its type, in a separate sub-statement at the end of a Create Table statement. The first method is only possible if the SQL key consists of one attribute. The second method must be used if the key consists of two or more attributes. April 6, 2017

Constraint Definition In either case, starting the assignment of an SQL key with the phrase Constraint constraint-name assigns a name to the key constraint as well   In Oracle, if a primary or alternate key constraint is assigned in its own sub-statement at the end of the Create Table statement, then it is mandatory to give the constraint a name. April 6, 2017

Examples of SQL Keys (1) In Oracle, note that : Create Table EMPLOYEE ( ENo Char( 2 ) Primary Key , EName Varchar2( 30 M - S 1 Check ( M In (‘S’, ‘M’, ‘W’, ‘D’ ) ), Sal Number ( Sal >999 And Sal < 100000 ) ) ; Sal < 100000 ), Primary Key( ENo ) ); These 2 versions of have a default constraint name. In Oracle, note that :  the first example is permissible  the second example would not be permissible, because no name has been assigned to the key constraint and so a default name must be given. April 6, 2017

Examples of SQL Keys (2) In Oracle, both examples are permissible. April 6, 2017

Examples of SQL Keys (3) Note that, in any version of SQL, only the second method is permissible if a primary or alternate key consists of 2 or more attributes. April 6, 2017