1 / 62

Database Modelling

Database Modelling. Week 2: Directed Reading Relational Database Design Nick Rossiter. 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

mea
Download Presentation

Database Modelling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Database Modelling Week 2: Directed Reading Relational Database Design Nick Rossiter

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

  3. 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)

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

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

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

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

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

  9. 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?

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

  11. Non-Atomic Data Example: A “relation” containing non-atomic data.

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

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

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

  15. Example of Using Data Types

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

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

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

  19. 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)

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

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

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

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

  24. SQL Data Types SQL data types are often based on their physical storage.

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

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

  27. 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!

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

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

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

  31. 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 ANDY < 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.

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

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

  34. 3-valued Logic Truth Tables In the tables, M stands for maybe.

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

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

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

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

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

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

  41. 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 CandidateKey.

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

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

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

  45. Candidate Keys: Example (1)

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

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

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

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

  50. 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 !

More Related