The Relational Model of Data Prof. Yin-Fu Huang CSIE, NYUST Chapter 2
Database Systems Yin-Fu Huang 2.1An Overview of Data Models What is a Data model Data model: a notation for describing data or information Three parts: 1) Structure of the data a. Conceptual model 2) Operations on the data a. Queries and modifications b. By limiting operations, it is possible for programmers to describe database operations at a very high level, yet have the DBMS implement the operations efficiently. 3) Constraints on the data
Database Systems Yin-Fu Huang Two important data models: 1) The relational model, including object-relational extensions 2) The semistructured-data model, including XML and related standards 2.1.2Important Data Models
Database Systems Yin-Fu Huang (See Fig. 2.1) This physical implementation is only one possible way the table could be implemented in physical data structures The Relational Model in Brief
Database Systems Yin-Fu Huang 2.1.3The Relational Model in Brief
Database Systems Yin-Fu Huang (See Fig. 2.2) XML: a way to represent data by hierarchically nested tagged elements The operations usually involve following paths in the implied tree from an element to one or more of its nested subelements, then to subelements nested within those, and so on. The constraints often involve the data type of values associated with a tag The Semistructured Model in Brief
Database Systems Yin-Fu Huang 2.1.4The Semistructured Model in Brief
Database Systems Yin-Fu Huang Object-relational model 1) Values can have structure, rather than being elementary types such as integer or strings. 2) Relations can have associated methods. Object-oriented model Hierarchical model Network model 2.1.5Other Data Models
Database Systems Yin-Fu Huang It appear that semistructured models have more flexibility than relations. However, 1) SQL enables the programmer to express their wishes at a very high level. 2) The short SQL programs can be optimized to run as fast, or faster than the code written in alternative languages Comparison of Modeling Approaches
Database Systems Yin-Fu Huang 2.2Basics of the Relational Model (See Fig. 2.3) Attributes Schemas Movies(title, year, length, genre) Database schema Tuples (Gone With the Wind, 1939, 231, drama)
Database Systems Yin-Fu Huang 2.2Basics of the Relational Model
Database Systems Yin-Fu Huang 2.2Basics of the Relational Model Domains Each component of each tuple should be atomic Equivalent Representations of a Relation Relations are sets of tuples, not lists of tuples (See Fig. 2.4) Relation Instances A relation about movies is not static; rather, relations change over time. It is less common for the schema of a relation to change. The current instance
Database Systems Yin-Fu Huang 2.2Basics of the Relational Model
Database Systems Yin-Fu Huang 2.2Basics of the Relational Model Keys of Relations A set of attributes forms a key for a relation if we do not allow two tuples in a relation instance to have the same values in all the attributes of the key An Example Database Schema (See Fig. 2.5)
Database Systems Yin-Fu Huang 2.2Basics of the Relational Model
Database Systems Yin-Fu Huang 2.3 Defining a Relation Schema in SQL Current standard for SQL: SQL-99 1) Data-definition sublanguage 2) Data-manipulation sublanguage Relations in SQL Stored relations called tables Views defined by a computation Temporary tables
Database Systems Yin-Fu Huang 2.3 Defining a Relation Schema in SQL Data Types CHAR(n), VARCHAR(n) BIT(n), BIT VARYING(n) BOOLEAN INT or INTEGER, SHORTINT FLOAT or REAL, DOUBLE PRECISION, DECIMAL(n,d) DATE, TIME
Database Systems Yin-Fu Huang 2.3 Defining a Relation Schema in SQL Simple Table Declarations CREATE TABLE (See Fig. 2.7 and Fig. 2.8) Modifying Relation Schemas DROP TABLE ALTER TABLE 1) ADD followed by an attribute name and its data type ALTER TABLE MovieStar ADD phone CHAR(16); 2) DROP followed by an attribute name ALTER TABLE MovieStar DROP birthdate;
Database Systems Yin-Fu Huang 2.3 Defining a Relation Schema in SQL
Database Systems Yin-Fu Huang 2.3 Defining a Relation Schema in SQL
Database Systems Yin-Fu Huang 2.3 Defining a Relation Schema in SQL Default Values gender CHAR(1) DEFAULT ‘?’, birthdate DATE DEFAULT DATE ‘ ’ ALTER TABLE MovieStar ADD phone CHAR(16) DEFAULT ‘unlisted’; Declaring Keys We may declare one attribute to be a key when that attribute is listed in the relation schema. We may add to the list of items declared in the schema an additional declaration that says a particular attribute or set of attributes forms the key.
Database Systems Yin-Fu Huang 2.3 Defining a Relation Schema in SQL Two declarations: 1) PRIMARY KEY 2) UNIQUE Two tuples in R cannot agree on all of the attributes in set S (i.e., key), unless one of them is NULL. If PRIMARY KEY is used, then attributes in S are not allowed to have NULL as a value for their components. (See Fig. 2.9, Fig. 2.10, and Fig. 2.11)
Database Systems Yin-Fu Huang 2.3 Defining a Relation Schema in SQL
Database Systems Yin-Fu Huang 2.3 Defining a Relation Schema in SQL
Database Systems Yin-Fu Huang 2.3 Defining a Relation Schema in SQL
Database Systems Yin-Fu Huang 2.4An Algebraic Query Language Relational algebra Why Do We Need a Special Query Language? Relational algebra is useful because it is less powerful than C or Java. Two huge rewards: 1) Ease of programming 2) Producing highly optimized code
Database Systems Yin-Fu Huang 2.4An Algebraic Query Language What Is an Algebra? Atomic operands: 1) Variables that stand for relations 2) Constants, which are finite relations Overview of Relational Algebra Operations: Four broad classes 1) The usual set operations – union, intersection, and difference 2) Operations that remove parts of a relation – selection and projection
Database Systems Yin-Fu Huang 2.4An Algebraic Query Language 3) Operations that combine the tuples of two relations – Cartesian product and join 4) An operation called renaming We generally shall refer to expressions of relational algebra as queries Set Operations on Relations R ∪ S, R∩S, R-S Some conditions on R and S: 1) Schemas with identical sets of attributes, and the types (domains) for each attribute must be the same
Database Systems Yin-Fu Huang 2.4An Algebraic Query Language 2) The order of attributes is the same for both relations. Different names ⇒ renaming operator (See Fig. 2.12) Projection π A1, A2, …, An (R) (See Fig. 2.13) π title, year, length (Movies), π genre (Movies) Selection σ C (R) σ length ≧ 100 (Movies), σ length ≧ 100 AND studioName=’Fox’ (Movies)
Database Systems Yin-Fu Huang 2.4An Algebraic Query Language
Database Systems Yin-Fu Huang 2.4An Algebraic Query Language
Database Systems Yin-Fu Huang 2.4An Algebraic Query Language Cartesian Product R×S The components from R precede the components from S in the attribute order for the result. If R and S should happen to have some attributes in common, then we need to invent new names for at least one of each pair of identical attributes. (See Fig. 2.14) Natural Joins R∞S (See Fig. 2.15)
Database Systems Yin-Fu Huang 2.4An Algebraic Query Language
Database Systems Yin-Fu Huang 2.4An Algebraic Query Language
Database Systems Yin-Fu Huang 2.4An Algebraic Query Language Joined tuple vs. dangling tuple (See Fig. 2.16) Theta-Joins R∞ C S Constructed as follows: 1) Take the product of R and S. 2) Select from the product only those tuples that satisfy the condition C. U∞ A<D V (See Fig. 2.17) U∞ A<D AND U.B≠V.B V
Database Systems Yin-Fu Huang 2.4An Algebraic Query Language
Database Systems Yin-Fu Huang 2.4An Algebraic Query Language
Database Systems Yin-Fu Huang 2.4An Algebraic Query Language Combining Operations to Form Queries One can construct expressions of relational algebra by applying operators to subexpressions, using parentheses when necessary to indicate grouping of operands. “What are the titles and years of movies made by Fox that are at least 100 minutes long?” Four steps: (See Fig. 2.18) π title, year (σ length ≧ 100 (Movies)∩σ studioName=’Fox’ (Movies)) π title, year (σ length ≧ 100 AND studioName=’Fox’ (Movies))
Database Systems Yin-Fu Huang 2.4An Algebraic Query Language
Database Systems Yin-Fu Huang 2.4An Algebraic Query Language Naming and Renaming ρ S(A1, A2, …, An) (R) R×ρ S(X, C, D) (S), ρ RS(A, B, X, C, D) (R×S) (See Fig. 2.19) Relationships among Operations Some of the operations can be expressed in terms of other relational-algebra operations. R∩S=R-(R-S) R∞ C S=σ C (R×S)
Database Systems Yin-Fu Huang 2.4An Algebraic Query Language
Database Systems Yin-Fu Huang 2.4An Algebraic Query Language R∞S=π L (σ C (R×S)) 1) C: R.A1=S.A1 AND R.A2=S.A2 AND … AND R.An=S.An 2) L: Attributes of R followed by those attributes in S that are not in R. U∞V ⇒ π A, U.B, U.C, D (σ U.B=V.B AND U.C=V.C (U×V)) U∞ A<D AND U.B≠V.B V ⇒ σ A<D AND U.B≠V.B (U×V) The six remaining operations – union, difference, selection, projection, product, and renaming – form an independent set.
Database Systems Yin-Fu Huang 2.4An Algebraic Query Language A Linear Notation for Algebraic Expressions The notation: 1) A relation name and parenthesized list of attributes for that relation 2) The assignment symbol := 3) Any algebraic expression on the right R(t, y, l, i, s, p):=σ length ≧ 100 (Movies) S(t, y, l, i, s, p):=σ studioName=’Fox’ (Movies) T(t, y, l, i, s, p):=R∩S Answer(title, year):=π t, y (T)
Database Systems Yin-Fu Huang 2.5Constraints on Relations Many kinds of constraints can be expressed in relational algebra Relational Algebra as a Constraint Language Two ways: 1) Equal-to-the-emptyset; e.g., R = 2) Set-containment; e.g., R ⊆ S Referential Integrity Constraints Movies(title, year, length, genre, studioName, producerC#) MovieExec(name, address, cert#, netWorth) π producerC# (Movies) ⊆ π certC# (MovieExec)
Database Systems Yin-Fu Huang 2.5Constraints on Relations StarsIn(movieTitle, movieYear, starName) Movies(title, year, length, genre, studioName, producerC#) π movieTitle, movieYear (StarsIn) ⊆ π title, year (Movies) Key Constraints If two tuples agree on name, then they must also agree on address. σ MS1.name=MS2.name AND MS1.address≠MS2.address (MS1×MS2) = Additional Constraint Examples Domain constraint σ gender≠’F’ AND gender≠’M’ (MovieStar) =
Database Systems Yin-Fu Huang 2.5Constraints on Relations MovieExec(name, address, cert#, netWorth) Studio(name, address, presC#) σ netWorth< (Studio∞ presC#=cert# MovieExec) = π presC# (Studio) ⊆ π cert# (σ netWorth ≧ (MovieExec))
Database Systems Yin-Fu Huang The End.