Presentation on theme: "Garcia-Molina, Ullman, and Widom Database Systems Chapter 2."— Presentation transcript:
Garcia-Molina, Ullman, and Widom Database Systems Chapter 2
Data Models A data model is a notation for describing the structure of the data in a database, – along with the constraints on that data. a day of the week is an integer between 1 and 7 The data model also normally provides a notation for describing operations on that data : – queries and data modifications.
Relational Model Relations are tables representing information. – The set of tuples for a given relation is an instance of that relation Columns are headed by attributes ; – each attribute has an associated domain, or data type. Rows are called tuples, and – a tuple has one component for each attribute of the relation.
Schemas A relation name, together with the attributes of that relation and their types, form the relation schema. – Movies(title, year, length, genre) A collection of relation schemas forms a database schema. Particular data for a relation or collection of relations is called an instance of that relation schema or database schema.
Keys An important type of constraint on relations is the assertion that an attribute or set of attributes forms a key for the relation. – known as key constraints No two tuples of a relation can agree on all attributes of the key, – although they can agree on some of the key attributes.
Semistructured Data Model In this model, data is organized in a tree or graph structure. XML is an important example of a semistructured data model – Tags similar to those used in HTML define the role played by different pieces of data much as the column headers do in the relational model
SQL The language SQL is the principal query language for relational database systems. The current standard is called SQL-99. Commercial systems generally vary from this standard but adhere to much of it.
Relation implementation Stored relations, called tables, exist in the database and can be modified by changing its tuples, as well as queried. Views are relations defined by a computation – these relations are not stored, but are constructed, in whole or in part, when needed Temporary tables are constructed by SQL during the execution of queries and data modification – after the transaction these are thrown away
Data Definition SQL has statements to declare elements of a database schema. The CREATE TABLE statement allows us to declare the schema for stored relations – specifying the attributes, their types, default values, and keys.
Altering Relation Schemas We can change parts of the database schema with an ALTER statement. These changes include – adding and removing attributes from relation schemas and – changing the default value associated with an attribute. We may also use a DROP statement to completely eliminate relations or other schema elements.
Modifying relations examples DROP TABLE R; Relation R is no longer part of the database schema, and its tuples can no longer be accessed ALTER TABLE MovieStar ADD phone CHAR(16); ALTER TABLE MovieStar DROP birthdate; CREATE TABLE MovieStar ( name CHAR(30), address VARCHAR(255), gender CHAR(1), birthdate DATE, PRIMARY KEY (name) );
Algebra An algebra consists of operators and atomic operands. In relational algebra, the atomic operands are – Variables that stand for relations – Constants which are finite relations
Relational Algebra This algebra underlies most query languages for the relational model. Its principal operators are – union, intersection, difference, selection, – projection, Cartesian product, – natural join, theta-join, and – renaming.
Section 2.4 Relational algebra and SQL From the text – Relational algebra is not used today as a query language in commercial DBMSs, although some of the early prototypes did use this algebra directly. – SQL incorporates relational algebra at its center, and many SQL programs are really syntactically sugared expressions of relational algebra.
Some SQL CREATE TABLE Product ( maker CHAR(30), model CHAR(10) PRIMARY KEY, type CHAR(15) ); CREATE TABLE PC ( model CHAR(30), speed DECIMAL(4,2), ram INTEGER, hd INTEGER, price DECIMAL(7,2) );
CREATE TABLE Laptop ( model CHAR(30), speed DECIMAL(4,2), ram INTEGER, hd INTEGER, screen DECIMAL(3,1), price DECIMAL(7,2) ); CREATE TABLE Printer ( model CHAR(30), color BOOLEAN, type CHAR (10), price DECIMAL(7,2) );
Selection and Projection The selection operator produces a result consisting of all tuples of the argument relation that satisfy the selection condition. Projection removes undesired columns from the argument relation to produce the result
Some Data for Product MMT aoy kdp eee r l A1001pc A1002pc A1003pc A2004laptop A2005laptop A2006laptop B1004pc B1005pc B1006pc B2007laptop C1007pc D1008pc D1009pc D1010pc D3004printer D3005printer E1011pc E1012pc E1013pc E2001laptop E2002laptop E2003laptop E3001printer E3002printer E3003printer F2008laptop F2009laptop G2010laptop H3006printer H3007printer
Some data for PC modelspeedramhdprice
What PC models have a speed of at least 3.00? R1 := σ speed 3.00 (PC) R2 := π model (R1) model
Some data for Laptop modelspeedramhdscreenprice
Which manufacturers make laptops with a hard disk of at least 100GB? R1 := σ hd 100 (Laptop) R2 := Product (R1) R3 := π maker (R2) maker E A B F G
Joins We join two relations by comparing tuples, one from each relation. In a natural join, we splice together those pairs of tuples that agree on all attributes common to the two relations. In a theta-join, pairs of tuples are concatenated if they meet a selection condition associated with the theta-join.
Constraints in Relational Algebra Many common kinds of constraints can be expressed as – the containment of one relational algebra expression in another, or – as the equality of a relational algebra expression to the empty set.