Presentation is loading. Please wait.

Presentation is loading. Please wait.

Garcia-Molina, Ullman, and Widom Database Systems Chapter 2.

Similar presentations

Presentation on theme: "Garcia-Molina, Ullman, and Widom Database Systems Chapter 2."— Presentation transcript:

1 Garcia-Molina, Ullman, and Widom Database Systems Chapter 2

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.

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

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

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

6 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

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

8 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

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

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

11 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) );

12 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

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

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

15 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) );

16 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) );

17 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

18 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

19 Some data for PC modelspeedramhdprice

20 What PC models have a speed of at least 3.00? R1 := σ speed 3.00 (PC) R2 := π model (R1) model

21 Some data for Laptop modelspeedramhdscreenprice

22 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

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

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

Download ppt "Garcia-Molina, Ullman, and Widom Database Systems Chapter 2."

Similar presentations

Ads by Google