CPSC-310 Database Systems

Slides:



Advertisements
Similar presentations
1 More SQL Database Modification Defining a Database Schema Views.
Advertisements

Tallahassee, Florida, 2014 COP4710 Database Systems Relational Model Fall 2014.
1 Database Systems Relations as Bags Grouping and Aggregation Database Modification.
1 More SQL Database Modification Defining a Database Schema Views Source: slides by Jeffrey Ullman.
Fall 2001Arthur Keller – CS 1808–1 Schedule Today Oct. 18 (TH) Schemas, Views. u Read Sections u Project Part 3 extended to Oct. 23 (T). Oct.
1 More SQL Defining a Database Schema Views. 2 Defining a Database Schema uA database schema comprises declarations for the relations (“tables”) of the.
Winter 2002Arthur Keller – CS 1808–1 Schedule Today: Jan. 29 (T) u Modifications, Schemas, Views. u Read Sections Assignment 3 due. Jan. 31 (TH)
Winter 2002Judy Cushing8–1 Schedule Jan. 30 (Wed) u Modifications, Schemas, Views. Read Sections This Week (Feb 4ff) u Constraints Read Sections.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
Database Modifications A modification command does not return a result as a query does, but it changes the database in some way. There are three kinds.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #3.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Spring 2008 Project 1, Lecture 3 Slides adapted from those used by Jeffrey Ullman, via Jennifer.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #2.
DB Modifications Modification = insert + delete + update. Insertion of a Tuple INSERT INTO relation VALUES (list of values). Inserts the tuple = list of.
CSCE 520- Relational Data Model Lecture 2. Relational Data Model The following slides are reused by the permission of the author, J. Ullman, from the.
1 IT 244 Database Management System Lecture 11 SQL Select-From-Where Statements Meaning of queries Subqueries Ref : -A First Course in Database System.
Winter 2006Keller Ullman Cushing8–1 Turning in Assignments Please turn in hard copy (use only in the direst of circumstances). I am not your secretary.
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Databases 1 Fourth lecture. Rest of SQL Defining a Database Schema Views Foreign Keys Local and Global Constraints Triggers 2.
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
1 CS1368 Introduction* Relational Model, Schemas, SQL Semistructured Model, XML * The slides in this lecture are adapted from slides used in Standford's.
SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections (except and 6.6.6) Next u Constraints. u Read.
1 Database Systems Defining Database Schema Views.
CSCE 520- Relational Data Model Lecture 2. Oracle login Login from the linux lab or ssh to one of the linux servers using your cse username and password.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
1 Views, Indexes Virtual and Materialized Views Speeding Accesses to Data.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
Databases : SQL-Schema Definition and View 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey.
1 Introduction to Database Systems, CS420 SQL Views and Indexes.
1 Introduction to Database Systems, CS420 SQL JOIN, Aggregate, Grouping, HAVING and DML Clauses.
1. 2 Design of databases. E/R model (entity-relationship model) Relational model (tables) UML (unified modeling language) Database programming. SQL, Relational.
Virtual and Materialized Views Speeding Accesses to Data
Slides are reused by the approval of Jeffrey Ullman’s
CPSC-310 Database Systems
COP4710 Database Systems Relational Model.
Outerjoins, Grouping/Aggregation Insert/Delete/Update
CS 480: Database Systems Lecture 13 February 13,2013.
Foreign Keys Local and Global Constraints Triggers
Databases : More about SQL
CPSC-310 Database Systems
Introduction to Database Systems, CS420
CPSC-608 Database Systems
CS 440 Database Management Systems
CPSC-608 Database Systems
CPSC-310 Database Systems
Database Models Relational Model
CPSC-310 Database Systems
CPSC-310 Database Systems
CPSC-310 Database Systems
2018, Fall Pusan National University Ki-Joune Li
CPSC-310 Database Systems
SQL OVERVIEW DEFINING A SCHEMA
IT 244 Database Management System
2018, Fall Pusan National University Ki-Joune Li
Defining a Database Schema
CMSC-461 Database Management Systems
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Virtual and Materialized Views Speeding Accesses to Data
CPSC-608 Database Systems
CPSC-608 Database Systems
CS4433 Database Systems Relational Model.
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
SQL – Constraints & Triggers
CPSC-608 Database Systems
CPSC-608 Database Systems
CMSC-461 Database Management Systems
Instructor: Zhe He Department of Computer Science
Select-From-Where Statements Multirelation Queries Subqueries
Presentation transcript:

CPSC-310 Database Systems Professor Jianer Chen Room 315C HRBB Lecture #11

Outline of Course Representing things by tables E-R model (Ch. 4) Good table structures functional dependencies (Ch.3) Basic operations on relations Relational Algebra (Ch. 2+5) Storage management (Chs. 13-14) SQL languages in DDL/DML (Ch. 6) Query processing (Chs. 15-16) More on SQL (Chs. 7-9) Transition processing (Chs. 17-19)

Outline of Course Representing things by tables E-R model (Ch. 4) Good table structures functional dependencies (Ch.3) Basic operations on relations Relational Algebra (Ch. 2+5) Storage management (Chs. 13-14) SQL languages in DDL/DML (Ch. 6) Query processing (Chs. 15-16) More on SQL (Chs. 7-9) Transition processing (Chs. 17-19)

SQL: Structured Query language

SQL: Structured Query language a very-high-level language.

SQL: Structured Query language a very-high-level language. * say “what to do” rather than “how to do it.”

SQL: Structured Query language a very-high-level language. * say “what to do” rather than “how to do it.” * avoid a lot of data-manipulation details needed in procedural languages like C++ or Java.

SQL: Structured Query language a very-high-level language. * say “what to do” rather than “how to do it.” * avoid a lot of data-manipulation details needed in procedural languages like C++ or Java. Database management system figures out the “best” way to execute queries * called “query optimization”

SQL: Structured Query language a very-high-level language. * say “what to do” rather than “how to do it.” * avoid a lot of data-manipulation details needed in procedural languages like C++ or Java. Database management system figures out the “best” way to execute queries * called “query optimization” For both data definition and data manipulation.

SQL: Structured Query language How does SQL create a table (i.e., format a table structure, or define a relation schema)?

SQL: Structured Query language How does SQL create a table (i.e., format a table structure, or define a relation schema)? This is the job of a data definition language (DDL). Everything that alters relation schema is done by DDL.

Defining a Database Schema A database schema comprises declarations for the relations (“tables”) of the database.

Defining a Database Schema A database schema comprises declarations for the relations (“tables”) of the database. Several other kinds of elements also may appear in the database schema, including views, indexes, and triggers, which we’ll introduce later.

Creating (Declaring) a Relation Simplest form is: CREATE TABLE <name> ( <list of elements> );

Elements of Table Declarations Most basic element: an attribute and its type. The most common types are: INT or INTEGER (synonyms). REAL or FLOAT (synonyms). CHAR(n) = fixed-length string of n characters. VARCHAR(n) = variable-length string of up to n characters.

Example: Create Table CREATE TABLE Sells ( bar CHAR(20), beer VARCHAR(20), price REAL );

Dates and Times DATE and TIME are types in SQL. The form of a date value is: DATE ’yyyy-mm-dd’ Example: DATE ’2004-09-30’ for Sept. 30, 2004.

Times as Values The form of a time value is: TIME ’hh:mm:ss’ with an optional decimal point and fractions of a second following. Example: TIME ’15:30:02.5’ = two and a half seconds after 3:30PM.

Declaring Keys An attribute or list of attributes may be declared PRIMARY KEY or UNIQUE.

Declaring Keys An attribute or list of attributes may be declared PRIMARY KEY or UNIQUE. Either says the attribute(s) so declared functionally determine all the attributes of the relation schema.

Declaring Keys An attribute or list of attributes may be declared PRIMARY KEY or UNIQUE. Either says the attribute(s) so declared functionally determine all the attributes of the relation schema. There are a few distinctions to be mentioned later.

Declaring Single-Attribute Keys Place PRIMARY KEY or UNIQUE after the type in the declaration of the attribute.

Declaring Single-Attribute Keys Place PRIMARY KEY or UNIQUE after the type in the declaration of the attribute. Example: CREATE TABLE Beers ( name CHAR(20) UNIQUE, manf CHAR(20) );

Declaring Multiattribute Keys A key declaration can also be another element in the list of elements of a CREATE TABLE statement.

Declaring Multiattribute Keys A key declaration can also be another element in the list of elements of a CREATE TABLE statement. This form is essential if the key consists of more than one attribute. * May be used even for one-attribute keys.

Example: Multiattribute Key The bar and beer together are the key for Sells: CREATE TABLE Sells ( bar CHAR(20), beer VARCHAR(20), price REAL, PRIMARY KEY (bar, beer) );

PRIMARY KEY Versus UNIQUE The SQL standard allows DBMS implementers to make their own distinctions between PRIMARY KEY and UNIQUE.

PRIMARY KEY Versus UNIQUE The SQL standard allows DBMS implementers to make their own distinctions between PRIMARY KEY and UNIQUE. Example: some DBMS might automatically create an index (data structure to speed search) in response to PRIMARY KEY, but not UNIQUE.

Required Distinctions However, standard SQL requires the following distinctions:

Required Distinctions However, standard SQL requires the following distinctions: There can be only one PRIMARY KEY for a relation, but several UNIQUE attributes.

Required Distinctions However, standard SQL requires the following distinctions: There can be only one PRIMARY KEY for a relation, but several UNIQUE attributes. No attribute of a PRIMARY KEY can ever be NULL in any tuple. But attributes declared UNIQUE may have NULL’s, and there may be several tuples with NULL.

Some Other Declarations for Attributes NOT NULL means that the value for this attribute may never be NULL.

Some Other Declarations for Attributes NOT NULL means that the value for this attribute may never be NULL. DEFAULT <value> says that if there is no specific value known for this attribute’s component in some tuple, use the stated <value>.

Example: Default Values CREATE TABLE Drinkers ( name CHAR(30) PRIMARY KEY, addr CHAR(50) DEFAULT ’123 Sesame St.’, phone CHAR(16) );

Effect of Defaults Suppose we insert the fact that Sally is a drinker, but we know neither her address nor her phone.

Effect of Defaults Suppose we insert the fact that Sally is a drinker, but we know neither her address nor her phone. CREATE TABLE Drinkers ( name CHAR(30) PRIMARY KEY, addr CHAR(50) DEFAULT ’123 Sesame St.’, phone CHAR(16) );

Effect of Defaults Suppose we insert the fact that Sally is a drinker, but we know neither her address nor her phone. An INSERT with a partial list of attributes makes the insertion possible: INSERT INTO Drinkers(name) VALUES(’Sally’); CREATE TABLE Drinkers ( name CHAR(30) PRIMARY KEY, addr CHAR(50) DEFAULT ’123 Sesame St.’, phone CHAR(16) );

Effect of Defaults INSERT INTO Drinkers(name) CREATE TABLE Drinkers ( VALUES(’Sally’); CREATE TABLE Drinkers ( name CHAR(30) PRIMARY KEY, addr CHAR(50) DEFAULT ’123 Sesame St.’, phone CHAR(16) );

Effect of Defaults The tuple appears in Drinkers is name addr phone INSERT INTO Drinkers(name) VALUES(’Sally’); CREATE TABLE Drinkers ( name CHAR(30) PRIMARY KEY, addr CHAR(50) DEFAULT ’123 Sesame St.’, phone CHAR(16) ); The tuple appears in Drinkers is name addr phone Sally 123 Sesame St Null

Effect of Defaults The tuple appears in Drinkers is INSERT INTO Drinkers(name) VALUES(’Sally’); CREATE TABLE Drinkers ( name CHAR(30) PRIMARY KEY, addr CHAR(50) DEFAULT ’123 Sesame St.’, phone CHAR(16) ); The tuple appears in Drinkers is name addr phone Sally 123 Sesame St Null If we had declared phone NOT NULL, this insertion would have been rejected.

Changing Relation Schema: Adding Attributes We may add a new attribute (“column”) to a relation schema by: ALTER TABLE <name> ADD <attribute declaration>;

Changing Relation Schema: Adding Attributes We may add a new attribute (“column”) to a relation schema by: ALTER TABLE <name> ADD <attribute declaration>; Example: ALTER TABLE Bars ADD phone CHAR(16)DEFAULT ’unlisted’;

Changing Relation Schema: Deleting Attributes Remove an attribute from a relation schema by: ALTER TABLE <name> DROP <attribute>;

Changing Relation Schema: Deleting Attributes Remove an attribute from a relation schema by: ALTER TABLE <name> DROP <attribute>; Example: we don’t really need the license attribute for bars: ALTER TABLE Bars DROP license;

Changing Relation Schema: Removing Relations To delete a relation: DROP TABLE <name>;

Views

Views A view is a “virtual table” = a relation defined in terms of the contents of other tables and views.

Views A view is a “virtual table” = a relation defined in terms of the contents of other tables and views. Declare by: CREATE VIEW <name> AS <table construction>;

Views A view is a “virtual table” = a relation defined in terms of the contents of other tables and views. Declare by: CREATE VIEW <name> AS <table construction>; A “Select … From … Where” query

Views A view is a “virtual table” = a relation defined in terms of the contents of other tables and views. Declare by: CREATE VIEW <name> AS <table construction>; Antonym: a relation whose value is really stored in the database is called a base table. A “Select … From … Where” query

Example: View Definition CanDrink(drinker, beer) is a view “containing” the drinker-beer pairs such that the drinker frequents at least one bar that serves the beer: Sells(bar, beer, price) Frequents(drinker, bar)

Example: View Definition CanDrink(drinker, beer) is a view “containing” the drinker-beer pairs such that the drinker frequents at least one bar that serves the beer: Sells(bar, beer, price) Frequents(drinker, bar) CREATE VIEW CanDrink AS SELECT drinker, beer FROM Frequents, Sells WHERE Frequents.bar = Sells.bar;

Example: View Definition CanDrink(drinker, beer) is a view “containing” the drinker-beer pairs such that the drinker frequents at least one bar that serves the beer: Sells(bar, beer, price) Frequents(drinker, bar) CREATE VIEW CanDrink AS SELECT drinker, beer FROM Frequents, Sells WHERE Frequents.bar = Sells.bar;

Example: Accessing a View Query a view as if it were a base table. Also: a limited ability to modify views if it makes sense as a modification of one underlying base table.

Example: Accessing a View Query a view as if it were a base table. Also: a limited ability to modify views if it makes sense as a modification of one underlying base table. Example query: SELECT beer FROM CanDrink WHERE drinker = ’Sally’;

What Happens When a View Is Used?

What Happens When a View Is Used? The DBMS starts by interpreting the query as if the view were a base table.

What Happens When a View Is Used? The DBMS starts by interpreting the query as if the view were a base table. Typical DBMS turns the query into something like relational algebra.

What Happens When a View Is Used? The DBMS starts by interpreting the query as if the view were a base table. Typical DBMS turns the query into something like relational algebra. The definitions of any views used by the query are also replaced by their algebraic equivalents, and “spliced into” the expression tree for the query.

Example: View Expansion

Example: View Expansion CREATE VIEW CanDrink AS SELECT drinker, beer FROM Frequents, Sells WHERE Frequents.bar = Sells.bar; Sells(bar, beer, price) Frequents(drinker, bar)

Example: View Expansion CREATE VIEW CanDrink AS SELECT drinker, beer FROM Frequents, Sells WHERE Frequents.bar = Sells.bar; πdrinker, beer Frequents Sells ⨝ Sells(bar, beer, price) Frequents(drinker, bar)

Example: View Expansion CREATE VIEW CanDrink AS SELECT drinker, beer FROM Frequents, Sells WHERE Frequents.bar = Sells.bar; πdrinker, beer Frequents Sells ⨝ SELECT beer FROM CanDrink WHERE drinker = ’Sally’; Sells(bar, beer, price) Frequents(drinker, bar)

Example: View Expansion πbeer CanDrink σdrinker=‘Sally’ CREATE VIEW CanDrink AS SELECT drinker, beer FROM Frequents, Sells WHERE Frequents.bar = Sells.bar; πdrinker, beer Frequents Sells ⨝ SELECT beer FROM CanDrink WHERE drinker = ’Sally’; Sells(bar, beer, price) Frequents(drinker, bar)

Example: View Expansion πbeer CanDrink σdrinker=‘Sally’ CREATE VIEW CanDrink AS SELECT drinker, beer FROM Frequents, Sells WHERE Frequents.bar = Sells.bar; πdrinker, beer Frequents Sells ⨝ replace SELECT beer FROM CanDrink WHERE drinker = ’Sally’; Sells(bar, beer, price) Frequents(drinker, bar)

Example: View Expansion πbeer CREATE VIEW CanDrink AS SELECT drinker, beer FROM Frequents, Sells WHERE Frequents.bar = Sells.bar; σdrinker=‘Sally’ CanDrink πdrinker, beer Frequents Sells ⨝ πdrinker, beer Frequents Sells ⨝ SELECT beer FROM CanDrink WHERE drinker = ’Sally’; Sells(bar, beer, price) Frequents(drinker, bar)

DMBS Optimization It is interesting to observe that the typical DBMS will then “optimize” the query by transforming the algebraic expression to one that can be executed faster.

DMBS Optimization It is interesting to observe that the typical DBMS will then “optimize” the query by transforming the algebraic expression to one that can be executed faster. Key optimizations:

DMBS Optimization It is interesting to observe that the typical DBMS will then “optimize” the query by transforming the algebraic expression to one that can be executed faster. Key optimizations: Push selections down the tree.

DMBS Optimization It is interesting to observe that the typical DBMS will then “optimize” the query by transforming the algebraic expression to one that can be executed faster. Key optimizations: Push selections down the tree. Eliminate unnecessary projections.

Example: View Expansion πbeer CREATE VIEW CanDrink AS SELECT drinker, beer FROM Frequents, Sells WHERE Frequents.bar = Sells.bar; σdrinker=‘Sally’ CanDrink πdrinker, beer Frequents Sells ⨝ πdrinker, beer Frequents Sells ⨝ SELECT beer FROM CanDrink WHERE drinker = ’Sally’; Sells(bar, beer, price) Frequents(drinker, bar)

Example: View Expansion πdrinker, beer Frequents Sells ⨝ πbeer σdrinker=‘Sally’

Example: View Expansion Push a selection down πdrinker, beer Frequents Sells ⨝ πbeer σdrinker=‘Sally’

Example: View Expansion πdrinker, beer Frequents Sells ⨝ πbeer σdrinker=‘Sally’ Push a selection down πdrinker, beer Frequents Sells ⨝ πbeer σdrinker=‘Sally’

Example: View Expansion πdrinker, beer Frequents Sells ⨝ πbeer σdrinker=‘Sally’ Eliminate unnecessary projects Push a selection down πdrinker, beer Frequents Sells ⨝ πbeer σdrinker=‘Sally’

Example: View Expansion πdrinker, beer Frequents Sells ⨝ πbeer σdrinker=‘Sally’ Eliminate unnecessary projects Push a selection down πdrinker, beer Frequents Sells ⨝ πbeer σdrinker=‘Sally’

Example: View Expansion πdrinker, beer Frequents Sells ⨝ πbeer σdrinker=‘Sally’ Eliminate unnecessary projects Push a selection down πdrinker, beer Frequents Sells ⨝ πbeer σdrinker=‘Sally’ πbeer ⨝ σdrinker=‘Sally’ Sells Frequents

Example: View Expansion πdrinker, beer Frequents Sells ⨝ πbeer σdrinker=‘Sally’ Eliminate unnecessary projects Push a selection down πdrinker, beer Frequents Sells ⨝ πbeer σdrinker=‘Sally’ πbeer ⨝ σdrinker=‘Sally’ Notice how most tuples are eliminated from Frequents before the expensive join. Sells Frequents